Microsoft DirectX 8.0 (Visual Basic)

Using Vertex Fog

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

To enable vertex fog from a Visual Basic application

  1. Enable fog blending by setting D3DRS_FOGENABLE to True.
  2. Set the fog color in the D3DRS_FOGCOLOR render state.
  3. Choose the desired fog formula by setting the D3DRS_FOGVERTEXMODE render state to a member of the CONST_D3DFOGMODE enumeration.
  4. Set the fog parameters as desired for the selected fog formula in the render states.

The following Microsoft® Visual Basic® example code shows what these steps might look like.

' 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 SetupVertexFog(Color As Long, Mode As CONST_D3DFOGMODE, _
                   UseRange As Boolean, Optional Density As Single)

    Dim StartFog As Single, _
        EndFog As Single
    
    ' Set linear fog distances
    StartFog = 0.5: EndFog = 0.8
 
    ' Enable fog blending.
    Call d3dDevice.SetRenderState(D3DRS_FOGENABLE, True)
 
    ' Set the fog color.
    Call d3dDevice.SetRenderState(D3DRS_FOGCOLOR, Color)
    
    ' Set fog parameters.
    If Mode = D3DFOG_LINEAR Then
        Call d3dDevice.SetRenderState(D3DRS_FOGVERTEXMODE, Mode)
        Call d3dDevice.SetRenderStateSingle(D3DRS_FOGSTART, StartFog)
        Call d3dDevice.SetRenderStateSingle(D3DRS_FOGEND, EndFog)
    Else
        Call d3dDevice.SetRenderState(D3DRS_FOGVERTEXMODE, Mode)
        Call d3dDevice.SetRenderStateSingle(D3DRS_FOGDENSITY, Density)
    End If

    ' Enable range-based fog if desired (only supported for vertex 
    ' fog). For this example, it is assumed that UseRange is set to 
    ' True only if the driver exposes the D3DPRASTERCAPS_FOGRANGE 
    ' capability.
    ' Note: This is slightly more performance intensive
    '       than non-range-based fog.
    If UseRange = True Then
        Call d3dDevice.SetRenderState( _
                       D3DRS_RANGEFOGENABLE, True)
    End If
End Sub