PDA

View Full Version : Simple tangent space gen

Rav
06-28-2002, 04:45 AM
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)

Relic
06-28-2002, 05:58 AM
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.)

Rav
06-28-2002, 06:12 AM
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

V-man
06-29-2002, 11:40 AM
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).]

jwatte
06-30-2002, 10:49 AM
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.

Rav
07-01-2002, 06:30 PM
What do you mean by regularly mapped?