Use the following steps to enable vertex fog in your application:
To enable vertex fog
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 IDirect3DDevice3 interface.
void SetupVertexFog(DWORD dwColor, BOOL fUseRange)
{
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.
//
// REMEMBER: Vertex fog formula parameters are connected
// to the lighting states, not render states.
g_lpDevice->SetLightState(D3DLIGHTSTATE_FOGMODE, D3DFOG_LINEAR);
g_lpDevice->SetLightState(D3DLIGHTSTATE_FOGSTART, *(DWORD *)(&fStart));
g_lpDevice->SetLightState(D3DLIGHTSTATE_FOGEND, *(DWORD *)(&fEnd));
// Enable range-based fog if desired (only supported for vertex fog).
// For this example, it is assumed that fUseRange is set to non-zero
// 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 IDirect3DDevice3::SetRenderState and IDirect3DDevice3::SetLightState methods only accept 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.