Jump to content
  • Advertisement
Sign in to follow this  
bluntman

OpenGL Converting from screen Z to OpenGL Z.

This topic is 4309 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

I am applying my modelview+projection matrix to 3D point in my scene to get their screen coordinates, and then trying to compare the Z components of these to values I have read back from the depth buffer via glReadPixels(GL_DEPTH_COMPONENT..). The problem is the value from the projection matrix is in actual units distance from the camera plane, where as the depth components from the depth buffer are normalized from 0.0 to 1.0. What is the equation to convert from my coordinates to the depth coordinates that OpenGL uses? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Hello bluntman,

you have to calculate the transformation from eye space to window/screen space.
To do this,

(1) transform the eye space (z(e)) with your projection matrix to convert to clip space (z(c)).

(2) do the perspective division (divide z(c) by w(c)) to get normalized device coordinates (z(n)).

(3) to map normalized device coordinates (z(n)) to actual screen coordinates (z(s)), the depth range values d(min) and d(max) are used:

z(s) = (d(max) - d(min))/2 * z(n) + (d(max) - d(min))

The depth range values describe the mapping of the front and back clipping planes onto the depth values. Normally the front clipping plane is mapped on 0 and the back clipping plane to 1, so you get

z(s) = 1/2 * z(n) + 1/2

Cheers,

GuentherKrass

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!