The io7m-r1 offers
a filter that provides simple distance fog
effects for roughly simulating the scattering of light due to atmosphere.
It can also be used for special effects such as smoke and dust.
The fog filter applies a configurable fog color to all pixels in the
scene, based on given near and far planes for the fog. The fog is
applied based on the current view direction (the local negative Z axis).
The implemented algorithm takes a color c,
an eye-space near fog distance near, and an
eye-space far fog distance far. It samples
the depth of each pixel in the current scene, and
reconstructs
the eye-space Z component of the surface that produced the pixel in question
and takes the absolute value of that component to yield a positive distance
z from the observer.
Then, a fog factorr
is calculated by fog_factor
[FogFactorZ.hs]:
module FogFactorZ where
clamp :: Float -> (Float, Float) -> Float
clamp x (lower, upper) = max (min x upper) lower
fog_factor :: Float -> (Float, Float) -> Float
fog_factor z (near, far) =
let r = (z - near) / (far - near) in
clamp r (0.0, 1.0)
The value of r is then used to interpolate
between the original color of the pixel and c.
Surfaces with z less than near
are unaffected by fog. Surfaces with z greater than or equal to
far are maximally affected by fog and will actually
have their color values completely replaced by c.
Surfaces with z values that fall between
near
and far will have differing amounts of fog applied
based on the type of fog progression selected.
The filter offers linear,
exponential, and logarithmic
fog progressions. With linear fog, the
r value linearly interpolates between the
original surface color and c. With
exponential fog, the value of
r * r is used to linearly interpolate
between the original surface color and c.
Finally, with logarithmic fog, the value of
sqrt(r) is used to linearly interpolate
between the original surface color and c.