Fog Formulas


C# applications can control how fog affects the color of objects in a scene by changing the way Microsoft Direct3D computes fog effects over distance. The FogMode enumerated type contains members that identify the three fog formulas (linear fog and two types of exponential fog). All formulas calculate a fog factor as a function of distance, given parameters that an application sets.

Linear Fog

This is set with Linear.

Linear fog equation

where

Exponential Fog

Linear and exponential formulas are supported for both pixel fog and vertex fog.

This is set with Exp.

Exponential fog equation

where

This is set with Exp2.

Exponential square fog equation

where

Note: The system stores the fog factor in the alpha component of the specular color for a vertex. If an application performs its own transformation and lighting, fog factor values can be inserted manually, to be applied by the system during rendering.

The following illustration graphs these formulas, using common values as in the formula parameters.

Fog formulas

Linear is 1.0 at start and 0.0 at end. It is not measured relative to the near or far planes.

When Direct3D calculates fog effects, it uses the fog factor from one of the preceding equations in a blending formula, shown here.

Fog calculation

This formula effectively scales the color of the current polygon C by the fog factor f, and adds the product to the fog color C, scaled by the bitwise inverse of the fog factor. The resulting color value is a blend of the fog color and the original color, as a factor of distance. The formula applies to all devices supported in Microsoft DirectX 7.0 and later. For the legacy ramp device, the fog factor scales the diffuse and specular color components, clamped to the range of 0.0 and 1.0, inclusive. The fog factor typically starts at 1.0 for the near plane and decreases to 0.0 at the far plane.


Send comments about this topic to Microsoft. © Microsoft Corporation. All rights reserved.

Feedback? Please provide us with your comments on this topic.
For more help, visit the DirectX Developer Center