Fallback for if GL_ARB_clip_control not available
If GL_ARB_clip_control not available then [znear, zfar] -> [1, -1] instead of [1, 0] in normalized device coordinates Thus, Z is still reversed, but not shifted, clipping should still work properly, and conversion to window coordinates does not cause accuracy regression compared to non-reversed Z rendering.
This commit is contained in:
@@ -1489,9 +1489,6 @@ void mjr_makeContext_offSize(const mjModel* m, mjrContext* con, int fontscale,
|
|||||||
if (!mjGLAD_GL_ARB_vertex_buffer_object) {
|
if (!mjGLAD_GL_ARB_vertex_buffer_object) {
|
||||||
mju_error("OpenGL ARB_vertex_buffer_object required");
|
mju_error("OpenGL ARB_vertex_buffer_object required");
|
||||||
}
|
}
|
||||||
if (!mjGLAD_GL_ARB_clip_control) {
|
|
||||||
mju_error("OpenGL ARB_clip_control required");
|
|
||||||
}
|
|
||||||
if (!mjGLAD_GL_ARB_depth_buffer_float) {
|
if (!mjGLAD_GL_ARB_depth_buffer_float) {
|
||||||
mju_error("OpenGL ARB_depth_buffer_float required");
|
mju_error("OpenGL ARB_depth_buffer_float required");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,6 +119,9 @@ void mjr_readPixels(unsigned char* rgb, float* depth,
|
|||||||
int N_pixels = viewport.width * viewport.height;
|
int N_pixels = viewport.width * viewport.height;
|
||||||
for (int i = 0; i < N_pixels; i++) depth[i] = 1.0 - depth[i]; // Reverse the reversed Z buffer
|
for (int i = 0; i < N_pixels; i++) depth[i] = 1.0 - depth[i]; // Reverse the reversed Z buffer
|
||||||
}
|
}
|
||||||
|
else if (!mjGLAD_GL_ARB_clip_control) {
|
||||||
|
mju_warning("ARB_clip_control unavailable while mjDEPTH_ZEROFAR requested, depth accuracy will be limited");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,11 +166,13 @@ void mjr_readPixels(unsigned char* rgb, float* depth,
|
|||||||
if (depth) {
|
if (depth) {
|
||||||
glReadPixels(viewport.left, viewport.bottom, viewport.width, viewport.height,
|
glReadPixels(viewport.left, viewport.bottom, viewport.width, viewport.height,
|
||||||
GL_DEPTH_COMPONENT, GL_FLOAT, depth);
|
GL_DEPTH_COMPONENT, GL_FLOAT, depth);
|
||||||
|
|
||||||
if (con->readDepthMap == mjDEPTH_ZERONEAR) {
|
if (con->readDepthMap == mjDEPTH_ZERONEAR) {
|
||||||
int N_pixels = viewport.width * viewport.height;
|
int N_pixels = viewport.width * viewport.height;
|
||||||
for (int i = 0; i < N_pixels; i++) depth[i] = 1.0 - depth[i]; // Reverse the reversed Z buffer
|
for (int i = 0; i < N_pixels; i++) depth[i] = 1.0 - depth[i]; // Reverse the reversed Z buffer
|
||||||
}
|
}
|
||||||
|
else if (!mjGLAD_GL_ARB_clip_control) {
|
||||||
|
mju_warning("ARB_clip_control unavailable while mjDEPTH_ZEROFAR requested, depth accuracy will be limited");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// restore currentBuffer
|
// restore currentBuffer
|
||||||
|
|||||||
+25
-7
@@ -498,7 +498,7 @@ static void initGL3(const mjvScene* scn, const mjrContext* con) {
|
|||||||
// common options
|
// common options
|
||||||
glDisable(GL_BLEND);
|
glDisable(GL_BLEND);
|
||||||
glEnable(GL_NORMALIZE);
|
glEnable(GL_NORMALIZE);
|
||||||
glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);
|
if (mjGLAD_GL_ARB_clip_control) glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
glDepthMask(GL_TRUE);
|
glDepthMask(GL_TRUE);
|
||||||
if (scn->flags[mjRND_CULL_FACE]) {
|
if (scn->flags[mjRND_CULL_FACE]) {
|
||||||
@@ -601,9 +601,15 @@ static void setView(int view, mjrRect viewport, const mjvScene* scn, const mjrCo
|
|||||||
// set projection
|
// set projection
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
// reverse Z rendering mapping [znear, zfar] -> [1, 0]
|
if (mjGLAD_GL_ARB_clip_control) {
|
||||||
glTranslatef(0.0f, 0.0f, 0.5f);
|
// reverse Z rendering mapping [znear, zfar] -> [1, 0] (ndc)
|
||||||
glScalef(1.0f, 1.0f, -0.5f);
|
glTranslatef(0.0f, 0.0f, 0.5f);
|
||||||
|
glScalef(1.0f, 1.0f, -0.5f);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// reverse Z rendering mapping without shift [znear, zfar] -> [1, -1] (ndc)
|
||||||
|
glScalef(1.0f, 1.0f, -1.0f);
|
||||||
|
}
|
||||||
glFrustum(cam.frustum_center - halfwidth,
|
glFrustum(cam.frustum_center - halfwidth,
|
||||||
cam.frustum_center + halfwidth,
|
cam.frustum_center + halfwidth,
|
||||||
cam.frustum_bottom,
|
cam.frustum_bottom,
|
||||||
@@ -682,6 +688,12 @@ void mjr_render(mjrRect viewport, mjvScene* scn, const mjrContext* con) {
|
|||||||
0.0f, 0.0f, 1.0f, 0.0f,
|
0.0f, 0.0f, 1.0f, 0.0f,
|
||||||
0.5f, 0.5f, 0.0f, 1.0f
|
0.5f, 0.5f, 0.0f, 1.0f
|
||||||
};
|
};
|
||||||
|
if (!mjGLAD_GL_ARB_clip_control) {
|
||||||
|
// account for conversion from ndc to window coordinates
|
||||||
|
biasMatrix[2*4+2] = 0.5;
|
||||||
|
biasMatrix[3*4+2] = 0.5;
|
||||||
|
}
|
||||||
|
|
||||||
float tempMatrix[16], textureMatrix[16];
|
float tempMatrix[16], textureMatrix[16];
|
||||||
mjvGeom *thisgeom, tempgeom;
|
mjvGeom *thisgeom, tempgeom;
|
||||||
mjvLight *thislight;
|
mjvLight *thislight;
|
||||||
@@ -1029,9 +1041,15 @@ void mjr_render(mjrRect viewport, mjvScene* scn, const mjrContext* con) {
|
|||||||
// set projection: from light viewpoint
|
// set projection: from light viewpoint
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
// reverse Z rendering mapping [znear, zfar] -> [1, 0]
|
if (mjGLAD_GL_ARB_clip_control) {
|
||||||
glTranslatef(0.0f, 0.0f, 0.5f);
|
// reverse Z rendering mapping [znear, zfar] -> [1, 0] (ndc)
|
||||||
glScalef(1.0f , 1.0f, -0.5f);
|
glTranslatef(0.0f, 0.0f, 0.5f);
|
||||||
|
glScalef(1.0f, 1.0f, -0.5f);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// reverse Z rendering mapping without shift [znear, zfar] -> [1, -1] (ndc)
|
||||||
|
glScalef(1.0f, 1.0f, -1.0f);
|
||||||
|
}
|
||||||
if (thislight->directional) {
|
if (thislight->directional) {
|
||||||
glOrtho(-con->shadowClip, con->shadowClip,
|
glOrtho(-con->shadowClip, con->shadowClip,
|
||||||
-con->shadowClip, con->shadowClip,
|
-con->shadowClip, con->shadowClip,
|
||||||
|
|||||||
Reference in New Issue
Block a user