31. D3D雾

为3D场景增加雾会得到更真实的渲染效果。雾可以增加大量真实感,尤其对地形、树林地区以及幽灵般的户外背景。同样,雾可以很好地限制观察者对环境的可见度。现实中并不存在无限的视觉距离。事实上,这就是虚拟环境的一个镜像。通过限制观察距离,同样可以限制发送给管道的多边形数目。这对于提高渲染时间更有用。如果读者还记得PS游戏机上的第一个游戏《寂静岭》(Silient Hill),该游戏物景中有许多雾,很难看到外面的事物,除非是事物就在游戏玩家的面前,就可以很清楚地了解这一点。

       Direct3D中有两类硬件雾可用:顶点雾和像素雾。顶点雾是按照每个顶点来计算雾的数据,而像素雾根据像素级别计算雾的数据。由于通常像素比顶点要多得多,因此两者相比而言,像素雾的渲染速度较慢。同样,顶点雾提供了很好的视觉效果。有了顶点雾,就可以在表面上逐点内插处理雾的信息。

 使用雾就要使用RenderScene()函数。需要设置雾和使用雾的代码非常简短,演示程序也十分简单。

CHAPTER6文件夹中的Fog演示程序就像Antialiasing演示程序一样代码很少。在Fog演示程序中正方形充当大地,并启用雾。除了要设置雾和绘制的几何图形数据之外,Fog演示程序是第4章中Texture Mapping演示程序的改进版。如果读者想跟随本书一起学习,那么可以从Texture Mapping演示程序中提取代码,修改InitializeObjects()函数以使用雾,并修改纹理正方形的大小和方位。程序清单6.3给出了完整的InitializeObjects()函数。

bool InitializeObjects()
{
D3DCAPS9 caps;
g_D3DDevice
->GetDeviceCaps(&caps);

// Start and end distance of the fog.
float start = 2, end = 8;

// Set fog properties.
// 设置雾的相关属性
g_D3DDevice->SetRenderState(D3DRS_FOGENABLE, true); // 启用雾效果

// 设置要渲染的雾的期望颜色(这里为灰色)
g_D3DDevice->SetRenderState(D3DRS_FOGCOLOR,
D3DCOLOR_XRGB(
128, 128, 128));

// 使用顶点雾
g_D3DDevice->SetRenderState(D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR);

// 指定雾的开始距离和终点距离
g_D3DDevice->SetRenderState(D3DRS_FOGSTART, *(DWORD*)(&start));
g_D3DDevice
->SetRenderState(D3DRS_FOGEND, *(DWORD*)(&end));

// Pixel Fog 像素雾
//g_D3DDevice->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);

// Can only use if hardware supports it.
// 如果支持,则启用基于距离的雾
if(caps.RasterCaps & D3DPRASTERCAPS_FOGRANGE)
g_D3DDevice
->SetRenderState(D3DRS_RANGEFOGENABLE, true);


// Object
stD3DVertex objData[] =
{
{
-2.5f, -0.5f, -6.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 1.0f},
{
2.5f, -0.5f, -6.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 1.0f},
{
2.5f, -0.5f, 6.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 0.0f},

{
2.5f, -0.5f, 6.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 0.0f},
{
-2.5f, -0.5f, 6.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 0.0f},
{
-2.5f, -0.5f, -6.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 1.0f}
};

// Create the vertex buffer.
if(FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0,
D3DFVF_VERTEX, D3DPOOL_DEFAULT,
&g_VertexBuffer, NULL))) return false;

// Fill the vertex buffer.
void *ptr;
if(FAILED(g_VertexBuffer->Lock(0, sizeof(objData),
(
void**)&ptr, 0))) return false;
memcpy(ptr, objData,
sizeof(objData));
g_VertexBuffer
->Unlock();


// Load the texture image from file.
if(D3DXCreateTextureFromFile(g_D3DDevice, "ground.bmp",
&g_Texture) != D3D_OK) return false;


// Set default rendering states.
g_D3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
g_D3DDevice
->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);


// Set the projection matrix.
D3DXMatrixPerspectiveFovLH(&g_projection, 45.0f,
WINDOW_WIDTH
/WINDOW_HEIGHT, 0.1f, 1000.0f);

g_D3DDevice
->SetTransform(D3DTS_PROJECTION, &g_projection);


// Define camera information.
D3DXVECTOR3 cameraPos(0.0f, 0.0f, -3.0f);
D3DXVECTOR3 lookAtPos(
0.0f, 0.0f, 0.0f);
D3DXVECTOR3 upDir(
0.0f, 1.0f, 0.0f);

// Build view matrix.
D3DXMatrixLookAtLH(&g_ViewMatrix, &cameraPos,
&lookAtPos, &upDir);

return true;
}

  代码中,从InitializeObjects()函数开始,先获取设备性能。稍后将用其测试硬件是否支持D3DRS_RANGEFOGENABLE以便使用硬件雾范围。之后,通过几次调用SetRenderState()函数设置雾。第一种状态D3DRS_FOGENABLE用于在API中启用雾效果。第二种状态D3DRS_FOGCOLOR用于设置要渲染的雾的期望颜色。第三种状态D3DRS_FOGVERTEXMODE指明了正在使用顶点雾。使用像素雾将需要使用D3DRS_FOGTABLEMODE而不是D3DRS_FOGVERTEXMODE。第四种和第五种状态D3DRS_FOGSTART和D3DRS_FOGEND指定雾的开始距离和终点距离。这就会让Direct3D知道雾在观察者前面的开始距离以及最远处的终点位置。令人感兴趣的最后一行代码是最后的渲染状态D3DRS_RANGEFOGENABLE,它将启用基于距离的雾,该值在变换和光照阶段由Direct3D计算得到。基于距离的雾只能在支持它的硬件上使用,不支持它的硬件上当然就不能使用。所以本书使用设备性能来测试运行演示程序的硬件是否支持它。

完整代码:

#include <d3d9.h>
#include
<d3dx9.h>

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")

#define WINDOW_CLASS "UGPDX"
#define WINDOW_NAME "Fog"
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480

// Function Prototypes...
bool InitializeD3D(HWND hWnd, bool fullscreen);
bool InitializeObjects();
void RenderScene();
void Shutdown();


// Direct3D object and device.
LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice
= NULL;

// Matrices.
D3DXMATRIX g_projection;
D3DXMATRIX g_ViewMatrix;

// Vertex buffer to hold the geometry.
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;

// Holds a texture image.
LPDIRECT3DTEXTURE9 g_Texture = NULL;

// A structure for our custom vertex type
struct stD3DVertex
{
float x, y, z;
unsigned
long color;
float tu, tv;
};

// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)


LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(
0);
return 0;
break;

case WM_KEYUP:
if(wParam == VK_ESCAPE) PostQuitMessage(0);
break;
}

return DefWindowProc(hWnd, msg, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst, LPSTR cmdLine, int show)
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
WINDOW_CLASS, NULL };
RegisterClassEx(
&wc);

// Create the application's window
HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW,
100, 100, WINDOW_WIDTH, WINDOW_HEIGHT,
GetDesktopWindow(), NULL, wc.hInstance, NULL);

// Initialize Direct3D
if(InitializeD3D(hWnd, false))
{
// Show the window
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);

// Enter the message loop
MSG msg;
ZeroMemory(
&msg, sizeof(msg));

while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(
&msg);
DispatchMessage(
&msg);
}
else
RenderScene();
}
}

// Release any and all resources.
Shutdown();

// Unregister our window.
UnregisterClass(WINDOW_CLASS, wc.hInstance);
return 0;
}


bool InitializeD3D(HWND hWnd, bool fullscreen)
{
D3DDISPLAYMODE displayMode;

// Create the D3D object.
g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
if(g_D3D == NULL) return false;

// Get the desktop display mode.
if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode)))
return false;

// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(
&d3dpp, sizeof(d3dpp));

if(fullscreen)
{
d3dpp.Windowed
= FALSE;
d3dpp.BackBufferWidth
= WINDOW_WIDTH;
d3dpp.BackBufferHeight
= WINDOW_HEIGHT;
}
else
d3dpp.Windowed
= TRUE;
d3dpp.SwapEffect
= D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat
= displayMode.Format;

// Create the D3DDevice
if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_D3DDevice)))
{
return false;
}

// Initialize any objects we will be displaying.
if(!InitializeObjects()) return false;

return true;
}


bool InitializeObjects()
{
D3DCAPS9 caps;
g_D3DDevice
->GetDeviceCaps(&caps);

// Start and end distance of the fog.
float start = 2, end = 8;

// Set fog properties.
g_D3DDevice->SetRenderState(D3DRS_FOGENABLE, true);
g_D3DDevice
->SetRenderState(D3DRS_FOGCOLOR, D3DCOLOR_XRGB(128, 128, 128));
g_D3DDevice
->SetRenderState(D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR);
g_D3DDevice
->SetRenderState(D3DRS_FOGSTART, *(DWORD*)(&start));
g_D3DDevice
->SetRenderState(D3DRS_FOGEND, *(DWORD*)(&end));

// Pixel Fog
//g_D3DDevice->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);

// Can only use if hardware supports it.
if(caps.RasterCaps & D3DPRASTERCAPS_FOGRANGE)
g_D3DDevice
->SetRenderState(D3DRS_RANGEFOGENABLE, true);


// Object
stD3DVertex objData[] =
{
{
-2.5f, -0.5f, -6.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 1.0f},
{
2.5f, -0.5f, -6.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 1.0f},
{
2.5f, -0.5f, 6.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 0.0f},

{
2.5f, -0.5f, 6.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 0.0f},
{
-2.5f, -0.5f, 6.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 0.0f},
{
-2.5f, -0.5f, -6.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 1.0f}
};

// Create the vertex buffer.
if(FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0,
D3DFVF_VERTEX, D3DPOOL_DEFAULT,
&g_VertexBuffer, NULL))) return false;

// Fill the vertex buffer.
void *ptr;
if(FAILED(g_VertexBuffer->Lock(0, sizeof(objData),
(
void**)&ptr, 0))) return false;
memcpy(ptr, objData,
sizeof(objData));
g_VertexBuffer
->Unlock();


// Load the texture image from file.
if(D3DXCreateTextureFromFile(g_D3DDevice, "ground.bmp",
&g_Texture) != D3D_OK) return false;


// Set default rendering states.
g_D3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
g_D3DDevice
->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);


// Set the projection matrix.
D3DXMatrixPerspectiveFovLH(&g_projection, 45.0f,
WINDOW_WIDTH
/WINDOW_HEIGHT, 0.1f, 1000.0f);

g_D3DDevice
->SetTransform(D3DTS_PROJECTION, &g_projection);


// Define camera information.
D3DXVECTOR3 cameraPos(0.0f, 0.0f, -3.0f);
D3DXVECTOR3 lookAtPos(
0.0f, 0.0f, 0.0f);
D3DXVECTOR3 upDir(
0.0f, 1.0f, 0.0f);

// Build view matrix.
D3DXMatrixLookAtLH(&g_ViewMatrix, &cameraPos,
&lookAtPos, &upDir);

return true;
}


void RenderScene()
{
// Clear the backbuffer.
g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(128,128,128), 1.0f, 0);

// Begin the scene. Start rendering.
g_D3DDevice->BeginScene();

// Apply the view (camera).
g_D3DDevice->SetTransform(D3DTS_VIEW, &g_ViewMatrix);

// Draw square.
g_D3DDevice->SetTexture(0, g_Texture);
g_D3DDevice
->SetStreamSource(0, g_VertexBuffer, 0, sizeof(stD3DVertex));
g_D3DDevice
->SetFVF(D3DFVF_VERTEX);
g_D3DDevice
->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);

// End the scene. Stop rendering.
g_D3DDevice->EndScene();

// Display the scene.
g_D3DDevice->Present(NULL, NULL, NULL, NULL);
}


void Shutdown()
{
if(g_D3DDevice != NULL) g_D3DDevice->Release();
g_D3DDevice
= NULL;

if(g_D3D != NULL) g_D3D->Release();
g_D3D
= NULL;

if(g_VertexBuffer != NULL) g_VertexBuffer->Release();
g_VertexBuffer
= NULL;

if(g_Texture != NULL) g_Texture->Release();
g_Texture
= NULL;
}

  

在Direct3D 中,雾化是通过将景物颜色与雾的颜色,以随物体到观察点距离增加而衰减的混合因子混合而实现的。

       两种雾化方法:顶点雾化和像素雾化。

       三种雾化公式:线性雾化,指数雾化,指数平方雾化。

       两种雾化处理:基于深度的雾化处理和基于范围的雾化处理。基于深度是指两个点之间的深度(Z)差值,基于范围则是两点间的直线距离。Direct3D默认的是基于深度的雾化。可设置基于范围的雾化,但要先检测设备是否支持:

       g_pd3dDevice->GetDeviceCaps(&staps);

       if ( stCaps.RasterCaps & D3DPASTERCAPS_FOGRANGE )

            return TRUE;

       Direct3D默认禁用雾化效果,可以激活雾化:g_pd3dDevice->SetRendState( D3DRS_FOGENABLE, true );

       设置雾的颜色 g_pd3dDevice->SetRendState( D3DRS_FOGCOLOR, 0xffffffff); //白色

       根据按键消息进行多种雾化效果的切换

        if (gInputSystem->KeyDown(DIK_V))
        {
             //禁用雾化
             g_pd3dDevice->SetRenderState( D3DRS_FOGENABLE,FALSE );
        }

        31. D3D雾

        if (gInputSystem->KeyDown(DIK_M))
        {

             g_pd3dDevice->SetRenderState( D3DRS_FOGENABLE,TRUE );
             //设置雾化混合因子计算公式 :  指数雾化
             g_pd3dDevice->GetDevice()->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_EXP);
             //设置雾的浓度

             static float fogDensity = 0.005f; 
             g_pd3dDevice->SetRenderState(D3DRS_FOGDENSITY, *(DWORD*)&fogDensity);
        }

        31. D3D雾


        if (gInputSystem->KeyDown(DIK_N))
        {

             g_pd3dDevice->SetRenderState( D3DRS_FOGENABLE,TRUE );

             //激活基于范围的雾化
             g_pd3dDevice->SetRenderState( D3DRS_RANGEFOGENABLE,TRUE );

             //设置雾化混合因子计算公式  :  线性雾化
             g_pd3dDevice->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);
             //设置线性雾化开始位置和结束位置
             static float fogStart = 20;
             static float fogEnd = 200;
             g_pd3dDevice->SetRenderState(D3DRS_FOGSTART,*(DWORD*)&fogStart);     

             g_pd3dDevice->SetRenderState(D3DRS_FOGEND,*(DWORD*)&fogEnd);

        }

        31. D3D雾
        if (gInputSystem->KeyDown(DIK_B))
        {
             g_pd3dDevice->SetRenderState( D3DRS_FOGENABLE,TRUE );
             //设置雾化混合因子计算公式 :  指数平方雾化
             g_pd3dDevice->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_EXP2);
             //设置雾的浓度

             static float fogDensity = 0.01f;
             g_pd3dDevice->SetRenderState(D3DRS_FOGDENSITY, *(DWORD*)&fogDensity);
        }

        31. D3D雾

        其中设置雾化混合因子计算公式时,SetRenderState的第一个参数可以是:

        D3DRS_FOGTABLEMODE(像素雾化)  或  D3DRS_FOGVERTEXMODE(顶点雾化)

       

你可能感兴趣的:(3D)