www.xbdev.net
xbdev - software development
Friday May 9, 2025
Home | Contact | Support | Special Effects with DirectX.. It's doing all the hard work for us...
>>
     
 

Special Effects with DirectX..

It's doing all the hard work for us...

 

FOG

by bkenwright@xbdev.net



Compare with and without fog. As you can easily see - fog helps you
Compare with and without fog. As you can easily see - fog helps you 'hide' things in the distance.


Fog is an essential feature in nearly all virtual worlds and games - and allows us to blend objects in the distance with the background - this is especially useful when you're loading in buildings or characters in the distance - instead of having them pop in - you can have them smoothly appear out of the horizon (come out of the fog).

Enabling Fog Effect in DirectX


So how do we go about adding fog to our DirectX game or application? Well you can accomplish it easily! DirectX 8.0 has a fog state - which you can enable wiht a few lines of code, as shown:

Code Snippet - How to Enable Fog in DirectX.8
// 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_FOGENABLETRUE);
      
pd3dDevice->SetRenderState(D3DRS_FOGCOLORdwColour);
 
      if(
true)
      {
            
pd3dDevice->SetRenderState(D3DRS_FOGTABLEMODED3DFOG_LINEAR);
            
pd3dDevice->SetRenderState(D3DRS_FOGSTART, *(DWORD*)(&start));
            
pd3dDevice->SetRenderState(D3DRS_FOGEND,   *(DWORD*)(&end));
      }
      else
      {
            
pd3dDevice->SetRenderState(D3DRS_FOGTABLEMODED3DFOG_EXP);
            
pd3dDevice->SetRenderState(D3DRS_FOGDENSITY, *(DWORD*)(&density));
      }
 
}


Types of Fog


There are two main types of fog - linear and exponential. As above in the code snipppet - you can switch fog on using a little
if(..)else(..)
statement - this can also be used to choose a different type of fog.

D3DFOG_NONE     0
D3DFOG_EXP      1
D3DFOG_EXP2     2
D3DFOG_LINEAR   3


We can show the impact the diffrent falloff values have on the fog density using a simple plot:



Linear, Exp and Exp2 fog falloff in DirectX.
Linear, Exp and Exp2 fog falloff in DirectX.



Resources & Links


• Visualization (Graph) fog falloff [LINK]





 
Advert (Support Website)

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