Monday, December 05, 2005

Texture mapping in Direct3D

  1. Defining a custom vertex
  2. Initializing screen geometry
  3. Rendering the scene
We map one texture to a piece of geometry by specifying D3DFVF_TEX_no

const DWORD FVF = ( D3DXYZRHW | D3DFVF_TEXno )

(TEX1, TEX2, ..... , TEX8) - upto 8 textures, no - number of coordinate pairs per vertex

To provide values to the texture coordinate pairs, we use a vertex structure to correspond to these vertex flags.

VERTEX Vertices[]
{

// x, y , rhw , tu , tv
}

m_pd3dDevice->SetTexture( 0, texturename);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0);

The texture coordinate pair is set for a particular texture with D3DTSS_TEXCOORDINDEX

Texture-Addressing Modes : When texture coordinate values lie outside the [0.0,1.0] range.
  • Map
  • Mirror
  • Border Color
  • Mirroronce
The graphics cards may not support all the addressing modes.

1. Wrap Mode [default] - repeat the texture on every integer junction and addressing mode

To switch it on , use

m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);

2. Mirror Mode - mirror the texture at every integer boundary so that the texels are flipped outside of the [0.0,1.0] boundary. The mirroring is done along each axis.

m_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR);
m_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_MIRROR);

3. Clamp texture-addressing mode - applies the texture to the polygons once, and then smears the color of the texture edge pixels. The pixel colors at the bottom of the columns and end of the rows are are extended to the bottom and right of the primitive.

m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);

This mode is useful if we want to stitch together multiple textures over a mesh, since the bilinear interpolation used by wrap mode at the edges introduces artifacts from the opposite edge of the texture. For better fixing of the aliasing, we need to modify the texture coordinates.

4. Border Color Texture Addressing Mode - draw a (specified) colored border around the texture

m_pd3dDevice->SetSamplerState (0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
m_pd3dDevice->SetSamplerState (0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
m_pd3dDevice->SetSamplerState (0, D3DSAMP_BORDERCOLOR, 0x00aaaaaa);

We can use this to fake large textures by specifying a matching border around a smaller texture.

5. Mirroronce Texture Addressing Mode - Mirror + Clamp modes hybrid
Texture mirrored within the range [-1.0,1.0] and clamped outside of this range.

m_pd3dDevice->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_MIRRORONCE);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_MIRRORONCE);

Texture Wrapping - decides how to interpolate between texture coordinates, and affects the way Direct3D rasterizes textured polygons and the way it utilizes the texture coordinates specified for each vertex

m_pd3dDevice->SetRenderState( D3DRS_WRAP0, D3DWRAP_U | D3DWRAP_V );

Texture coordinates outside the 0.0 and 1.0 become invalid and texture addressing is turned off.

Texture Filtering and Anti-Aliasing

As the textured object moves away from the screen of projection, two differently colored pixels might project to the same pixel on the screen. This will produce a pixel with a mix of the two pixels' colors and hence an inaccurate image.
This is called Aliasing.

Staircase effect - mapping a smaller texture to a larger polygon, will produce a staircase like image for an originally smooth line (magnification)
Swimming pixels - created on mapping a larger texture onto a smaller polygon (minification)

Filtering - way to get the texels from the texture map given the u,v coordinate

Basic texture-filtering methods:
  • Nearest Point Sampling
  • Linear Texture Filtering
  • Anisotropic Texture Filtering
Newer hardware supports:
  • Point Sampling
  • Bilinear filtering (linear filtering)
  • Trilinear filtering (linear filtering + mipmapping)

Mipmaps - A series of textures, each containing a progressively lower resolution of the image which represents the texture. Each level has a height and width, half the corresponding values in the previous level. The levels can be either square or rectangular.

Mipmaps are produced and used automatically if the right parameters are provided to the CreateTexture() method, which uses the D3DXCreateTextureFromFileEx() method.

They help in
  • reducing memory traffic
  • saving time
Direct3D automatically picks the MIP level which makes the texel-to-pixel ration closest to 1.0

0 Comments:

Post a Comment

<< Home