Graphics Reference
In-Depth Information
Fog, with and without Noise
OpenGL allows you to create the appearance of fog and haze in the background
of your scene. This is used to good effect, especially in games and simulators,
to hide the far clipping plane. Objects can be clipped from the scene as they
recede into the background without them appearing to “pop” out of view.
However, the standard OpenGL fog looks too uniform. That is, everything at
the same depth gets the same amount of fog blended into it. Real fog doesn't
behave that way. This example fragment shader shows how using a 3D noise
function to modulate a fragment's Z depth can be used to create a less uniform
fog effect. This is shown in Figure 16.25 that we call “Dinos in the Mist.”
uniform float uNoiseScale;
uniform float uNoiseFreq;
uniform float uDepthFront, uDepthBack;
uniform sampler3D Noise3;
in float vZ; // equal to -EC.z (dist in front of the eye)
in vec4 vColor;
in vec3 vMCposition;
in float vLightIntensity;
out vec4 fFragColor;
const vec4 FOG = vec4( 0.5, 0.5, 0.5, 1. );
void
main( )
{
vec4 nv = texture( Noise3, uNoiseFreq * vMCposition );
float size = nv.r + nv.g + nv.b + nv.a; // [1.,3.]
size -= 2.; // [-1.,+1.]
float deltaz = uNoiseScale * size;
float fogFactor =
((vZ+deltaz) - uDepthFront)/(uDepthBack - uDepthFront);
fogFactor = clamp( fogFactor, 0., 1. );
fogFactor = smoothstep( 0., 1., fogFactor );
vec3 rgb = mix( vColor.rgb * vLightIntensity, FOG.rgb,
fogFactor );
fFragColor = vec4( rgb, 1. );
}
Search WWH ::




Custom Search