<p>You can ray trace the distance to the second surface. This may be conceptually easiest, but not necessarily fastest method*.</p>

<p>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.</p>

<p><a href="https://i.sstatic.net/UcYKY.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/UcYKY.png" alt="dmap"></a></p>

<p><strong>Image 1</strong>: Render depth map, consult depth map when shading the bottom of your pond.</p>

<p>* It might be in certain cases.</p>

CC BY-SA 3.0
2015-08-09T11:52:38.473Z
2015-08-09T12:54:20.610Z
2015-08-09T12:54:20.610Z
joojaa
<p>I'm aware of glFog, which blends between the fog and surface colour based on distance to the camera, like this:</p>

<p><a href="https://i.sstatic.net/2pERbm.jpg" rel="noreferrer"><img src="https://i.sstatic.net/2pERbm.jpg" alt="enter image description here"></a></p>

<p>The equations are even given in the <a href="https://www.opengl.org/sdk/docs/man2/xhtml/glFog.xml" rel="noreferrer">docs</a>.
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:</p>

<p><a href="https://i.sstatic.net/uEXcwm.png" rel="noreferrer"><img src="https://i.sstatic.net/uEXcwm.png" alt="enter image description here"></a></p>

<p>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.</p>

<p><a href="https://i.sstatic.net/GsYC5.png" rel="noreferrer"><img src="https://i.sstatic.net/GsYC5.png" alt="enter image description here"></a></p>

<opengl><fog><depth-map>
Rendering fog underneath water
jozxyqk
# Rendering fog underneath water

(Asked by: username_0 on 2015-08-09)

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](https://i.sstatic.net/2pERbm.jpg)](https://i.sstatic.net/2pERbm.jpg)

The equations are even given in the [docs](https://www.opengl.org/sdk/docs/man2/xhtml/glFog.xml).
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](https://i.sstatic.net/uEXcwm.png)](https://i.sstatic.net/uEXcwm.png)

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](https://i.sstatic.net/GsYC5.png)](https://i.sstatic.net/GsYC5.png)

**Tags:** opengl, fog, depth-map


---

## 1 answers

### Answer by username_1 on 2015-08-09. Score: 5 | Accepted answer

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](https://i.sstatic.net/UcYKY.png)](https://i.sstatic.net/UcYKY.png)

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

\* It might be in certain cases.


---

