# Depth Buffer Problem

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

## Recommended Posts

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 on other sites
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 on other sites
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 ?

##### Share on other sites
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 on other sites
suppose:
MV = Identity
and try the example you will also the same problem?

##### Share on other sites
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 on other sites
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 on other sites
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).

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 22
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631766
• Total Posts
3002221
×