Jump to content
  • Advertisement
Sign in to follow this  
zaneski13

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.

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

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by maspeir
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.


w doesn't stay 1 after the transformation with modelviewprojection

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!