xbdev - software development
Sunday March 26, 2023
home | about | contact | Donations


by bkenwright@xbdev.net


So how do we go about adding fog to our DirectX game or application?  Well you can accomplish it with a relatively few lines of code, as shown:


Code Snippet

// IDirect3DDevice8* pd3dDevice


void Fog()


      DWORD dwColour = 0xff666666;

      float start = 20.5f;

      float end  = 50.8f;


      float density = 0.66f;  // 0.0 to 1.0



      pd3dDevice->SetRenderState(D3DRS_FOGENABLE, TRUE);

      pd3dDevice->SetRenderState(D3DRS_FOGCOLOR, dwColour);




            pd3dDevice->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);

            pd3dDevice->SetRenderState(D3DRS_FOGSTART, *(DWORD*)(&start));

            pd3dDevice->SetRenderState(D3DRS_FOGEND,   *(DWORD*)(&end));




            pd3dDevice->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_EXP);

            pd3dDevice->SetRenderState(D3DRS_FOGDENSITY, *(DWORD*)(&density));





Where there are two main types of fog - linear and exponential.  As above I've shown how you switch fog on, and with the little if(..)else(..) statement you can choose whether you what type of fog you want.


    D3DFOG_NONE       0

    D3DFOG_EXP          1

    D3DFOG_EXP2        2
















Copyright (c) 2002-2023 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.