# OpenGL How is the depth buffer calculated?

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

## Recommended Posts

Just a quick question guys, How does opengl calculate the pixels in the depth buffer? It looks like it might be 1.0f - (zpos/farClippingPlane) but I'm not completely sure.

##### Share on other sites
The 3D positions of your vertices are turned into 4D vectors by appending a 1 on the end (x,y,z,1). This 4D vector is transformed by the world/view/projection matrices.
The projection matrix contains near/far plane information and does the division that you're guessing at ;)
You can look up "perspective projection matrix" and "matrix multiplication" to see how this math actually works.

These matrix multiplications result in a "projected" 4D vector which is interpolated across your triangles.
For each pixel in the triangle, it's depth value is computed by pos.z/pos.w (i.e. pos[2]/pos[3])

##### Share on other sites
OK, So I'm implementing deferred lighting which requires the depthbuffer image. Would I be better off making the depthbuffer image with a shader or is there an easy way to grab the depth buffer? And then once I have that depth buffer, how would I get the z position of the pixel I'm targeting? (Multiply by pos.w, only I'm working with an image so it doesn't make much sense to that)

##### Share on other sites
I've been meaning to ask this question for some time now...

If w == 1, why bother with the division? You'll always end up with z.

##### Share on other sites
Quote:
 Original post by maspeirI've been meaning to ask this question for some time now...If w == 1, why bother with the division? You'll always end up with z.

w doesn't stay 1 after the transformation with modelviewprojection

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 13
• 9
• 12
• 9
• ### Forum Statistics

• Total Topics
631438
• Total Posts
3000073
×