Microsoft DirectX 8.1 (Visual Basic)

Pixel Fog

Pixel fog gets its name from the fact that it is calculated on a per-pixel basis in the device driver, unlike vertex fog, in which Microsoft® Direct3D® computes fog effects when it performs transformation and lighting. Pixel fog is sometimes called table fog because some drivers use a precalculated look-up table to determine the fog factor, using the depth of each pixel, to apply in blending computations.

Pixel fog can be applied using any fog formula identified by members of the CONST_D3DFOGMODE enumeration. Pixel-fog formula implementations are driver-specific, and if a driver doesn't support a complex fog formula, it should degrade to a less complex formula.

Note  Pixel fog is not supported when using a vertex shader.

Eye-Relative vs. Z-Based Depth

To alleviate fog-related graphic artifacts caused by uneven distribution of z-values within a depth buffer, most hardware devices use eye-relative depth instead of z-based depth values for pixel fog. Eye-relative depth is essentially the w element from a homogeneous coordinate set. Microsoft® Direct3D® takes the reciprocal of the RHW element from a device space coordinate set to reproduce true w. If a device supports eye-relative fog, it sets the D3DPRASTERCAPS_WFOG flag in the RasterCaps member of the D3DCAPS8 type when you call the Direct3DDevice8.GetDeviceCaps method.

Note  With the exception of the reference rasterizer, software devices always use z to calculate pixel fog effects.

When eye-relative fog is supported, the system automatically use eye-relative depth rather than z-based depth if the provided projection matrix produces z-values in world space that are equivalent to w-values in device space. You set the projection matrix by calling the Direct3DDevice8.SetTransform method, using the D3DTS_PROJECTION value from CONST_D3DTRANSFORMSTATETYPE and passing a D3DMATRIX type that represents the desired matrix. If the projection matrix isn't compliant with this requirement, fog effects are not applied properly. For details about producing a compliant matrix, see A W-Friendly Projection Matrix. The perspective projection matrix provided in What Is the Projection Transformation? produces a compliant projection matrix.

Direct3D uses the currently set projection matrix in its w-based depth calculations. As a result, an application must set a compliant projection matrix to receive the desired w-based features, even if it does not use the Direct3D transformation pipeline.

Direct3D checks the fourth column of the projection matrix. If the coefficients are [0,0,0,1] (for an affine projection) the system will use z-based depth values for fog. In this case, you must also specify the start and end distances for linear fog effects in device space, which ranges from 0.0 at the nearest point to the user, and 1.0 at the farthest point.

Using Pixel Fog

Use the following steps to enable pixel fog in your application.

Use the following steps to enable pixel fog in your application.

To enable pixel fog in a Visual Basic application

  1. Enable fog blending by setting the D3DRS_FOGENABLE render state to 1.
  2. Set the desired fog color in the D3DRS_FOGCOLOR render state.
  3. Choose the fog formula to use by setting the D3DRS_FOGTABLEMODE render state to the corresponding member of the CONST_D3DFOGMODE enumeration.
  4. Set the fog parameters as desired for the selected fog mode in the associated render states. This includes the start and end distances for linear fog, and fog density for exponential fog mode.

The following example shows what these steps might look like in code.

' For brevity, error values in this example are not checked
' after each call. A real-world application should check
' these values appropriately.
'
' For the purposes of this example, d3dDevice is a valid
' reference to a Direct3DDevice8 object.
Sub SetupPixelFog(Color As Long, Mode As CONST_D3DFOGMODE)
    Dim StartFog As Single, _
        EndFog As Single, _
        Density As Single
    
    ' For linear mode
    StartFog = 0.5: EndFog = 0.8
    
    ' For exponential mode
    Density = 0.66
 
    ' Enable fog blending.
    Call d3dDevice.SetRenderState(D3DRS_FOGENABLE, 1)
 
    ' Set the fog color.
    Call d3dDevice.SetRenderState(D3DRS_FOGCOLOR, Color)
    
    ' Set fog parameters.
    If Mode = D3DFOG_LINEAR Then
        Call d3dDevice.SetRenderState(D3DRS_FOGTABLEMODE, Mode)
        Call d3dDevice.SetRenderState(D3DRS_FOGSTART, StartFog)
        Call d3dDevice.SetRenderState(D3DRS_FOGEND, EndFog)
    Else
        Call d3dDevice.SetRenderState(D3DRS_FOGTABLEMODE, Mode)
        Call d3dDevice.SetRenderState(D3DRS_FOGDENSITY, Density)
    End If
End Sub