# DirectX/10.0/Direct3D/Texturing

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

The texture vertex shader is similar to the previous color shader except that there have been some changes to accommodate texturing.
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.
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.

## 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.
The PixelInputType for the texture pixel shader is also modified using texture coordinates instead of the color values.
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.

## Textureclass.h

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.
The first two functions will load a texture from a given file name and unload that texture when it is no longer needed.
The GetTexture function returns a pointer to the texture resource so that it can be used for rendering by shaders.
This is the private texture resource.

## Textureclass.cpp

The class constructor will initialize the texture shader resource pointer to null.
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.
The Shutdown function releases the texture resource if it has been loaded and then sets the pointer to null.
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.

## Modelclass.h

The ModelClass has had changes since the previous tutorial so that it can now accommodate texturing.
The TextureClass header is now included in the ModelClass header.
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.
The ModelClass also has a GetTexture function so it can pass its own texture resource to shaders that will draw this model.
ModelClass has both a private LoadTexture and ReleaseTexture for loading and releasing the texture that will be used to render this model.
The m_Texture variable is used for loading, releasing, and accessing the texture resource for this model.

## Modelclass.cpp

The class constructor now initializes the new texture object to null.
Initialize now takes as input the file name of the .dds texture that the model will be using.
The Initialize function now calls a new private function that will load the texture.
The Shutdown function now calls a new private function to release the texture object that was loaded during initialization.
GetTexture returns the model texture resource. The texture shader will need access to this texture to render the model.
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.
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.
The ReleaseTexture function will release the texture object that was created and loaded during the LoadTexture function.

## Textureshaderclass.h

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.
There is a new private variable for the sampler state pointer. This pointer will be used to interface with the texture shader.

## Textureshaderclass.cpp

The new sampler variable is set to null in the class constructor.
The new texture.vs and texture.ps HLSL files are loaded for this shader.
The Shutdown function calls the release of the shader variables.
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.
InitializeShader sets up the texture shader.
We have a new variable to hold the description of the texture sampler that will be setup in this function.
Load in the new texture vertex and pixel shaders.
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.
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.
The ShutdownShader function releases all the variables used in the TextureShaderClass.
The ShutdownShader function now releases the new sampler state that was created during initialization.
OutputShaderErrorMessage writes out errors to a text file if the HLSL shader could not be loaded.
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.
The SetShaderParameters function has been modified from the previous tutorial to include setting the texture in the pixel shader now.
RenderShader calls the shader technique to render the polygons.
The RenderShader function has been changed to include setting the sample state in the pixel shader before rendering.

## Graphicsclass.h

The GraphicsClass now includes the new TextureShaderClass header and the ColorShaderClass header has been removed.
A new TextureShaderClass private object has been added.

## Graphicsclass.cpp

The m_TextureShader variable is set to null in the constructor.
The ModelClass::Initialize function now takes in the name of the texture that will be used for rendering the model.
The new TextureShaderClass object is created and initialized.
The TextureShaderClass object is also released in the Shutdown function.
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.

## Summary

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

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.