Depth test + Geometry shader output

Hello, I’m seeing weird depth test issue I can’t seem to shake. I am rendering a bunch of squares by passing in single vertices via a VBO and outputting 4 vertices in a geometry shader. The output looks like it’s just following the draw order rather than performing the depth test.

The squares are drawn in two layers with coloring corresponding to green in X, blue in Y and red for the layer pushed back in Z. I know that my z values are being passed correctly to the geometry shader because otherwise the squares would not be drawn at the correct sizes for the two different depths. Here’s the geom shader code anyway:

//TL
gl_Position = (gl_in[0].gl_Position + ciModelViewProjection * vec4(-halfW, -halfW, 0.0f, 0.0f));
EmitVertex();
//TR
gl_Position = (gl_in[0].gl_Position + ciModelViewProjection * vec4( halfW, -halfW, 0.0f, 0.0f));
EmitVertex();
//BL
gl_Position = (gl_in[0].gl_Position + ciModelViewProjection * vec4(-halfW, halfW, 0.0f, 0.0f));
EmitVertex();
//BR
gl_Position = (gl_in[0].gl_Position + ciModelViewProjection * vec4( halfW, halfW, 0.0f, 0.0f));
EmitVertex();
EndPrimitive();

To get the view above I’m using a CameraPersp to set the scene:

mCamera.lookAt(vec3(appMiddle, mCameraEyeDist), vec3(appMiddle, 0));
mCamera.setPerspective(mCameraFov, getWindowAspectRatio(), 0.00001f, 100000);
//flip Y to match shader output
mCamera.setWorldUp(vec3(0,-1,0));

I’ve tried all combinations of calls to the following methods and still see the issue.

gl::ScopedDepthTest scpDepthTest(true);
gl::ScopedDepth scpDepth(true);
gl::enableDepth(true);

gl::enableDepthRead();
gl::enableDepthWrite();

Is there something I’m missing? Am I accidentally overwriting/losing the Z/depth data when I create the geometry? Why is the depth test not being performed (or failing)?

Try changing the values for your near and far clip planes. With the current values (0.00001f for the near plane, 100000 for the far plane), you’re quickly running out of depth precision, which can lead to z-fighting. For starters, I’d suggest to set the near clip plane to 0.01f.

If you’re rendering to an Fbo, make sure it has a depth buffer.

1 Like

Ugh, that’s exactly what it was :man_facepalming: Thanks Paul!