View Full Version : Depth Test Problem

08-12-2002, 09:00 AM

I'm rendering some object, which I rotate. However, when I enable the depth test and rotate, there's a lot of flickering and objects that should be behind the others aren't. I'm not sure if I'm doing something wrong or depth test is haphazard. Any suggestions are more than welcome.


08-12-2002, 09:14 AM
what are your near and far clipping planes, sounds like it might be a z-fighting issue

08-12-2002, 09:47 AM
I'm using gluPerspective with a Z near of 0.0 and a Z far of 100.0.

08-12-2002, 09:50 AM
Ok, I just changed my near to -1.0 and it worked! Thanks so much for the suggestion!

08-12-2002, 09:57 AM
zNear really should be greater than 0.

08-12-2002, 11:23 AM
Yeah, I that's what I thought too, but it didn't work when I put a positive number...I wasn't able to see anything in the window. I tried adjusting glLookAt, but that didnt' seem to work. Do you have any suggestions? Thanks!

08-14-2002, 05:12 AM
If you use a negative zNear your going to cause yourself problems, it should be set >0. In your case try a value of 1. The negative value of your near clipping plane may have caused you to see things that are actually behind you or other strange artifacts. Make sure that you draw objects at some distance into the screen ie. at a negative z coordinate, or you won't see anything.

Assuming the near and far clipping planes correspond to 0 and 1 in the depth buffer then the relation from eye to screen is:

1 f+n 1 2fn 1
Zval = -.--- + -.------ + -
2 f-n 2 z(f-n) 2
where Zval is the value in the z-buffer and z is the real-world depth.

Taking your value of 100 for the far clipping plane and solving for both 1 and -1 near clipping planes you get:

znear = 1, Zval = 1 + 1/z
znear = -1, Zval = 1 - 1/z

Thus for real-world z values which are positive that is behind you, Zval is [0,1] for the znear of -1 and [-1,0] for znear 1. But Zval MUST be in the range [0,1]. So here the objects behind you get drawn, but things maybe distorted because the depth buffer is not linear and the negative clipping plane will do funny things to the z buffer equations. Hence silly flickering and other artifacts.

EDIT: Equation got messed up - sorted

[This message has been edited by thelamberto (edited 08-14-2002).]