title: How to implement a ground fog GLSL shader
body: <p>I'm trying to implement a ground fog shader for my terrain rendering engine.
  The technique is described in this article: <a href="http://www.iquilezles.org/www/articles/fog/fog.htm" rel="noreferrer">http://www.iquilezles.org/www/articles/fog/fog.htm</a></p>
  
  <p>The idea is to consider the ray going from the camera to the fragment and integrate the fog density function along this ray.</p>
  
  <p>Here's my shader code: </p>
  
  <pre><code>#version 330 core
  in vec2 UV;
  in vec3 posw;
  
  out vec3 color;
  
  uniform sampler2D tex;
  
  uniform vec3 ambientLightColor;
  uniform vec3 camPos;
  
  const vec3 FogBaseColor = vec3(1., 1., 1.);
  
  void main()
  {
      vec3 light = ambientLightColor;
      vec TexBaseColor = texture(tex,UV).rgb;
  
      //***************************FOG********************************************
      vec3 camFrag = posw - camPos;
      float distance = length(camFrag);
      float a = 0.02;
      float b = 0.01;
  
      float fogAmount = a * exp(-camPos.z*b) * ( 1.0-exp( -distance*camFrag.z*b ) ) / (b*camFrag.z);
      color = mix( light*TexBaseColor, light*FogBaseColor, fogAmount );
  }
  </code></pre>
  
  <p>The first thing is that I don't understand how to choose a and b and what are their physical role in the fog density function.</p>
  
  <p>Then, the result is not what I expect… 
  I have a ground fog but the transition of fogAmount from 0 to 1 is always centered at the camera altitude. I've tried a lot of different a and b but when I don't have a transition at camera altitude, I either have a full fogged or not fogged at all terrain.</p>
  
  <p>I checked the data I use and everything's correct:</p>
  
  <ul>
  <li>camPos.z is the altitude of my camera</li>
  <li>camFrag.z is the vertical component of the vector going from the camera to the fragment </li>
  </ul>
  
  <p>I can't get to understand what part of the equation cause this.</p>
  
  <p>Any idea about this ?</p>
  
  <p>EDIT : Here's the effect I'm looking for :
  <a href="http://www.prepar3d.com/wp-content/uploads/2013/10/v2ExampleFog2On.jpg" rel="noreferrer">image1</a>
  <a href="http://wiki.flightgear.org/images/c/c4/Ground_fog01.jpg" rel="noreferrer">image2</a></p>