# 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.

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

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

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

• Total Topics
633338
• Total Posts
3011416
• ### Who's Online (See full list)

There are no registered users currently online

×