Hi,
I found this code somewhere and I don’t understand all thoery behind it. It’s calculating the tangent to use when creating a tangent space for one vertices in a model. It works good for me but could someone explain why? Why is for example that crossproduct needed?

There are per-pixel bumpmapping presentations by nvidia that shows the math behind it. All that code is doing is computing partial derivatives from three plane equations to form a matrix to transform something into surface-local space, or tangent space if you will. The three plane equations look like this:

Ax + Bu + Cv + D = 0
Ay + Bu + Cv + D = 0
Az + Bu + Cv + D = 0

Where the u and v are texture coords of course. Then they compute the following partials:

Once you have the tangent space vectors for each triangle then to get the tangent space for each vertex you would do something similar to computing vertex normals. For each vertex, average the tangent space vectors for each triangle that shares that vertex.

Originally posted by Ninja:
[b]Ok, so you use the same tangent for all the vertices in the triangel?

//Ninja[/b]

There is a one to one ratio between tangents and normals. If you have face normals then it would be one tangent per triangle. One thing you have to be aware of with that algorithm (I think thats the same code that I looked at a while back, I could be wrong) for generating tangents is that they will be based around the direction of your texture coordinates which can cause seams if your texture coordinates use mirroring.