View Full Version : depth

12-01-2000, 06:01 AM

i have a scene where the object closest to the viewer gets at certain angles objects appear to disappear also obscure the view of other objects when they should not - i.e they are draw in front of or behind other objects when it should be the opposite also the also disappear at certain angles
I have the following in my initOGL()

glCullFace (GL_BACK);
glEnable( GL_DEPTH_TEST ); // Enables Depth Testing

glEnable (GL_LINE_SMOOTH);//makes grids on floor look smooth

glEnable (GL_CULL_FACE);

and in onPaint() i have
glDepthFunc(GL_EQUAL); // The Type Of Depth Testing To Do
glClearDepth( 1.0f ); // Set Depth Buffer to Max Dist. when cleared
glClearColor( 0.0f,0.0f,0.0f,0.0f ); // Black Background


any ideas?

12-01-2000, 07:00 AM
Why do you use GL_EQUAL as comparison function ?
With this function only the fragments that have the same depth value as what's already in the depth buffer can pass the depth test. Usually (by default), GL_LESS is used.
Another issue might be the near and far clipping planes. What Z value did you set for those planes (near plane must not be 0.0f and far plane must not be 1.0f)?

Tim Stirling
12-01-2000, 09:21 AM
What Z value did you set for those planes (near plane must not be 0.0f and far plane must not be 1.0f)

I had a similar problem an hour ago, I didn't have the z near plane to 0 but it was very small (0.005), changing this to 1.0
made it work. http://www.opengl.org/discussion_boards/ubb/smile.gif Now I have to reprogram my collision detection routine http://www.opengl.org/discussion_boards/ubb/frown.gif

12-01-2000, 10:22 AM
this can happen too. When the ratio far plane / near plane is too big, you lose resolution in the depth buffer (which has a finite resolution, i.e. 16 or 32 bits...) and uses a hyperbolic scale (which means the farther an object is the least relative precision you'll have on the depth values of its fragments).
What happens when you set the near plane to a too small value is that all your objects are in a region where the depth resolution is very poor.
Thus, you should always set the near clipping plane to a reasonably big value and
limit the ratio far plane / near plane.

12-02-2000, 12:59 AM
Yes, i have my near plane=0 but if increase this value I get a blank screen - also I use GL_EQUAL because GL_LESS or any other function worsens the scene - the objects that appear to be drawn fine - appear and disappear or the back plane is drawn infront of the front and vice versa

I'll change the near plane but what else should I modify for the depth

12-02-2000, 04:51 AM
I must say that I am confused, I don't know what might cause your problem.
For the type of rendering you seem to be doing, I really think you should use GL_LESS, GL_EQUAL is rather for things such as multipass texturing.
Are you sure your near and far planes are not too far apart (remember, far apart would mean (far plane / near plane) is big rather than (far plane - near plane) is big).

12-02-2000, 06:08 AM
The near plane should never be 0, also you should use GL_LESS or GL_LEQUAL for the depth test (assuming depth range is mapped accordingly). Simply fixing one of those (the near plane or depth test) will not solve your problem. Both must be corrected.

12-06-2000, 12:32 AM
thanx - i managed to fix the problem i'm just wondering why the near plane should never be 0.0?- what is the reason?

12-06-2000, 12:36 AM
right - it's because you can't have a divide by zero - far/near plane - but why can't the far plane be 1.0? also is if the near plane is 1.0, is it okay to set the far plane to 300? - it seems to work fine

[This message has been edited by fox (edited 12-06-2000).]

[This message has been edited by fox (edited 12-06-2000).]

12-06-2000, 12:45 AM
Far plane can indeed be 1.0. But only if near plane is less than 1.0 and greater than 0.0.