Looking at this tutorial, I noticed an interesting little calculation to get per-fragment depth from the camera.

float z = gl_FragCoord.z / gl_FragCoord.w;I find this works for me, and that it appears to be fairly accurate in terms of distance.

Now, certain other people are very very certain that this is not a valid way to get depth /distance from the camera.

Can anyone shed any light onto the following:

(a) Whether this is in fact a valid and accurate way to get depth;

(b) How the mathematics behind it work.