z-buffer backwards

Started by
14 comments, last by _the_phantom_ 19 years, 4 months ago
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?
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
I guess you could try GL_GEQUAL for depth test so triangles at the same location can overdraw eachother.
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.
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).
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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.
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?
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?

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

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.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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).
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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)?

This topic is closed to new replies.

Advertisement