DirectX/10.0/Direct3D/Texturing

From Wikibooks, open books for an open world
Jump to: navigation, search

This tutorial will explain how to use texturing in DirectX 11. Texturing allows us to add photorealism to our scenes by applying photographs and other images onto polygon faces. For example in this tutorial we will take the following image:

And then apply it to the polygon from the previous tutorial to produce the following:

The format of the textures we will be using are .dds files. This is the Direct Draw Surface format that DirectX uses. The tool used to produce .dds files comes with the DirectX SDK. It is under DirectX Utilities and is called DirectX Texture Tool. You can create a new texture of any size and format and then cut and paste your image or other format texture onto it and save it as a .dds file. It is very simple to use.

And before we get into the code we should discuss how texture mapping works. To map pixels from the .dds image onto the polygon we use what is called the Texel Coordinate System. This system converts the integer value of the pixel into a floating point value between 0.0f and 1.0f. For example if a texture width is 256 pixels wide then the first pixel will map to 0.0f, the 256th pixel will map to 1.0f, and a middle pixel of 128 would map to 0.5f.

In the texel coordinate system the width value is named "U" and the height value is named "V". The width goes from 0.0 on the left to 1.0 on the right. The height goes from 0.0 on the top to 1.0 on the bottom. For example top left would be denoted as U 0.0, V 0.0 and bottom right would be denoted as U 1.0, V 1.0. I have made a diagram below to illustrate this system:

Now that we have a basic understanding of how to map textures onto polygons we can look at the updated frame work for this tutorial:

The changes to the frame work since the previous tutorial is the new TextureClass which is inside ModelClass and the new TextureShaderClass which replaces the ColorShaderClass. We'll start the code section by looking at the new HLSL texture shaders first.

Texture.vs[edit]

The texture vertex shader is similar to the previous color shader except that there have been some changes to accommodate texturing.

////////////////////////////////////////////////////////////////////////////////
// Filename: texture.vs
////////////////////////////////////////////////////////////////////////////////


/////////////
// GLOBALS //
/////////////
cbuffer MatrixBuffer
{
    matrix worldMatrix;
    matrix viewMatrix;
    matrix projectionMatrix;
};

We are no longer using color in our vertex type and have instead moved to using texture coordinates. Since texture coordinates take a U and V float coordinate we use float2 as its type. The semantic for texture coordinates is TEXCOORD0 for vertex shaders and pixel shaders. You can change the zero to any number to indicate which set of coordinates you are working with as multiple texture coordinates are allowed.

//////////////
// TYPEDEFS //
//////////////
struct VertexInputType
{
    float4 position : POSITION;
    float2 tex : TEXCOORD0;
};

struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
};


////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////
PixelInputType TextureVertexShader(VertexInputType input)
{
    PixelInputType output;
    

    // Change the position vector to be 4 units for proper matrix calculations.
    input.position.w = 1.0f;

    // Calculate the position of the vertex against the world, view, and projection matrices.
    output.position = mul(input.position, worldMatrix);
    output.position = mul(output.position, viewMatrix);
    output.position = mul(output.position, projectionMatrix);

The only difference in the texture vertex shader in comparison to the color vertex shader from the previous tutorial is that instead of taking a copy of the color from the input vertex we take a copy of the texture coordinates and pass them to the pixel shader.

    // Store the texture coordinates for the pixel shader.
    output.tex = input.tex;
    
    return output;
}

Texture.ps[edit]

////////////////////////////////////////////////////////////////////////////////
// Filename: texture.ps
////////////////////////////////////////////////////////////////////////////////

The texture pixel shader has two global variables. The first is Texture2D shaderTexture which is the texture resource. This will be our texture resource that will be used for rendering the texture on the model. The second new variable is the SamplerState SampleType. The sampler state allows us to modify how the pixels are written to the polygon face when shaded. For example if the polygon is really far away and only makes up 8 pixels on the screen then we use the sample state to figure out which pixels or what combination of pixels will actually be drawn from the original texture. The original texture may be 256 pixels by 256 pixels so deciding which pixels get drawn is really important to ensure that the texture still looks decent on the really small polygon face. We will setup the sampler state in the TextureShaderClass also and then attach it to the resource pointer so this pixel shader can use it to determine which sample of pixels to draw.

/////////////
// GLOBALS //
/////////////
Texture2D shaderTexture;
SamplerState SampleType;

The PixelInputType for the texture pixel shader is also modified using texture coordinates instead of the color values.

//////////////
// TYPEDEFS //
//////////////
struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
};

The pixel shader has been modified so that it now uses the HLSL sample function. The sample function uses the sampler state we defined above and the texture coordinates for this pixel. It uses these two variables to determine and return the pixel value for this UV location on the polygon face.

////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 TexturePixelShader(PixelInputType input) : SV_TARGET
{
    float4 textureColor;


    // Sample the pixel color from the texture using the sampler at this texture coordinate location.
    textureColor = shaderTexture.Sample(SampleType, input.tex);

    return textureColor;
}

Textureclass.h[edit]

The TextureClass encapsulates the loading, unloading, and accessing of a single texture resource. For each texture needed an object of this class must be instantiated.

////////////////////////////////////////////////////////////////////////////////
// Filename: textureclass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _TEXTURECLASS_H_
#define _TEXTURECLASS_H_


//////////////
// INCLUDES //
//////////////
#include <d3d11.h>
#include <d3dx11tex.h>


////////////////////////////////////////////////////////////////////////////////
// Class name: TextureClass
////////////////////////////////////////////////////////////////////////////////
class TextureClass
{
public:
        TextureClass();
        TextureClass(const TextureClass&);
        ~TextureClass();

The first two functions will load a texture from a given file name and unload that texture when it is no longer needed.

  bool Initialize(ID3D11Device*, WCHAR*);
        void Shutdown();

The GetTexture function returns a pointer to the texture resource so that it can be used for rendering by shaders.

  ID3D11ShaderResourceView* GetTexture();

private:

This is the private texture resource.

  ID3D11ShaderResourceView* m_texture;
};

#endif

Textureclass.cpp[edit]

////////////////////////////////////////////////////////////////////////////////
// Filename: textureclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "textureclass.h"

The class constructor will initialize the texture shader resource pointer to null.

TextureClass::TextureClass()
{
        m_texture = 0;
}


TextureClass::TextureClass(const TextureClass& other)
{
}


TextureClass::~TextureClass()
{
}

Initialize takes in the Direct3D device and file name of the texture and then loads the texture file into the shader resource variable called m_texture. The texture can now be used to render with.

bool TextureClass::Initialize(ID3D11Device* device, WCHAR* filename)
{
        HRESULT result;


        // Load the texture in.
        result = D3DX11CreateShaderResourceViewFromFile(device, filename, NULL, NULL, &m_texture, NULL);
        if(FAILED(result))
        {
                return false;
        }

        return true;
}

The Shutdown function releases the texture resource if it has been loaded and then sets the pointer to null.

void TextureClass::Shutdown()
{
        // Release the texture resource.
        if(m_texture)
        {
                m_texture->Release();
                m_texture = 0;
        }

        return;
}

GetTexture is the function that is called by other objects that need access to the texture shader resource so that they can use the texture for rendering.

ID3D11ShaderResourceView* TextureClass::GetTexture()
{
        return m_texture;
}

Modelclass.h[edit]

The ModelClass has had changes since the previous tutorial so that it can now accommodate texturing.

////////////////////////////////////////////////////////////////////////////////
// Filename: modelclass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _MODELCLASS_H_
#define _MODELCLASS_H_


//////////////
// INCLUDES //
//////////////
#include <d3d11.h>
#include <d3dx10math.h>

The TextureClass header is now included in the ModelClass header.

///////////////////////
// MY CLASS INCLUDES //
///////////////////////
#include "textureclass.h"


////////////////////////////////////////////////////////////////////////////////
// Class name: ModelClass
////////////////////////////////////////////////////////////////////////////////
class ModelClass
{
private:

The VertexType has replaced the color component with a texture coordinate component. The texture coordinate is now replacing the green color that was used in the previous tutorial.

     struct VertexType
        {
                D3DXVECTOR3 position;
                D3DXVECTOR2 texture;
        };

public:
        ModelClass();
        ModelClass(const ModelClass&);
        ~ModelClass();

        bool Initialize(ID3D11Device*, WCHAR*);
        void Shutdown();
        void Render(ID3D11DeviceContext*);

        int GetIndexCount();

The ModelClass also has a GetTexture function so it can pass its own texture resource to shaders that will draw this model.

     ID3D11ShaderResourceView* GetTexture();

private:
        bool InitializeBuffers(ID3D11Device*);
        void ShutdownBuffers();
        void RenderBuffers(ID3D11DeviceContext*);

ModelClass has both a private LoadTexture and ReleaseTexture for loading and releasing the texture that will be used to render this model.

     bool LoadTexture(ID3D11Device*, WCHAR*);
        void ReleaseTexture();

private:
        ID3D11Buffer *m_vertexBuffer, *m_indexBuffer;
        int m_vertexCount, m_indexCount;

The m_Texture variable is used for loading, releasing, and accessing the texture resource for this model.

     TextureClass* m_Texture;
};

#endif

Modelclass.cpp[edit]

////////////////////////////////////////////////////////////////////////////////
// Filename: modelclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "modelclass.h"


ModelClass::ModelClass()
{
        m_vertexBuffer = 0;
        m_indexBuffer = 0;

The class constructor now initializes the new texture object to null.

     m_Texture = 0;
}


ModelClass::ModelClass(const ModelClass& other)
{
}


ModelClass::~ModelClass()
{
}

Initialize now takes as input the file name of the .dds texture that the model will be using.

bool ModelClass::Initialize(ID3D11Device* device, WCHAR* textureFilename)
{
        bool result;


        // Initialize the vertex and index buffer that hold the geometry for the triangle.
        result = InitializeBuffers(device);
        if(!result)
        {
                return false;
        }

The Initialize function now calls a new private function that will load the texture.

     // Load the texture for this model.
        result = LoadTexture(device, textureFilename);
        if(!result)
        {
                return false;
        }

        return true;
}


void ModelClass::Shutdown()
{

The Shutdown function now calls a new private function to release the texture object that was loaded during initialization.

     // Release the model texture.
        ReleaseTexture();

        // Release the vertex and index buffers.
        ShutdownBuffers();

        return;
}


void ModelClass::Render(ID3D11DeviceContext* deviceContext)
{
        // Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.
        RenderBuffers(deviceContext);

        return;
}


int ModelClass::GetIndexCount()
{
        return m_indexCount;
}

GetTexture returns the model texture resource. The texture shader will need access to this texture to render the model.

ID3D11ShaderResourceView* ModelClass::GetTexture()
{
        return m_Texture->GetTexture();
}


bool ModelClass::InitializeBuffers(ID3D11Device* device)
{
        VertexType* vertices;
        unsigned long* indices;
        D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
        D3D11_SUBRESOURCE_DATA vertexData, indexData;
        HRESULT result;

        
        // Set the number of vertices in the vertex array.
        m_vertexCount = 3;

        // Set the number of indices in the index array.
        m_indexCount = 3;

        // Create the vertex array.
        vertices = new VertexType[m_vertexCount];
        if(!vertices)
        {
                return false;
        }

        // Create the index array.
        indices = new unsigned long[m_indexCount];
        if(!indices)
        {
                return false;
        }

The vertex array now has a texture component instead of a color component. The texture vector is always U first and V second. For example the first texture coordinate is bottom left of the triangle which corresponds to U 0.0, V 1.0. Use the diagram at the top of this page to figure out what your coordinates need to be. Note that you can change the coordinates to map any part of the texture to any part of the polygon face. In this tutorial I'm just doing a direct mapping for simplicity reasons.

  // Load the vertex array with data.
        vertices[0].position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);  // Bottom left.
        vertices[0].texture = D3DXVECTOR2(0.0f, 1.0f);

        vertices[1].position = D3DXVECTOR3(0.0f, 1.0f, 0.0f);  // Top middle.
        vertices[1].texture = D3DXVECTOR2(0.5f, 0.0f);

        vertices[2].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);  // Bottom right.
        vertices[2].texture = D3DXVECTOR2(1.0f, 1.0f);

        // Load the index array with data.
        indices[0] = 0;  // Bottom left.
        indices[1] = 1;  // Top middle.
        indices[2] = 2;  // Bottom right.

        // Set up the description of the vertex buffer.
        vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
        vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount;
        vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
        vertexBufferDesc.CPUAccessFlags = 0;
        vertexBufferDesc.MiscFlags = 0;
        vertexBufferDesc.StructureByteStride = 0;

        // Give the subresource structure a pointer to the vertex data.
        vertexData.pSysMem = vertices;
        vertexData.SysMemPitch = 0;
        vertexData.SysMemSlicePitch = 0;

        // Now create the vertex buffer.
        result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
        if(FAILED(result))
        {
                return false;
        }

        // Set up the description of the static index buffer.
        indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
        indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;
        indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
        indexBufferDesc.CPUAccessFlags = 0;
        indexBufferDesc.MiscFlags = 0;
        indexBufferDesc.StructureByteStride = 0;

        // Give the subresource structure a pointer to the index data.
        indexData.pSysMem = indices;

        // Create the index buffer.
        result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
        if(FAILED(result))
        {
                return false;
        }

        // Release the arrays now that the vertex and index buffers have been created and loaded.
        delete [] vertices;
        vertices = 0;

        delete [] indices;
        indices = 0;

        return true;
}


void ModelClass::ShutdownBuffers()
{
        // Release the index buffer.
        if(m_indexBuffer)
        {
                m_indexBuffer->Release();
                m_indexBuffer = 0;
        }

        // Release the vertex buffer.
        if(m_vertexBuffer)
        {
                m_vertexBuffer->Release();
                m_vertexBuffer = 0;
        }

        return;
}


void ModelClass::RenderBuffers(ID3D11DeviceContext* deviceContext)
{
        unsigned int stride;
        unsigned int offset;


        // Set vertex buffer stride and offset.
        stride = sizeof(VertexType); 
        offset = 0;
    
        // Set the vertex buffer to active in the input assembler so it can be rendered.
        deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);

        // Set the index buffer to active in the input assembler so it can be rendered.
        deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);

        // Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.
        deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

        return;
}

LoadTexture is a new private function that will create the texture object and then initialize it with the input file name provided. This function is called during initialization.

bool ModelClass::LoadTexture(ID3D11Device* device, WCHAR* filename)
{
        bool result;


        // Create the texture object.
        m_Texture = new TextureClass;
        if(!m_Texture)
        {
                return false;
        }

        // Initialize the texture object.
        result = m_Texture->Initialize(device, filename);
        if(!result)
        {
                return false;
        }

        return true;
}

The ReleaseTexture function will release the texture object that was created and loaded during the LoadTexture function.

void ModelClass::ReleaseTexture()
{
        // Release the texture object.
        if(m_Texture)
        {
                m_Texture->Shutdown();
                delete m_Texture;
                m_Texture = 0;
        }

        return;
}

Textureshaderclass.h[edit]

The TextureShaderClass is just an updated version of the ColorShaderClass from the previous tutorial. This class will be used to draw the 3D models using vertex and pixel shaders.

////////////////////////////////////////////////////////////////////////////////
// Filename: textureshaderclass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _TEXTURESHADERCLASS_H_
#define _TEXTURESHADERCLASS_H_


//////////////
// INCLUDES //
//////////////
#include <d3d11.h>
#include <d3dx10math.h>
#include <d3dx11async.h>
#include <fstream>
using namespace std;


////////////////////////////////////////////////////////////////////////////////
// Class name: TextureShaderClass
////////////////////////////////////////////////////////////////////////////////
class TextureShaderClass
{
private:
        struct MatrixBufferType
        {
                D3DXMATRIX world;
                D3DXMATRIX view;
                D3DXMATRIX projection;
        };

public:
        TextureShaderClass();
        TextureShaderClass(const TextureShaderClass&);
        ~TextureShaderClass();

        bool Initialize(ID3D11Device*, HWND);
        void Shutdown();
        bool Render(ID3D11DeviceContext*, int, D3DXMATRIX, D3DXMATRIX, D3DXMATRIX, ID3D11ShaderResourceView*);

private:
        bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
        void ShutdownShader();
        void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);

        bool SetShaderParameters(ID3D11DeviceContext*, D3DXMATRIX, D3DXMATRIX, D3DXMATRIX, ID3D11ShaderResourceView*);
        void RenderShader(ID3D11DeviceContext*, int);

private:
        ID3D11VertexShader* m_vertexShader;
        ID3D11PixelShader* m_pixelShader;
        ID3D11InputLayout* m_layout;
        ID3D11Buffer* m_matrixBuffer;

There is a new private variable for the sampler state pointer. This pointer will be used to interface with the texture shader.

  ID3D11SamplerState* m_sampleState;
};

#endif

Textureshaderclass.cpp[edit]

////////////////////////////////////////////////////////////////////////////////
// Filename: textureshaderclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "textureshaderclass.h"


TextureShaderClass::TextureShaderClass()
{
        m_vertexShader = 0;
        m_pixelShader = 0;
        m_layout = 0;
        m_matrixBuffer = 0;

The new sampler variable is set to null in the class constructor.

  m_sampleState = 0;
}


TextureShaderClass::TextureShaderClass(const TextureShaderClass& other)
{
}


TextureShaderClass::~TextureShaderClass()
{
}


bool TextureShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
{
        bool result;

The new texture.vs and texture.ps HLSL files are loaded for this shader.

  // Initialize the vertex and pixel shaders.
        result = InitializeShader(device, hwnd, L"../Engine/texture.vs", L"../Engine/texture.ps");
        if(!result)
        {
                return false;
        }


        return true;
}

The Shutdown function calls the release of the shader variables.

void TextureShaderClass::Shutdown()
{
        // Shutdown the vertex and pixel shaders as well as the related objects.
        ShutdownShader();

        return;
}

The Render function now takes a new parameter called texture which is the pointer to the texture resource. This is then sent into the SetShaderParameters function so that the texture can be set in the shader and then used for rendering.

bool TextureShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, D3DXMATRIX worldMatrix, D3DXMATRIX viewMatrix, 
                                D3DXMATRIX projectionMatrix, ID3D11ShaderResourceView* texture)
{
        bool result;


        // Set the shader parameters that it will use for rendering.
        result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture);
        if(!result)
        {
                return false;
        }

        // Now render the prepared buffers with the shader.
        RenderShader(deviceContext, indexCount);

        return true;
}

InitializeShader sets up the texture shader.

bool TextureShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
{
        HRESULT result;
        ID3D10Blob* errorMessage;
        ID3D10Blob* vertexShaderBuffer;
        ID3D10Blob* pixelShaderBuffer;
        D3D11_INPUT_ELEMENT_DESC polygonLayout[2];
        unsigned int numElements;
        D3D11_BUFFER_DESC matrixBufferDesc;

We have a new variable to hold the description of the texture sampler that will be setup in this function.

  D3D11_SAMPLER_DESC samplerDesc;


        // Initialize the pointers this function will use to null.
        errorMessage = 0;
        vertexShaderBuffer = 0;
        pixelShaderBuffer = 0;

Load in the new texture vertex and pixel shaders.

  // Compile the vertex shader code.
        result = D3DX11CompileFromFile(vsFilename, NULL, NULL, "TextureVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, 
                                       &vertexShaderBuffer, &errorMessage, NULL);
        if(FAILED(result))
        {
                // If the shader failed to compile it should have writen something to the error message.
                if(errorMessage)
                {
                        OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
                }
                // If there was nothing in the error message then it simply could not find the shader file itself.
                else
                {
                        MessageBox(hwnd, vsFilename, L"Missing Shader File", MB_OK);
                }

                return false;
        }

        // Compile the pixel shader code.
        result = D3DX11CompileFromFile(psFilename, NULL, NULL, "TexturePixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, 
                                       &pixelShaderBuffer, &errorMessage, NULL);
        if(FAILED(result))
        {
                // If the shader failed to compile it should have writen something to the error message.
                if(errorMessage)
                {
                        OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
                }
                // If there was  nothing in the error message then it simply could not find the file itself.
                else
                {
                        MessageBox(hwnd, psFilename, L"Missing Shader File", MB_OK);
                }

                return false;
        }

        // Create the vertex shader from the buffer.
        result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
        if(FAILED(result))
        {
                return false;
        }

        // Create the pixel shader from the buffer.
        result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
        if(FAILED(result))
        {
                return false;
        }

The input layout has changed as we now have a texture element instead of color. The first position element stays unchanged but the SemanticName and Format of the second element have been changed to TEXCOORD and DXGI_FORMAT_R32G32_FLOAT. These two changes will now align this layout with our new VertexType in both the ModelClass definition and the typedefs in the shader files.

  // Create the vertex input layout description.
        // This setup needs to match the VertexType stucture in the ModelClass and in the shader.
        polygonLayout[0].SemanticName = "POSITION";
        polygonLayout[0].SemanticIndex = 0;
        polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
        polygonLayout[0].InputSlot = 0;
        polygonLayout[0].AlignedByteOffset = 0;
        polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
        polygonLayout[0].InstanceDataStepRate = 0;

        polygonLayout[1].SemanticName = "TEXCOORD";
        polygonLayout[1].SemanticIndex = 0;
        polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
        polygonLayout[1].InputSlot = 0;
        polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
        polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
        polygonLayout[1].InstanceDataStepRate = 0;

        // Get a count of the elements in the layout.
        numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);

        // Create the vertex input layout.
        result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), 
                                           &m_layout);
        if(FAILED(result))
        {
                return false;
        }

        // Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
        vertexShaderBuffer->Release();
        vertexShaderBuffer = 0;

        pixelShaderBuffer->Release();
        pixelShaderBuffer = 0;

        // Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
        matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
        matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
        matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
        matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
        matrixBufferDesc.MiscFlags = 0;
        matrixBufferDesc.StructureByteStride = 0;

        // Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class.
        result = device->CreateBuffer(&matrixBufferDesc, NULL, &m_matrixBuffer);
        if(FAILED(result))
        {
                return false;
        }

The sampler state description is setup here and then can be passed to the pixel shader after. The most important element of the texture sampler description is Filter. Filter will determine how it decides which pixels will be used or combined to create the final look of the texture on the polygon face. In the example here I use D3D11_FILTER_MIN_MAG_MIP_LINEAR which is more expensive in terms of processing but gives the best visual result. It tells the sampler to use linear interpolation for minification, magnification, and mip-level sampling.

AddressU and AddressV are set to Wrap which ensures that the coordinates stay between 0.0f and 1.0f. Anything outside of that wraps around and is placed between 0.0f and 1.0f. All other settings for the sampler state description are defaults.

  // Create a texture sampler state description.
        samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
        samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
        samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
        samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
        samplerDesc.MipLODBias = 0.0f;
        samplerDesc.MaxAnisotropy = 1;
        samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
        samplerDesc.BorderColor[0] = 0;
        samplerDesc.BorderColor[1] = 0;
        samplerDesc.BorderColor[2] = 0;
        samplerDesc.BorderColor[3] = 0;
        samplerDesc.MinLOD = 0;
        samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;

        // Create the texture sampler state.
        result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
        if(FAILED(result))
        {
                return false;
        }

        return true;
}

The ShutdownShader function releases all the variables used in the TextureShaderClass.

void TextureShaderClass::ShutdownShader()
{

The ShutdownShader function now releases the new sampler state that was created during initialization.

  // Release the sampler state.
        if(m_sampleState)
        {
                m_sampleState->Release();
                m_sampleState = 0;
        }

        // Release the matrix constant buffer.
        if(m_matrixBuffer)
        {
                m_matrixBuffer->Release();
                m_matrixBuffer = 0;
        }

        // Release the layout.
        if(m_layout)
        {
                m_layout->Release();
                m_layout = 0;
        }

        // Release the pixel shader.
        if(m_pixelShader)
        {
                m_pixelShader->Release();
                m_pixelShader = 0;
        }

        // Release the vertex shader.
        if(m_vertexShader)
        {
                m_vertexShader->Release();
                m_vertexShader = 0;
        }

        return;
}

OutputShaderErrorMessage writes out errors to a text file if the HLSL shader could not be loaded.

void TextureShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
{
        char* compileErrors;
        unsigned long bufferSize, i;
        ofstream fout;


        // Get a pointer to the error message text buffer.
        compileErrors = (char*)(errorMessage->GetBufferPointer());

        // Get the length of the message.
        bufferSize = errorMessage->GetBufferSize();

        // Open a file to write the error message to.
        fout.open("shader-error.txt");

        // Write out the error message.
        for(i=0; i<bufferSize; i++)
        {
                fout Release();
        errorMessage = 0;

        // Pop a message up on the screen to notify the user to check the text file for compile errors.
        MessageBox(hwnd, L"Error compiling shader.  Check shader-error.txt for message.", shaderFilename, MB_OK);

        return;
}

SetShaderParameters function now takes in a pointer to a texture resource and then assigns it to the shader using the new texture resource pointer. Note that the texture has to be set before rendering of the buffer occurs.

bool TextureShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, D3DXMATRIX worldMatrix, D3DXMATRIX viewMatrix, 
                                             D3DXMATRIX projectionMatrix, ID3D11ShaderResourceView* texture)
{
        HRESULT result;
        D3D11_MAPPED_SUBRESOURCE mappedResource;
        MatrixBufferType* dataPtr;
        unsigned int bufferNumber;


        // Transpose the matrices to prepare them for the shader.
        D3DXMatrixTranspose(&worldMatrix, &worldMatrix);
        D3DXMatrixTranspose(&viewMatrix, &viewMatrix);
        D3DXMatrixTranspose(&projectionMatrix, &projectionMatrix);

        // Lock the constant buffer so it can be written to.
        result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
        if(FAILED(result))
        {
                return false;
        }

        // Get a pointer to the data in the constant buffer.
        dataPtr = (MatrixBufferType*)mappedResource.pData;

        // Copy the matrices into the constant buffer.
        dataPtr->world = worldMatrix;
        dataPtr->view = viewMatrix;
        dataPtr->projection = projectionMatrix;

        // Unlock the constant buffer.
        deviceContext->Unmap(m_matrixBuffer, 0);

        // Set the position of the constant buffer in the vertex shader.
        bufferNumber = 0;

        // Now set the constant buffer in the vertex shader with the updated values.
        deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);

The SetShaderParameters function has been modified from the previous tutorial to include setting the texture in the pixel shader now.

  // Set shader texture resource in the pixel shader.
        deviceContext->PSSetShaderResources(0, 1, &texture);

        return true;
}

RenderShader calls the shader technique to render the polygons.

void TextureShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
{
        // Set the vertex input layout.
        deviceContext->IASetInputLayout(m_layout);

        // Set the vertex and pixel shaders that will be used to render this triangle.
        deviceContext->VSSetShader(m_vertexShader, NULL, 0);
        deviceContext->PSSetShader(m_pixelShader, NULL, 0);

The RenderShader function has been changed to include setting the sample state in the pixel shader before rendering.

  // Set the sampler state in the pixel shader.
        deviceContext->PSSetSamplers(0, 1, &m_sampleState);

        // Render the triangle.
        deviceContext->DrawIndexed(indexCount, 0, 0);

        return;
}

Graphicsclass.h[edit]

////////////////////////////////////////////////////////////////////////////////
// Filename: graphicsclass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _GRAPHICSCLASS_H_
#define _GRAPHICSCLASS_H_


///////////////////////
// MY CLASS INCLUDES //
///////////////////////
#include "d3dclass.h"
#include "cameraclass.h"
#include "modelclass.h"

The GraphicsClass now includes the new TextureShaderClass header and the ColorShaderClass header has been removed.

#include "textureshaderclass.h"


/////////////
// GLOBALS //
/////////////
const bool FULL_SCREEN = true;
const bool VSYNC_ENABLED = true;
const float SCREEN_DEPTH = 1000.0f;
const float SCREEN_NEAR = 0.1f;


////////////////////////////////////////////////////////////////////////////////
// Class name: GraphicsClass
////////////////////////////////////////////////////////////////////////////////
class GraphicsClass
{
public:
        GraphicsClass();
        GraphicsClass(const GraphicsClass&);
        ~GraphicsClass();

        bool Initialize(int, int, HWND);
        void Shutdown();
        bool Frame();

private:
        bool Render();

private:
        D3DClass* m_D3D;
        CameraClass* m_Camera;
        ModelClass* m_Model;

A new TextureShaderClass private object has been added.

     TextureShaderClass* m_TextureShader;
};

#endif

Graphicsclass.cpp[edit]

////////////////////////////////////////////////////////////////////////////////
// Filename: graphicsclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "graphicsclass.h"

The m_TextureShader variable is set to null in the constructor.

GraphicsClass::GraphicsClass()
{
        m_D3D = 0;
        m_Camera = 0;
        m_Model = 0;
        m_TextureShader = 0;
}


GraphicsClass::GraphicsClass(const GraphicsClass& other)
{
}


GraphicsClass::~GraphicsClass()
{
}


bool GraphicsClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
{
        bool result;

                
        // Create the Direct3D object.
        m_D3D = new D3DClass;
        if(!m_D3D)
        {
                return false;
        }

        // Initialize the Direct3D object.
        result = m_D3D->Initialize(screenWidth, screenHeight, VSYNC_ENABLED, hwnd, FULL_SCREEN, SCREEN_DEPTH, SCREEN_NEAR);
        if(!result)
        {
                MessageBox(hwnd, L"Could not initialize Direct3D.", L"Error", MB_OK);
                return false;
        }

        // Create the camera object.
        m_Camera = new CameraClass;
        if(!m_Camera)
        {
                return false;
        }

        // Create the model object.
        m_Model = new ModelClass;
        if(!m_Model)
        {
                return false;
        }

The ModelClass::Initialize function now takes in the name of the texture that will be used for rendering the model.

  // Initialize the model object.
        result = m_Model->Initialize(m_D3D->GetDevice(), L"../Engine/data/seafloor.dds");
        if(!result)
        {
                MessageBox(hwnd, L"Could not initialize the model object.", L"Error", MB_OK);
                return false;
        }

The new TextureShaderClass object is created and initialized.

     // Create the texture shader object.
        m_TextureShader = new TextureShaderClass;
        if(!m_TextureShader)
        {
                return false;
        }

        // Initialize the texture shader object.
        result = m_TextureShader->Initialize(m_D3D->GetDevice(), hwnd);
        if(!result)
        {
                MessageBox(hwnd, L"Could not initialize the texture shader object.", L"Error", MB_OK);
                return false;
        }

        return true;
}


void GraphicsClass::Shutdown()
{

The TextureShaderClass object is also released in the Shutdown function.

     // Release the texture shader object.
        if(m_TextureShader)
        {
                m_TextureShader->Shutdown();
                delete m_TextureShader;
                m_TextureShader = 0;
        }

        // Release the model object.
        if(m_Model)
        {
                m_Model->Shutdown();
                delete m_Model;
                m_Model = 0;
        }

        // Release the camera object.
        if(m_Camera)
        {
                delete m_Camera;
                m_Camera = 0;
        }

        // Release the Direct3D object.
        if(m_D3D)
        {
                m_D3D->Shutdown();
                delete m_D3D;
                m_D3D = 0;
        }

        return;
}


bool GraphicsClass::Frame()
{
        bool result;


        // Render the graphics scene.
        result = Render();
        if(!result)
        {
                return false;
        }

        return true;
}


bool GraphicsClass::Render()
{
        D3DXMATRIX viewMatrix, projectionMatrix, worldMatrix;
        bool result;


        // Clear the buffers to begin the scene.
        m_D3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);

        // Generate the view matrix based on the camera's position.
        m_Camera->Render();

        // Get the view, projection, and world matrices from the camera and d3d objects.
        m_Camera->GetViewMatrix(viewMatrix);
        m_D3D->GetProjectionMatrix(projectionMatrix);
        m_D3D->GetWorldMatrix(worldMatrix);

        // Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
        m_Model->Render(m_D3D->GetDevice());

The texture shader is called now instead of the color shader to render the model. Notice it also takes the texture resource pointer from the model so the texture shader has access to the texture from the model object.

     // Render the model using the texture shader.
        result = m_TextureShader->Render(m_D3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, 
                                         m_Model->GetTexture());
        if(!result)
        {
                return false;
        }

        // Present the rendered scene to the screen.
        m_D3D->EndScene();

        return true;
}

Summary[edit]

You should now understand the basics of loading a texture, mapping it to a polygon face, and then rendering it with a shader.

To Do Exercises[edit]

1. Re-compile the code and ensure that a texture mapped triangle does appear on your screen. Press escape to quit once done.

2. Create your own dds texture and place it in the same directory with seafloor.dds. Inside the GraphicsClass::Initialize function change the model initialization to have your texture name and then re-compile and run the program.

3. Change the code to create two triangles that form a square. Map the entire texture to this square so that the entire texture shows up correctly on the screen.

4. Move the camera to different distances to see the effect of the MIN_MAG_MIP_LINEAR filter.

5. Try some of the other filters and move the camera to different distances to see the different results.