Contents / Previous / Next


Fog Equations

Fog blends a fog color with an incoming fragment's color using a fog blending factor. This factor, f, is computed with one of these three equations and then clamped to the range [0,1].

[IMAGE]

where z is the eye-coordinate distance between the viewpoint and the fragment center. The values for density, start, and end are all specified with glFog*().

 void glFog{if}{v}(GLenum pname, TYPE param);

glFog*() sets the parameters and function for calculating fog. If pname is GL_FOG_MODE, then param is either GL_EXP (the default), GL_EXP2, or GL_LINEAR to select one of the three fog factors.
If pname is GL_FOG_DENSITY, GL_FOG_START, or GL_FOG_END, then param is (or points to, with the vector version of the command) a value for density, start, or end in the equations. (The default values are 1, 0, and 1, respectively.) In RGBA mode, pname can be GL_FOG_COLOR, in which case param points to four values that specify the fog's RGBA color values. The corresponding value for pname in color-index mode is GL_FOG_INDEX, for which param is a single value specifying the fog's color index.

The figure plots the fog-density equations for various values of the parameters. You can use linear fog to achieve a depth-cuing effect.

[IMAGE]

Example code:
      gl.glEnable( GL.GL_FOG );
      gl.glHint( GL.GL_FOG_HINT, GL.GL_NICEST );
      //gl.glHint( GL.GL_FOG_HINT, GL.GL_FASTEST );

      //gl.glFogi( GL.GL_FOG_MODE, GL.GL_EXP );
      //gl.glFogi( GL.GL_FOG_MODE, GL.GL_EXP2 );
      //gl.glFogi( GL.GL_FOG_MODE, GL.GL_LINEAR );

      gl.glFogf( GL.GL_FOG_DENSITY, 1.0f );
      gl.glFogf( GL.GL_FOG_START, 0 );
      gl.glFogf( GL.GL_FOG_END, 4);
      float fogColor[] = {0, 0, 0, 0.5f };
      gl.glFogfv( GL.GL_FOG_COLOR, fogColor, 0 );

      // Select how fog distance is calculated.
      //gl.glFogi( GL.GL_FOG_COORD_SRC, GL.GL_FOG_COORD );
      gl.glFogi( GL.GL_FOG_COORD_SRC, GL.GL_FRAGMENT_DEPTH );
GL.GL_FOG_COORD_SRC you can select if the fog distance is calculated directly from depth of the fragment (GL.GL_FRAGMENT_DEPTH ) or be interpolated between vertices (GL.GL_FOG_COORD ), which is faster.