Depth Testing Issue?

Hi Everyone,

This is my first post on the forums. I’m having what I believe to be some depth-test related issues? For example, in the first image you can see through the nearest wall to the furthest one behind it. This can also be seen in the second image? I know a common mistake can be forgetting “GL_DEPTH_BUFFER_BIT” when calling glClear but I’ve double checked and the flag is indeed set.

Any help with this would be much appreciated.

Image 1:
[ATTACH=CONFIG]772[/ATTACH]

Image 2:
[ATTACH=CONFIG]773[/ATTACH]

Hum, this looks like the bug I have been stuck with for the last 3 days: A whitish overlay coming from nowhere. In my case, it looks like an uninitialized 2nd texture superimposed. I am also getting the bands like on your 1st shot in the back. The catch is I am only using one texture stage. And you?
If the checkerboard we sort of see on the floor and ceiling is part of your intended texture with the right spacing, then I’d say you also have something superimposed. Is it consistent? In my case the ghost texture will vary from plain white to snow, to a random image based on what I have run before. I describe it in the previous thread with the shader and a few routines I use.

Hey, thanks for the reply. I don’t think my issue is with texturing, I believe it’s more a depth issue since the farthest wall is visible instead of the nearest wall. Similarly, in the second picture, faces/polygons which shouldn’t be visible are visible. Have added a third image, this one is more of a close-up of the second.

[ATTACH=CONFIG]774[/ATTACH]

Oh I see the bands are where the wall should be. Then you are probably having a winding problem with your triangles not defined in the correct order ( clockwise vs counter clockwise).

You can check if you are with
glFrontFace( GL_CCW ); or glFrontFace( GL_CW);
And do your triangles have the corresponding winding.

It’s currently set to counter-clockwise, changing it to CW didn’t do anything for it, I also tried disabling culling and the issue persists. You can see in the image below, where the dress is being drawn in favor of the hand (which is closer to the camera and no the camera is sufficiently far away so it’s not just clipping through).

[ATTACH=CONFIG]775[/ATTACH]

Some things you may want to check:

  • do you request a depth buffer when creating your OpenGL context?
  • are near/far set to sensible values given the extents of your scene?
  • missing glEnable(GL_DEPTH_TEST)?
  • do you disable depth writes (glDepthMask(GL_FALSE))?
  • is your depth function glDepthFunc(GL_LESS) or glDepthFunc(GL_LEQUAL)?
  • is your projection matrix perhaps inverting the sign on the z axis?

Ok, it gets more precise.
You are sure you have z writes enabled with : glDepthMask( GL_TRUE );
and z tests also : glEnable( GL_DEPTH_TEST );

You could test if your depth testing is initialized correctly by just drawing 2 lonely triangles over each other and see whether the correct one gets displayed.

Thanks for the replies guys, will start working through them and get back to you.

  • do you request a depth buffer when creating your OpenGL context? -> Am using SFML which does allow you to specify the bits used for depth, I’ve set this to 24 at context creation.

  • are near/far set to sensible values given the extents of your scene? -> I didn’t have these set before, have added: glDepthRange(0.f, 1.f);

  • missing glEnable(GL_DEPTH_TEST)? -> Nope, this was always present

  • do you disable depth writes (glDepthMask(GL_FALSE))? -> I didn’t have a call to this either for true or false, have added glDepthMask(GL_TRUE) after the call to glEnable(GL_DEPTH_TEST)

  • is your depth function glDepthFunc(GL_LESS) or glDepthFunc(GL_LEQUAL)? -> Again, was not explicitly set, have tried both and neither really help

  • is your projection matrix perhaps inverting the sign on the z axis? -> I am inverting the vertex positions z component, I did this when loading obj files with DX11

This image is with newly added depth settings:

glEnable( GL_DEPTH_TEST );
glDepthMask( GL_TRUE );
glDepthFunc( GL_LEQUAL );
glDepthRange(0.f, 1.f);

[ATTACH=CONFIG]776[/ATTACH]

I am inverting the vertex positions z component, I did this when loading obj files with DX11

Well, there’s your problem. DirectX uses a left handed coordinate system OpenGL a right handed one, so no need to invert the z axis.
And the questions about the defaults where mostly in case you change them elsewhere in your code and have a code path that does not set them back to the expected values.

Thank you for the reply, thing is I tried removing the inversion while trying out the other suggestions made and even with the inversion removed the model still renders incorrectly? See the image below:

[ATTACH=CONFIG]777[/ATTACH]

This isn’t what Carsten is talking about. He’s talking about the near/far distances in the projection matrix (gluPerspective or similar). If the near distance is too small, the depth buffer resolution will be too coarse in most of the scene.

The default depth range setting (equivalent to glDepthRange(0.f, 1.f)) is fine.

If the model isn’t symmetrical about the Z=0 plane, inverting the model’s Z will be visibly apparent. If the model is symmetrical, inverting its Z won’t matter (except that it will reverse the winding direction, which only matters if you’re using glEnable(GL_CULL_FACE) or two-sided lighting).

How are you generating the projection matrix? If you aren’t using gluPerspective, check that the resulting matrix matches that in the gluPerspective documentation in terms of signs. The signs should look like:


[+ 0 0 0]
[0 + 0 0]
[0 0 - -]
[0 0 - 0]

The model-view matrix should have a positive determinant and be affine (i.e. the bottom row should be [0 0 0 1]).

Hi, thanks for getting back to me. I’m using glm perspective with fov: 90, aspect ratio: 4/3, near: 0.0 and far 1.0. The resulting matrix though is mostly empty? The signs are + along the top row with the others being zero… this is extremely frustrating.

Never use a near value of 0, it breaks the math used for the projection, see also the “Notes” section of gluPerspective. You can use a small positive number for near, but generally want to use as large a value as possible considering the size of the scene - if possible keep the ratio of far/near below 10^5 for good depth buffer resolution.

I’ve tried all sorts of values absolutely nothing works. I’m just going to give up on it to be honest.