Jump to content
  • Advertisement
  • entries
  • comments
  • views

DevLog #10 - OpenGL fog

Sign in to follow this  


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_end
density_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;

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.
Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!