In place change to reverse Z rendering and 32 bit float offscreen depth buffer
This commit is contained in:
@@ -1062,11 +1062,7 @@ static void makeShadow(const mjModel* m, mjrContext* con) {
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
|
||||
if (con->depthMapping == mjDB_ONETOZERO) {
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_GEQUAL);
|
||||
} else {
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
|
||||
}
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_GEQUAL);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
|
||||
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
|
||||
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
|
||||
@@ -1125,17 +1121,16 @@ static void makeOff(mjrContext* con) {
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, con->offColor);
|
||||
|
||||
// create depth and stencil buffer
|
||||
GLenum depth_buffer_format = con->depthPrecision == mjDB_FLOAT32 ? GL_DEPTH32F_STENCIL8 : GL_DEPTH24_STENCIL8;
|
||||
glGenRenderbuffers(1, &con->offDepthStencil);
|
||||
if (!con->offDepthStencil) {
|
||||
mju_error("Could not allocate offscreen depth and stencil buffer");
|
||||
}
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, con->offDepthStencil);
|
||||
if (con->offSamples) {
|
||||
glRenderbufferStorageMultisample(GL_RENDERBUFFER, con->offSamples, depth_buffer_format,
|
||||
glRenderbufferStorageMultisample(GL_RENDERBUFFER, con->offSamples, GL_DEPTH32F_STENCIL8,
|
||||
con->offWidth, con->offHeight);
|
||||
} else {
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, depth_buffer_format, con->offWidth, con->offHeight);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH32F_STENCIL8, con->offWidth, con->offHeight);
|
||||
}
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
|
||||
GL_RENDERBUFFER, con->offDepthStencil);
|
||||
@@ -1174,7 +1169,7 @@ static void makeOff(mjrContext* con) {
|
||||
mju_error("Could not allocate offscreen depth and stencil buffer_r");
|
||||
}
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, con->offDepthStencil_r);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, depth_buffer_format, con->offWidth, con->offHeight);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH32F_STENCIL8, con->offWidth, con->offHeight);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
|
||||
GL_RENDERBUFFER, con->offDepthStencil_r);
|
||||
|
||||
@@ -1546,15 +1541,7 @@ void mjr_makeContext_offSize(const mjModel* m, mjrContext* con, int fontscale,
|
||||
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
|
||||
// free previous context but keep depth settings
|
||||
// Doing this is the only way users can control these
|
||||
// settings without breaking changes to either the
|
||||
// behaviour of mjr_makeContext or the signature
|
||||
int oldDepthMapping = con->depthMapping;
|
||||
int oldDepthPrecision = con->depthPrecision;
|
||||
mjr_freeContext(con);
|
||||
con->depthMapping = oldDepthMapping;
|
||||
con->depthPrecision = oldDepthPrecision;
|
||||
|
||||
// no model: offscreen and font only
|
||||
if (!m) {
|
||||
@@ -1847,13 +1834,12 @@ MJAPI void mjr_resizeOffscreen(int width, int height, mjrContext* con) {
|
||||
}
|
||||
|
||||
|
||||
GLenum depth_buffer_format = con->depthPrecision == mjDB_FLOAT32 ? GL_DEPTH32F_STENCIL8 : GL_DEPTH24_STENCIL8;
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, con->offDepthStencil);
|
||||
if (con->offSamples) {
|
||||
glRenderbufferStorageMultisample(GL_RENDERBUFFER, con->offSamples, depth_buffer_format,
|
||||
glRenderbufferStorageMultisample(GL_RENDERBUFFER, con->offSamples, GL_DEPTH32F_STENCIL8,
|
||||
con->offWidth, con->offHeight);
|
||||
} else {
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, depth_buffer_format, con->offWidth, con->offHeight);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH32F_STENCIL8, con->offWidth, con->offHeight);
|
||||
}
|
||||
|
||||
if (con->offSamples) {
|
||||
@@ -1861,6 +1847,6 @@ MJAPI void mjr_resizeOffscreen(int width, int height, mjrContext* con) {
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, con->offWidth, con->offHeight);
|
||||
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, con->offDepthStencil_r);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, depth_buffer_format, con->offWidth, con->offHeight);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH32F_STENCIL8, con->offWidth, con->offHeight);
|
||||
}
|
||||
}
|
||||
|
||||
+10
-38
@@ -498,11 +498,7 @@ static void initGL3(const mjvScene* scn, const mjrContext* con) {
|
||||
// common options
|
||||
glDisable(GL_BLEND);
|
||||
glEnable(GL_NORMALIZE);
|
||||
if (con->depthMapping == mjDB_ONETOZERO) {
|
||||
glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);
|
||||
} else {
|
||||
glClipControl(GL_LOWER_LEFT, GL_NEGATIVE_ONE_TO_ONE);
|
||||
}
|
||||
glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthMask(GL_TRUE);
|
||||
if (scn->flags[mjRND_CULL_FACE]) {
|
||||
@@ -511,19 +507,11 @@ static void initGL3(const mjvScene* scn, const mjrContext* con) {
|
||||
glDisable(GL_CULL_FACE);
|
||||
}
|
||||
glShadeModel(GL_SMOOTH);
|
||||
if (con->depthMapping == mjDB_ONETOZERO) {
|
||||
glDepthFunc(GL_GEQUAL);
|
||||
} else {
|
||||
glDepthFunc(GL_LEQUAL);
|
||||
}
|
||||
glDepthFunc(GL_GEQUAL);
|
||||
glDepthRange(0, 1);
|
||||
glAlphaFunc(GL_GEQUAL, 0.99f);
|
||||
glClearColor(0, 0, 0, 0);
|
||||
if (con->depthMapping == mjDB_ONETOZERO) {
|
||||
glClearDepth(0);
|
||||
} else {
|
||||
glClearDepth(1);
|
||||
}
|
||||
glClearDepth(0);
|
||||
glClearStencil(0);
|
||||
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
||||
|
||||
@@ -613,11 +601,9 @@ static void setView(int view, mjrRect viewport, const mjvScene* scn, const mjrCo
|
||||
// set projection
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
if (con->depthMapping == mjDB_ONETOZERO) {
|
||||
// account for GL_ZERO_TO_ONE and reverse Z
|
||||
glTranslatef(0,0,0.5);
|
||||
glScalef(1,1,-0.5);
|
||||
}
|
||||
// reverse Z rendering mapping (znear, zfar) -> (1, 0)
|
||||
glTranslatef(0,0,0.5);
|
||||
glScalef(1,1,-0.5);
|
||||
glFrustum(cam.frustum_center - halfwidth,
|
||||
cam.frustum_center + halfwidth,
|
||||
cam.frustum_bottom,
|
||||
@@ -690,24 +676,12 @@ void mjr_render(mjrRect viewport, mjvScene* scn, const mjrContext* con) {
|
||||
float temp[4], headpos[3], forward[3], skyboxdst;
|
||||
float camProject[16], camView[16], lightProject[16], lightView[16];
|
||||
double clipplane[4];
|
||||
float biasMatrixOneToZero[16] = {
|
||||
float biasMatrix[16] = {
|
||||
0.5f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.5f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.0f, 1.0f
|
||||
};
|
||||
float biasMatrixNegOneToOne[16] = {
|
||||
0.5f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.5f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 0.5f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f
|
||||
};
|
||||
float* biasMatrix;
|
||||
if (con->depthMapping == mjDB_ONETOZERO) {
|
||||
biasMatrix = biasMatrixOneToZero;
|
||||
} else {
|
||||
biasMatrix = biasMatrixNegOneToOne;
|
||||
}
|
||||
|
||||
float tempMatrix[16], textureMatrix[16];
|
||||
mjvGeom *thisgeom, tempgeom;
|
||||
@@ -1056,11 +1030,9 @@ void mjr_render(mjrRect viewport, mjvScene* scn, const mjrContext* con) {
|
||||
// set projection: from light viewpoint
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
if (con->depthMapping == mjDB_ONETOZERO) {
|
||||
// account for GL_ZERO_TO_ONE and reverse Z
|
||||
glTranslatef(0,0,0.5);
|
||||
glScalef(1,1,-0.5);
|
||||
}
|
||||
// reverse Z rendering mapping (znear, zfar) -> (1, 0)
|
||||
glTranslatef(0,0,0.5);
|
||||
glScalef(1,1,-0.5);
|
||||
if (thislight->directional) {
|
||||
glOrtho(-con->shadowClip, con->shadowClip,
|
||||
-con->shadowClip, con->shadowClip,
|
||||
|
||||
Reference in New Issue
Block a user