DirectX9: Texture Basics

I. Introduction

Texture mapping is a technique for imparting the image data is triangular

In Direct3D texture by IDirect3DTexture9 interface represented, a textured surface is a matrix of pixels is mapped on the triangle

 

II. Texture coordinates

Direct3D texture coordinate using a system, which is the horizontal direction of the -axis (positive for the right) and the vertical direction v -axis (positive downward) configuration

 

// texture coordinate system representation

struct Vertex

{

  float _x, _y, _z;

  float _nx, _ny, _nz;

  float _u, _v;

  static const DWORD FVF;
};

const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;

 

III. Creating Textures

1.D3DXCreateTextureFromFile()

Texture data can be read from the image file stored on disk, placed IDirect3DTexture9 abstract texture type

Supported image formats: BMP DDS DIB JPG PNG TGA

 

HRESULT D3DXCreateTextureFromFile(

  LPDIRECT3DDEVICE9 pDevice,

  LPCSTR pSrcFile,

  LPDIRECT3DTEXTURE9* ppTexture

);

 

HRESULT IDirect3DDevice9::SetTexture(

  DWORD Stage,

  IDirect3DBaseTexture9* pTexture

);

 

// 创建纹理
IDirect3Dtexture9* _stonewall;
D3DXCreateTextureFromFile(_device, "stonewall.bmp", &_stonewall);

// 设置纹理
Device->SetTexture(0, _stonewall);

 

IV. Filter

Textures are mapped onto the screen triangle, and the triangle is not possible as large as texture, when the need arises to deformation texture, it is necessary with filter (Filtering) technology to make a smooth deformation

Direct3D provides three different filters, each level of quality and speed vary

 

1.Nearest point sampling

The default filtering method, the worst quality, fastest

Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);

 

2.Linear filtering

Recommended, good filtering effect generated

Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);

 

3.Anisotropic filtering

Filtration yielded the best quality, the longest processing time

Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC);
Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);

 

4.Mipmaps filter

If the video card supports Mipmaps, Direct3D will automatically select the best match of the triangular Mipmap

Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);  // 不使用 mipmap
Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

 

V. Addressing Modes

Between the texture coordinates must be specified in [0, 1]

Direct3D There are four addressing modes: Surround texture addressing mode border color texture addressing mode taken texture image texture addressing mode addressing mode

1. surround texture addressing mode (wrap address mode)

if (::GetAsyncKeyState('W') & 0x8000f)
{
    Device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
    Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);   
}

 

2. Border Color texture addressing mode (border color address mode)

if (::GetAsyncKeyState('B') & 0x8000f)
{
    Device>SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
    Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
    Device->SetSamplerState(0, D3DSAMP_BORDERCOLOR, 0x000000ff);
}

 

3. Intercept texture addressing mode (clamp address mode)

if (::GetAsyncKeyState('C') & 0x8000f)
{
    Device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
    Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
}

 

4. Mirror texture addressing mode (mirror address mode)

if (::GetAsyncKeyState('M') & 0x8000f)
{
    Device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR);
    Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_MIRROR);
}

 

VI. Examples

 

Guess you like

Origin www.cnblogs.com/k5bg/p/11126843.html