Converting from screen Z to OpenGL Z.

Started by
0 comments, last by GuentherKrass 17 years, 2 months ago
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!
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

This topic is closed to new replies.

Advertisement