I guess the next natural step is texturing a model. In this episode I am going to cover basic texturing, then go onto what I call glow maps, then bump mapping and then reflective textures. Now ALL the models and the textures in this episode are taken directly from the DirectX 9 SDK. The only thing I have altered is the sphere.x file as it has a reference to the diffuse (colour map) texture and as I like to store my textures else where and I am also not covering the content pipeline here, I have removed the reference. Also, this is my take on how the textures should be used, and I think it works quite well, guess you guys and girls will be the best judge of that though.
So, we have a model that we want to apply a texture to, in this case it's a sphere and the texture is a colour map of the earth. What you need to do is see the colour map as the resulting diffuse colour on the model, so we take the diffuse value on the model and multiply the texture colour by it to get the resulting colour required. As ever we require the World * View * Projection matrix, and the World matrix, I am also going to include ambient light here, but not pass it and just use the default values, but this will show how you can add the earlier lighting to this effect and as we are doing diffuse lighting we need a light direction again. Now comes the new bit, two new elements, a texture and a sampler. The texture is the parameter that will hold the texture we are passing to the shader, in this case the ColorMap. The sampler is what is used in the pixel shader to reference the texture and get the required pixel from it.
VS_IN has a new member added to it, TexCoord, now your model NEEDS to have these in them or you can not (to my knowledge) apply a texture to it correctly. The TexCoord is a float2 (Vector2) value at each vert that ranges from 0 to 1 and represents a position on the texture to be applied to the model. So if a vert had the value 0.5,0.5 for it's TexCoord then this would be a pixel at the very centre of the texture. The two values are also referred to as U and V, like X and Y but for textures, hence UV mapping. Our VS_OUT structure has the usual Position, Light and Normal, but again has a TexCoord added.
In our vertex shader we do all the same things we did before for the diffuse lighting only we add the bit to pass on the TexCoord to the pixel shader.
In our pixel shader we calculate the light direction and the amount of diffuse light as before as well as the ambient light, but we also get the current pixel for this given set of TexCoords from the sampler for this pixel and adjust it's colour by the diffused light we just calculated. This is then added to the ambient light value to give the pixel colour.
That was pretty painless eh... So is this. We add another texture and sampler to the shader so we can pass in the glow map texture.
The structures stay the same and we just add the glow map calculation to the pixel shader. The glow map is effectively multiplying the glow map pixel by the opposite of the calculated diffuse, we then add this to the colour to be passed to the screen making the pixel shader look like this.
This gives us the following effect:
Now to get bump mapping to work we have to do a few things to both the shader and the model (via the core content importer). First of all we need our model to have "Tangents". Tangents are required to generate the correct "tangent space" for the calculation of the effect, Wolfgang explains this much better than I could and I don't wish to plagiarise him here so if you would like a better description I suggest you buy the book and read chapter 7 page 111. To tell the content pipeline that we want our model to use tangents we simply select the model, expand the Content Processor and set the Generate Tangent Frame to True, it is False by default.
Now we have done that when the model compiles (if it has the correct Vertex Channels, if it does not you will get a compile error, many posts ago I posted a workaround to this, but event hen you will still not get tangent data if the channels are not there) it will have tangent data that we can use in the shader.
We add yet another texture map for the bump map
The structures change too....
We have added the Tangent data to the VS_IN structure so we can use the newly generated tangent data and in the VS_OUT structure we have removed the Normal member. This is because we are going to use the world tangent space to transpose the light in the vertex shader and then use the bump (normal) map to obtain the normal.
The vertex shader now looks like this
As you can see, we transform the position as before, then calculate the tangent space matrix, and using that transform the light direction.
Now this is as easy as the colour and the glow map implementations. What we are going to do is implement specular lighting effect but only where the reflection map tells us to. Again we add another texture and sampler to manage the reflection map.
We now need to add the members we require for specular lighting as we did in the last episode to the structures. VS_IN stays the same but VS_OUT needs to be modified for specular lighting.
We then add our specular lighting code to the vertex shader
And now we apply the specular effect to the areas the reflection map tells us to in the pixel shader
I have high lighted the import ant bit in purple and bold. We now have an effect that looks like this.
Also, if you open up my solution and take a look at the images, in the Content Processor of each I have also set the Resize To Power of Two to True, this makes the images more graphics pipeline friendly.
You can download the sample for this episode here.