• <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)而不息

            D3D中的Alpha顏色混合(1)

             

            提示:

            閱讀本文需要一定的3D圖形學(xué)和DirectX9基礎(chǔ),如果你發(fā)現(xiàn)閱讀困難,請(qǐng)參閱D3D中的材質(zhì)和光照處理
            本文用到的坐標(biāo)系統(tǒng)變換函數(shù)請(qǐng)參閱DirectX 9的坐標(biāo)系統(tǒng)變換



            渲染管道流水線通常需要將來(lái)自頂點(diǎn)的顏色,紋理像素的顏色,光照顏色以及物體表面材質(zhì)反射光顏色進(jìn)行混合,生成計(jì)算機(jī)屏幕的像素顏色。將多種顏色混合在一起,必須考慮各種顏色的成分比例,這個(gè)比例由Alpha因子決定。對(duì)于游戲開(kāi)發(fā)來(lái)說(shuō),利用Alpha顏色混合可產(chǎn)生背景透明的渲染效果。

            顏色混合原理

            一般的,屏幕像素的當(dāng)前顏色值SrcColor可與目標(biāo)像素顏色值DestColor進(jìn)行如下運(yùn)算,然后將獲得的顏色值Color作為該像素的新顏色,以實(shí)現(xiàn)像素的目標(biāo)顏色與源顏色的混合。

            Color = SrcColor * SrcBlend + DestColor * DestBlend

            這里,SrcBlend和DestBlend為源混合因子和目標(biāo)混合因子,分別乘以源顏色和目標(biāo)顏色。SrcColor ,SrcBlend , DestColor ,DestBlend都是一個(gè)4維向量,而乘法運(yùn)算 * 則是一個(gè)一個(gè)向量點(diǎn)積運(yùn)算。

            假設(shè)4維向量SrcColor=(Rs, Gs, Bs, As),SrcBlend=(S1, S2, S3, S4), DestColor=(Rd, Gd, Bd, Ad),DestBlend(D1, D2, D3, D4),則混合顏色Color可用4維向量表示為:

            Color = (Rs * S1 + Rd * D1, Gs * S2 + Gd * D2, Bs * S3 + Bd * D3, As * S4 + Ad * D4)

            利用Direct3D設(shè)備接口提供的SetRenderState函數(shù)可將所要使用的混合因子設(shè)置給渲染管道流水線。此時(shí),函數(shù)的第一個(gè)參數(shù)必須指定為D3DRS_SRCBLEND或D3DRS_DESTBLEND,分別表示設(shè)置源混合因子和目標(biāo)混合因子,如下所示:
             
            // IDirect3DDevice9* _d3d_device;

            // set alpha blend for source color
             _d3d_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);

             
            // set alpha blend for dest color
              _d3d_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

            D3DBLEND_SRCALPHA和D3DBLEND_INVSRCALPHA均為DirectX預(yù)定義的混合因子宏,來(lái)看看具體定義:

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

            由于渲染管道流水線的默認(rèn)Alpha顏色混合功能是禁用的,因此必須調(diào)用SetRenderState函數(shù)設(shè)置D3DRS_ALPHABLENDENABLE為true.
             
            // enable alpha-blended transparency
            _d3d_device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);

             

            來(lái)看一個(gè)具體的例子:

            需要在工程中設(shè)置鏈接d3dx9.lib d3d9.lib。
            由于文件中用到了GE_APP這個(gè)類,它的具體使用說(shuō)明請(qǐng)參閱 主窗口和DirectInput的封裝。


            若發(fā)現(xiàn)代碼中存在錯(cuò)誤,敬請(qǐng)指出。

            源碼下載

            來(lái)看看AlphaBlend.h的定義:

             
            /*************************************************************************************
             [Include File]

             PURPOSE: 
                Define for alpha blend.
            *************************************************************************************/


            #ifndef ALPHA_BLEND_H
            #define ALPHA_BLEND_H

            struct CUSTOM_VERTEX
            {
                float x, y, z;
                float nx, ny, nz;
            };

            #define CUSTOM_VERTEX_FVF   (D3DFVF_XYZ | D3DFVF_NORMAL)

            class ALPHA_BLEND
            {
            private:
                IDirect3D9* _d3d;
                IDirect3DDevice9* _d3d_device;
                IDirect3DVertexBuffer9* _vertex_buffer1;
                IDirect3DVertexBuffer9* _vertex_buffer2;

            public:
                ALPHA_BLEND();
                ~ALPHA_BLEND();

                bool Create_D3D_Device(HWND hwnd, bool full_screen = true);
                bool Init_Vertex_Buffer1();
                bool Init_Vertex_Buffer2();
                void Compute_Triangle_Normal(D3DXVECTOR3& v1, D3DXVECTOR3& v2, D3DXVECTOR3& v3, D3DVECTOR& normal);
                void Set_Camera();
                void Set_Point_Light();
                void Set_Object_Material(D3DCOLORVALUE& dif, D3DCOLORVALUE& amb, D3DCOLORVALUE& spe, 
                                         D3DCOLORVALUE& emi, float power);
                void Render();
                void Release_COM_Object();
            };

            #endif

            以上的頭文件定義了兩個(gè)三棱錐的頂點(diǎn)格式和頂點(diǎn)結(jié)構(gòu)體,函數(shù)Init_Vertex_Buffer1個(gè)Init_Vertex_Buffer2分別用來(lái)裝入這兩個(gè)三棱錐的頂點(diǎn)數(shù)據(jù),Render函數(shù)則設(shè)置了渲染管道流水線的 Alpha顏色混合狀態(tài)值。

            再來(lái)看看AlphaBlend.cpp的定義:

             
            /*************************************************************************************
             [Implement File]

             PURPOSE: 
                Define for alpha blend.
            *************************************************************************************/


            #include "GE_COMMON.h"
            #include "AlphaBlend.h"

            //------------------------------------------------------------------------------------
            // Constructor, initialize all pointer with NULL.
            //------------------------------------------------------------------------------------
            ALPHA_BLEND::ALPHA_BLEND()
            {
                _d3d            = NULL;
                _d3d_device     = NULL;
                _vertex_buffer1 = NULL;
                _vertex_buffer2 = NULL;
            }

            //------------------------------------------------------------------------------------
            // Destructor, release all COM object.
            //------------------------------------------------------------------------------------
            ALPHA_BLEND::~ALPHA_BLEND()
            {
                Release_COM_Object();
            }

            //------------------------------------------------------------------------------------
            // Create direct3D interface and direct3D device.
            //------------------------------------------------------------------------------------
            bool ALPHA_BLEND::Create_D3D_Device(HWND hwnd, bool full_screen)
            {
                // Create a IDirect3D9 object and returns an interace to it.
                _d3d = Direct3DCreate9(D3D_SDK_VERSION);
                if(_d3d == NULL)
                    return false;

                // retrieve adapter capability
                D3DCAPS9 d3d_caps;    
                _d3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3d_caps);
                
                bool hardware_process_enable = (d3d_caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT ? true : false);

                // Retrieves the current display mode of the adapter.
                D3DDISPLAYMODE display_mode;
                if(FAILED(_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &display_mode)))
                    return false;

                // set present parameter for direct3D device
                D3DPRESENT_PARAMETERS present_param;

                ZeroMemory(&present_param, sizeof(present_param));

                present_param.BackBufferWidth      = WINDOW_WIDTH;
                present_param.BackBufferHeight     = WINDOW_HEIGHT;
                present_param.BackBufferFormat     = display_mode.Format;
                present_param.BackBufferCount      = 1;
                present_param.hDeviceWindow        = hwnd;
                present_param.Windowed             = !full_screen;
                present_param.SwapEffect           = D3DSWAPEFFECT_FLIP;
                present_param.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;

                // Creates a device to represent the display adapter.
                DWORD behavior_flags;

                behavior_flags = hardware_process_enable ?
             D3DCREATE_HARDWARE_VERTEXPROCESSING : D3DCREATE_SOFTWARE_VERTEXPROCESSING;

                if(FAILED(_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, behavior_flags, 
                                             &present_param, &_d3d_device)))
                {
                    return false;
                }
                
                // create successfully
                return true;
            }

            //------------------------------------------------------------------------------------
            // Initialize vertex buffer for cone.
            //------------------------------------------------------------------------------------
            bool ALPHA_BLEND::Init_Vertex_Buffer1()
            {
                CUSTOM_VERTEX custom_vertex[12];
                
                D3DXVECTOR3 v[] = 
                {
                    D3DXVECTOR3(5.0f, 6.0f, 5.0f),    // left triangle
                    D3DXVECTOR3(6.0f, 0.0f, 3.0f),
                    D3DXVECTOR3(1.0f, 0.0f, 7.0f),  
                    D3DXVECTOR3(5.0f, 6.0f, 5.0f),    // right triangle
                    D3DXVECTOR3(10.0f, 0.0f, 8.0f),
                    D3DXVECTOR3(6.0f, 0.0f, 3.0f), 
                    D3DXVECTOR3(5.0f, 6.0f, 5.0f),    // back triangle
                    D3DXVECTOR3(1.0f, 0.0f, 7.0f),
                    D3DXVECTOR3(10.0f, 0.0f, 8.0f),
                    D3DXVECTOR3(1.0f, 0.0f, 7.0f),    // bottom triangle
                    D3DXVECTOR3(6.0f, 0.0f, 3.0f),
                    D3DXVECTOR3(10.0f, 0.0f, 8.0f)      
                };

                D3DVECTOR normal;

                // compute all triangle normal
                for(int i = 0; i < 12; i += 3)
                {
                    // compute current triangle's normal
                    Compute_Triangle_Normal(v[i], v[i+1], v[i+2], normal);

                    // assign current vertex coordinate and current triangle normal to custom vertex array
                    for(int j = 0; j < 3; j++)
                    {
                        int k = i + j;

                        custom_vertex[k].x  = v[k].x;
                        custom_vertex[k].y  = v[k].y;
                        custom_vertex[k].z  = v[k].z;
                        custom_vertex[k].nx = normal.x;
                        custom_vertex[k].ny = normal.y;
                        custom_vertex[k].nz = normal.z;
                    }
                }

                BYTE* vertex_data;

                // create vertex buffer
                if(FAILED(_d3d_device->CreateVertexBuffer(12 * sizeof(CUSTOM_VERTEX), 0, CUSTOM_VERTEX_FVF,
                                        D3DPOOL_DEFAULT, &_vertex_buffer1, NULL)))
                {
                    return false;
                }

                // get data pointer to vertex buffer
                if(FAILED(_vertex_buffer1->Lock(0, 0, (void **) &vertex_data, 0)))
                    return false;

                // copy custom vertex data into vertex buffer
                memcpy(vertex_data, custom_vertex, sizeof(custom_vertex));

                // unlock vertex buffer
                _vertex_buffer1->Unlock();

                return true;
            }

            //------------------------------------------------------------------------------------
            // Initialize vertex buffer for cone.
            //------------------------------------------------------------------------------------
            bool ALPHA_BLEND::Init_Vertex_Buffer2()
            {
                CUSTOM_VERTEX custom_vertex[12];

                float add = 1.3f;
                
                D3DXVECTOR3 v[] = 
                {
                    D3DXVECTOR3(5.0f + add, 6.0f + add, 5.0f + add),    // left triangle
                    D3DXVECTOR3(6.0f + add, 0.0f + add, 3.0f + add),
                    D3DXVECTOR3(1.0f + add, 0.0f + add, 7.0f + add),  
                    D3DXVECTOR3(5.0f + add, 6.0f + add, 5.0f + add),    // right triangle
                    D3DXVECTOR3(10.0f + add, 0.0f + add, 8.0f + add),
                    D3DXVECTOR3(6.0f + add, 0.0f + add, 3.0f + add), 
                    D3DXVECTOR3(5.0f + add, 6.0f + add, 5.0f + add),    // back triangle
                    D3DXVECTOR3(1.0f + add, 0.0f + add, 7.0f + add),
                    D3DXVECTOR3(10.0f + add, 0.0f + add, 8.0f + add),
                    D3DXVECTOR3(1.0f + add, 0.0f + add, 7.0f + add),    // bottom triangle
                    D3DXVECTOR3(6.0f + add, 0.0f + add, 3.0f + add),
                    D3DXVECTOR3(10.0f + add, 0.0f + add, 8.0f + add)      
                };

                D3DVECTOR normal;

                // compute all triangle normal
                for(int i = 0; i < 12; i += 3)
                {
                    // compute current triangle's normal
                    Compute_Triangle_Normal(v[i], v[i+1], v[i+2], normal);

                    // assign current vertex coordinate and current triangle normal to custom vertex array
                    for(int j = 0; j < 3; j++)
                    {
                        int k = i + j;

                        custom_vertex[k].x  = v[k].x;
                        custom_vertex[k].y  = v[k].y;
                        custom_vertex[k].z  = v[k].z;
                        custom_vertex[k].nx = normal.x;
                        custom_vertex[k].ny = normal.y;
                        custom_vertex[k].nz = normal.z;
                    }
                }

                BYTE* vertex_data;

                // create vertex buffer
                if(FAILED(_d3d_device->CreateVertexBuffer(12 * sizeof(CUSTOM_VERTEX), 0, CUSTOM_VERTEX_FVF,
                                        D3DPOOL_DEFAULT, &_vertex_buffer2, NULL)))
                {
                    return false;
                }

                // get data pointer to vertex buffer
                if(FAILED(_vertex_buffer2->Lock(0, 0, (void **) &vertex_data, 0)))
                    return false;

                // copy custom vertex data into vertex buffer
                memcpy(vertex_data, custom_vertex, sizeof(custom_vertex));

                // unlock vertex buffer
                _vertex_buffer2->Unlock();

                return true;
            }

            //------------------------------------------------------------------------------------
            // Set camera position.
            //------------------------------------------------------------------------------------
            void ALPHA_BLEND::Set_Camera()
            {
                D3DXVECTOR3 eye(-6.0, 1.5, 10.0);
                D3DXVECTOR3 at(6.0, 2.0, 3.0);
                D3DXVECTOR3 up(0.0, 1.0, 0.0);

                D3DXMATRIX view_matrix;

                // Builds a left-handed, look-at matrix.
                D3DXMatrixLookAtLH(&view_matrix, &eye, &at, &up);

                // Sets d3d device view transformation state.
                _d3d_device->SetTransform(D3DTS_VIEW, &view_matrix);

                D3DXMATRIX proj_matrix;

                // Builds a left-handed perspective projection matrix based on a field of view.
                D3DXMatrixPerspectiveFovLH(&proj_matrix, D3DX_PI/2, WINDOW_WIDTH / WINDOW_HEIGHT, 1.0, 1000.0);
                
                // Sets d3d device projection transformation state.
                _d3d_device->SetTransform(D3DTS_PROJECTION, &proj_matrix);
                // enable automatic normalization of vertex normals
                _d3d_device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
            }

            //------------------------------------------------------------------------------------
            // Set point light.
            //------------------------------------------------------------------------------------
            void ALPHA_BLEND::Set_Point_Light()
            {
                D3DLIGHT9 light;

                // clear memory with 0
                ZeroMemory(&light, sizeof(D3DLIGHT9));

                light.Type          = D3DLIGHT_POINT;

                light.Diffuse.r     = 1.0;
                light.Diffuse.g     = 0.0;
                light.Diffuse.b     = 0.0;

                light.Ambient.r     = 0.0;
                light.Ambient.g     = 1.0;
                light.Ambient.b     = 0.0;

                light.Specular.r    = 0.0;
                light.Specular.g    = 0.0;
                light.Specular.b    = 0.0;
                
                light.Position.x    = 5.0;
                light.Position.y    = 6.0;
                light.Position.z    = -20.0;

                light.Attenuation0  = 1.0;
                light.Attenuation1  = 0.0;
                light.Attenuation2  = 0.0;

                light.Range         = 1000.0;

                // Assigns point lighting properties for this device
                _d3d_device->SetLight(0, &light);
                // enable point light
                _d3d_device->LightEnable(0, TRUE);
                // enable light 
                _d3d_device->SetRenderState(D3DRS_LIGHTING, TRUE);
                // add ambient light
                _d3d_device->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));
            }

            //------------------------------------------------------------------------------------
            // Sets the material properties for the device.
            //------------------------------------------------------------------------------------
            void ALPHA_BLEND::Set_Object_Material(D3DCOLORVALUE& dif, D3DCOLORVALUE& amb, D3DCOLORVALUE& spe, 
                                                   D3DCOLORVALUE& emi, float power)
            {
                D3DMATERIAL9 material;

                material.Diffuse  = dif;
                material.Ambient  = amb;
                material.Specular = spe;
                material.Emissive = emi;
                material.Power    = power;

                // Sets the material properties for the device.
                _d3d_device->SetMaterial(&material);
            }

            //------------------------------------------------------------------------------------
            // Compute triangle normal.
            //------------------------------------------------------------------------------------
            void ALPHA_BLEND::Compute_Triangle_Normal(D3DXVECTOR3& v1, D3DXVECTOR3& v2, D3DXVECTOR3& v3, D3DVECTOR& normal)
            {
                D3DXVECTOR3 vec1 = v1 - v2;
                D3DXVECTOR3 vec2 = v1 - v3;
                D3DXVECTOR3 normal_vec;

                D3DXVec3Cross(&normal_vec, &vec1, &vec2);
                D3DXVec3Normalize(&normal_vec, &normal_vec);

                normal = (D3DVECTOR) normal_vec;
            }

            //------------------------------------------------------------------------------------
            // Draw cones.
            //------------------------------------------------------------------------------------
            void ALPHA_BLEND::Render()
            {
                if(_d3d_device == NULL)
                    return;

                // clear surface with black
                _d3d_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0, 0);

                // begin scene
                _d3d_device->BeginScene();

                // 1) draw cone 1

                // Binds a vertex buffer to a device data stream.
                _d3d_device->SetStreamSource(0, _vertex_buffer1, 0, sizeof(CUSTOM_VERTEX));

                // Sets the current vertex stream declaration.
                _d3d_device->SetFVF(CUSTOM_VERTEX_FVF);

                // Renders a sequence of nonindexed, geometric primitives of the specified type from the current 
                // set of data input streams.
                _d3d_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 4);

                // enable alpha-blended transparency
                _d3d_device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
                
                // set alpha blend for source cone
                _d3d_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
                // set alpha blend for dest cone
                _d3d_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

                // 2) draw cone 2

                // Binds a vertex buffer to a device data stream. 
                _d3d_device->SetStreamSource(0, _vertex_buffer2, 0, sizeof(CUSTOM_VERTEX));

                // Sets the current vertex stream declaration.
                _d3d_device->SetFVF(CUSTOM_VERTEX_FVF);

                // draw square
                _d3d_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 4);

                // disable alpha blend for d3d device
                _d3d_device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);

                // end scene
                _d3d_device->EndScene();

                // Presents the contents of the next buffer in the sequence of back buffers owned by the device.
                _d3d_device->Present(NULL, NULL, NULL, NULL);
            }

            //------------------------------------------------------------------------------------
            // Release all COM object.
            //------------------------------------------------------------------------------------
            void ALPHA_BLEND::Release_COM_Object()
            {
                Safe_Release(_vertex_buffer1);
                Safe_Release(_vertex_buffer2);
                Safe_Release(_d3d_device);
                Safe_Release(_d3d);
            }

            main.cpp的實(shí)現(xiàn)很簡(jiǎn)單,它首先調(diào)用類ALPHA_BLEND的函數(shù)創(chuàng)建兩個(gè)三棱錐的頂點(diǎn)緩沖區(qū),然后進(jìn)行取景并設(shè)置材質(zhì)光源,最后調(diào)用Render函數(shù)進(jìn)行混色渲染。

             
            /*************************************************************************************
             [Implement File]

             PURPOSE: 
                Test for alpha blending.
            *************************************************************************************/


            #define DIRECTINPUT_VERSION 0x0800

            #include "GE_COMMON.h"
            #include "GE_APP.h"
            #include "AlphaBlend.h"

            #pragma warning(disable : 4305 4996)

            int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR cmd_line, int cmd_show)
            {
                GE_APP ge_app;
                ALPHA_BLEND alpha_blend;

                MSG msg = {0};

                
            // create window
                if(! ge_app.Create_Window("Alpha blending test", instance, cmd_show))
                    
            return false;

                HWND hwnd = ge_app.Get_Window_Handle();    

                SetWindowPos(hwnd, 0, 0,0,0,0, SWP_NOSIZE);
                SetCursorPos(0, 0);
                
                
            // Create direct3D interface and direct3D device.
                if(! alpha_blend.Create_D3D_Device(hwnd, false))
                    
            return false;

                
            // Initialize cone 1 vertex buffer with curstom vertex structure.
                if(! alpha_blend.Init_Vertex_Buffer1())
                    
            return false;

                
            // Initialize cone 2 vertex buffer with curstom vertex structure.
                if(! alpha_blend.Init_Vertex_Buffer2())
                    
            return false;
                
                
            // Set camera position.
                alpha_blend.Set_Camera();

                D3DCOLORVALUE dif = {1.0f, 1.0f, 1.0f, 0.6f};
                D3DCOLORVALUE amb = {1.0f, 1.0f, 1.0f, 0.0f};
                D3DCOLORVALUE spe = {0.0f, 0.0f, 0.0f, 0.0f};
                D3DCOLORVALUE emi = {0.0f, 0.0f, 0.0f, 0.0f};

                
            // Sets the material properties for the device.
                alpha_blend.Set_Object_Material(dif, amb, spe, emi, 0);

                
            // Set point light.
                alpha_blend.Set_Point_Light();

                
            // Draw all cones
                alpha_blend.Render();

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

                UnregisterClass(WINDOW_CLASS_NAME, instance);

                
            return true;
            }
             

            運(yùn)行效果:



            閱讀下篇:D3D中的Alpha顏色混合(2)

            posted on 2007-05-15 00:54 lovedday 閱讀(4821) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            公告

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            隨筆分類(178)

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

            搜索

            最新評(píng)論

            久久久久无码精品| 色诱久久久久综合网ywww| 久久精品无码免费不卡| 无码国内精品久久人妻麻豆按摩| 久久精品中文字幕一区| 久久精品国产只有精品2020| 青青青青久久精品国产h久久精品五福影院1421 | 久久性精品| 久久w5ww成w人免费| 欧美一级久久久久久久大| 丁香狠狠色婷婷久久综合| 中文精品99久久国产| 久久国产精品-国产精品| 久久久久久久久66精品片| 精品久久久久久久| 色综合久久综合中文综合网| 精品久久久久久国产免费了| 日韩精品久久久久久久电影蜜臀 | 国产精品久久久久久搜索| 欧美日韩久久中文字幕| 国产成人精品久久亚洲高清不卡| 久久精品国产日本波多野结衣| 国产精品丝袜久久久久久不卡 | 国产亚洲精品自在久久| 一本一本久久aa综合精品 | 久久久久人妻一区二区三区vr| 蜜臀久久99精品久久久久久| 久久久久久a亚洲欧洲aⅴ| 精品国产乱码久久久久久1区2区| 中文字幕久久亚洲一区| 久久婷婷五月综合成人D啪| 精品久久久久国产免费| 亚洲综合精品香蕉久久网97| 精品久久香蕉国产线看观看亚洲| 欧美丰满熟妇BBB久久久| 色诱久久久久综合网ywww| 亚洲人成伊人成综合网久久久| 国产偷久久久精品专区| 狠狠综合久久AV一区二区三区| 中文字幕无码久久精品青草| 国产精品美女久久福利网站|