Add a depth attachment to the FBO you are using to render the cubemap faces. You only need one texture/renderbuffer (same dimension as a cubemap face with a depth format) and need to clear it between faces.

@carsten_neumann Got it! Here is the result

If I calculate the gradient across this image I get the following,

There appear some very tiny discontinuities (e.g. very tiny peaks along the horizon). So the results are great!

Just for the sake of clarity:

- Adding the depth component took care of the z-buffering, correct?
- The results I got represent the distance of the 3D vector that connects the center of the cube (camera) to the first intercept at each fragment, correct? Because we are still using the zbuffer, is this calculation still affected by the inaccuracies of the depth buffer the further away it is from the near plane?

Thanks again @carsten_neumann for your input! As well as the other people who have helped me get this far!!

Since you are writing distance into a floating point texture and are not reconstructing the distance from the (quantized) depth buffer values the distance values should not suffer from inaccuracies (up to floating point precision anyway). However, if there are surfaces closer together than the depth buffer can resolve at that distance from the view space origin you’d get the distance to one of those surfaces - the distance value would be accurate (up to floating point precision), but it could be the distance to either surface. Basically you have z-fighting and which fragment writes pass the depth test becomes somewhat unpredictable.

Thanks @carsten_neumann. I have read that there are techniques to obtain more accurate depth readings but these are only achieved post processing the value obtained from the z-buffer.

Thanks once again for your help!