Researching per vertex TBN matrix has uncovered three completely different and I would have thought, incompatible, algorithms. Looking through this site I can find no definitive explanation of the subject so I was hoping this could be rectified.
The different algorithms all calculate the edges the same. Namely, of a triangle with the vertices v1, v2, and v3:
Vec3 v2v1=v2.xyz - v1.xyz;
Vec3 v3v1=v3.xyz - v1.xyz ;
And the tex coords :
Vec2 c2c1=c2.xy - c1.xy ;
Vec2 c3c1=c3.xy – c1.xy ;
They also calculate a common scale factor, whose purpose I don’t quite understand and have not found explained :
Float scaleFactor=c2c1.x * c3c1.y – c3c1.x * c2c1.y;
If(scaleFactor==0)
Use identity matrix for TBN
Else
scaleFactor=1.0/scaleFactor;
The disparity comes when calculating the tangent and bitangent vectors. The three versions are:
T=(c3c1.y * v2v1.xyz - c2c1.y * v3v1.xyz) * scaleFactor ;
B=(-c3c1.x * v2v1.xyz – c2c1.x * v3v1.xyz) * scaleFactor ;
T=(-c3c1.y * v2v1.xyz – c2c1.y * v3v1.xyz) * scaleFactor ;
B=(-c3c1.x * v2v1.xyz – c2c1.x * v3v1.xyz) * scaleFactor ;
T=(c3c1.y * v2v1.xyz – c2c1.y * v3v1.xyz) * scaleFactor ;
B=(c2c1.x * v3v1.xyz – c3c1.x * v2v1.xyz) * scaleFactor ;
It would be useful if this forum contained a definitive ruling on which of three versions is correct for openGL?
I have also come across a rather involved method for inverting the TBN matrix so light vector and eye vector can be transformed into tangent space. Could someone throw an eye over it and verify that it is right or even needed…
Float inv=(T.x * B.y * N.z – T.z * B.y * N.x)
+ (B.x * N.y * T.z – B.z * N.y * T.x) + (N.x * T.y * B.z – N.z * T.y * B.x);
T.x=Bcross(N).x * inv;
T.y=-Ncross(T).x * inv;
T.z=T cross(B).x * inv;
Normalize(T);
B.x= -Bcross(N).y * inv;
B.y= Ncross(T).y * inv;
B.z=-Tcross(B).y * inv;
Normalize(B);
N.x=Bcross(N).z * inv;
N.y=-Ncross(T).z * inv;
N.z=Tcross(B).z * inv ;
Normalize(N);
Thanks in advance.