|
An Introduction to Texture Mapping in Direct3D
with Examples in C++ |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
struct:
float values for the texture
coordinates \(u\) and \(v\)
D3DFVF_TEX1
// Specify the struct for a vertex
struct CUSTOMVERTEX
{
float x, y, z;
float u, v; // Texture coordinates
};
// Specify the flexible vertex format (FVF) for the struct above
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_TEX1)
// Color Definitions
DWORD colorBlue = 0xFF0000FF;
DWORD colorGold = 0xFFC2A14D; // JMU Gold
DWORD colorGray = 0xFFAAAAAA;
DWORD colorGreen = 0xFF00FF00;
DWORD colorPurple = 0xFF450084; // JMU Purple
DWORD colorRed = 0xFFFF0000;
DWORD colorYellow = 0xFFFFFF00;
// Create the vertex elements
CUSTOMVERTEX vertices[] =
{
{-1.0f, 1.0f,-1.0f, 0.0f, 0.0f}, // Front
{ 1.0f, 1.0f,-1.0f, 1.0f, 0.0f},
{-1.0f,-1.0f,-1.0f, 0.0f, 1.0f},
{ 1.0f,-1.0f,-1.0f, 1.0f, 1.0f},
{-1.0f, 1.0f, 1.0f, 1.0f, 0.0f}, // Back
{-1.0f,-1.0f, 1.0f, 1.0f, 1.0f},
{ 1.0f, 1.0f, 1.0f, 0.0f, 0.0f},
{ 1.0f,-1.0f, 1.0f, 0.0f, 1.0f},
{-1.0f, 1.0f, 1.0f, 0.0f, 0.0f}, // Top
{ 1.0f, 1.0f, 1.0f, 1.0f, 0.0f},
{-1.0f, 1.0f,-1.0f, 0.0f, 1.0f},
{ 1.0f, 1.0f,-1.0f, 1.0f, 1.0f},
{-1.0f,-1.0f, 1.0f, 0.0f, 0.0f}, // Bottom
{-1.0f,-1.0f,-1.0f, 1.0f, 0.0f},
{ 1.0f,-1.0f, 1.0f, 0.0f, 1.0f},
{ 1.0f,-1.0f,-1.0f, 1.0f, 1.0f},
{ 1.0f, 1.0f,-1.0f, 0.0f, 0.0f}, // Right
{ 1.0f, 1.0f, 1.0f, 1.0f, 0.0f},
{ 1.0f,-1.0f,-1.0f, 0.0f, 1.0f},
{ 1.0f,-1.0f, 1.0f, 1.0f, 1.0f},
{-1.0f, 1.0f,-1.0f, 1.0f, 0.0f}, // Left
{-1.0f,-1.0f,-1.0f, 1.0f, 1.0f},
{-1.0f, 1.0f, 1.0f, 0.0f, 0.0f},
{-1.0f,-1.0f, 1.0f, 0.0f, 1.0f}
};
HRESULT D3DXCreateTextureFromFile(LPDIRECT3DDEVICE9 device, LPCTSTR sourceFile, LPDIRECT3DTEXTURE9* texture)
IDirect3DTexture9
interface
// Allocate memory for the textures
LPDIRECT3DTEXTURE9 texture[6];
ZeroMemory(texture, 6*sizeof(LPDIRECT3DTEXTURE9));
// Create one texture for each face
D3DXCreateTextureFromFile(d3dDevice, L"brick.bmp", &texture[0]);
D3DXCreateTextureFromFile(d3dDevice, L"brownpaper.bmp", &texture[1]);
D3DXCreateTextureFromFile(d3dDevice, L"flagstone.bmp", &texture[2]);
D3DXCreateTextureFromFile(d3dDevice, L"frostedglass.bmp", &texture[3]);
D3DXCreateTextureFromFile(d3dDevice, L"lake.bmp", &texture[4]);
D3DXCreateTextureFromFile(d3dDevice, L"oak.bmp", &texture[5]);
HRESULT IDirect3DDevice9::SetTexture(DWORD sampler, IDirect3DBaseTexture9* texture)
// Draw the various shapes (two triangles for each face)
d3dDevice->SetTexture(0, texture[0]);
d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
d3dDevice->SetTexture(0, texture[1]);
d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);
d3dDevice->SetTexture(0, texture[2]);
d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 8, 2);
d3dDevice->SetTexture(0, texture[3]);
d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 12, 2);
d3dDevice->SetTexture(0, texture[4]);
d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 16, 2);
d3dDevice->SetTexture(0, texture[5]);
d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 20, 2);
HRESULT IDirect3DDevice9::SetTextureStageState(DWORD stage, D3DTEXTURESTAGESTATETYPE name, DWORD value)
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