Originally posted by DFrey:
[b]Oh, and the value stored in the Z buffer is not between [0,1]. OpenGL maps the near and far depth ranges (not planes mind you) to the use the full extent of values available in the bits available. Or at least that’s how I understand it.
Not completely right. The values in the Z buffer are always between [0,1], but the range which is mapped on [0,1] differs on the near and far clipping planes you use. E.g. when you get a very small object, say 0.01 diameter and you use 20000 as far and -20000 as near clipping plane, the object will look very distorted, because the z-buffer gets too unprecise (40000 is mapped to 32bit, so 0.01 is not in the resolution).
To get your distance from the camera: I think you can do it by calling gluunproject with z as the z-buffer value. Then you get a point in object space from which you can calculate the distance to your camera.