real depth values

I need to know the “real” depth value of a pixel in the depth bufer. When i am using glReadPixels to read the depth buffer i found that the values are clamp to be between 0.0 and 1.0 . I know that there’s a maping between the real values to that range and that it is connected to the clipping plane of the projection. Is anyone knows the exact formula to convert the depth values?
And another problem. I am using this code:

glBegin(GL_POLYGON);
glVertex3d(200.0, 200.0, 0);
glVertex3d(200.0, -200.0, 0);
glVertex3d(-200.0, -200.0, 0);
glEnd();

glReadPixels(0, 0, prcView->right, prcView->bottom, GL_DEPTH_COMPONENT, GL_FLOAT, pixelsDepth);

The polygon here has a constant depth of 0. But when i’m looking in the array(pixelsDepth) the values are almost the same but not exactly. Why is that?

depth values are scaled and clamped, so that 0.0 maps to the near plane and 1.0 maps to the far plane. It isn’t a linear map, though. the easy solution (for me, because i’m lazy and my ogl book with the proj matrix is on the otherside of the office=) would be to say look at the projection matrix and calculate the inverse function for z.

I mean, projection is v’=Pv

where v’.z == (the third row of P * V ) / (the fourth row of P * V) (to conv homogeneous to cartesian).
uh. i think the fourth row is likely to just be 1.0, tho’. so you probably don’t need the div. anyway, that’s the fucntion to map world to 0.0…1.0. compute the inverse =)

cheers,
John