Wednesday, December 12, 2012

Normals and the World Inverse Transpose Matrix

On the last assignment and in the exam review, there was a question about the world inverse transpose matrix.  I perused a bunch of online material on this issue, and thought this discussion had a good explanation:
The purpose of transforming a normal by the world matrix is to rotate it so that it's a direction in world-space.  This is typically done by only using the 3x3 portion of the world matrix, since this includes all of the rotation data and none of translation (you don't want to translate a normal). 

The 3x3 portion of the matrix also can contain scaling data.  When it contains scaling data, the normal is scaled by the scaling factor.  As long as the scale is uniform (which means that xScale == yScale == zScale), the scaling is okay provided your normalize the resultant normal vector before using it.  However if the scale is non-uniform, this won't work.  The solution in the case of non-uniform scaling is to calculatate the inverse transpose outside the shader, pass it in, and then transform normals (and also tangents/bitangents, if necessary) by that matrix.

So in summary:  if you're using no scaling or uniform scaling in your world matrix you can use the world matrix, if you're using non-uniform scaling then you need to use the inverse transpose.
So, in part 4 of the last assignment, changing the scaling of the sphere to be non-uniform caused the normals to be incorrect until you used the inverse transpose world matrix instead of the world matrix.

No comments:

Post a Comment