OpenGL depth values are normalized values in [0,1] that represent the range between the near and far planes. The actual screen-space depth is the distance from the eye-plane through Z=0. The near and far planes are at Z=Zn and Z=Zf respectively. Basically, the center-of-projection - or eye - is at the origin and the viewing direction is down the positive Z axis.
z = normalized OpenGL depth-buffer value as returned by glReadPixels in [0,1] Z = actual Z value as distance from the plane through the center-of-projection (Z=0) Zn = distance from the eye-plane to the near plane (Z=Zn) Zf = distance from the eye-plane to the far plane (Z=ZfGiven the screen-space depth Z and the distance to the near and far planes (Zn and Zf), we can compute the OpenGL normalized depth value as follows:
Screen-Space Z to OpenGL z: z = (1/Zn - 1/Z) / (1/Zn - 1/Zf)by solving for Z, we can determine the inverse conversion:
OpenGL z to Screen-Space Z: Z = Zn*Zf / (Zf - z*(Zf-Zn))