• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天行健 君子當(dāng)自強(qiáng)而不息

            深度測(cè)試與alpha混合(1)

            在繪制復(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.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;
            }

             

            下載示例工程


            posted on 2008-05-09 17:45 lovedday 閱讀(3737) 評(píng)論(6)  編輯 收藏 引用

            評(píng)論

            # re: 深度測(cè)試與alpha混合(1) 2008-07-21 16:09 宋明建

            真的寫(xiě)的不錯(cuò),看了你寫(xiě)的東西后,我總算看懂了個(gè)一二三。不像有的書(shū),看了半天,不知所云。
            頂!  回復(fù)  更多評(píng)論   

            # re: 深度測(cè)試與alpha混合(1) 2008-11-25 15:27 flysnow

            寫(xiě)的太好了,狂頂下,感謝樓主  回復(fù)  更多評(píng)論   

            # re: 深度測(cè)試與alpha混合(1)[未登錄](méi) 2008-11-25 22:52 Flash


            無(wú)論從哪個(gè)角度看,

            你對(duì)生活,對(duì)技術(shù)都是理解很深的人!

              回復(fù)  更多評(píng)論   

            # re: 深度測(cè)試與alpha混合(1) 2009-06-05 08:53 ROCK

            3q  回復(fù)  更多評(píng)論   

            # re: 深度測(cè)試與alpha混合(1) 2010-11-11 23:36 bob

            嗯 寫(xiě)的不錯(cuò) 多謝了  回復(fù)  更多評(píng)論   

            # re: 深度測(cè)試與alpha混合(1)[未登錄](méi) 2012-01-17 00:31 star

            THX,讓知識(shí)鞏固了不少  回復(fù)  更多評(píng)論   

            公告

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關(guān)鏈接

            搜索

            最新評(píng)論

            日韩电影久久久被窝网| 少妇无套内谢久久久久| 性高湖久久久久久久久| 久久精品国产精品亚洲| 亚洲欧美精品伊人久久| 久久精品国产亚洲av麻豆色欲| 久久久久久久久久久| 亚洲人成电影网站久久| 无码乱码观看精品久久| 久久久久久亚洲精品不卡| 久久99精品久久久久久9蜜桃| 91精品国产综合久久四虎久久无码一级| 久久精品aⅴ无码中文字字幕重口| 人妻丰满AV无码久久不卡| 久久精品国产亚洲AV无码娇色| 久久精品aⅴ无码中文字字幕重口| 国产精品一区二区久久国产| 72种姿势欧美久久久久大黄蕉| 国产一级做a爰片久久毛片| 青青青伊人色综合久久| 久久久久久久久久久免费精品| 日日狠狠久久偷偷色综合96蜜桃| 久久久久99这里有精品10 | 亚洲综合伊人久久大杳蕉| 天天躁日日躁狠狠久久| 99久久国产热无码精品免费 | 精品人妻伦一二三区久久| 久久综合九色综合欧美就去吻| 亚洲国产婷婷香蕉久久久久久| 狠狠色丁香婷婷久久综合| 日韩人妻无码一区二区三区久久| 国产成人久久激情91| 久久国产福利免费| 精品人妻伦九区久久AAA片69| 国产精品久久久久久搜索| 久久久免费观成人影院 | 韩国三级大全久久网站| 久久久久久A亚洲欧洲AV冫| 亚洲国产精品无码久久| 日韩精品久久久久久| 色青青草原桃花久久综合|