• entries
13
13
• views
14495

# DevLog #10 - OpenGL fog

1473 views

OpenGL provides 3 fog types - GL_LINEAR, GL_EXP and GL_EXP2.
Some time ago I've found an image showing the characteristics of these modes which helped me to understand them a bit more. I can't find it now again, so I tried to recreate it. It isn't absolutely accurate, but it could help understand the differences between the fog modes.

There are several fog parameters that can be set. Except of fog color it's GL_FOG_START, GL_FOG_END, and GL_FOG_DENSITY.

The GL_FOG_START and GL_FOG_END is meaningful only for LINEAR fog.
The GL_FOG_DENSITY is meaningful only for EXP and EXP2 fogs.

Because the LINEAR fog works with FOG_START / END parameters, it is the easiest type to set. You simply set the distance where the fog should be starting and a distance where the fog should be ending (covering objects completely).
Setting density for EXP fogs is a bit more tricky. Mostly it's a set & try over and over until you find the density that looks good.

From the equations for fog calculation we can however retrieve an equation for calculating the desired density.
The EXP fog mode uses these formulas for fog calculations:
EXP: f = e^( -(density * z) )EXP2: f = e^( -(density * z)^2 )

With several modifications we can get an equation for density:
density_EXP = ( -ln( fog_end_intensity ) ) / fog_enddensity_EXP2 = sqrt( -ln( fog_end_intensity ) ) / fog_end
The fog_end_intensity is an intensity of picture color at distance of fog_end (e.g. 0.01 or something similar close to 0)

The easiest thing then to do is to create a MACRO for giving you back a density for fog end distance:
// The fog intensity used is 0.01f #define FOG_EXP_DENSITY_FOR(fogEnd) 4.605170186f/fogEnd#define FOG_EXP2_DENSITY_FOR(fogEnd) 2.145966026f/fogEnd

Anywhere in code you can now set the density more easily. If you would like for example the EXP2 fog to end 100 units from camera, you set the density like this:
float fogEnd = 100.0f;glFogf( GL_FOG_DENSITY, FOG_EXP2_DENSITY_FOR( fogEnd ) );

I hope this helped in understanding the exponential fog modes and also reduce the amount of time needed to find the proper density to meet your needs.

There are no comments to display.

## Create an account

Register a new account