Wednesday, July 4, 2007

Why multiply normals by the inverse transpose?

This question has bugged me for a long time; I can do the math and figure it out but I've never seen the actual methodology behind it:

The Question:
As you transform your models into world/view space using some matrix M you inevitably (either OGL does it for you or you do it yourself) have to transform the normals of that model by the inverse transpose of the matrix M. But why??

The Explanation:
Consider that when we transform the vertexes of a model we are also transforming the vectors tangent to each part of the surface of that model by the same transform. Thus, a vector v, tangent to the surface of our model is transformed by some matrix M. Now, by definition any normal n to a particular part of the surface with tangent vector v must satisfy the following formula
n dot v = 0 (1)

But after transforming v by M we are left with the formula
n dot Mv (2)

This no longer necessarily equals zero and we must figure out a method of properly transforming the normal to meet the requirements of its definition.
Consider that given a matrix M, if we multiply M by its inverse (M-1) we will get the identity matrix (I). Now let's reconsider (2):
nM-1Mv = 0 (3)

In cases where we are dealing with a right handed system we will multiply vectors as if they were column vectors (i.e., matrices must be on the left side of the vector), to do this we simply take (nM-1)T which is equal to (M-1)Tn. Substituting this back into (3) we arrive at the final equation:
(M-1)TnMv = 0 (4)

Thus, through mathematical trickery it becomes evident why one must multiply by the inverse transpose.