The Code So Far

[Previous] [Next]

By varying the lighting, you can simulate day and night in your 3D world, but by adding fog to your application, you can simulate other important atmospheric effects. In addition, as mentioned at the beginning of the chapter, fog hides popping effects.

The code to add fog to our RoadRage application is fairly simple. You first need to add two member variables to your applications class:

 BOOL m_toggleFog; BOOL m_tableFog; 

You then need to add a code segment to set the render states necessary to generate either vertex or pixel fog. In the window's message-handling routine, CMyD3DApplication::MsgProc contained in the RoadRage.cpp file, you add code to the case statement in which the WM_COMMAND message is handled.

The code to set the appropriate fog states based on the fog type selection follows:

 // // Possible modes // -------------- // Vertex fog (fog type) //     Linear (fog mode) //         (Can set to range-based) //         Start //         End // // Pixel (table) fog (fog type) //     Fog mode - Linear //         Start //         End //     Fog mode - Exponential //         Density (0.0 - 1.0) //         case MENU_FOG_OFF:     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE,                                  FALSE);     m_toggleFog = FALSE;     m_tableFog = FALSE;     break; case MENU_VERTEXFOG:     // Set fog render states.     fStart =  13.0;      fEnd   =  500.0;     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, TRUE);     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGCOLOR,                                  // Highest 8 bits aren't used.                                  RGB_MAKE(0, 0, 80));      // Set the fog parameters.     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGTABLEMODE,                                    D3DFOG_NONE);     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGVERTEXMODE,                                   D3DFOG_LINEAR);     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGSTART,                                   *(DWORD *)(&fStart));     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGEND,                                     *(DWORD *)(&fEnd));     m_tableFog = FALSE;     break; case MENU_VERTEXFOG_RANGE:     // Set fog render states.     fStart =  13.0;     fEnd   =  500.0;     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, TRUE);     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGCOLOR,                                  // Highest 8 bits aren't used.                                  RGB_MAKE(0, 0, 80));      // Set the fog parameters.     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGTABLEMODE,                                  D3DFOG_NONE);     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGVERTEXMODE,                                   D3DFOG_LINEAR);     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGSTART,                                   *(DWORD *)(&fStart));     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGEND,                                     *(DWORD *)(&fEnd));     // Enable range-based fog if desired (supported only 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.     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_RANGEFOGENABLE,                                  TRUE);         m_tableFog = FALSE;     break; case MENU_TABLEFOGLIN:  // Pixel fog - linear     m_tableFog = TRUE;     fStart =  (float)0.8;      fEnd   =  100.0;        // Enable fog blending.     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, TRUE);       // Set the fog color.     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGCOLOR,                                   RGB_MAKE(0, 0, 80));              // Set the fog parameters.     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGVERTEXMODE,                                  D3DFOG_NONE );     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGTABLEMODE,                                   D3DFOG_LINEAR );     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGSTART,                                   *(DWORD *)(&fStart));     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGEND,                                     *(DWORD *)(&fEnd));     break; case MENU_TABLEFOGEXP:  // Pixel fog - exponential     m_tableFog = TRUE;     fDensity = 0.01f;  // For exponential modes       // Enable fog blending.     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, TRUE);       // Set the fog color.     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGCOLOR,                                   RGB_MAKE(0, 0, 80));              // Set the fog parameters.     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGVERTEXMODE,                                  D3DFOG_NONE);     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGTABLEMODE,                                  D3DFOG_EXP);     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGDENSITY,                                   *(DWORD *)(&fDensity));     break; case MENU_TABLEFOGEXP2:  // Pixel fog - exponential     m_tableFog = TRUE;     fDensity = 0.01f;  // for exponential modes       // Enable fog blending.     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, TRUE);       // Set the fog color.     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGCOLOR,                                   RGB_MAKE(0, 0, 80));              // Set the fog parameters.     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGVERTEXMODE,                                  D3DFOG_NONE);     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGTABLEMODE,                                  D3DFOG_EXP2);     m_pd3dDevice->SetRenderState(D3DRENDERSTATE_FOGDENSITY,                                   *(DWORD *)(&fDensity));     break; 

With the fog options added to the menu handler, you now need to add the Fog menu option to the Lighting drop-down menu. You can add this option when using Microsoft Visual C++ by adding a set of menu items to the lighting menu for each of the fog types to be generated. Figure 9-5 illustrates the lighting menu with the new fog additions (as well as a scene rendered with vertex fog to illustrate the effect).

click to view at full size.

Figure 9-5 The Lighting menu

With that, you've implemented everything you need to add fog to your programs. Figures 9-6 and 9-7 illustrate the effect you've created by adding fog to your scene. Notice that as you move forward in the scene from the position illustrated in Figure 9-6 to the one in Figure 9-7, the scenery in the distance becomes more visible. Also, you can see how the fog effect adds to the feel of the scene.

click to view at full size.

Figure 9-6 The effect of fog on distant scenery

click to view at full size.

Figure 9-7 The effect of fog on objects as the viewpoint moves closer to the objects



Inside Direct3D
Inside Direct3D (Dv-Mps Inside)
ISBN: 0735606137
EAN: 2147483647
Year: 1999
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net