Platform SDK: DirectX

Fog Formulas

[C++]

C++ applications can control how fog affects the color of objects in a scene by changing how Direct3D computes fog effects over distance. The D3DFOGMODE enumerated type contains members that identify the three fog formulas. All formulas calculate a fog factor as a function of distance, given parameters that your application sets. How distance itself is computed varies on the projection matrix or if range-based fog is enabled. For more information, see Eye-Relative vs. Z-based Depth and Range-Based Fog.

[Visual Basic]

Applications written in Visual Basic can control how fog affects the color of objects in a scene by changing how Direct3D computes fog effects over distance. The CONST_D3DFOGMODE enumeration contains members that identify the three fog formulas. All formulas calculate a fog factor as a function of distance, given parameters that your application sets. How distance itself is computed varies on the projection matrix or if range-based fog is enabled. For more information, see Eye-Relative vs. Z-based Depth and Range-Based Fog.

D3DFOG_LINEAR

In the linear formula, start is the distance at which fog effects begin, end is the distance at which fog effects no longer increase, and d represents depth (or distance from the viewpoint) within a scene. Values for d increase as objects become more distant. The linear formula is supported for both pixel fog and vertex fog, but the exponential formulas are currently only supported when using pixel fog:

D3DFOG_EXP

D3DFOG_EXP2

In the preceding two exponential formulas, e is the base of natural logarithms (approximately 2.71828), density is an arbitrary fog density that can range from 0.0 to 1.0, and d is depth (or distance from the viewpoint) within a scene.

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

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

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

This formula effectively scales the color of the current polygon Ci by the fog factor f, and adds the product to the fog color Cf 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 DirectX 7.0. 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.