Using Pixel Fog
Use the following steps to enable pixel fog in your application.
- Enable fog blending by setting the D3DRS_FOGENABLE render state to TRUE.
- Set the desired fog color in the D3DRS_FOGCOLOR render state.
- Choose the fog formula to use by setting the D3DRS_FOGTABLEMODE render state to the corresponding member of the D3DFOGMODE enumerated type.
- 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_pDevice is a valid // pointer to an IDirect3DDevice9 interface. void SetupPixelFog(DWORD Color, DWORD Mode) { float Start = 0.5f; // For linear mode float End = 0.8f; float Density = 0.66f; // For exponential modes // Enable fog blending. g_pDevice->SetRenderState(D3DRS_FOGENABLE, TRUE); // Set the fog color. g_pDevice->SetRenderState(D3DRS_FOGCOLOR, Color); // Set fog parameters. if( Mode == D3DFOG_LINEAR ) { g_pDevice->SetRenderState(D3DRS_FOGTABLEMODE, Mode); g_pDevice->SetRenderState(D3DRS_FOGSTART, *(DWORD *)(&Start)); g_pDevice->SetRenderState(D3DRS_FOGEND, *(DWORD *)(&End)); } else { g_pDevice->SetRenderState(D3DRS_FOGTABLEMODE, Mode); g_pDevice->SetRenderState(D3DRS_FOGDENSITY, *(DWORD *)(&Density)); }