Lesson 4 : Fog

Fog is generaly used to creates atmospheric effects like fog, smoke, mist ...
Adding fog in a scene can make it more realistic. It is also quiet simple to use fog when we have understand how it works.
 

The fog is like to blend the fog color with the object drawn (see Lesson 3 to know what is blending).

When the object is out of the fog, we keeps 100% of the object's color.
When the object enter inside the fog, we only keeps a x% of its color and add 100-x% of the fog color. The x value is determinated by using a specific equation that depends on the distance of the object and other parameters. x% is called the fog factor.
 

OpenGl can use 3 differents equations to calculate the fog factor (that we should nammed f).
These equations are :
    GL_LINEAR : f = (GL_FOG_END-z)/(GL_FOG_END-GL_FOG_START)
    GL_EXP    : f = e^(-GL_FOD_DENSITY*z)
    GL_EXP2   : f = e^(-(GL_FOD_DENSITY*z)^2)
The fog factor f is clamped in the interval [0, 1]. If f is equal to 0, we see only the fog. If f is equal to 1, we see all the color object.

Rem : ^ is the power symbole
 


Picture Copyrith @ Addison-Wesley Publishing Company

We can see that the fog equation GL_LINEAR depends on two parameters : GL_FOG_START and GL_FOG_END.
The fog equations GL_EXP and GL_EXP2 depends in GL_FOG_DENSITY.

All of these parameters can be setted using :
    gl.glFogf(parameter, value)
parameter is : GL_FOG_START, GL_FOG_END or GL_FOG_DENSITY.
value is the (float) value to be affected to the parameter.

We can also select the fog mode with :
    gl.glFogi(GL_FOG_MODE, mode)
mode is one of these constants : GL_LINEAR, GL_EXP or GL_EXP2.
 

We have seen how the fog factor is calculate but what is the final color.
I said in the introduction that fog is like blending : it mix the fog color with the object's color.
The resulting color is :
    f*objectColor+(1-f)*GL_FOG_COLOR

By comparison to blending, objectColor is the source, GL_FOG_COLOR is the destination, f is the source factor and 1-f is the destination factor.
Here, fog factor f is dependent to the z value of the object.

The fog color (GL_FOG_COLOR) can be defines with :
    gl.glFogfv(GL_FOG_COLOR, color)
color is a rgba color defined with a float array.
 

Fog is a mode that can be enable and disable at any time with :
    gl.glEnable(GL_FOG)
 

We can defines if we would the OpdnGl calculates fog for each pixels or for each vertex with :
    gl.glHint(GL_FOG_HINT, value)
value is one of these constants GL_DONT_CARE, GL_NICEST (per pixel calculation) or GL_FASTEST (per vertex calculation).
 

Previous Lesson

Back

Next Lesson

Last modified on 01/07/2010
Copyright © 2004-2012 Jérôme JOUVIE - All rights reserved. http://jerome.jouvie.free.fr/