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

            天行健 君子當自強而不息

            紋理映射基礎(5)

            多級漸進紋理過濾

            多級漸進紋理由一組分辨率逐漸降低的紋理序列組成,每一級紋理寬度和高度都是上一級紋理寬度和高度的一半。寬和高不一定相等,也就是說,這些紋理不一定都是正方形。

            Direct3D在紋理映射時,自動選擇一幅與物體大小最接近的紋理進行渲染。當物體離投影平面較遠時,Direct3D會選擇一張尺寸較小、分辨率較低的紋理進行渲染;當物體離投影平面較近時,Direct3D會選擇一張尺寸較大、分辨率較高的紋理進行渲染。Direct3D將紋理序列看成一條多級漸進紋理鏈。鏈頭處紋理的分辨率最高,下一級往后依次遞減,鏈尾處紋理的分辨率最低。

            Direct3D能估計出多級漸進紋理鏈中哪幅紋理的分辨率最接近想要的輸出結果,然后它將像素映射到紋理空間。當最終顯示的圖形大小介于任意兩級紋理圖形之間時,Direct3D將兩級紋理的相應元素進行混合后顯示。

            多級漸進紋理過濾能夠有效地提高圖形渲染速度,當物體離投影平面較遠時,Direct3D會選擇一張尺寸較小的紋理進行渲染,而無需經過復雜的諸如各項異性紋理過濾,并且由于這時紋理需要的顯存比不使用多級漸進紋理時小,因此能有效地減少紋理載入顯存的時間。

             

            生成多級漸進紋理

            可以通過調用Direct3D擴展實用庫函數D3DXCreateTextureFromFileEx()自動生成多級漸進紋理序列,該函數的聲明如下:

            Creates a texture from a file. This is a more advanced function than D3DXCreateTextureFromFile.

            HRESULT D3DXCreateTextureFromFileEx(
            LPDIRECT3DDEVICE9 pDevice,
            LPCTSTR pSrcFile,
            UINT Width,
            UINT Height,
            UINT MipLevels,
            DWORD Usage,
            D3DFORMAT Format,
            D3DPOOL Pool,
            DWORD Filter,
            DWORD MipFilter,
            D3DCOLOR ColorKey,
            D3DXIMAGE_INFO * pSrcInfo,
            PALETTEENTRY * pPalette,
            LPDIRECT3DTEXTURE9 * ppTexture
            );

            Parameters

            pDevice
            [in] Pointer to an IDirect3DDevice9 interface, representing the device to be associated with the texture.
            pSrcFile
            [in] Pointer to a string that specifies the filename. If the compiler settings require Unicode, the data type LPCTSTR resolves to LPCWSTR. Otherwise, the string data type resolves to LPCSTR. See Remarks.
            Width
            [in] Width in pixels. If this value is zero or D3DX_DEFAULT, the dimensions are taken from the file and rounded up to a power of two. If the device supports non-power of 2 textures and D3DX_DEFAULT_NONPOW2 is specified, the size will not be rounded.
            Height
            [in] Height, in pixels. If this value is zero or D3DX_DEFAULT, the dimensions are taken from the file and rounded up to a power of two. If the device supports non-power of 2 textures and D3DX_DEFAULT_NONPOW2 is sepcified, the size will not be rounded.
            MipLevels
            [in] Number of mip levels requested. If this value is zero or D3DX_DEFAULT, a complete mipmap chain is created. If D3DX_FROM_FILE, the size will be taken exactly as it is in the file, and the call will fail if this violates device capabilities.
            Usage
            [in] 0, D3DUSAGE_RENDERTARGET, or D3DUSAGE_DYNAMIC. Setting this flag to D3DUSAGE_RENDERTARGET indicates that the surface is to be used as a render target. The resource can then be passed to the pNewRenderTarget parameter of the IDirect3DDevice9::SetRenderTarget method. If either D3DUSAGE_RENDERTARGET or D3DUSAGE_DYNAMIC is specified, Pool must be set to D3DPOOL_DEFAULT, and the application should check that the device supports this operation by calling IDirect3D9::CheckDeviceFormat. D3DUSAGE_DYNAMIC indicates that the surface should be handled dynamically. See Using Dynamic Textures.
            Format
            [in] Member of the D3DFORMAT enumerated type, describing the requested pixel format for the texture. The returned texture might have a different format from that specified by Format. Applications should check the format of the returned texture. If D3DFMT_UNKNOWN, the format is taken from the file. If D3DFMT_FROM_FILE, the format is taken exactly as it is in the file, and the call will fail if this violates device capabilities.
            Pool
            [in] Member of the D3DPOOL enumerated type, describing the memory class into which the texture should be placed.
            Filter
            [in] A combination of one or more D3DX_FILTER constants controlling how the image is filtered. Specifying D3DX_DEFAULT for this parameter is the equivalent of specifying D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER.
            MipFilter
            [in] A combination of one or more D3DX_FILTER constants controlling how the image is filtered. Specifying D3DX_DEFAULT for this parameter is the equivalent of specifying D3DX_FILTER_BOX. In addition, use bits 27-31 to specify the number of mip levels to be skipped (from the top of the mipmap chain) when a .dds texture is loaded into memory; this allows you to skip up to 32 levels.
            ColorKey
            [in] D3DCOLOR value to replace with transparent black, or 0 to disable the color key. This is always a 32-bit ARGB color, independent of the source image format. Alpha is significant and should usually be set to FF for opaque color keys. Thus, for opaque black, the value would be equal to 0xFF000000.
            pSrcInfo
            [in, out] Pointer to a D3DXIMAGE_INFO structure to be filled in with a description of the data in the source image file, or NULL.
            pPalette
            [out] Pointer to a PALETTEENTRY structure, representing a 256-color palette to fill in, or NULL.
            ppTexture
            [out] Address of a pointer to an IDirect3DTexture9 interface, representing the created texture object.

            Return Values

            If the function succeeds, the return value is D3D_OK. If the function fails, the return value can be one of the following: D3DERR_INVALIDCALL.

            D3DERR_NOTAVAILABLED3DERR_OUTOFVIDEOMEMORYD3DXERR_INVALIDDATAE_OUTOFMEMORY

            Remarks

            The compiler setting also determines the function version. If Unicode is defined, the function call resolves to D3DXCreateTextureFromFileExW. Otherwise, the function call resolves to D3DXCreateTextureFromFileExA because ANSI strings are being used.

            Use D3DXCheckTextureRequirements to determine if your device can support the texture given the current state.

            This function supports the following file formats: .bmp, .dds, .dib, .hdr, .jpg, .pfm, .png, .ppm, and .tga. See D3DXIMAGE_FILEFORMAT.

            Mipmapped textures automatically have each level filled with the loaded texture. When loading images into mipmapped textures, some devices are unable to go to a 1x1 image and this function will fail. If this happens, then the images need to be loaded manually.

            For the best performance when using D3DXCreateTextureFromFileEx:

            1. Doing image scaling and format conversion at load time can be slow. Store images in the format and resolution they will be used. If the target hardware requires power of 2 dimensions, then create and store images using power of 2 dimensions.
            2. For mipmap image creation at load time, filter using D3DX_FILTER_BOX. A box filter is much faster than other filter types such as D3DX_FILTER_TRIANGLE.
            3. Consider using DDS files. Since DDS files can be used to represent any Direct3D 9 texture format, they are very easy for D3DX to read. Also, they can store mipmaps, so any mipmap-generation algorithms can be used to author the images.

            When skipping mipmap levels while loading a .dds file, use the D3DX_SKIP_DDS_MIP_LEVELS macro to generate the MipFilter value. This macro takes the number of levels to skip and the filter type and returns the filter value, which would then be passed into the MipFilter parameter.

            參數Format是D3DFORMAT枚舉類型,指定紋理圖形的格式,還可以將它設置為D3DFMT_DXT1 ~ D3DFMT_DXT5的值之一,表示生成DXT壓縮紋理以節省空間。


            Texture Constants

            #define Description
            D3DFMT_FROM_FILE Take the format exactly from a file.
            D3DX_DEFAULT A default value.
            D3DX_DEFAULT_NONPOW2 Do not round up numbers such as width or height to a power of two.
            D3DX_FROM_FILE Take the texture dimensions exactly from a file.

            These #defines are declared in d3dx9.h.


            D3DXIMAGE_INFO

            Returns a description of the original contents of an image file.

            typedef struct D3DXIMAGE_INFO {
            UINT Width;
            UINT Height;
            UINT Depth;
            UINT MipLevels;
            D3DFORMAT Format;
            D3DRESOURCETYPE ResourceType;
            D3DXIMAGE_FILEFORMAT ImageFileFormat;
            } D3DXIMAGE_INFO, *LPD3DXIMAGE_INFO;

            Members

            Width
            Width of original image in pixels.
            Height
            Height of original image in pixels.
            Depth
            Depth of original image in pixels.
            MipLevels
            Number of mip levels in original image.
            Format
            A value from the D3DFORMAT enumerated type that most closely describes the data in the original image.
            ResourceType
            Represents the type of the texture stored in the file. It is either D3DRTYPE_TEXTURE, D3DRTYPE_VOLUMETEXTURE, or D3DRTYPE_CubeTexture.
            ImageFileFormat
            Represents the format of the image file.

            PALETTEENTRY

            Specifies the color and usage of an entry in a logical palette.

            typedef struct PALETTEENTRY {
            BYTE peRed;
            BYTE peGreen;
            BYTE peBlue;
            BYTE peFlags;
            } PALETTEENTRY, *LPPALETTEENTRY;

            Members

            peRed
            The red intensity value for the palette entry.
            peGreen
            The green intensity value for the palette entry.
            peBlue
            The blue intensity value for the palette entry.
            peFlags
            The alpha intensity value for the palette entry. Note that as of DirectX 8, this member is treated differently than documented in the Platform SDK.

             

            設置多級漸進紋理過濾方式

            當最終顯示的紋理貼圖大小介于任意兩級紋理之間時,Direct3D能夠取得兩級紋理元素進行混合后顯示,具體的混合方式由指定的多級漸進紋理過濾方式決定。可以調用函數IDirect3DDevice9::SetSamplerState()設置多級漸進紋理過濾方式,將第一個參數設為紋理層序號,第二個參數設為D3DSAMP_MIPFILTER表示多級漸進紋理過濾,第三個參數設為在相鄰紋理級之間的過濾方式,可取枚舉類型D3DTEXTUREFILTERTYPE的任意值。下面的示例代碼設置相鄰紋理級之間的過濾方式為線性過濾。

            g_device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

            如果將第三個參數設為D3DTEXF_NONE,那么就會一直使用最高一級的紋理,即禁用多級漸進紋理過濾。如果將其設為D3DTEXF_POINT,就會只使用與圖元大小最匹配的一級紋理。如果將其設為D3DTEXF_LINEAR,Direct3D就將與圖元大小最匹配的兩級紋理以線性方式混合。

            需要注意的是,多級紋理過濾是縮小和放大過濾器的結合。例如,如果將縮小和方法過濾器設為線性過濾,但是多級紋理過濾方式設為最近點采樣,Direct3D就會選擇與要顯示的紋理貼圖大小最接近的紋理級別,在該級紋理上完成雙線性紋理過濾,并將結果作為像素的值。如果將縮小、放大過濾器和多級漸進紋理都設置為線性過濾,則Direct3D就會在兩個最接近的紋理級別上都進行雙線性紋理過濾,然后再對相鄰兩級紋理圖形上對應的兩個紋理顏色進行加權平均,最后的結果作為單個像素值。這種為了圖元中的一個像素,而結合了兩幅紋理,共8個像素的技術,稱為“三線性過濾”,因為它在紋理的三個方向----u、 v和紋理級別上都進行了線性過濾。

            可以通過IDirect3DDevice9::SetSamplerState()函數設置實際渲染時紋理過濾的最大級數,其中需要將第二個參數設為D3DSAMP_MAXMIPLEVEL,第三個參數設為實際渲染時紋理過濾的最大級數。下面的示例代碼設置紋理層0的最大多級紋理過濾級數為16。

            g_device->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 16);

            還可以通過將IDirect3DDevice9::SetSamplerState()的第二個參數設為D3DSAMP_MIPMAPLODBIAS,設置多級紋理映射級數偏移值。如果對某個紋理映射設置正偏移值,得到的圖形結果就會比原來的更清晰,但鋸齒更多;反之設為負偏移值,得到的圖形結果就會更模糊。

             

            多級漸進紋理過濾示例程序

            該程序創建了一幅多級漸進紋理和一幅單級別紋理,按下數字鍵“1”則使用多級漸進紋理,按下數字鍵“2”則使用單級別紋理。按下“↓”和“↑”則縮小和放大顯示的圖形,仔細觀察圖像的變化,可以看到多級漸進紋理的效果。

            使用MipMap紋理過濾

             

            使用單級紋理過濾(即禁用多級紋理過濾)

             

            源程序:

            #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;
            IDirect3DVertexBuffer9
            * g_vertex_buffer;
            IDirect3DTexture9
            *        g_texture;
            IDirect3DTexture9
            *        g_mip_texture;
            float                    g_distance = -10;    // distance from camera to image
            bool                    g_use_mip_texture = true;

            struct sCustomVertex
            {
                
            float x, y, z;
                
            float u, v;
            };

            #define D3DFVF_CUSTOM_VERTEX (D3DFVF_XYZ | D3DFVF_TEX1) 

            void setup_matrices()
            {
                
            // build world matrix
                
                D3DXMATRIX mat_world;
                D3DXMatrixIdentity(
            &mat_world);
                g_device
            ->SetTransform(D3DTS_WORLD, &mat_world);

                
            // setup view matrix

                D3DXVECTOR3 eye(
            0.0f0.0f, g_distance);
                D3DXVECTOR3 at(
            0.0f0.0f0.0f);
                D3DXVECTOR3 up(
            0.0f1.0f0.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.0f100.0f);
                g_device
            ->SetTransform(D3DTS_PROJECTION, &mat_proj);
            }

            bool init_graphics()
            {    
                
            /*
                HRESULT D3DXCreateTextureFromFileEx(
                  LPDIRECT3DDEVICE9 pDevice,
                  LPCTSTR pSrcFile,
                  UINT Width,
                  UINT Height,
                  UINT MipLevels,
                  DWORD Usage,
                  D3DFORMAT Format,
                  D3DPOOL Pool,
                  DWORD Filter,
                  DWORD MipFilter,
                  D3DCOLOR ColorKey,
                  D3DXIMAGE_INFO * pSrcInfo,
                  PALETTEENTRY * pPalette,
                  LPDIRECT3DTEXTURE9 * ppTexture
                );
                
            */

                
            // create mipmap texture object
                if(FAILED(D3DXCreateTextureFromFileEx(g_device, "texture.jpg"0000, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED,
                                                      D3DX_DEFAULT, D3DX_DEFAULT, 
            0xFF000000, NULL, NULL, &g_mip_texture)))
                {
                    MessageBox(NULL, 
            "Create mipmap texture failed!""ERROR", MB_OK);
                    
            return false;
                }
                
                
            // create normal texture object, set MipLevels as 1, disable mipmap.
                if(FAILED(D3DXCreateTextureFromFileEx(g_device, "texture.jpg"0010, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED,
                                                      D3DX_DEFAULT, D3DX_DEFAULT, 
            0xFF000000, NULL, NULL, &g_texture)))
                {
                    MessageBox(NULL, 
            "Create texture failed!""ERROR", MB_OK);
                    
            return false;
                }

                sCustomVertex vertices[] 
            =
                {
                    { 
            -3,   -3,  0.0f,  0.0f1.0f},   
                    { 
            -3,    3,  0.0f,  0.0f0.0f},    
                    {  
            3,   -3,  0.0f,  1.0f1.0f},    
                    {  
            3,    3,  0.0f,  1.0f0.0f }

                };

                g_device
            ->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_MANAGED, &g_vertex_buffer, NULL);

                
            void* ptr;

                g_vertex_buffer
            ->Lock(00, (void**)&ptr, 0);
                memcpy(ptr, vertices, 
            sizeof(vertices));    
                g_vertex_buffer
            ->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;

                
            if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                              
            &d3dpp, &g_device)))
                {
                    
            return false;
                }
                
                
            if(! init_graphics())
                    
            return false;

                g_device
            ->SetRenderState(D3DRS_LIGHTING, FALSE);
                g_device
            ->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
                
                
            return true;
            }

            void cleanup()
            {
                release_com(g_texture);
                release_com(g_mip_texture);
                release_com(g_vertex_buffer);
                release_com(g_device);
                release_com(g_d3d);
            }

            void render()
            {
                setup_matrices();

                g_device
            ->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(555), 1.0f0);

                g_device
            ->BeginScene();

                g_device
            ->SetTexture(0, g_use_mip_texture ? g_mip_texture : g_texture);

                g_device
            ->SetStreamSource(0, g_vertex_buffer, 0sizeof(sCustomVertex));
                g_device
            ->SetFVF(D3DFVF_CUSTOM_VERTEX);
                g_device
            ->DrawPrimitive(D3DPT_TRIANGLESTRIP, 02);

                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 VK_ESCAPE:
                        DestroyWindow(hwnd);
                        
            break;

                    
            case 49:    // press key "1", use mipmap texture filter mode
                        g_use_mip_texture = true;
                        
            break;

                    
            case 50:    // press key "2", use normal texture filter mode
                        g_use_mip_texture = false;
                        
            break;

                    
            case VK_DOWN:
                        g_distance 
            -= 0.1f;
                        
            break;

                    
            case VK_UP:
                        g_distance 
            += 0.1f;
                        
            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, 200100800600,
                                         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();
                        Sleep(
            10);
                    }
                }

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

                
            return 0;
            }

             

            posted on 2008-05-07 11:53 lovedday 閱讀(2309) 評論(0)  編輯 收藏 引用

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            亚洲va中文字幕无码久久| 久久这里的只有是精品23| 久久99精品久久久大学生| 97久久国产露脸精品国产| 亚洲精品无码久久一线| 久久久久女人精品毛片| 国产L精品国产亚洲区久久| 中文成人无码精品久久久不卡| 99久久精品免费看国产一区二区三区| 国产午夜福利精品久久2021| 久久青青草原精品影院| 怡红院日本一道日本久久| 中文字幕无码久久人妻| 久久久久四虎国产精品| 久久精品桃花综合| 国产精品免费久久久久久久久 | 国产精品丝袜久久久久久不卡 | 品成人欧美大片久久国产欧美| 久久天天婷婷五月俺也去| 久久精品国产免费一区| 亚洲精品无码成人片久久| 久久久久国色AV免费看图片| 国产成人久久精品区一区二区| 亚洲性久久久影院| 精品久久久久久无码国产| 91精品国产综合久久精品| 久久人妻少妇嫩草AV蜜桃| 欧美大战日韩91综合一区婷婷久久青草| 久久丫精品国产亚洲av不卡| 久久青青色综合| 久久99九九国产免费看小说| 久久久99精品成人片中文字幕| 久久99精品国产麻豆宅宅| 久久精品亚洲精品国产色婷| 亚洲午夜久久久久久久久电影网| 香蕉99久久国产综合精品宅男自| 国内精品久久久久久久影视麻豆 | 国产婷婷成人久久Av免费高清| 一本色道久久综合亚洲精品| 久久人人爽人人爽人人片av麻烦 | 日本道色综合久久影院|