Tangent-Space Normal Mapping
One of my earlier blogs was about Normal mapping as a whole. There, I talked about how normal maps work, and the effects that they add to your game. What I didn't talk about was the difference of object space normal maps to tangent space normal maps. Is there a big difference you ask? Well yes. Object space normal maps are great if you want to get that bump mapping effect on objects within your world. There is one caveat however, if you morph them in anyway (animation), it looks hideous. This is due to the matrix orientations for each vertex being in object space. The way to apply a normal map, and be able to animate something without distorting the effect, is through tangent space mapping.
The left image shows the matrix orientations per vertex for tangent space mapping, and the right image shows it for object space mapping. The grey vector represents the surface normals, and the red, green and blue vectors represent the basis vectors. Imagine the character is walking. If your use tangent space mapping (left image), the surface normals will remain the same due to them being orientated in tangent space (local to the vertex). If you use object space mapping (right image), the normals will be shifting differently from the boot, because they are oriented in object space mapping (local to the object). This will create that distortion effect that I mentioned earlier. To get around all this, you have to bring your object into tangent space. This is done by multiplying the object space matrix, with the TNB matrix which you have to find.
First off, you get your normal from your normal map (tangent space). So that is done for you immediately. Now, you need to find your tangent and bi-tangent (bi-normal). To do this, you need to find the deltaU and deltaV of your vertex. Imagine your origin is at U0,V0. You can find the triangle edges E1 and E2 as a combination of your T (tangent) and B (bi-tangent).
E1 = (U1 - U0)T + (V1 - V0)B
E1 = (U1 - U0)T + (V1 - V0)B
E2 = (U2 - U0)T + (V2 - V0)B
What you are calculating are the changes, or derivatives of U and V. This only needs to be done once, so it can be calculated on the CPU. You can then easily transform this into a matrix to represent your tangent and bi-tangent. From there, you grab the inverse of this matrix (transpose) and then multiply it by your object space matrix. This then results moving your object from object space, into tangent space. From there, your normal mapping will look much better, especially when you are doing animations.