Rendering fog underneath water I'm aware of glFog, which blends between the fog and surface colour based on distance to the camera, like this:



The equations are even given in the docs.
But the fog is applied immediately in front of the camera. I'm trying to render a water surface and want it to have some depth, for example:



At the moment I'm just rendering the water surface with alpha blending, as below. What I need is to have the fog based on the distance from the water surface to the bottom of the water. How can I get this distance? For the moment I'm assuming the camera will always be above the water.
You can ray trace the distance to the second surface. This may be conceptually easiest, but not necessarily fastest method*.

But there is a neat trick, you can multi pass render a depth map to the water surface from the camera. The distance in water is now pixel depth minus the depth map depth. This does not work in all situations such as when you exit water and re-enter later. But should work in many cases, like the one depicted. You can even use same technique for Boolean operations.



Image 1: Render depth map, consult depth map when shading the bottom of your pond.

* It might be in certain cases.