- Forward


Fog in OpenGL


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • A Problem:
    • Scenes may not appear realistic
  • One Fix:
    • Make objects fade into the distance (i.e., fade objects based on their distance from the viewpoint)
Fog Equations
Back SMYC Forward
  • Interpolation Factor:
    • \(f(z) = e^{(-d \cdot z)}\)
    • \(f(z) = e^{(-d \cdot z)^2}\)
    • \(f(z) = \frac{U-z}{U-L}\)
    • where \(d\) is the density, \(U\) is the "end", and \(L\) is the "start"
  • Color Calculation:
    • \(C(z) = f(z) S + [1 - f(z)] F\)
    • where \(S\) is the "source" color and \(F\) is the "fog" color
Using Fog in OpenGL
Back SMYC Forward
  • Choose the Fog Color:
    • void glFog*v(GLenum pname, TYPE *params)
    • where pname is GL_FOG_COLOR and params points to the RGBA values
  • Choose the Fog Equation:
    • void glFog*(GLenum pname, TYPE params)
    • where pname is GL_FOG_MODE and TYPE is either GL_EXP, GL_EXP2, or GL_LINEAR
  • Set the Parameters for the Fog Equation:
    • pname can be GL_FOG_DENSITY, GL_FOG_START, or GL_FOG_END
  • Enable Fog:
    • glEnable(GL_FOG);
An Example
Back SMYC Forward

Initialization

openglexamples/fog/fog.c (Fragment: initFog)
 
An Example (cont.)
Back SMYC Forward

Rendering

openglexamples/fog/fog.c (Fragment: display)
 
There's Always More to Learn
Back -