在繪制復(fù)雜的三維場(chǎng)景時(shí),不可避免地會(huì)出現(xiàn)物體間的相互遮擋,在這種情況下,為了正確地繪制場(chǎng)景需要使用深度測(cè)試。半透明物體的繪制不同于不透明物體,Direct3D通過(guò)alpha混合實(shí)現(xiàn)半透明物體的繪制。深度測(cè)試可以簡(jiǎn)化復(fù)雜場(chǎng)景的繪制,alpha混合可以使繪制的三維場(chǎng)景更完整、更逼真。
在復(fù)雜的場(chǎng)景中,通常有多個(gè)物體需要繪制,這些物體之間通常會(huì)存在遮擋關(guān)系,離觀察點(diǎn)較遠(yuǎn)的物體會(huì)因?yàn)榻幬矬w的者的遮擋而不可見(jiàn)或只有部分可見(jiàn),Direct3D圖形系統(tǒng)提供了深度測(cè)試功能來(lái)實(shí)現(xiàn)這種效果。
深度緩沖區(qū)與深度測(cè)試
要理解深度測(cè)試,首先需要理解深度緩沖區(qū)。深度緩沖區(qū)是Direct3D用來(lái)存儲(chǔ)繪制到屏幕上的每個(gè)像素點(diǎn)的深度信息的一塊內(nèi)存緩沖區(qū)。當(dāng)Direct3D將一個(gè)場(chǎng)景渲染到目標(biāo)表面上時(shí),它使用深度緩沖區(qū)來(lái)決定光柵化后各個(gè)多邊形的像素的前后遮擋關(guān)系,最終決定哪個(gè)顏色值被繪制出來(lái)。也就是說(shuō),Direct3D通過(guò)比較當(dāng)前繪制的像素點(diǎn)的深度和對(duì)應(yīng)深度緩沖區(qū)的點(diǎn)的深度值來(lái)決定是否繪制當(dāng)前像素。如果深度測(cè)試結(jié)果為TRUE,則繪制當(dāng)前像素,并用當(dāng)前像素點(diǎn)的深度來(lái)更新深度緩沖區(qū),反之則不予繪制。通常情況下,深度緩沖區(qū)對(duì)應(yīng)于屏幕大小的一塊二維區(qū)域。
對(duì)一個(gè)啟用了深度緩沖區(qū)的場(chǎng)景進(jìn)行光柵化操作時(shí),渲染表面上的每個(gè)點(diǎn)都要進(jìn)行深度測(cè)試。在深度測(cè)試開(kāi)始時(shí),深度緩沖區(qū)的深度值被設(shè)置為該場(chǎng)景可能出現(xiàn)的最大值,渲染表面上的顏色值被設(shè)置為背景顏色值。然后測(cè)試場(chǎng)景內(nèi)即將繪制的每個(gè)多邊形,看它是否小于存儲(chǔ)在深度緩沖區(qū)中的深度值,如果該多邊形的深度值更小,則該深度值被更新到深度緩沖區(qū)中,并將渲染表面上當(dāng)前點(diǎn)的顏色值替換為該多邊形的顏色。如果多邊形在這一點(diǎn)的深度值更大,將繼續(xù)測(cè)試列表中的下一個(gè)多邊形。
創(chuàng)建深度緩沖區(qū)
若要在Direct3D圖形程序中應(yīng)用深度測(cè)試,首先必須在創(chuàng)建Direct3D渲染設(shè)備時(shí)創(chuàng)建深度緩沖區(qū),示例代碼如下:
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE; // 表示由Direct3D創(chuàng)建并管理一個(gè)深度緩沖區(qū)
d3dpp.AutoDepthStencilFormat = D3DFMT_D16; // 表示深度緩沖區(qū)中每一個(gè)像素的深度值由16位的二進(jìn)制數(shù)表示
if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_device)))
{
return false;
}
激活深度測(cè)試
深度緩沖區(qū)隨Direct3D渲染設(shè)備創(chuàng)建好后,調(diào)用Direct3D渲染狀態(tài)設(shè)置函數(shù)IDirect3DDevice9::SetRenderState(),將第一個(gè)參數(shù)設(shè)為D3DRS_ZENABLE,第二個(gè)參數(shù)設(shè)為TRUE,激活深度測(cè)試:
g_device->SetRenderState(D3DRS_ZENABLE, TRUE);
設(shè)置深度測(cè)試函數(shù)
接下來(lái)依然調(diào)用IDirect3DDevice9::SetRenderState()函數(shù)設(shè)置深度測(cè)試函數(shù),第一個(gè)參數(shù)設(shè)置為D3DRS_ZFUNC:
- D3DRS_ZFUNC
- One member of the D3DCMPFUNC enumerated type. The
default value is D3DCMP_LESSEQUAL. This member enables an application to
accept or reject a pixel, based on its distance from the camera.
The depth value of the pixel is compared with the
depth-buffer value. If the depth value of the pixel passes the comparison
function, the pixel is written.
The depth value is written to the depth buffer only
if the render state is TRUE.
Software rasterizers and many hardware accelerators
work faster if the depth test fails, because there is no need to filter and
modulate the texture if the pixel is not going to be rendered.
第二個(gè)參數(shù)設(shè)置為想要設(shè)置的深度測(cè)試函數(shù),它屬于D3DCMPFUNC枚舉類型,定義如下:
Defines the supported compare functions.
typedef enum D3DCMPFUNC
{
D3DCMP_NEVER = 1,
D3DCMP_LESS = 2,
D3DCMP_EQUAL = 3,
D3DCMP_LESSEQUAL = 4,
D3DCMP_GREATER = 5,
D3DCMP_NOTEQUAL = 6,
D3DCMP_GREATEREQUAL = 7,
D3DCMP_ALWAYS = 8,
D3DCMP_FORCE_DWORD = 0x7fffffff,
} D3DCMPFUNC, *LPD3DCMPFUNC;
Constants
- D3DCMP_NEVER
- Always fail the test.
- D3DCMP_LESS
- Accept the new pixel if its value is less than the
value of the current pixel.
- D3DCMP_EQUAL
- Accept the new pixel if its value equals the value
of the current pixel.
- D3DCMP_LESSEQUAL
- Accept the new pixel if its value is less than or
equal to the value of the current pixel.
- D3DCMP_GREATER
- Accept the new pixel if its value is greater than
the value of the current pixel.
- D3DCMP_NOTEQUAL
- Accept the new pixel if its value does not equal
the value of the current pixel.
- D3DCMP_GREATEREQUAL
- Accept the new pixel if its value is greater than
or equal to the value of the current pixel.
- D3DCMP_ALWAYS
- Always pass the test.
- D3DCMP_FORCE_DWORD
- Forces this enumeration to compile to 32 bits in
size. Without this value, some compilers would allow this enumeration to
compile to a size other than 32 bits. This value is not used.
Remarks
The values in this enumerated type define the supported
compare functions for the D3DRS_ZFUNC, D3DRS_ALPHAFUNC, and D3DRS_STENCILFUNC
render states.
通常情況下,深度測(cè)試函數(shù)設(shè)置為D3DCMP_LESS,表示當(dāng)測(cè)試點(diǎn)深度值小于深度緩沖區(qū)中相應(yīng)值時(shí),通過(guò)深度測(cè)試并繪制相關(guān)像素,這樣沒(méi)有被遮擋的物體才顯示,而被遮擋的物體就不顯示。示例代碼如下:
g_device->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
更新深度緩沖區(qū)
設(shè)置了深度測(cè)試函數(shù)后,還需要設(shè)置深度測(cè)試成功時(shí)對(duì)深度緩沖區(qū)如何操作,是保持原來(lái)的深度值,還是用當(dāng)前像素的深度值更新對(duì)應(yīng)的數(shù)值。
- D3DRS_ZWRITEENABLE
- TRUE to enable the application to write to the
depth buffer. The default value is TRUE. This member enables an application
to prevent the system from updating the depth buffer with new depth values.
If FALSE, depth comparisons are still made according to the render state
D3DRS_ZFUNC, assuming that depth buffering is taking place, but depth values
are not written to the buffer.
示例代碼如下:
g_device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
表示如果通過(guò)測(cè)試,則用當(dāng)前像素的深度值更新深度緩沖區(qū)中對(duì)應(yīng)的數(shù)值,這是最常用的設(shè)置,也是默認(rèn)設(shè)置。
示例程序
該示例程序繪制了一個(gè)矩形和一個(gè)坦克模型,其中先繪制矩形,再繪制坦克模型,而且坦克模型在矩形的后面被遮擋住。如果按下"1"鍵,則禁用深度測(cè)試,這時(shí)坦克雖然被矩形遮擋住,但仍然被繪制出來(lái)了;如果按下"2"鍵,則啟用深度測(cè)試,這時(shí)坦克模型被矩形遮擋住,沒(méi)有繪制出來(lái)。

按下數(shù)字鍵"1",禁用深度測(cè)試,被矩形部分遮住的坦克被繪制出來(lái)。

按下數(shù)字鍵"2",啟用深度測(cè)試,被矩形部分遮住的坦克沒(méi)有被繪制出來(lái)。
源程序:
#include <d3dx9.h>
#pragma warning(disable : 4127)
#define CLASS_NAME "GameApp"
#define release_com(p) do { if(p) { (p)->Release(); (p) = NULL; } } while(0)
IDirect3D9* g_d3d;
IDirect3DDevice9* g_device;
ID3DXMesh* g_mesh;
D3DMATERIAL9* g_mesh_materials;
IDirect3DTexture9** g_mesh_textures;
DWORD g_num_materials;
IDirect3DVertexBuffer9* g_shutter_vb;
D3DXMATRIX g_mat_tank;
D3DXMATRIX g_mat_shutter;
struct sCustomVertex
{
float x, y, z;
DWORD color;
};
#define D3DFVF_CUSTOM_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)
void setup_matrices()
{
// setup world matrix
D3DXMATRIX mat_world;
D3DXMatrixIdentity(&mat_world);
g_device->SetTransform(D3DTS_WORLD, &mat_world);
// setup view matrix
D3DXVECTOR3 eye(0.0f, 2.0f, -30.0f);
D3DXVECTOR3 at(0.0f, 2.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX mat_view;
D3DXMatrixLookAtLH(&mat_view, &eye, &at, &up);
g_device->SetTransform(D3DTS_VIEW, &mat_view);
// setup projection matrix
D3DXMATRIX mat_proj;
D3DXMatrixPerspectiveFovLH(&mat_proj, D3DX_PI/4, 1.0f, 1.0f, 500.0f);
g_device->SetTransform(D3DTS_PROJECTION, &mat_proj);
}
bool init_geometry()
{
ID3DXBuffer* material_buffer;
/*
D3DXLoadMeshFromXA(
LPCSTR pFilename,
DWORD Options,
LPDIRECT3DDEVICE9 pD3DDevice,
LPD3DXBUFFER *ppAdjacency,
LPD3DXBUFFER *ppMaterials,
LPD3DXBUFFER *ppEffectInstances,
DWORD *pNumMaterials,
LPD3DXMESH *ppMesh);
*/
if(FAILED(D3DXLoadMeshFromX("tank.x", D3DXMESH_SYSTEMMEM, g_device, NULL, &material_buffer, NULL,
&g_num_materials, &g_mesh)))
{
MessageBox(NULL, "Could not find tank.x", "ERROR", MB_OK);
return false;
}
D3DXMATERIAL* xmaterials = (D3DXMATERIAL*) material_buffer->GetBufferPointer();
g_mesh_materials = new D3DMATERIAL9[g_num_materials];
g_mesh_textures = new IDirect3DTexture9*[g_num_materials];
for(DWORD i = 0; i < g_num_materials; i++)
{
g_mesh_materials[i] = xmaterials[i].MatD3D;
// set ambient reflected coefficient, because .x file do not set it.
g_mesh_materials[i].Ambient = g_mesh_materials[i].Diffuse;
g_mesh_textures[i] = NULL;
if(xmaterials[i].pTextureFilename != NULL && strlen(xmaterials[i].pTextureFilename) > 0)
D3DXCreateTextureFromFile(g_device, xmaterials[i].pTextureFilename, &g_mesh_textures[i]);
}
material_buffer->Release();
// create shutter vertex buffer
sCustomVertex vertices[] =
{
{ -8, -2, -10.0f, 0xFFFFFF00},
{ -8, 6, -10.0f, 0xFFFFFF00},
{ 8, -2, -10.0f, 0xFFFFFF00},
{ 8, 6, -10.0f, 0xFFFFFF00}
};
g_device->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_DEFAULT, &g_shutter_vb, NULL);
void* ptr;
g_shutter_vb->Lock(0, sizeof(vertices), &ptr, 0);
memcpy(ptr, vertices, sizeof(vertices));
g_shutter_vb->Unlock();
return true;
}
bool init_d3d(HWND hwnd)
{
g_d3d = Direct3DCreate9(D3D_SDK_VERSION);
if(g_d3d == NULL)
return false;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_device)))
{
return false;
}
if(! init_geometry())
return false;
setup_matrices();
g_device->SetRenderState(D3DRS_ZENABLE, TRUE);
g_device->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
g_device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
g_device->SetRenderState(D3DRS_AMBIENT, 0xFF00BB00);
// rotate tank
D3DXQUATERNION quat;
D3DXMATRIX mat_rotation;
D3DXMatrixIdentity(&g_mat_tank);
D3DXQuaternionRotationYawPitchRoll(&quat, D3DX_PI/4, -D3DX_PI/4, 0.0f);
D3DXMatrixRotationQuaternion(&mat_rotation, &quat);
D3DXMatrixMultiply(&g_mat_tank, &mat_rotation, &g_mat_tank);
D3DXMatrixIdentity(&g_mat_shutter);
return true;
}
void cleanup()
{
delete[] g_mesh_materials;
if(g_mesh_textures)
{
for(DWORD i = 0; i < g_num_materials; i++)
release_com(g_mesh_textures[i]);
delete[] g_mesh_textures;
}
release_com(g_mesh);
release_com(g_shutter_vb);
release_com(g_device);
release_com(g_d3d);
}
void render()
{
g_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(5, 5, 5), 1.0f, 0);
g_device->BeginScene();
// render shutter
g_device->SetRenderState(D3DRS_LIGHTING, FALSE);
g_device->SetTransform(D3DTS_WORLD, &g_mat_shutter);
g_device->SetStreamSource(0, g_shutter_vb, 0, sizeof(sCustomVertex));
g_device->SetFVF(D3DFVF_CUSTOM_VERTEX);
g_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
// render tank
g_device->SetRenderState(D3DRS_LIGHTING, TRUE);
g_device->SetTransform(D3DTS_WORLD, &g_mat_tank);
for(DWORD i = 0; i < g_num_materials; i++)
{
g_device->SetMaterial(&g_mesh_materials[i]);
g_device->SetTexture(0, g_mesh_textures[i]);
g_mesh->DrawSubset(i);
}
g_device->EndScene();
g_device->Present(NULL, NULL, NULL, NULL);
}
LRESULT WINAPI WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_KEYDOWN:
switch(wParam)
{
case 49: // press key "1", disable depth test.
g_device->SetRenderState(D3DRS_ZENABLE, FALSE);
break;
case 50: // press key "2", enable depth test.
g_device->SetRenderState(D3DRS_ZENABLE, TRUE);
break;
case VK_ESCAPE:
DestroyWindow(hwnd);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR, INT)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_CLASSDC;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = inst;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = CLASS_NAME;
wc.hIconSm = NULL;
if(! RegisterClassEx(&wc))
return -1;
HWND hwnd = CreateWindow(CLASS_NAME, "Direct3D App", WS_OVERLAPPEDWINDOW, 200, 100, 640, 480,
NULL, NULL, wc.hInstance, NULL);
if(hwnd == NULL)
return -1;
if(init_d3d(hwnd))
{
ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
render();
}
}
cleanup();
UnregisterClass(CLASS_NAME, wc.hInstance);
return 0;
}
下載示例工程