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


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Parts of the Process
Defining a Custom Vertex Format
Defining a Custom Vertex Format (cont.)

An Example

directx9examples/textures/cube/cube.cpp (Fragment: vertices)
        // 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}
};

        
Creating a Texture
Creating a Texture (cont.)

An Example

directx9examples/textures/cube/cube.cpp (Fragment: create)
        // 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]);
        
Selecting a Texture
Selecting a Texture (cont.)

An Example

directx9examples/textures/cube/cube.cpp (Fragment: select)
                        // 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);
        
Specifiying Parameters
Specifiying Parameters (cont.)

An Example

directx9examples/textures/cube/cube.cpp (Fragment: setup)
                // Set texture-mapping properties
        d3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
    d3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);