- Forward


An Introduction to Texture Mapping in Direct3D
with Examples in C++


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Parts of the Process
Back SMYC Forward
  • Define a custom vertex format
  • Create a texture
  • Select a texture
  • Specify parameters
  • Enabling texture mapping
Defining a Custom Vertex Format
Back SMYC Forward
  • The Custom Vertex struct:
    • Add two float values for the texture coordinates \(u\) and \(v\)
  • Set the Flexible Vertex Format (FVF):
    • Use the bitflag D3DFVF_TEX1
Defining a Custom Vertex Format (cont.)
Back SMYC Forward

An Example

directx9examples/textures/cube/cube.cpp (Fragment: vertices)
 
Creating a Texture
Back SMYC Forward
  • From a file:
    • HRESULT D3DXCreateTextureFromFile(LPDIRECT3DDEVICE9 device, LPCTSTR sourceFile, LPDIRECT3DTEXTURE9* texture)
  • Creates:
    • An object that implements the IDirect3DTexture9 interface
Creating a Texture (cont.)
Back SMYC Forward

An Example

directx9examples/textures/cube/cube.cpp (Fragment: create)
 
Selecting a Texture
Back SMYC Forward
  • The Method:
    • HRESULT IDirect3DDevice9::SetTexture(DWORD sampler, IDirect3DBaseTexture9* texture)
  • Remember:
    • This changes the state of the renderer so it may need to be called once or multiple times (e.g., once for each primitive) depending on the application
Selecting a Texture (cont.)
Back SMYC Forward

An Example

directx9examples/textures/cube/cube.cpp (Fragment: select)
 
Specifiying Parameters
Back SMYC Forward
  • The Method:
    • HRESULT IDirect3DDevice9::SetTextureStageState(DWORD stage, D3DTEXTURESTAGESTATETYPE name, DWORD value)
  • The name:
    • D3DTSS_COLOROP,D3DTSS_COLORARG1, D3DTSS_COLORARG2,D3DTSS_ALPHAOP, D3DTSS_ALPHAARG1,D3DTSS_ALPHAARG2, D3DTSS_BUMPENVMAT00,D3DTSS_BUMPENVMAT01, D3DTSS_BUMPENVMAT10,D3DTSS_BUMPENVMAT11, D3DTSS_TEXCOORDINDEX,D3DTSS_BUMPENVLSCALE, D3DTSS_BUMPENVLOFFSET, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTSS_COLORARG0,D3DTSS_ALPHAARG0, D3DTSS_RESULTARG,D3DTSS_CONSTANT, D3DTSS_FORCE_DWORD
Specifiying Parameters (cont.)
Back SMYC Forward

An Example

directx9examples/textures/cube/cube.cpp (Fragment: setup)
 
There's Always More to Learn
Back -