Platform SDK: DirectX

Using Pixel Fog

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

[C++]

To enable pixel fog in a C++ application

  1. Enable fog blending by setting the D3DRENDERSTATE_FOGENABLE render state to TRUE.
  2. Set the desired fog color in the D3DRENDERSTATE_FOGCOLOR render state.
  3. Choose the fog formula you want to use by setting the D3DRENDERSTATE_FOGTABLEMODE render state to the corresponding member of the D3DFOGMODE enumerated type.
  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, g_lpDevice is a valid
// pointer to an IDirect3DDevice7 interface.
void SetupPixelFog(DWORD dwColor, DWORD dwMode)
{
    float fStart = 0.5f,    // for linear mode
          fEnd   = 0.8f,
          fDensity = 0.66;  // for exponential modes
 
    // Enable fog blending.
    g_lpDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, TRUE);
 
    // Set the fog color.
    g_lpDevice->SetRenderState(D3DRENDERSTATE_FOGCOLOR, dwColor);
    
    // Set fog parameters.
    if(D3DFOG_LINEAR == dwMode)
    {
        g_lpDevice->SetRenderState(D3DRENDERSTATE_FOGTABLEMODE, dwMode);
        g_lpDevice->SetRenderState(D3DRENDERSTATE_FOGSTART, *(DWORD *)(&fStart));
        g_lpDevice->SetRenderState(D3DRENDERSTATE_FOGEND,   *(DWORD *)(&fEnd));
    }
    else
    {
        g_lpDevice->SetRenderState(D3DRENDERSTATE_FOGTABLEMODE, dwMode);
        g_lpDevice->SetRenderState(D3DRENDERSTATE_FOGDENSITY, *(DWORD *)(&fDensity));
    }
 
}

Note  Some fog parameters are required as floating-point values, even though the IDirect3DDevice7::SetRenderState method only accepts DWORD values in the second parameter. The preceding example provides the floating-point values to SetRenderState without data translation by casting the addresses of the floating-point variables as DWORD pointers, then dereferencing them.

[Visual Basic]

To enable pixel fog in a Visual Basic application

  1. Enable fog blending by setting the D3DRENDERSTATE_FOGENABLE render state to True.
  2. Set the desired fog color in the D3DRENDERSTATE_FOGCOLOR render state.
  3. Choose the fog formula you want to use by setting the D3DRENDERSTATE_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, g_d3dDevice is a valid
' pointer to a Direct3DDevice7 object.
Sub SetupPixelFog(lColor 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 g_d3dDevice.SetRenderState(D3DRENDERSTATE_FOGENABLE, True)
 
    ' Set the fog color.
    Call g_d3dDevice.SetRenderState(D3DRENDERSTATE_FOGCOLOR, lColor)
    
    ' Set fog parameters.
    If Mode = D3DFOG_LINEAR Then
        Call g_d3dDevice.SetRenderState(D3DRENDERSTATE_FOGTABLEMODE, Mode)
        Call g_d3dDevice.SetRenderStateSingle(D3DRENDERSTATE_FOGSTART, StartFog)
        Call g_d3dDevice.SetRenderStateSingle(D3DRENDERSTATE_FOGEND, EndFog)
    Else
        Call g_d3dDevice.SetRenderState(D3DRENDERSTATE_FOGTABLEMODE, Mode)
        Call g_d3dDevice.SetRenderStateSingle(D3DRENDERSTATE_FOGDENSITY, Density)
    End If
End Sub