Sign in to follow this  
eGamer

Depth Buffer Problem

Recommended Posts

eGamer    101
does depth buffer function consider the sign of the z-value, i put: glDepthFunc(GL_GREATER); glEnable(GL_DEPTH_TEST); glClearDepth(-1); and then i render polygons on two z values -4, -8 i see that the far polygon of -8 z value is obscuring the -4 one. though -4 is greater than -8 ? any help please ? to go further try to put glClearDepth(+1) and you see nothing on the screen and +1 > -4 +1 > -8 is that ok ? please help.

Share this post


Link to post
Share on other sites
nicolasbol    115
1/ The z-buffer contains only value in [0,1] (this is the result of a process called "w divide")
2/ As you can read in the glClearDepth spec:

Values specified by glClearDepth are clamped to the range [0,1].

So, when you specify glClearDepth(1), combined with glDepthFunc(GL_GREATER), no poloygon will ever be farther than 1: Nothing gets rendered.

If you specify glClearDepth(-1), it becomes glClearDepth(0), everything is at a greater distance than 0 (because openGL is a right handed referencial): You see everything correctly

Share this post


Link to post
Share on other sites
eGamer    101
i render polygons on two z values -4, -8
i see that the far polygon of -8 z value is obscuring the -4 one.

though -4 is greater than -8 ?
any help please ?

Share this post


Link to post
Share on other sites
Tree Penguin    262
The depth function is the condition in which a polygon will be drawn. Right now you say if a pixel of a polygon is further away than a previously drawn pixel, overwrite it. See some tutorials on depth functions and you'll see that GL_LEQUAL is the depth function you should use.

Also, whether or not a pixel at z value -8 is further away than something at -4 depends all on how your world space is transformed (how you used glTranslate, glRotate, gluLookAt, etc.).
Because the polygons get moved, rotated and scaled they might end up at a different spot, with the original far polygon being the closest to the camera, and therefor being drawn in front of the other polygon.

Share this post


Link to post
Share on other sites
Brother Bob    10344
And what is the projection matrix? It is the projection matrix that is, most commonly, responsible for transforming the eye space Z-coordinate into the depth values used for depth buffering and testing.

Share this post


Link to post
Share on other sites
eGamer    101
ok, i see

according to this it is impossible to predict the result of specifying
a depth function in the application, right ?

besides all the books say put GL_LEQUAL to see nearest objects obscurring further ones.

Share this post


Link to post
Share on other sites
Lord_Evil    680
No, it is possible to predict the result of a depth function (in most cases).

Depth comparison works - as the name suggests - on the depth of a pixel which is in range [0,1].

So depending on your modelview matrix and your projection matrix, a pixel might obscure another or not.

Consider the following (simplified) example:

Let's say MV = Identity
Perspective is an orthogonal projection matrix with near (depth 0) = -5 and far (depth 1) = 5.

Now a point at -4 will obscure a point at -2 since it's depth is lower (-4 -> depth = 0.1, -2 -> depth = 0.3).

Hope that makes it clearer.

Edit: GL_LEQUAL = less or equal depth wins, so 0.1 wins over 0.1 to 1.0 :)
Thus nearer objects (less depth) obscure farther objects (more depth).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this