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

            像素著色器入門(2)

            新建網(wǎng)頁(yè) 1

            18.4 HLSL采樣器對(duì)象

            在像素著色器中使用HLSL的內(nèi)建函數(shù)tex*XXXX給紋理采樣。注意:采樣時(shí)引用紋理上圖素的坐標(biāo)索引和采樣器狀態(tài)來(lái)生成像素。

            通常這些函數(shù)需要我們做2件事:

            使用紋理中的索引建立(u, v)紋理坐標(biāo)。

            給特定的紋理中編入索引。

            將紋理坐標(biāo)(u, v)輸入到像素著色器,在一個(gè)指定的HLSL對(duì)象中的像素著色器中,我們想編入索引的紋理是在像素著色器中被定義過(guò)的,在HLSL中叫作采樣器。(The particular texture that we want to index into is identified in the pixel shader by a special HLSL object called a sampler.),我們可以把采樣器對(duì)象想象成定義紋理和采樣器階段的對(duì)象。例如:假如我們使用3張紋理,這意味著我們需要在像素著色器里能夠引用3個(gè)階段中的每個(gè)一個(gè)。在像素著色器中我們這樣寫:

                            
                    

                    sampler FirstTex;

                    

                    sampler SecondTex;

                    

                    sampler ThirdTex;

            Direct3D將給每個(gè)采樣器對(duì)象連接一個(gè)唯一的紋理級(jí)別(stage),在應(yīng)用程序中我們找出與采樣器對(duì)象相關(guān)聯(lián)的階段,并設(shè)置相應(yīng)的紋理和采樣器狀態(tài)給該階段。下列代碼將舉例說(shuō)明如何在應(yīng)用程序中設(shè)置紋理并把采樣器狀態(tài)設(shè)置為FirstTex

                   
                

                    //         創(chuàng)建

                    IDirect3DTexture9* Tex;

                    D3DXCreateTextureFromFile(Device, "tex.bmp", &Tex);

                    

                    … …

                    

                    //         取得常量FirstTex的句柄

                    FirstTexHandle = MultiTexCT->GetConstantByName(0, "FirstTex");

                            

                    //         取得常量的描述

                    D3DXCONSTANT_DESC FirstTexDesc;

                    

                    UINT         count;

                    MultiTexCT->GetConstantDesc(FirstTexHandle, &FirstTexDesc, &count);

                    

                     … …

                    

                    //         FirstTex設(shè)置紋理和采樣器狀態(tài).        

                    

                    // We identify the stage FirstTex is associated with from the           D3DXCONSTANT_DESC::RegisterIndex member:

                    

                    Device->SetTexture(FirstTexDesc.RegisterIndex, Tex);

                    

                    Device->SetSamplerState(FirstTexDesc.RegisterIndex,  D3DSAMP_MAGFILTER,      D3DTEXF_LINEAR);

                    

                    Device->SetSamplerState(FirstTexDesc.RegisterIndex,  D3DSAMP_MINFILTER,       D3DTEXF_LINEAR);

                    

                    Device->SetSamplerState(FirstTexDesc.RegisterIndex,  D3DSAMP_MIPFILTER,       D3DTEXF_LINEAR);

            注意:作為選擇,替換使用采樣器類型,你可以使用更多特殊的、強(qiáng)大的類型,如:sampler1Dsampler2Dsampler3D,和samplerCube類型,這些類型更安全并且它們只使用tex*函數(shù)。例如:一個(gè)sampler2D對(duì)象只使用tex2D*函數(shù),同樣一個(gè)sampler3D對(duì)象只使用tex3D*函數(shù)。

             

             

            18.5例子程序:Multitexturing in a Pixel Shader

            這章中的例子演示了在像素著色器中使用多紋理,這個(gè)例子渲染的目標(biāo)是一個(gè)木箱紋理,一個(gè)聚光燈紋理,和一個(gè)包含字符串的紋理。這就是例子程序:Pixel Shader

             

            這個(gè)例子可以不使用像素著色器來(lái)實(shí)現(xiàn),但實(shí)現(xiàn)這個(gè)程序是簡(jiǎn)單直接,它允許我們示范如何編寫,創(chuàng)建,而且使用像素著色器實(shí)現(xiàn)一些特效不必使用那些復(fù)雜的算法。

            雖然在這個(gè)例子中一次只使用3張紋理,檢查采樣器對(duì)象的成員以確定每個(gè)像素著色器能夠使用的版本,這是值得的。換句話說(shuō),我們一次能使用多少紋理,這依賴于使用的像素著色器的版本:

            像素著色器的版本ps_1_1 ps_1_3支持4個(gè)紋理采樣器。

            像素著色器的版本ps_1_4支持6個(gè)紋理采樣器。

            像素著色器的版本ps_2_0 ps_3_0支持16個(gè)紋理采樣器。

             

                 /******************************************************************************
                  Pixel shader that does multi texturing.
                 ******************************************************************************/

                
                sampler g_base_texture;
                sampler g_spotlight_texture;
                sampler g_string_texture;
                
                
            struct sPixelInput
                {
                    float2 
            base         : TEXCOORD0;
                    float2 spotlight : TEXCOORD1;
                    float2 text         : TEXCOORD2;
                };
                
                
            struct sPixelOutput
                {
                    vector diffuse : COLOR0;
                };
                
                
                /////////////////////////////////////////////////////////////////////////////////
                

                sPixelOutput main(sPixelInput input)
                {
                    sPixelOutput output = (sPixelOutput) 0;
                
                    
            // sample appropriate textures
                
                    vector base_color       = tex2D(g_base_texture,        input.base);
                    vector spotlight_color = tex2D(g_spotlight_texture, input.spotlight);
                    vector text_color       = tex2D(g_string_texture,    input.text);
                
                    
            // combine texel colors
                
                    vector color = base_color * spotlight_color + text_color;
                
                    
            // increase the intensity of the pixel slightly
                
                color += 0.1f;
                
                    
            // save the resulting pixel color
                
                output.diffuse = color;
                
                    
            return output;
                }

            tex2D的函數(shù)使用說(shuō)明如下:

            There are two overloaded tex2D texture lookup functions:

            • 2D texture lookup
            •     
            • 2D texture lookup with partial derivatives     

            2D texture lookup

            This function performs a 2D texture lookup.

            Syntax

                             
            ret tex2D(s, t)

            Where:

                                                                                                                                                                                                                                  
            NameIn/OutTemplate TypeComponent TypeSize
            sinobjectsampler2D1
            tinvectorfloat2
            retoutvectorfloat4

            2D texture lookup with partial derivatives

            This function performs a 2D texture lookup also, but also uses the partial derivatives to help pick the LOD.

            Syntax

                             
            ret tex2D(s, t, ddx, ddy)

            Where:

                                                                                                                                                                                                                                                                                                                                                
            NameIn/OutTemplate TypeComponent TypeSize
            sinobjectsampler2D1
            tinvectorfloat2
            ddxinvectorfloat2
            ddyinvectorfloat2
            retoutvectorfloat4

             

            首先像素著色器定義了3個(gè)sampler對(duì)象,要渲染的每個(gè)紋理,接下來(lái)定義是inputoutput結(jié)構(gòu)。注意:我們沒有將任何的顏色值輸入到像素著色器中,這是因?yàn)槲覀兪褂眉y理自己的顏色和光照;即BaseTex保存表面的顏色,SpotLightTex是光照?qǐng)D。像素著色器輸出只一個(gè)簡(jiǎn)顏色值,指定了我們計(jì)算過(guò)的這個(gè)特定像素的顏色。

            Main函數(shù)使用tex2D函數(shù)采樣3個(gè)紋理,即它取得每個(gè)紋理的圖素,計(jì)算映射到的像素,這通常依賴于指定的紋理坐標(biāo)和采樣器對(duì)象。然后我們混合圖素的顏色用公式:c = b * s + t。接下來(lái)我們讓全部的像素變亮一個(gè)bit,給每個(gè)部分增加0.1f。最后我們保存結(jié)果像素顏色并返回它。

            執(zhí)行程序:

                 /**************************************************************************************************
                  Deomstrates multi-texturing using a pixel shader.  You will have to switch to the REF 
                  device to run this sample if your hardware doesn't support pixel shaders.
                 **************************************************************************************************/

                
                #include "d3dUtility.h"
                
                #pragma warning(disable : 4100)
                
                
            struct sMultiTextureVertex
                {
                    sMultiTextureVertex(
            float x, float y, float z,
                                        
            float u0, float v0,
                                        
            float u1, float v1,
                                        
            float u2, float v2)
                    {
                        _x =  x;  _y =  y; _z = z;
                        _u0 = u0; _v0 = v0; 
                        _u1 = u1; _v1 = v1;
                        _u2 = u2, _v2 = v2;
                    }
                
                    
            float _x, _y, _z;
                    
            float _u0, _v0;
                    
            float _u1, _v1;
                    
            float _u2, _v2;
                };
                
                
            const DWORD MULTI_TEXTURE_VERTEX_FVF = D3DFVF_XYZ | D3DFVF_TEX3; 
                
                
                /////////////////////////////////////////////////////////////////////////////////////////////
                

                
            const int WIDTH  = 640;
                
            const int HEIGHT = 480;
                
                IDirect3DDevice9*        g_device;
                IDirect3DPixelShader9*    g_pixel_shader;
                ID3DXConstantTable*        g_constant_table;
                IDirect3DVertexBuffer9*    g_vertex_buffer;
                
                IDirect3DTexture9*        g_base_texture;
                IDirect3DTexture9*        g_spotlight_texture;
                IDirect3DTexture9*        g_string_texture;
                
                D3DXHANDLE                g_base_texture_handle;
                D3DXHANDLE                g_spotlight_texture_handle;
                D3DXHANDLE                g_string_texture_handle;
                
                D3DXCONSTANT_DESC        g_base_texture_desc;
                D3DXCONSTANT_DESC        g_spotlight_texture_desc;
                D3DXCONSTANT_DESC        g_string_texture_desc;
                
                
                ////////////////////////////////////////////////////////////////////////////////////////////////////
                

                
            bool setup()
                {    
                    
            // create geometry
                

                    g_device->CreateVertexBuffer(6 * 
            sizeof(sMultiTextureVertex), D3DUSAGE_WRITEONLY,
                        MULTI_TEXTURE_VERTEX_FVF, D3DPOOL_MANAGED, &g_vertex_buffer, NULL);
                
                    sMultiTextureVertex* v;
                
                    g_vertex_buffer->Lock(0, 0, (
            void**)&v, 0);
                
                    v[0] = sMultiTextureVertex(-10.0f, -10.0f, 5.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f);
                    v[1] = sMultiTextureVertex(-10.0f,  10.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
                    v[2] = sMultiTextureVertex( 10.0f,  10.0f, 5.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
                
                    v[3] = sMultiTextureVertex(-10.0f, -10.0f, 5.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f);
                    v[4] = sMultiTextureVertex( 10.0f,  10.0f, 5.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
                    v[5] = sMultiTextureVertex( 10.0f, -10.0f, 5.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f);
                
                    g_vertex_buffer->Unlock();
                
                    
            // compile shader
                

                    ID3DXBuffer*    shader_buffer;
                    ID3DXBuffer*    error_buffer;
                
                    HRESULT hr = D3DXCompileShaderFromFile("MultiTextureShader.cxx", NULL, NULL, "main", "ps_2_0", D3DXSHADER_DEBUG,
                                                           &shader_buffer, &error_buffer, &g_constant_table);
                
                    
            // output any error messages
                
                if(error_buffer)
                    {
                        MessageBox(NULL, (
            char*) error_buffer->GetBufferPointer(), "ERROR", MB_OK);
                        safe_release<ID3DXBuffer*>(error_buffer);
                    }
                
                    
            if(FAILED(hr))
                    {
                        MessageBox(NULL, "D3DXCompileShaderFromFile() - FAILED", "ERROR", MB_OK);
                        
            return false;
                    }
                
                    hr = g_device->CreatePixelShader((DWORD*) shader_buffer->GetBufferPointer(), &g_pixel_shader);
                
                    
            if(FAILED(hr))
                    {
                        MessageBox(NULL, "CreatePixelShader - FAILED", "ERROR", MB_OK);
                        
            return false;
                    }
                
                    safe_release<ID3DXBuffer*>(shader_buffer);
                
                    
            // load textures
                
                    D3DXCreateTextureFromFile(g_device, "crate.bmp",     &g_base_texture);
                    D3DXCreateTextureFromFile(g_device, "spotlight.bmp", &g_spotlight_texture);
                    D3DXCreateTextureFromFile(g_device, "text.bmp",         &g_string_texture);
                
                    
            // get handles
                
                    g_base_texture_handle        = g_constant_table->GetConstantByName(NULL, "g_base_texture");
                    g_spotlight_texture_handle    = g_constant_table->GetConstantByName(NULL, "g_spotlight_texture");
                    g_string_texture_handle        = g_constant_table->GetConstantByName(NULL, "g_string_texture");
                
                    
            // set constant descriptions
                

                    UINT count;
                
                    g_constant_table->GetConstantDesc(g_base_texture_handle,      &g_base_texture_desc,         &count);
                    g_constant_table->GetConstantDesc(g_spotlight_texture_handle, &g_spotlight_texture_desc, &count);
                    g_constant_table->GetConstantDesc(g_string_texture_handle,      &g_string_texture_desc,     &count);    
                
                    g_constant_table->SetDefaults(g_device);
                
                    
            // set the projection matrix
                
                D3DXMATRIX proj;
                    D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI/4.0f, (
            float)WIDTH/HEIGHT, 1.0f, 1000.0f);
                    g_device->SetTransform(D3DTS_PROJECTION, &proj);
                    
                    
            //g_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
                
                
                    
            return true;
                }
                
                
                ///////////////////////////////////////////////////////////////////////////////////////////////////////
                

                
            void cleanup()
                {    
                    safe_release<IDirect3DVertexBuffer9*>(g_vertex_buffer);
                
                    safe_release<IDirect3DTexture9*>(g_base_texture);
                    safe_release<IDirect3DTexture9*>(g_spotlight_texture);
                    safe_release<IDirect3DTexture9*>(g_string_texture);
                    
                    safe_release<IDirect3DPixelShader9*>(g_pixel_shader);
                    safe_release<ID3DXConstantTable*>(g_constant_table);
                }
                
                
                ///////////////////////////////////////////////////////////////////////////////////////////////////////
                

                
            bool display(float time_delta)
                {    
                    
            // update the scene: allow user to rotate around scene.
                

                    
            static float angle  = (3.0f * D3DX_PI) / 2.0f;
                    
            static float radius = 20.0f;
                
                    
            if(GetAsyncKeyState(VK_LEFT) & 0x8000f)
                        angle -= 0.5f * time_delta;
                
                    
            if(GetAsyncKeyState(VK_RIGHT) & 0x8000f)
                        angle += 0.5f * time_delta;
                
                    
            if(GetAsyncKeyState(VK_UP) & 0x8000f)
                        radius -= 2.0f * time_delta;
                
                    
            if(GetAsyncKeyState(VK_DOWN) & 0x8000f)
                        radius += 2.0f * time_delta;
                
                    D3DXVECTOR3 position(cosf(angle) * radius, 0.0f, sinf(angle) * radius);
                    D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
                    D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
                
                    D3DXMATRIX view_matrix;
                    D3DXMatrixLookAtLH(&view_matrix, &position, &target, &up);
                    g_device->SetTransform(D3DTS_VIEW, &view_matrix);
                        
                    
            // render now
                

                    g_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xFFFFFFFF, 1.0f, 0);
                
                    g_device->BeginScene();
                
                    g_device->SetPixelShader(g_pixel_shader);
                
                    g_device->SetFVF(MULTI_TEXTURE_VERTEX_FVF);
                    g_device->SetStreamSource(0, g_vertex_buffer, 0, 
            sizeof(sMultiTextureVertex));
                
                    
            // base texture
                
                    g_device->SetTexture(g_base_texture_desc.RegisterIndex, g_base_texture);
                    g_device->SetSamplerState(g_base_texture_desc.RegisterIndex, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
                    g_device->SetSamplerState(g_base_texture_desc.RegisterIndex, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
                    g_device->SetSamplerState(g_base_texture_desc.RegisterIndex, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
                
                    
            // spotlight texture
                
                    g_device->SetTexture(g_spotlight_texture_desc.RegisterIndex, g_spotlight_texture);
                    g_device->SetSamplerState(g_spotlight_texture_desc.RegisterIndex, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
                    g_device->SetSamplerState(g_spotlight_texture_desc.RegisterIndex, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
                    g_device->SetSamplerState(g_spotlight_texture_desc.RegisterIndex, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
                
                    
            // string texture
                
                    g_device->SetTexture(g_string_texture_desc.RegisterIndex, g_string_texture);
                    g_device->SetSamplerState(g_string_texture_desc.RegisterIndex, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
                    g_device->SetSamplerState(g_string_texture_desc.RegisterIndex, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
                    g_device->SetSamplerState(g_string_texture_desc.RegisterIndex, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
                
                    g_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
                    
                    g_device->EndScene();
                
                    g_device->Present(NULL, NULL, NULL, NULL);
                
                    
            return true;
                }
                
                
                ///////////////////////////////////////////////////////////////////////////////////////////////////////
                

                LRESULT CALLBACK wnd_proc(HWND hwnd, UINT msg, WPARAM word_param, LPARAM long_param)
                {
                    
            switch(msg)
                    {
                    
            case WM_DESTROY:
                        PostQuitMessage(0);
                        
            break;
                
                    
            case WM_KEYDOWN:
                        
            if(word_param == VK_ESCAPE)
                            DestroyWindow(hwnd);
                
                        
            break;
                    }
                
                    
            return DefWindowProc(hwnd, msg, word_param, long_param);
                }
                
                
                ///////////////////////////////////////////////////////////////////////////////////////////////////////
                

                
            int WINAPI WinMain(HINSTANCE inst, HINSTANCE, PSTR cmd_line, int cmd_show)
                {
                    
            if(! init_d3d(inst, WIDTH, HEIGHT, true, D3DDEVTYPE_HAL, &g_device))
                    {
                        MessageBox(NULL, "init_d3d() - failed.", 0, MB_OK);
                        
            return 0;
                    }
                
                    
            if(! setup())
                    {
                        MessageBox(NULL, "Steup() - failed.", 0, MB_OK);
                        
            return 0;
                    }
                
                    enter_msg_loop(display);
                
                    cleanup();
                    g_device->Release();
                
                    
            return 0;
                }

            setup函數(shù)執(zhí)行下列功能:

            填充方形的頂點(diǎn)緩存

            編譯著像素色器

            創(chuàng)建像素色器

            讀取紋理

            設(shè)置投影矩陣,不使用光照

            取得采樣器(sampler)對(duì)象的句柄

            取得采樣器對(duì)象的描述


            display函數(shù)設(shè)置像素著色器,使用2個(gè)紋理,并且在渲染方格前設(shè)置他們對(duì)應(yīng)的采樣器狀態(tài)。


            運(yùn)行截圖:

             

            下載源程序


            posted on 2008-04-11 13:07 lovedday 閱讀(2762) 評(píng)論(0)  編輯 收藏 引用


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


            公告

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            隨筆分類(178)

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

            搜索

            最新評(píng)論

            狠狠狠色丁香婷婷综合久久俺| 99国产欧美精品久久久蜜芽 | 九九久久99综合一区二区| 人妻中文久久久久| 久久se精品一区二区影院| 久久精品国产亚洲麻豆| 粉嫩小泬无遮挡久久久久久| 国产毛片欧美毛片久久久| 久久久国产亚洲精品| 久久久一本精品99久久精品88| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 久久精品免费一区二区| 久久只有这里有精品4| 思思久久好好热精品国产| 亚洲国产综合久久天堂| 亚洲精品视频久久久| 一本色道久久88精品综合| 无码人妻精品一区二区三区久久| 香蕉久久夜色精品升级完成| 久久一日本道色综合久久| 97久久精品无码一区二区 | 99久久亚洲综合精品成人| 国产精品久久久久久久午夜片| 国产精品久久久久久久久久免费| 理论片午午伦夜理片久久| 久久久久久免费视频| 精品熟女少妇a∨免费久久| 亚洲国产精品久久久久| 亚洲国产精品狼友中文久久久| 久久人人爽人人爽人人片AV不| 久久久av波多野一区二区| 中文精品久久久久国产网址 | 一本色道久久综合亚洲精品| 久久综合狠狠综合久久| 色噜噜狠狠先锋影音久久| 欧美日韩精品久久久久| 久久精品亚洲日本波多野结衣 | 久久精品国产精品亚洲人人 | 97久久天天综合色天天综合色hd| 国产精品欧美久久久久无广告| 久久久无码精品亚洲日韩京东传媒|