Piercing the Fog


In the real world, the view is rarely crystal clear as we look off into the distance. There always tends to be some haze in the air, whether that is water vapor or blowing sand. The fog capability built into Direct3D and video cards provides an easy implementation for fog and haze. Technically speaking, the fog is simply the integration between the colors defined for objects and a fog color based on the range between the pixel in the scene and the eye point.

The great thing about the use of fog in games is its ability to soften the image. The human eye is not used to seeing crisp images of objects that are far off in the distance. It also helps hide artifacts caused by the far clipping plane. Remember that any object beyond the far clipping plane is not rendered. Without some way of transitioning distant objects into view, they would seem to pop into place as they come within the far clipping distance. The proper use of fog hides this popping effect.

To hide this popping, we should configure fog so that it is fully opaque (i.e., fully fogged) at the far clipping distance. By adjusting the fog starting range and the fog mode and density, we can adjust how abruptly the scene transitions from no fog to fully fogged.

The Managed DirectX 9 implementation for fog is very clean and concise . Only seven attributes of the rendering state need to be adjusted to do anything we wish with fog. Therefore, there is no reason for the game engine to implement a special class just for managing fog. Instead, we will create seven static properties of the CGameEngine class that provide access to the rendering device s fog attributes. Listing 7 “15 illustrates these seven properties.

Listing 7.15: Fog Properties
start example
 public static Color FogColor {     set { m_pd3dDevice.RenderState.FogColor = value; } }  public static FogMode FogTableMode {     set { m_pd3dDevice.RenderState.FogTableMode = value; } }  public static FogMode FogVertexMode {     set { m_pd3dDevice.RenderState.FogVertexMode = value; } }  public static float FogDensity {     set { m_pd3dDevice.RenderState.FogDensity = value; } }  public static float FogStart {     set { m_pd3dDevice.RenderState.FogStart = value; } }  public static float FogEnd {     set { m_pd3dDevice.RenderState.FogEnd = value; } }  public static bool FogEnable {     set { m_pd3dDevice.RenderState.FogEnable = value; } } 
end example
 

The color we use for fog should ideally blend well with the sky color at the horizon. We can also use the fog for an environmental look. Let s say we are creating an underwater game. Aqua-green fog would help to give an underwater look. The code in Listing 7 “16 shows how we could configure fog through the game engine s static properties. We will use some beige fog for that windblown sand look. Figure 7 “5 shows what our scene looks like with these fog settings. We ve gone back to day lighting so that we can properly see the fog.

click to expand
Figure 7 “5: The scene with fog
Listing 7.16: Using Fog
start example
 CGameEngine.FogColor = Color.Beige;  CGameEngine.FogDensity = 0.5f;  CGameEngine.FogEnable = true;  CGameEngine.FogStart = 100.0f;  CGameEngine.FogEnd = 500.0f;  CGameEngine.FogTableMode = FogMode.Linear; 
end example
 



Introduction to 3D Game Engine Design Using DirectX 9 and C#
Introduction to 3D Game Engine Design Using DirectX 9 and C#
ISBN: 1590590813
EAN: 2147483647
Year: 2005
Pages: 98

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net