You define how fog affects the color objects in a scene by specifying how Direct3D computes the fog intensity over distance. Fog is a measure of visibility; the lower the value produced by the fog equations, the less visible the object is. You control fog by using the D3DFOGMODE enumerated type, whose members identify the three available fog formulas:
typedef enum _D3DFOGMODE { D3DFOG_NONE = 0, D3DFOG_EXP = 1, D3DFOG_EXP2 = 2, D3DFOG_LINEAR = 3 D3DFOG_FORCE_DWORD = 0x7fffffff, } D3DFOGMODE; |
These members are defined as follows:
Each of the three formulas in the preceding list calculates a fog factor as a function of distance by using the parameters you pass it. How Direct3D computes distance varies depending on the projection matrix you use and on whether you've enabled range-based fog.
You use the first formula for computing linear fog. For linear fog, the start value defines the distance at which fog effects begin, end defines the distance at which fog effects no longer increase, and d specifies the distance from the scene's viewpoint. For all these values, 0.0 corresponds to the near plane and 1.0 corresponds to the far plane. Both pixel fog and vertex fog support linear fog.
The other two formulas Direct3D provides are D3DFOG_EXP and D3DFOG_EXP2. Only pixel fog supports these exponential fog formulas. In these formulas, e is the base of natural logarithms (~2.71828); density is an arbitrary fog density, which can range from 0.0 through 1.0; and d is the distance from the scene's viewpoint.
Figure 9-1 shows a graph of the three fog formulas. Densities of 0.33 and 0.66 are the formula parameters for both exponential formulas.
Figure 9-1 Comparison of fog formulas
The fog factors for each of the three fog effects, which are computed using the equations shown in Figure 9-1, are used in the Direct3D blending formula, which is computed as follows:
This formula (used for all DirectX devices) scales the color of the current polygon, Ci, by the fog factor, f, and then adds the product to the fog color, Cf, scaled by the inverse of the fog factor. The color value that is computed is a blend of the fog color and the original color, with more of the fog color and less of the original color being blended as the distance increases.