It’s a quite interesting question, because there is very few example (in fact none) with fog and GLSL.
I’ve done some fog using ARB fragment/vertex program once.
Each time, you need to compute your Z fog value in the vertex and use it in the fragment program
It’s quite annoying because you cannot do fog in OpenGL with vertex shaders on video card that doesn’t have GL_ARB_fragment_program - Or at least, I’ve never managed to do so -
For GLSL, here some couple of functions that come in my mind (untested).
You should try and improves:
Vertex program:
float FogEyePlane(float4 Rh)
{
float4 Re = Rh / Rh.w;
float z = -Re.z;
return clamp((z - gl_Fog.start) * gl_Fog.scale, 0.0, 1.0);
}
float FogEyeAbsolute(float4 Rh)
{
float4 Re = Rh / Rh.w;
float z = abs(Re.z);
return clamp((z - gl_Fog.start) * gl_Fog.scale, 0.0, 1.0);
}
float FogEyeRadial(float4 Rh)
{
float4 Re = Rh / Rh.w;
float z = length(Re);
return clamp((z - gl_Fog.start) * gl_Fog.scale, 0.0, 1.0);
}
// And add in the vertex program section (parameter is the vertex in the view space).
gl_FogCoord = FogEyePlane(gl_ModelViewMatrix * gl_Vertex);
Fragment program:
gl_FragColor = mix(color, gl_Fog.color, gl_FogCoord);
There is also a gl_FogFragCoord. Never tried it.