FOG effect in Direct93D

zhaozj2021-02-16  68

FOG effect in Direct93D

Increasing the FOG effect in a 3D scene is conducive to increasing the real feeling of the scene, the environment of the environment, shows the author's mood, and the blurred artistic sense. Microsoft DirectX9 supports two modes of Mode FOG: Pixel Fog and Vertex Fog, they all have their own characteristics and programming interfaces.

In essence, the occurrence of the fog effect is to mix the color of the object in the scene in the rendering scene, and the ratio of the fog color in the rendering process is in the scene during the rendering. The depth or distance from the perspective is proportional, that is, the farther away from the perspective (the greater the depth in the scene), the bigger the color of the FOG is in the proportion of the color in the rendering process. This causes the effect of the effect in reality.

Direct3D provides two ways to increase fog in a scene: Pixel Fog and Vertex Fog. Pixel Fog (also known as a Table FOG) is done in the device driver, and Vertex FOG is done in the light processing of Direct3D.

The above elaborates the probably principles of FOG, and we describe the programming of Pixel Fog and Vertex Fog:

First, use Pixel Fog

Follow the steps below to add a FOG effect in the program:

1. Start the FOG effect by setting the D3DRS_FOGENABLE rendering status to True.

IDirect3ddevice9 :: setrenderstate (D3DRS_FOGENABLE, TRUE);

2, set the FOG color we want by setting the D3DRS_FOGCOLOR rendering status as an integer value

IDirect3DDevice9 :: setrenderstate (D3DRS_FOGCOLOR, LFOGCOLOR);

3, set the D3DRS_FOGTABLEMODE rendering status to one of the enumeration values ​​of D3DFOGMODE to choose

Fog rules.

IDirect3DDevice9 :: Trenderstate (D3DRS_FogTableMode, Imode);

The value of Imode is one of the following:

D3DFOG_LINEAR, D3DFOG_NONE, D3DFOG_EXP, D3DFOG_EXP2

4. The corresponding parameter values ​​of the FOG are set in accordance with the different modes of the FOG selected in the third step, including information such as the start and end points of the FOG, the concentration of FOG.

If Imode is D3DFOG_LINEAR, set the start and termination points of the FOG:

IDirect3DDevice9 :: Trenderstate (D3DRS_FOGSTART, NSTARTPOS);

Idirect3DDevice9 :: Trenderstate (D3DRS_FoGEND, NENDPOS);

Otherwise, set the concentration of the FOG:

Idirect3dDevice9 :: Trenderstate (D3DRS_FOGDENSITY, NDENSITY);

The following example shows these steps:

/ / For short, all error code is slightly

// For this example, g_pdevice is a pointer to the 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 beding.

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 *));

g_pdevice-> setrenderstate (D3DRS_FOGEND, * (DWORD *));

}

Else

{

g_pdevice-> setrenderstate (D3DRS_FOGTABLEMODE, MODE);

g_pdevice-> setrenderstate (D3DRS_FOGDENSITY, * (DWORD *));

}

}

Second, use Vertex Fog

The use of Vertex Fog is almost identical to the use of Pixel Fog, except that the D3DRS_FOGTABLEMODE of step 3 is changed to D3DRS_FOGVERTEXMODE, and other exactly the same.

IDirect3ddevice9 :: Trenderstate (D3DRS_FogvertExmode, Imode);

转载请注明原文地址:https://www.9cbs.com/read-23804.html

New Post(0)