Fog Formulas

[Previous] [Next]

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:

  • D3DFOG_NONE No fog is used.
  • D3DFOG_LINEAR The fog increases linearly between the start and end points, using the following formula:
  • D3DFOG_EXP The fog increases exponentially, using the following formula:
  • D3DFOG_EXP2 The fog increases exponentially with the square of the distance, using the following formula:
  • D3DFOG_FORCE_DWORD This member forces this enumerated type to be 32 bits.

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.

click to view at full size.

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.



Inside Direct3D
Inside Direct3D (Dv-Mps Inside)
ISBN: 0735606137
EAN: 2147483647
Year: 1999
Pages: 131

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