| DirectX SDK |
Use the following steps to enable vertex fog in your application:
To enable vertex fog in a C++ application
The following example, written in C++, 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 SetupVertexFog(DWORD dwColor, DWORD dwMode, BOOL fUseRange, FLOAT fDensity)
{
float fStart = 0.5f, // linear fog distances
fEnd = 0.8f;
// 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_FOGVERTEXMODE, dwMode);
g_lpDevice->SetRenderState(D3DRENDERSTATE_FOGSTART, *(DWORD *)(&fStart));
g_lpDevice->SetRenderState(D3DRENDERSTATE_FOGEND, *(DWORD *)(&fEnd));
}
else
{
g_lpDevice->SetRenderState(D3DRENDERSTATE_FOGVERTEXMODE, dwMode);
g_lpDevice->SetRenderState(D3DRENDERSTATE_FOGDENSITY, *(DWORD *)(&fDensity));
}
// Enable range-based fog if desired (only supported for vertex fog).
// For this example, it is assumed that fUseRange is set to a nonzero value
// only if the driver exposes the D3DPRASTERCAPS_FOGRANGE capability.
//
// Note: this is slightly more performance intensive
// than non-range-based fog.
if(fUseRange)
g_lpDevice->SetRenderState(
D3DRENDERSTATE_RANGEFOGENABLE,
TRUE);
}
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 successfully provides the floating-point values to these methods without data translation by casting the addresses of the floating-point variables as DWORD pointers, then dereferencing them.
To enable vertex fog from a Visual Basic application
The following Visual Basic example code 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
' reference to a Direct3DDevice7 object.
Sub SetupVertexFog(lColor As Long, Mode As CONST_D3DFOGMODE, _
bUseRange 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 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_FOGVERTEXMODE, Mode)
Call g_d3dDevice.SetRenderStateSingle(D3DRENDERSTATE_FOGSTART, StartFog)
Call g_d3dDevice.SetRenderStateSingle(D3DRENDERSTATE_FOGEND, EndFog)
Else
Call g_d3dDevice.SetRenderState(D3DRENDERSTATE_FOGVERTEXMODE, Mode)
Call g_d3dDevice.SetRenderStateSingle(D3DRENDERSTATE_FOGDENSITY, Density)
End If
' Enable range-based fog if desired (only supported for vertex fog).
' For this example, it is assumed that bUseRange 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 bUseRange = True Then
Call g_d3dDevice.SetRenderState( _
D3DRENDERSTATE_RANGEFOGENABLE, True)
End If
End Sub