Jump to content
  • Advertisement
Sign in to follow this  
Shannon Barber

z-buffer backwards

This topic is 4979 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

For rendering normal opaque triangles, what should the z-buffer be cleared to and what depth test should be used? It seems virtually all the example code I see sets the clear value to 1.0f and the depth test to GL_LEQUAL (sometimes GL_LESS), however for the scene I'm rendering I have to set the clear value to 0.0f and the test to GL_GREATER in order for the triangles to shade correctly (I get wierd overlapps that directly indicate a z-buffer issue with 1.0f/less). What could I have setup differently that would cause a flip in the z/w values?

Share this post


Link to post
Share on other sites
Advertisement
I can't imagine why you want to use anything else that less/lequal. You don't really describe the problem, however I'm sure it wouldn't be solved with flipping the z-values, even if that was possible. If you get z-fighting, try pushing your near clip plane as far as acceptable or increasing the depth precision.

Share this post


Link to post
Share on other sites
It's not z-fighting, triangles that are in back of others are completely drawn on top. GL_GEQUAL/GL_GREATER essentially produce the same results here (again no z-fighting).

Share this post


Link to post
Share on other sites
Quote:
Original post by Magmai Kai Holmlor
...triangles that are in back of others are completely drawn on top.


Have you forgot to turn on depth testing? I can't really think of something else.

Share this post


Link to post
Share on other sites
yeah, that would have been my first question [smile]

I've just had a thought, ok the z-buffer stores z-values in a log. format (iirc) 0 being near, 1 being far.
Now, normally you set to 1 and use GL_LEQUAL/GL_LESS to make sure that all objects closer to you get drawn (firstly replacing the value 1.0f then whatever is closer to 1.0f for each fragment).

Now, in this case the value is set to 0.0f and you tell it "anything further away you have to draw"... I hope you can see where I'm going with this [grin] anything with a z-value close to 1.0f is going to pass the test and overwrite anything close to the near plane (z-value approching 0.0f), thus your problem.

So, based on that the question now becomes (a) can you reverse this mapping? and (b) what are the issues with 1.0f and lequal/less?

Share this post


Link to post
Share on other sites
It sounds like a problem with your projection matrix. How are you setting up the projection?

Also, have you touched glDepthRange at all? It could cause the problem you describe. I assume when you said you set the clear value to 1.0 you meant with glClearDepth rather than glDepthRange?

Share this post


Link to post
Share on other sites
Fair enough, I checked and I do a ::glEnable(GL_DEPTH_TEST)

It's almost like my near & far clipping planes are backwards - and indeed swapping the clipping planes allows me to use 1.0f/less, I use gluPerspective though and the docs say I have them right near,far (i.e. 0.25,50.0) which looks correct with 0.0f/greater.

Share this post


Link to post
Share on other sites
Quote:
Original post by benjamin bunny
It sounds like a problem with your projection matrix. How are you setting up the projection?

Also, have you touched glDepthRange at all? It could cause the problem you describe. I assume when you said you set the clear value to 1.0 you meant with glClearDepth rather than glDepthRange?


I read a blurb about glDepthRange not working right on older hardware, and am just getting this thing started so I am using glDepthClear for now.


//Scene Clear code
::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
::glMatrixMode(GL_MODELVIEW);
::glLoadIdentity();


//Camera positioning code
::glMatrixMode(GL_PROJECTION);
::glLoadIdentity();
::gluPerspective(45.0, GLdouble(width/height), 0.25, 50.0);

::glMatrixMode(GL_MODELVIEW)
::glMultMatrix(camera->Orientation());


I currently am only using one camera so that code is only called once (per frame).

Share this post


Link to post
Share on other sites
Quote:

::gluPerspective(45.0, GLdouble(width/height), 0.25, 50.0);


This should be GLdouble(width)/height, but other than that I don't see anything else wrong. Weird. Try swapping the order in which triangles are drawn. Do you see any difference(triangles that were drawn on top now being hided and vice versa)?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!