• <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>

            天行健 君子當自強而不息

            深度測試與alpha混合(2)

            前面的一些例子中,已經遇到了alpha值的概念,如結構體D3DCOLORVALUE中的成員變量a,但它并沒有被使用,因為還沒有激活alpha混合(alpha blending),并且alpha總是被賦值為1.0f(默認渲染狀態下alpha為1.0f,表示完全不透明)。

             

            alpha混合原理

            在前面介紹的示例程序中,繪制圖形的顏色總是替換當前顏色緩沖區中存在的顏色,這樣后面的物體總是覆蓋在原有的物體上。但是當想要繪制類似于玻璃、水等具有透明效果的物體時,這種方法顯然滿足不了要求。通過定義一個表示物體半透明度的alpha值和一個半透明計算公式,可以將要繪制的物體顏色與顏色緩沖區中存在的顏色相混合,從而繪制出具有半透明效果的物體。Direct3D計算alpha顏色混合的方法如下:

            color = (RGBsrc * Ksrc) OP (RGBdst * Kdst)

            其中color表示alpha混合后的顏色值,RGBsrc表示源顏色值,即將要繪制的圖元的顏色值;Ksrc表示源混合系數,通常賦值為表示半透明程度的alpha值,也可以是屬于枚舉類型D3DBLEND的任意值,用來和RGBsrc相乘。RGBdst表示目標顏色值,即當前顏色緩沖區中的顏色值,Kdst表示目標混合系數,可以是屬于枚舉D3DBLEND的任意值,用來和RGBdst相乘。OP表示源計算結果與顏色緩沖區計算結果的混合方法,默認狀態下OP為D3DBLEND_ADD,即源計算結果與顏色緩沖區計算結果相加。

            圖形顯示中,對alpha混合最普遍的用法是:把Ksrc賦值為D3DBLEND_SRCALPHA,即當前繪制像素的alpha值;把Kdst賦值為D3DBLEND_INVSRCALPHA,即1減去當前繪制像素的alpha值;把OP賦值為D3DBLEND_ADD,使源計算結果和顏色緩沖區計算結果相加,這樣一來,alpha混合顏色的公式變為:

            color = (RGBsrc * Ksrc) + (RGBdst * Kdst)

            上面的設置可以較好地模擬大多數半透明物體的效果。

             

            啟用alpha混合

            想要繪制半透明物體,首先需要激活Direct3D的alpha混合運算,調用Direct3D渲染狀態設置函數IDirect3DDevice9:::SetRenderState(),將第一個參數設置為D3DRS_ALPHABLENDENABLE,第二個參數設置為TRUE,可以激活alpha混合,代碼如下:

            g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
             

            設置alpha混合系數

            在上面介紹的alpha混合原理中提到的源混合系數和目標混合系數,也是通過Direct3D渲染狀態設置函數IDirect3DDevice9::SetRenderState()設置的。若將第一個參數設置為D3DRS_SRCBLEND,則用于設置源混合系數,若將第一個參數設置為D3DRS_DESTBLEND,則用于設置目標混合系數,第二個參數可以設置為D3DBLEND枚舉常量,各具體枚舉常量的含義如下:

            Defines the supported blend mode.

            typedef enum D3DBLEND
            {
            D3DBLEND_ZERO = 1,
            D3DBLEND_ONE = 2,
            D3DBLEND_SRCCOLOR = 3,
            D3DBLEND_INVSRCCOLOR = 4,
            D3DBLEND_SRCALPHA = 5,
            D3DBLEND_INVSRCALPHA = 6,
            D3DBLEND_DESTALPHA = 7,
            D3DBLEND_INVDESTALPHA = 8,
            D3DBLEND_DESTCOLOR = 9,
            D3DBLEND_INVDESTCOLOR = 10,
            D3DBLEND_SRCALPHASAT = 11,
            D3DBLEND_BOTHSRCALPHA = 12,
            D3DBLEND_BOTHINVSRCALPHA = 13,
            D3DBLEND_BLENDFACTOR = 14,
            D3DBLEND_INVBLENDFACTOR = 15,
            D3DBLEND_SRCCOLOR2 = 16,
            D3DBLEND_INVSRCCOLOR2 = 17,
            D3DBLEND_FORCE_DWORD = 0x7fffffff,
            } D3DBLEND, *LPD3DBLEND;

            Constants

            D3DBLEND_ZERO
            Blend factor is (0, 0, 0, 0).
            D3DBLEND_ONE
            Blend factor is (1, 1, 1, 1).
            D3DBLEND_SRCCOLOR
            Blend factor is (Rs, Gs, Bs, As).
            D3DBLEND_INVSRCCOLOR
            Blend factor is (1 - Rs, 1 - Gs, 1 - Bs, 1 - As).
            D3DBLEND_SRCALPHA
            Blend factor is (As, As, As, As).
            D3DBLEND_INVSRCALPHA
            Blend factor is ( 1 - As, 1 - As, 1 - As, 1 - As).
            D3DBLEND_DESTALPHA
            Blend factor is (Ad Ad Ad Ad).
            D3DBLEND_INVDESTALPHA
            Blend factor is (1 - Ad 1 - Ad 1 - Ad 1 - Ad).
            D3DBLEND_DESTCOLOR
            Blend factor is (Rd, Gd, Bd, Ad).
            D3DBLEND_INVDESTCOLOR
            Blend factor is (1 - Rd, 1 - Gd, 1 - Bd, 1 - Ad).
            D3DBLEND_SRCALPHASAT
            Blend factor is (f, f, f, 1); where f = min(As, 1 - Ad).
            D3DBLEND_BOTHSRCALPHA
            Obsolete. Starting with DirectX 6, you can achieve the same effect by setting the source and destination blend factors to D3DBLEND_SRCALPHA and D3DBLEND_INVSRCALPHA in separate calls.
            D3DBLEND_BOTHINVSRCALPHA
            Source blend factor is (1 - As, 1 - As, 1 - As, 1 - As), and destination blend factor is (As, As, As, As); the destination blend selection is overridden. This blend mode is supported only for the D3DRS_SRCBLEND render state.
            D3DBLEND_BLENDFACTOR
            Constant color blending factor used by the frame-buffer blender. This blend mode is supported only if D3DPBLENDCAPS_BLENDFACTOR is set in the SrcBlendCaps or DestBlendCaps members of D3DCAPS9.
            D3DBLEND_INVBLENDFACTOR
            Inverted constant color-blending factor used by the frame-buffer blender. This blend mode is supported only if the D3DPBLENDCAPS_BLENDFACTOR bit is set in the SrcBlendCaps or DestBlendCaps members of D3DCAPS9.
            D3DBLEND_SRCCOLOR2
            Blend factor is (PSOutColor[1]r, PSOutColor[1]g, PSOutColor[1]b, not used). See Render Target Blending.
            Differences between Direct3D 9 and Direct3D 9Ex:

            This flag is available in Direct3D 9Ex only.

            D3DBLEND_INVSRCCOLOR2
            Blend factor is (1 - PSOutColor[1]r, 1 - PSOutColor[1]g, 1 - PSOutColor[1]b, not used)). See Render Target Blending.
            Differences between Direct3D 9 and Direct3D 9Ex:

            This flag is available in Direct3D 9Ex only.

            D3DBLEND_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

            In the preceding member descriptions, the RGBA values of the source and destination are indicated by the s and d subscripts.

            The values in this enumerated type are used by the following render states:

            • D3DRS_DESTBLEND
            • D3DRS_SRCBLEND
            • D3DRS_DESTBLENDALPHA
            • D3DRS_SRCBLENDALPHA

            See D3DRENDERSTATETYPE

            Render Target Blending

            Direct3D 9Ex has improved text rendering capabilities. Rendering clear-type fonts would normally require two passes. To eliminate the second pass, a pixel shader can be used to output two colors, which we can call PSOutColor[0] and PSOutColor[1]. The first color would contain the standard 3 color components (RGB). The second color would contain 3 alpha components (one for each component of the first color).

            These new blending modes are only used for text rendering on the first render target.

            設置alpha混合系數的代碼示例如下:

            g_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
            g_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

             

            設置alpha混合方法

            alpha混合方法指定源顏色和目標顏色的混合方法,通過Direct3D渲染狀態設置函數IDirect3DDevice9::SetRenderState()設置,其中第一個參數設置為 D3DRS_BLENDOP,第二個參數設置為D3DBLENDOP枚舉常量,各常量的含義如下:

            Defines the supported blend operations. See Remarks for definitions of terms.

            typedef enum D3DBLENDOP
            {
            D3DBLENDOP_ADD = 1,
            D3DBLENDOP_SUBTRACT = 2,
            D3DBLENDOP_REVSUBTRACT = 3,
            D3DBLENDOP_MIN = 4,
            D3DBLENDOP_MAX = 5,
            D3DBLENDOP_FORCE_DWORD = 0x7fffffff,
            } D3DBLENDOP, *LPD3DBLENDOP;

            Constants

            D3DBLENDOP_ADD
            The result is the destination added to the source. Result = Source + Destination
            D3DBLENDOP_SUBTRACT
            The result is the destination subtracted from to the source. Result = Source - Destination
            D3DBLENDOP_REVSUBTRACT
            The result is the source subtracted from the destination. Result = Destination - Source
            D3DBLENDOP_MIN
            The result is the minimum of the source and destination. Result = MIN(Source, Destination)
            D3DBLENDOP_MAX
            The result is the maximum of the source and destination. Result = MAX(Source, Destination)
            D3DBLENDOP_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

            Source, Destination, and Result are defined as:

            Term Type Description
            Source Input Color of the source pixel before the operation.
            Destination Input Color of the pixel in the destination buffer before the operation.
            Result Output Returned value that is the blended color resulting from the operation.

            This enumerated type defines values used by the following render states:

            • D3DRS_BLENDOP
            • D3DRS_BLENDOPALPHA

             

            示例程序:

            該示例程序模擬了直升飛機螺旋槳的半透明效果。在程序的初始化階段,載入Heli.x文件,它是一個包含直升飛機的三維模型文件,其中螺旋槳的材質漫反射屬性為(R, G, B, A) = (0.183700; 0.183700; 0.183700; 0.500000; ),可以用文本方式打開Heli.x查看它的材質屬性,Heli.x是一個文本格式的.x文件。

            示例程序中沒有設置alpha混合方法,所以應用程序將采用默認的alpha混合方法D3DBLEND_ADD,即將源計算結果與顏色緩沖區計算結果相加。由于直升機螺旋槳的材質的alpha值為0.5f,因此它的最終顏色就是50%的玻璃顏色加上50%的背景顏色。

            按下數字鍵"1",激活alpha混合。

             

            按下數字鍵"0",禁用alpha混合。

             

            設置alpha混合方法為D3DBLENDOP_SUBTRACT的效果圖,啟用alpha混合時。

            g_device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);

             

            設置以下代碼時的效果圖,可以看到直升機變亮了,啟用alpha混合:

            g_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
            g_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_DESTCOLOR);

             

            設置以下代碼時的效果圖,可以看到直升機變暗了,啟用alpha混合:

            g_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
            g_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVDESTCOLOR);

             

            可以根據需要自己修改設置,以實現不同的顏色混合效果。

             

            由于alpha混合是當前繪制的像素顏色與顏色緩沖區中存在的顏色的混合運算,因此,在繪制半透明物體前,必須保證位于半透明物體后的物體先于半透明物體繪制,也就是說,先繪制不透明物體,再繪制半透明物體。

            有些計算機硬件由于功能的限制,可能不支持某些混合方法,這時Direct3D會自動使用alpha混合方法D3DBLENDOP_ADD。

             

            源程序:

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

            void setup_world_matrix()
            {
                D3DXMATRIX mat_world;
                D3DXMatrixRotationY(
            &mat_world, timeGetTime() / 1000.0f);
                g_device
            ->SetTransform(D3DTS_WORLD, &mat_world);
            }

            void setup_view_proj_matrices()
            {
                
            // setup view matrix

                D3DXVECTOR3 eye(
            0.0f15.0f-20.0f);
                D3DXVECTOR3 at(
            0.0f,  0.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("heli.x", D3DXMESH_SYSTEMMEM, g_device, NULL, &material_buffer, NULL,
                                            
            &g_num_materials, &g_mesh)))
                {
                    MessageBox(NULL, 
            "Could not find heli.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();

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

                g_device
            ->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
                g_device
            ->SetRenderState(D3DRS_SRCBLEND,         D3DBLEND_SRCALPHA);
                g_device
            ->SetRenderState(D3DRS_DESTBLEND,         D3DBLEND_INVSRCALPHA);    

                
            //g_device->SetRenderState(D3DRS_SRCBLEND,         D3DBLEND_ONE);
                
            //g_device->SetRenderState(D3DRS_DESTBLEND,         D3DBLEND_INVDESTCOLOR);    

                
            //g_device->SetRenderState(D3DRS_BLENDOP,             D3DBLENDOP_SUBTRACT);        

                g_device
            ->SetRenderState(D3DRS_ZENABLE,         TRUE);
                g_device
            ->SetRenderState(D3DRS_ZFUNC,         D3DCMP_LESS);
                g_device
            ->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);

                g_device
            ->SetRenderState(D3DRS_AMBIENT, 0xFFFFBB55);
                
                
            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_device);
                release_com(g_d3d);
            }

            void render()
            {
                g_device
            ->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(555), 1.0f0);

                g_device
            ->BeginScene();

                setup_world_matrix();

                
            // render opaque object first
                for(DWORD i = 0; i < g_num_materials; i++)
                {
                    
            if(g_mesh_materials[i].Diffuse.a == 1.0f)
                    {
                        g_device
            ->SetMaterial(&g_mesh_materials[i]);
                        g_device
            ->SetTexture(0, g_mesh_textures[i]);

                        g_mesh
            ->DrawSubset(i);
                    }        
                }        

                
            // render transparent object second
                for(DWORD i = 0; i < g_num_materials; i++)
                {
                    
            if(g_mesh_materials[i].Diffuse.a != 1.0f)
                    {
                        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 48// press key "0", disable alpha blend.
                        g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
                        
            break;

                    
            case 49// press key "1", enable alpha blend.
                        g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, 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-10 11:44 lovedday 閱讀(4309) 評論(1)  編輯 收藏 引用

            評論

            # re: 深度測試與alpha混合(2) 2012-10-18 01:23 小馬甲

            再一次的跑到朋友這里。。。  回復  更多評論   

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            精品亚洲综合久久中文字幕| 亚洲精品乱码久久久久久不卡| 久久久久亚洲AV成人片| 狠狠干狠狠久久| 久久久高清免费视频| 伊人久久久AV老熟妇色| 久久这里只有精品首页| 精品久久久久久无码不卡| 国产一级持黄大片99久久| 一级a性色生活片久久无| 99久久精品免费国产大片| 久久精品国产免费观看| 精品久久国产一区二区三区香蕉| 无码专区久久综合久中文字幕| 国产精品成人久久久久久久| 久久亚洲精品中文字幕| 色狠狠久久综合网| 久久久久久亚洲精品无码| 久久被窝电影亚洲爽爽爽| 亚洲精品乱码久久久久久蜜桃不卡 | 无码超乳爆乳中文字幕久久| 久久久99精品成人片中文字幕| 久久99毛片免费观看不卡| 亚洲精品无码久久久久sm| 久久久久久久97| 国产精品久久久香蕉| 精品久久久一二三区| 2019久久久高清456| 2021国产精品久久精品| 久久91精品国产91久| 久久久午夜精品| 99久久99久久精品国产片果冻| 欧美粉嫩小泬久久久久久久 | 伊人色综合久久天天人守人婷| 狠狠色综合久久久久尤物| 久久99精品免费一区二区| 精品无码久久久久久国产| 久久精品亚洲欧美日韩久久| 日本久久久久久久久久| 久久亚洲sm情趣捆绑调教| 亚洲AV无码久久|