# Simple tangent space gen

On a paper on the microsoft developers website, I found a forumula for generating tangentspace that goes like the following (pseudocode-ish):

``````Vec Vector1 = ( vertex3 - vertex2 )
Vec Vector2 = ( vertex1 - vertex2 )

float delta1 = vertex3.v - vertex2.v
float delta2 = vertex1.v - vertex2.v

Vector1.scale( delta2 );
Vector2.scale( delta1 );

tangentSpaceY.set( Vector1 - Vector2 )
tangentSpaceY.normalize();

tangentSpaceX.Cross3( tangentSpaceY, MainTriangle.normal );
tangentSpaceX.normalize3();

tangentSpaceZ.Cross3( tangentSpaceX, tangentSpaceY );
tangentSpaceZ.normalize();
``````

After this code, tangentSpaceX,Y,Z should contain a matrix of “negative” tangentspace. Anyways, I tried it and it works, however I dont understand the following line:

``````tangentSpaceY.set( Vector1 - Vector2 )
``````

how does that generate the tangent space vector for Y(or V)

Never used those functions. Take a pen and follow the instructions on paper, pretty simple. From the code

tangentSpaceY.set( Vector1 - Vector2 )
tangentSpaceY.normalize();

I would say this generates a unit vector from the endpoint of the unit vector along edge v2v3 to the endpoint of unit vector from v2v1.
That means e.g. for a counterclockwise triangle looking from vertex2 into the triangle this is a unit vector to the left.
Cross of that with the normal gives a vector in between the two edge vectors, and crossing those two gives a vector in normal direction.
(The last cross looks like nonsense, take the normal directly instead.)

Yes, but I dont follow how the vectors scaled by the other vector’s v texture coordinate, then subtracted gives you the u tangent space vector

I don’t think that scaling is needed, and the last crossproduct is unnecessary as well. The code can be simplified to :

Vec Vector1 = ( vertex3 - vertex2 )
Vec Vector2 = ( vertex1 - vertex2 )

tangentSpaceY.set( Vector1 - Vector2 )
tangentSpaceY.normalize();

tangentSpaceX.Cross3( tangentSpaceY, MainTriangle.normal );
tangentSpaceX.normalize3();

tangentSpaceZ.set( MainTriangle.normal );
tangentSpaceZ.normalize();

V-man

[This message has been edited by V-man (edited 06-29-2002).]

You want to involve u and v rate of growth as well; otherwise your tangent space will twist and wind over the surface and you’ll get weird normal map interactions, unless your mesh is entirely regularly mapped.

What do you mean by regularly mapped?