以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  纹理映射基础(1)  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=69917)


--  作者:卷积内核
--  发布时间:11/28/2008 10:51:00 AM

--  纹理映射基础(1)
为了使渲染的图形看起来更真实,Direct3D提供了在物体表面绘制纹理的功能。一般说来,纹理是表示物体表面细节的一幅或几幅二维图形,也称纹理贴图(texture)。当把纹理按照特定的方式映射到物体表面的时候,能使物体看上去更加真实。当前流行的图形系统中,纹理绘制已经成为一种必不可少的渲染方法。在理解纹理映射时,可以将纹理看作应用程序在物体表面的像素颜色。在真实世界中,纹理表示一个对象的颜色、图案以及触觉特征。但在Direct3D中,纹理只表示对象表面的彩色图案,它不能改变对象的几何形式。更进一步说,它只是一种高强度计算行为。

纹理位图

Direct3D纹理位图是用来表示物体表面图案的二维数组,数组的每一个元素都存储有一个颜色值,称为纹理元素(texel)。经常见到的后缀名为bmp、jpg、tga的图形文件都可以作为纹理位图。

纹理对象

在Direct3D中,纹理是以COM对象的形式存在的,要对物体表面进行纹理映射,首先需要创建纹理对象,创建时需要指定纹理的宽度、高度、格式等属性,然后还需要将图形文件加载到纹理对象中,整个过程比较复杂,为此,Direct3D提供了一个辅助函数D3DXCreateTextureFromFile(),可以直接方便地从磁盘上的图形文件中创建纹理对象,并使用默认设置。如果从文件创建纹理时不想使用各种默认设置,还可以使用另外一个辅助函数D3DXCreateTextureFromFileEx(),该函数也可以直接从磁盘上的图形文件中创建纹理对象,而且还可以指定创建的纹理对象的宽度、高度、格式等。

纹理坐标与纹理元素

纹理相当于一幅位图,在使用时贴到物体表面上。纹理位图是一个二维数组,数组的每一个元素称为纹理元素,它存储有一个颜色值。

纹理元素在表示纹理的数组中的二维下标(即它在位图中的二维坐标)称为纹理坐标。一般以字母(u, v)表示,可以想象成纹理元素所对应的行数和列数,也称实际纹理坐标。假设位图的宽、高分别为w、h,显然

0 ≤ u ≤ w, 0 ≤ v ≤ h

纹理坐标是指纹理空间坐标,即纹理坐标是相对于纹理空间原点而言的。

由于在一个图形显示系统中往往存在多幅不同的纹理,它们的宽、高也不尽相同,用实际纹理坐标表示纹理元素的位置在计算上很难统一,例如在同一个物体表面应用两幅大小不同的纹理。所以经常使用相对纹理坐标(u', v')代替实际纹理坐标(在下文中如无特别说明,纹理坐标就是指相对纹理坐标),u'、v'分别表示u、v所占宽、高的百分比:

u' = u/w,   v' = v/h

一般情况下,所有的纹理元素相对坐标的地址都在[0.0, 1.0]范围内,当然从技术上讲,可以使用[0.0, 1.0]范围之外的纹理坐标实现特殊效果。

Direct3D将纹理空间中的纹理元素映射到屏幕空间像素的映射过程常常是一个反映射。也就是说,对于屏幕空间的每个像素,计算其中纹理空间中相应的纹理元素的位置,然后采样该点或该点附近的纹理颜色。这个映射过程称为纹理过滤。

为了将纹理映射到物体表面,需要为物体顶点指定纹理坐标。

创建纹理对象

图形显示的纹理大都存储在磁盘图形文件中(诸如.bmp、.tga、.jpg文件),要想使用它们,必须创建Direct3D纹理对象,并载入图形文件内容。

函数IDirect3DDevice9::CreateTexture()用来创建一个Direct3D纹理对象,该函数声明如下:

Creates a texture resource.

HRESULT CreateTexture(  UINT Width,  UINT Height,  UINT Levels,  DWORD Usage,  D3DFORMAT Format,  D3DPOOL Pool,  IDirect3DTexture9** ppTexture,  HANDLE* pSharedHandle);
Parameters
Width
[in] Width of the top-level of the texture, in pixels. The pixel dimensions of subsequent levels will be the truncated value of half of the previous level's pixel dimension (independently). Each dimension clamps at a size of 1 pixel. Thus, if the division by 2 results in 0, 1 will be taken instead.
Height
[in] Height of the top-level of the texture, in pixels. The pixel dimensions of subsequent levels will be the truncated value of half of the previous level's pixel dimension (independently). Each dimension clamps at a size of 1 pixel. Thus, if the division by 2 results in 0, 1 will be taken instead.
Levels
[in] Number of levels in the texture. If this is zero, Direct3D will generate all texture sublevels down to 1 by 1 pixels for hardware that supports mipmapped textures. Call IDirect3DBaseTexture9::GetLevelCount to see the number of levels generated.
Usage
[in] Usage can be 0, which indicates no usage value. However, if usage is desired, use a combination of one or more D3DUSAGE constants. It is good practice to match the usage parameter with the behavior flags in IDirect3D9::CreateDevice.
Format
[in] Member of the D3DFORMAT enumerated type, describing the format of all levels in the texture.
Pool
[in] Member of the D3DPOOL enumerated type, describing the memory class into which the texture should be placed.
ppTexture
[out, retval] Pointer to an IDirect3DTexture9 interface, representing the created texture resource.
pSharedHandle
[in] Reserved. Set this parameter to NULL.
Return Values
If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be one of the following: D3DERR_INVALIDCALL, D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY.

Remarks
An application can discover support for Automatic Generation of Mipmaps (Direct3D 9) in a particular format by calling IDirect3D9::CheckDeviceFormat with D3DUSAGE_AUTOGENMIPMAP. If IDirect3D9::CheckDeviceFormat returns D3DOK_NOAUTOGEN, IDirect3DDevice9::CreateTexture will succeed but it will return a one-level texture.


--  作者:卷积内核
--  发布时间:11/28/2008 10:52:00 AM

--  
Usages
The following table summarizes the available usage options.

#define Description
D3DUSAGE_AUTOGENMIPMAP The resource will automatically generate mipmaps. See Automatic Generation of Mipmaps (Direct3D 9). Automatic generation of mipmaps is not supported for volume textures and depth stencil surfaces/textures. This usage is not valid for a resource in system memory (D3DPOOL_SYSTEMMEM).
D3DUSAGE_DEPTHSTENCIL The resource will be a depth stencil buffer. D3DUSAGE_DEPTHSTENCIL can only be used with D3DPOOL_DEFAULT.
D3DUSAGE_DMAP The resource will be a displacement map.
D3DUSAGE_DONOTCLIP Set to indicate that the vertex buffer content will never require clipping. When rendering with buffers that have this flag set, the D3DRS_CLIPPING render state must be set to false.
D3DUSAGE_DYNAMIC Set to indicate that the vertex buffer requires dynamic memory use. This is useful for drivers because it enables them to decide where to place the buffer. In general, static vertex buffers are placed in video memory and dynamic vertex buffers are placed in AGP memory. Note that there is no separate static use. If you do not specify D3DUSAGE_DYNAMIC, the vertex buffer is made static. D3DUSAGE_DYNAMIC is strictly enforced through the D3DLOCK_DISCARD and D3DLOCK_NOOVERWRITE locking flags. As a result, D3DLOCK_DISCARD and D3DLOCK_NOOVERWRITE are valid only on vertex buffers created with D3DUSAGE_DYNAMIC. They are not valid flags on static vertex buffers. For more information, see Managing Resources (Direct3D 9).
For more information about using dynamic vertex buffers, see Performance Optimizations (Direct3D 9).

D3DUSAGE_DYNAMIC and D3DPOOL_MANAGED are incompatible and should not be used together. See D3DPOOL.

Textures can specify D3DUSAGE_DYNAMIC. However, managed textures cannot use D3DUSAGE_DYNAMIC. For more information about dynamic textures, see Using Dynamic Textures.

D3DUSAGE_NONSECURE Allow a shared surface created by a secure application to be opened by a non-secure application that has the shared handle. Differences between Direct3D 9 and Direct3D 9Ex:
This flag is available in Direct3D 9Ex only.


D3DUSAGE_NPATCHES Set to indicate that the vertex buffer is to be used for drawing N-patches.
D3DUSAGE_POINTS Set to indicate that the vertex or index buffer will be used for drawing point sprites. The buffer will be loaded in system memory if software vertex processing is needed to emulate point sprites.
D3DUSAGE_RENDERTARGET The resource will be a render target. D3DUSAGE_RENDERTARGET can only be used with D3DPOOL_DEFAULT.
D3DUSAGE_RTPATCHES Set to indicate that the vertex buffer is to be used for drawing high-order primitives.
D3DUSAGE_SOFTWAREPROCESSING If this flag is used, vertex processing is done in software. If this flag is not used, vertex processing is done in hardware.
The D3DUSAGE_SOFTWAREPROCESSING flag can be set when mixed-mode or software vertex processing (D3DCREATE_MIXED_VERTEXPROCESSING / D3DCREATE_SOFTWARE_VERTEXPROCESSING) is enabled for that device. D3DUSAGE_SOFTWAREPROCESSING must be set for buffers to be used with software vertex processing in mixed mode, but it should not be set for the best possible performance when using hardware index processing in mixed mode (D3DCREATE_HARDWARE_VERTEXPROCESSING). However, setting D3DUSAGE_SOFTWAREPROCESSING is the only option when a single buffer is used with both hardware and software vertex processing. D3DUSAGE_SOFTWAREPROCESSING is allowed for mixed and software devices.

D3DUSAGE_SOFTWAREPROCESSING is used with IDirect3D9::CheckDeviceFormat to find out if a particular texture format can be used as a vertex texture during software vertex processing. If it can, the texture must be created in D3DPOOL_SCRATCH.

D3DUSAGE_TEXTAPI This usage flag must be specified for vertex buffers and source surfaces, used in calls to IDirect3DDevice9Ex::ComposeRect. Textures created with this usage flag cannot be used for texture filtering. Vertex buffers, created with this usage flag, cannot be used as input stream sources. Differences between Direct3D 9 and Direct3D 9Ex:
This flag is available in Direct3D 9Ex only.


D3DUSAGE_WRITEONLY Informs the system that the application writes only to the vertex buffer. Using this flag enables the driver to choose the best memory location for efficient write operations and rendering. Attempts to read from a vertex buffer that is created with this capability will fail. Buffers created with D3DPOOL_DEFAULT that do not specify D3DUSAGE_WRITEONLY might suffer a severe performance penalty.


--  作者:卷积内核
--  发布时间:11/28/2008 10:53:00 AM

--  
纹理映射基础(2)
Direct3D纹理对象生成后,纹理对象指针指向的纹理资源指针还没有被有效赋值,如果想要通过磁盘图形文件载入纹理,那么必须将磁盘图形文件的内容载入Direct3D纹理资源,因为不同的文件具有不同的格式,所以为了将文件内容载入到Direct3D纹理资源,就要了解各种图形文件的格式,并针对不同的格式编写不同的代码将文件内容载入到纹理资源中。

从磁盘文件生成并载入纹理相当繁琐,它涉及到各种图形文件的存储格式。幸运的是,Direct3D扩展实用库函数D3DXCreateTextureFromFile()为我们提供了从磁盘图形文件创建纹理并载入纹理内容的快捷方法,该函数声明如下:

Creates a texture from a file.

HRESULT D3DXCreateTextureFromFile(  LPDIRECT3DDEVICE9 pDevice,  LPCTSTR pSrcFile,  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.
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_NOTAVAILABLED3DERR_OUTOFVIDEOMEMORYD3DERR_INVALIDCALLD3DXERR_INVALIDDATAE_OUTOFMEMORY
Remarks
The compiler setting also determines the function version. If Unicode is defined, the function call resolves to D3DXCreateTextureFromFileW. Otherwise, the function call resolves to D3DXCreateTextureFromFileA because ANSI strings are being used.

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

The function is equivalent to D3DXCreateTextureFromFileEx(pDevice, pSrcFile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, ppTexture).

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, the images need to be loaded manually.

Note that a resource created with this function will be placed in the memory class denoted by D3DPOOL_MANAGED.

Filtering is automatically applied to a texture created using this method. The filtering is equivalent to D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER in D3DX_FILTER.

For the best performance when using D3DXCreateTextureFromFile:

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 two dimensions, create and store images using power of two dimensions.
Consider using DirectDraw surface (DDS) files. Because 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.

设置当前渲染纹理

Direct3D设备接口函数IDirect3DDevice9::SetTexture()将Direct3D纹理与指定的纹理层关联,该函数的声明如下:

Assigns a texture to a stage for a device.

HRESULT SetTexture(  DWORD Sampler,  IDirect3DBaseTexture9 * pTexture);
Parameters
Sampler
Zero based sampler number. Textures are bound to samplers; samplers define sampling state such as the filtering mode and the address wrapping mode. Textures are referenced differently by the programmable and the fixed function pipeline:

Programmable shaders reference textures using the sampler number. The number of samplers available to a programmable shader is dependent on the shader version. For vertex shaders, see Sampler (Direct3D 9 asm-vs). For pixel shaders see Sampler (Direct3D 9 asm-ps).
The fixed function pipeline on the other hand, references textures by texture stage number. The maximum number of samplers is determined from two caps: MaxSimultaneousTextures and MaxTextureBlendStages of the D3DCAPS9 structure.
[in] There are two other special cases for stage/sampler numbers.
A special number called D3DDMAPSAMPLER is used for Displacement Mapping (Direct3D 9).
A programmable vertex shader uses a special number defined by a D3DVERTEXTEXTURESAMPLER when accessing Vertex Textures in vs_3_0 (DirectX HLSL).
  

pTexture
[in] Pointer to an IDirect3DBaseTexture9 interface, representing the texture being set.
Return Values
If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.

Remarks
IDirect3DDevice9::SetTexture is not allowed if the texture is created with a pool type of D3DPOOL_SCRATCH. IDirect3DDevice9::SetTexture is not allowed with a pool type of D3DPOOL_SYSTEMMEM texture unless DevCaps is set with D3DDEVCAPS_TEXTURESYSTEMMEMORY.


--  作者:卷积内核
--  发布时间:11/28/2008 10:53:00 AM

--  
设置纹理相关状态

纹理相关状态包括纹理过滤方式、纹理寻址模式、纹理阶段混合状态。因为Direct3D 9.0最多支持8层纹理贴图,所以最多存在8个纹理阶段,这些纹理相关状态需要针对每个纹理阶段分别进行设置,用于控制每个阶段纹理的相关操作。

示例程序:

按此在新窗口浏览图片


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

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.0f, 0.0f, -10.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/4, 1.0f, 1.0f, 100.0f);
    g_device->SetTransform(D3DTS_PROJECTION, &mat_proj);
}

bool init_graphics()
{    
    if(FAILED(D3DXCreateTextureFromFile(g_device, "texture.jpg", &g_texture)))
    {
        MessageBox(NULL, "Create texture failed!", "ERROR", MB_OK);
        return false;
    }

    sCustomVertex vertices[] =
    {
        { -3,   -3,  0.0f,  0.0f, 1.0f},   
        { -3,    3,  0.0f,  0.0f, 0.0f},    
        {  3,   -3,  0.0f,  1.0f, 1.0f},    
        {  3,    3,  0.0f,  1.0f, 0.0f }

    };

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

    void* ptr;

    g_vertex_buffer->Lock(0, 0, (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;

    setup_matrices();

    g_device->SetRenderState(D3DRS_LIGHTING, FALSE);    
    
    return true;
}

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

void render()
{
    g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(5, 5, 5), 1.0f, 0);

    g_device->BeginScene();

    g_device->SetTexture(0, g_texture);

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

    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:
        if(wParam == VK_ESCAPE)
            DestroyWindow(hwnd);
        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, 200, 100, 640, 480,
                             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, 0, 0, PM_REMOVE))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
                
            render();
        }
    }

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

    return 0;
}


--  作者:卷积内核
--  发布时间:11/28/2008 10:54:00 AM

--  
纹理映射基础(3)
当Direct3D渲染一个图元时,必须将它通过坐标变换映射到二维屏幕上。如果图元有纹理,Direct3D就需要用纹理来产生图元的二维渲染图像上每个像素的颜色。对于图元在二维屏幕上图像的每个像素来说,都必须从纹理中获得一个颜色,从纹理中为每个像素获取颜色的过程称为纹理过滤(texture filtering)。

大多数情况下,屏幕显示的图形大小与纹理贴图大小不相同,换句话说,这个纹理将被映射到一个比它大或小的图元的图像上,这样纹理常常会被放大或缩小。对纹理的放大会造成许多像素被映射到同一个纹理元素,图形渲染结果会有色块的感觉。缩小一个纹理意味着一个像素被映射到许多纹理元素,图形看上去会闪烁失真或有锯齿。为了解决这些问题,可以将相关纹理元素的颜色融合到一个像素上,如何将多个纹理元素的颜色融合到一个像素上取决于纹理过滤方式。

Direct3D支持4种纹理过滤方式,分别是:最近点采样(nearest-point sampling)、线性纹理过滤(linear texture filtering)、各向异性纹理过滤(anisotropic texture filtering)和多级渐进纹理过滤(texture filtering with mipmaps)。不同的纹理过滤方式产生的图像效果差别可能很大。

4种纹理过滤方法各有优缺点。例如,线性过滤生成的图像较粗糙,但计算量小。多级渐进纹理过滤的效果通常最好,特别是和各项异性过滤联合使用时效果更好,但是它需要Direct3D提供的内存空间最多,计算量也最大。

纹理采样属性设置函数IDirect3DDevice9::SetSamplerState()可以用来设置纹理过滤方式。该函数的声明如下:

Sets the sampler state value.

HRESULT SetSamplerState(  DWORD Sampler,  D3DSAMPLERSTATETYPE Type,  DWORD Value);
Parameters
Sampler
[in] The sampler stage index.
Type
[in] This parameter can be any member of the D3DSAMPLERSTATETYPE enumerated type.
Value
[in] State value to set. The meaning of this value is determined by the Type parameter.
Return Values
If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.

参数Type属于D3DSAMPLERSTATETYPE类型,用来指定对哪种纹理采样属性赋值。

D3DSAMPLERSTATETYPE
Sampler states define texture sampling operations such as texture addressing and texture filtering. Some sampler states set-up vertex processing, and some set-up pixel processing. Sampler states can be saved and restored using stateblocks (see State Blocks Save and Restore State (Direct3D 9)).

typedef enum D3DSAMPLERSTATETYPE{    D3DSAMP_ADDRESSU = 1,    D3DSAMP_ADDRESSV = 2,    D3DSAMP_ADDRESSW = 3,    D3DSAMP_BORDERCOLOR = 4,    D3DSAMP_MAGFILTER = 5,    D3DSAMP_MINFILTER = 6,    D3DSAMP_MIPFILTER = 7,    D3DSAMP_MIPMAPLODBIAS = 8,    D3DSAMP_MAXMIPLEVEL = 9,    D3DSAMP_MAXANISOTROPY = 10,    D3DSAMP_SRGBTEXTURE = 11,    D3DSAMP_ELEMENTINDEX = 12,    D3DSAMP_DMAPOFFSET = 13,    D3DSAMP_FORCE_DWORD = 0x7fffffff,} D3DSAMPLERSTATETYPE, *LPD3DSAMPLERSTATETYPE;
Constants
D3DSAMP_ADDRESSU
Texture-address mode for the u coordinate. The default is D3DTADDRESS_WRAP. For more information, see D3DTEXTUREADDRESS.
D3DSAMP_ADDRESSV
Texture-address mode for the v coordinate. The default is D3DTADDRESS_WRAP. For more information, see D3DTEXTUREADDRESS.
D3DSAMP_ADDRESSW
Texture-address mode for the w coordinate. The default is D3DTADDRESS_WRAP. For more information, see D3DTEXTUREADDRESS.
D3DSAMP_BORDERCOLOR
Border color or type D3DCOLOR. The default color is 0x00000000.
D3DSAMP_MAGFILTER
Magnification filter of type D3DTEXTUREFILTERTYPE. The default value is D3DTEXF_POINT.
D3DSAMP_MINFILTER
Minification filter of type D3DTEXTUREFILTERTYPE. The default value is D3DTEXF_POINT.
D3DSAMP_MIPFILTER
Mipmap filter to use during minification. See D3DTEXTUREFILTERTYPE. The default value is D3DTEXF_NONE.
D3DSAMP_MIPMAPLODBIAS
Mipmap level-of-detail bias. The default value is zero.
D3DSAMP_MAXMIPLEVEL
level-of-detail index of largest map to use. Values range from 0 to (n - 1) where 0 is the largest. The default value is zero.
D3DSAMP_MAXANISOTROPY
DWORD maximum anisotropy. The default value is 1.
D3DSAMP_SRGBTEXTURE
Gamma correction value. The default value is 0, which means gamma is 1.0 and no correction is required. Otherwise, this value means that the sampler should assume gamma of 2.2 on the content and convert it to linear (gamma 1.0) before presenting it to the pixel shader.
D3DSAMP_ELEMENTINDEX
When a multielement texture is assigned to the sampler, this indicates which element index to use. The default value is 0.
D3DSAMP_DMAPOFFSET
Vertex offset in the presampled displacement map. This is a constant used by the tessellator, its default value is 0.
D3DSAMP_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.
其中,D3DSAMP_MAGFILTER、D3DSAMP_MINFILTER、D3DSAMP_MIPFILTER、D3DSAMP_MIPMAPLODBIAS、D3DSAMP_MAXMIPLEVEL和D3DSAMP_MAXANISOTROPY用来控制纹理过滤方式。

D3DTEXTUREFILTERTYPE
Defines texture filtering modes for a texture stage.

typedef enum D3DTEXTUREFILTERTYPE{    D3DTEXF_NONE = 0,    D3DTEXF_POINT = 1,    D3DTEXF_LINEAR = 2,    D3DTEXF_ANISOTROPIC = 3,    D3DTEXF_PYRAMIDALQUAD = 6,    D3DTEXF_GAUSSIANQUAD = 7,    D3DTEXF_CONVOLUTIONMONO = 8,    D3DTEXF_FORCE_DWORD = 0x7fffffff,} D3DTEXTUREFILTERTYPE, *LPD3DTEXTUREFILTERTYPE;
Constants
D3DTEXF_NONE
Mipmapping disabled. The rasterizer should use the magnification filter instead.
D3DTEXF_POINT
Point filtering used as a texture magnification or minification filter. The texel with coordinates nearest to the desired pixel value is used. The texture filter to be used between mipmap levels is nearest-point mipmap filtering. The rasterizer uses the color from the texel of the nearest mipmap texture.
D3DTEXF_LINEAR
Bilinear interpolation filtering used as a texture magnification or minification filter. A weighted average of a 2 x 2 area of texels surrounding the desired pixel is used. The texture filter to use between mipmap levels is trilinear mipmap interpolation. The rasterizer linearly interpolates pixel color, using the texels of the two nearest mipmap textures.
D3DTEXF_ANISOTROPIC
Anisotropic texture filtering used as a texture magnification or minification filter. Compensates for distortion caused by the difference in angle between the texture polygon and the plane of the screen.
D3DTEXF_PYRAMIDALQUAD
A 4-sample tent filter used as a texture magnification or minification filter.
D3DTEXF_GAUSSIANQUAD
A 4-sample Gaussian filter used as a texture magnification or minification filter.
D3DTEXF_CONVOLUTIONMONO
Convolution filter for monochrome textures. See D3DFMT_A1. Differences between Direct3D 9 and Direct3D 9Ex:
This flag is available in Direct3D 9Ex only.

D3DTEXF_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
To check if a format supports texture filter types other than D3DTEXF_POINT (which is always supported), call IDirect3D9::CheckDeviceFormat with D3DUSAGE_QUERY_FILTER.

Set a texture stage's magnification filter by calling IDirect3DDevice9::SetSamplerState with the D3DSAMP_MAGFILTER value as the second parameter and one member of this enumeration as the third parameter.

Set a texture stage's minification filter by calling IDirect3DDevice9::SetSamplerState with the D3DSAMP_MINFILTER value as the second parameter and one member of this enumeration as the third parameter.

Set the texture filter to use between-mipmap levels by calling IDirect3DDevice9::SetSamplerState with the D3DSAMP_MIPFILTER value as the second parameter and one member of this enumeration as the third parameter.

Not all valid filtering modes for a device will apply to volume maps. In general, D3DTEXF_POINT and D3DTEXF_LINEAR magnification filters will be supported for volume maps. If D3DPTEXTURECAPS_MIPVOLUMEMAP is set, then the D3DTEXF_POINT mipmap filter and D3DTEXF_POINT and D3DTEXF_LINEAR minification filters will be supported for volume maps. The device may or may not support the D3DTEXF_LINEAR mipmap filter for volume maps. Devices that support anisotropic filtering for 2D maps do not necessarily support anisotropic filtering for volume maps. However, applications that enable anisotropic filtering will receive the best available filtering (probably linear) if anisotropic filtering is not supported.

Unsigned Formats
Data in an unsigned format must be positive. Unsigned formats use combinations of (R)ed, (G)reen, (B)lue, (A)lpha, (L)uminance, and (P)alette data. Palette data is also referred to as color indexed data because the data is used to index a color palette.

Unsigned format flags Value Format
D3DFMT_R8G8B8 20 24-bit RGB pixel format with 8 bits per channel.
D3DFMT_A8R8G8B8 21 32-bit ARGB pixel format with alpha, using 8 bits per channel.
D3DFMT_X8R8G8B8 22 32-bit RGB pixel format, where 8 bits are reserved for each color.
D3DFMT_R5G6B5 23 16-bit RGB pixel format with 5 bits for red, 6 bits for green, and 5 bits for blue.
D3DFMT_X1R5G5B5 24 16-bit pixel format where 5 bits are reserved for each color.
D3DFMT_A1R5G5B5 25 16-bit pixel format where 5 bits are reserved for each color and 1 bit is reserved for alpha.
D3DFMT_A4R4G4B4 26 16-bit ARGB pixel format with 4 bits for each channel.
D3DFMT_R3G3B2 27 8-bit RGB texture format using 3 bits for red, 3 bits for green, and 2 bits for blue.
D3DFMT_A8 28 8-bit alpha only.
D3DFMT_A8R3G3B2 29 16-bit ARGB texture format using 8 bits for alpha, 3 bits each for red and green, and 2 bits for blue.
D3DFMT_X4R4G4B4 30 16-bit RGB pixel format using 4 bits for each color.
D3DFMT_A2B10G10R10 31 32-bit pixel format using 10 bits for each color and 2 bits for alpha.
D3DFMT_A8B8G8R8 32 32-bit ARGB pixel format with alpha, using 8 bits per channel.
D3DFMT_X8B8G8R8 33 32-bit RGB pixel format, where 8 bits are reserved for each color.
D3DFMT_G16R16 34 32-bit pixel format using 16 bits each for green and red.
D3DFMT_A2R10G10B10 35 32-bit pixel format using 10 bits each for red, green, and blue, and 2 bits for alpha.
D3DFMT_A16B16G16R16 36 64-bit pixel format using 16 bits for each component.
D3DFMT_A8P8 40 8-bit color indexed with 8 bits of alpha.
D3DFMT_P8 41 8-bit color indexed.
D3DFMT_L8 50 8-bit luminance only.
D3DFMT_L16 81 16-bit luminance only.
D3DFMT_A8L8 51 16-bit using 8 bits each for alpha and luminance.
D3DFMT_A4L4 52 8-bit using 4 bits each for alpha and luminance.
D3DFMT_A1 118 1-bit monochrome. Differences between Direct3D 9 and Direct3D 9Ex:
This flag is available in Direct3D 9Ex only.


D3DFMT_BINARYBUFFER 199 Binary format indicating that the data has no inherent type. Differences between Direct3D 9 and Direct3D 9Ex:
This flag is available in Direct3D 9Ex only.



W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
109.375ms