PDA

View Full Version : Depth Testing Issue?



Skeith
10-04-2014, 09:03 AM
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:
1470

Image 2:
1471

lamigo
10-04-2014, 09:57 AM
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.

Skeith
10-04-2014, 10:10 AM
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.

1472

lamigo
10-04-2014, 10:32 AM
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.

Skeith
10-04-2014, 10:49 AM
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).

1473

carsten neumann
10-04-2014, 10:58 AM
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?

lamigo
10-04-2014, 11:00 AM
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 );

Cornix
10-04-2014, 11:04 AM
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.

Skeith
10-04-2014, 11:27 AM
Thanks for the replies guys, will start working through them and get back to you.

Skeith
10-04-2014, 11:58 AM
- 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);

1474

carsten neumann
10-04-2014, 12:13 PM
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.

Skeith
10-04-2014, 01:07 PM
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:

1475

GClements
10-06-2014, 03:40 AM
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);

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.



- 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

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 (https://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml) 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]).

Skeith
10-13-2014, 05:16 AM
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.

carsten neumann
10-13-2014, 06:38 AM
Never use a near value of 0, it breaks the math used for the projection, see also the "Notes" section of gluPerspective (https://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml). 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.

Skeith
10-13-2014, 05:34 PM
I've tried all sorts of values absolutely nothing works. I'm just going to give up on it to be honest.