9
$\begingroup$

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

enter image description here

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:

enter image description here

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.

enter image description here

$\endgroup$

1 Answer 1

5
$\begingroup$

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.

dmap

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

* It might be in certain cases.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.