深度測試與alpha混合(1)

在繪製複雜的三維場景時,不可避免地會出現物體間的相互遮擋,在這種情況下,爲了正確地繪製場景需要使用深度測試。半透明物體的繪製不同於不透明物體,Direct3D通過alpha混合實現半透明物體的繪製。深度測試可以簡化複雜場景的繪製,alpha混合可以使繪製的三維場景更完整、更逼真。

在複雜的場景中,通常有多個物體需要繪製,這些物體之間通常會存在遮擋關係,離觀察點較遠的物體會因爲近處物體的者的遮擋而不可見或只有部分可見,Direct3D圖形系統提供了深度測試功能來實現這種效果。

 

深度緩衝區與深度測試

要理解深度測試,首先需要理解深度緩衝區。深度緩衝區是Direct3D用來存儲繪製到屏幕上的每個像素點的深度信息的一塊內存緩衝區。當Direct3D將一個場景渲染到目標表面上時,它使用深度緩衝區來決定光柵化後各個多邊形的像素的前後遮擋關係,最終決定哪個顏色值被繪製出來。也就是說,Direct3D通過比較當前繪製的像素點的深度和對應深度緩衝區的點的深度值來決定是否繪製當前像素。如果深度測試結果爲TRUE,則繪製當前像素,並用當前像素點的深度來更新深度緩衝區,反之則不予繪製。通常情況下,深度緩衝區對應於屏幕大小的一塊二維區域。

對一個啓用了深度緩衝區的場景進行光柵化操作時,渲染表面上的每個點都要進行深度測試。在深度測試開始時,深度緩衝區的深度值被設置爲該場景可能出現的最大值,渲染表面上的顏色值被設置爲背景顏色值。然後測試場景內即將繪製的每個多邊形,看它是否小於存儲在深度緩衝區中的深度值,如果該多邊形的深度值更小,則該深度值被更新到深度緩衝區中,並將渲染表面上當前點的顏色值替換爲該多邊形的顏色。如果多邊形在這一點的深度值更大,將繼續測試列表中的下一個多邊形。

 

創建深度緩衝區

若要在Direct3D圖形程序中應用深度測試,首先必須在創建Direct3D渲染設備時創建深度緩衝區,示例代碼如下:

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed			= TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE; // 表示由Direct3D創建並管理一個深度緩衝區
d3dpp.AutoDepthStencilFormat = D3DFMT_D16; // 表示深度緩衝區中每一個像素的深度值由16位的二進制數表示
if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_device)))
{
return false;
}

 

激活深度測試

深度緩衝區隨Direct3D渲染設備創建好後,調用Direct3D渲染狀態設置函數IDirect3DDevice9::SetRenderState(),將第一個參數設爲D3DRS_ZENABLE,第二個參數設爲TRUE,激活深度測試:

g_device->SetRenderState(D3DRS_ZENABLE,		 TRUE);

 

設置深度測試函數

接下來依然調用IDirect3DDevice9::SetRenderState()函數設置深度測試函數,第一個參數設置爲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.

第二個參數設置爲想要設置的深度測試函數,它屬於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.

通常情況下,深度測試函數設置爲D3DCMP_LESS,表示當測試點深度值小於深度緩衝區中相應值時,通過深度測試並繪製相關像素,這樣沒有被遮擋的物體才顯示,而被遮擋的物體就不顯示。示例代碼如下:

g_device->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);

 

更新深度緩衝區

設置了深度測試函數後,還需要設置深度測試成功時對深度緩衝區如何操作,是保持原來的深度值,還是用當前像素的深度值更新對應的數值。

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

表示如果通過測試,則用當前像素的深度值更新深度緩衝區中對應的數值,這是最常用的設置,也是默認設置。

 

示例程序

該示例程序繪製了一個矩形和一個坦克模型,其中先繪製矩形,再繪製坦克模型,而且坦克模型在矩形的後面被遮擋住。如果按下"1"鍵,則禁用深度測試,這時坦克雖然被矩形遮擋住,但仍然被繪製出來了;如果按下"2"鍵,則啓用深度測試,這時坦克模型被矩形遮擋住,沒有繪製出來。

按下數字鍵"1",禁用深度測試,被矩形部分遮住的坦克被繪製出來。

 

按下數字鍵"2",啓用深度測試,被矩形部分遮住的坦克沒有被繪製出來。

 

源程序:

#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.0f2.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/41.0f1.0f500.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(0sizeof(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/40.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(555), 1.0f0);

    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, 0sizeof(sCustomVertex));
    g_device
->SetFVF(D3DFVF_CUSTOM_VERTEX);
    g_device
->DrawPrimitive(D3DPT_TRIANGLESTRIP, 02);

    
// 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, 200100640480,
                             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, 00, PM_REMOVE))
            {
                TranslateMessage(
&msg);
                DispatchMessage(
&msg);
            }
                
            render();
        }
    }

    cleanup();
    UnregisterClass(CLASS_NAME, wc.hInstance);    

    
return 0;
}


 

下載示例工程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章