How to compute binormal( bitangent ) vector?

How to compute binormal vector for bump mapping calculations?

What’s the idea behind the code? Note that I don’t like to buy the book “Mathematics for 3D Game Programming & Computer Graphics” to only read one section;-)

Basic idea is that given a mesh that has position, normal and a texcood, you can calculate the tangent/bi-tangent by getting the rate of change of the texture coordinates over the mesh surface.

If you want more details, look up the GDC slides from 2003/2004 era.

The idea is to use the derivative of texture coordinates across triangle vertices to calculate the derivative of the cardinal texture axes in 3D.

In that example sdir and tdir are the tangent & binormal.

The stuff at the end ensures the tangent and normal axes are orthognal and it looks like binormal is the implicit cross product (a common approximation) since tdir isn’t corrected the way sdir is. There’s no ‘right’ way to handle bad content here, this is just tidying things up, normalize is the most important operation in the second part of the code.

The final part is flipping w based on the axis orientation (basically adding support for mirrored coordinates but it needs support in the vertex shader to use this correctly).

This example shows definite signs of being lifted from somewhere without proper cleanup, especially the way tdir is handled.