Uh, copied it a long time ago. Code ......
// Let p = V1-V0
D3dxvector3 P = V1.pos - V0.pos;
// Let q = v2-V0
D3dxvector3 Q = V2.pos - V0.pos;
Float S1 = V1.s - V0.s;
Float T1 = V1.t - V0.t;
Float S2 = V2.s - V0.s;
Float T2 = V2.t - V0.t;
// We need to solve the equation
// P = S1 * t + T1 * B
// Q = S2 * t + T2 * B
// For T and B
// This is a linear system with six unknowns and six equatinos, for txtytz bxbybz
// [PX, Py, PZ] = [S1, T1] * [Tx, Ty, TZ]
// QX, QY, qz S2, T2 BX, by, BZ
// Multiplying both sides by the inverse of the S, T matrix gives
// [Tx, Ty, TZ] = 1/(s1t2-s2t1) * [T2,-T1] * [PX, Py, PZ]
// BX, by, BZ-S2, S1 QX, QY, qz
// Solve this for the unormalized T and B to get from tangent to object space
Float TMP = 0.0f ;
If (Fabsf (S1 * T2 - S2 * T1) <= 0.0001f )
{
TMP= 1.0f;
}
Else
{
TMP= 1.0f/(S1*T2-S2*T1 );
}
Tangent. x = (T2 * P.x - T1 * Q. X );
Tangent. Y = (T2 * P. Y - T1 * Q. y );
Tangent. Z = (T2 * P. Z - T1 * Q. z );
Tangent = TMP * Tangent;
Binormal. x = (S1 * Q. x - S2 * P. X );
Binormal. Y = (S1 * Q. Y - S2 * P. y );
Binormal. Z = (S1 * Q. Z - S2 * P. z );
Binormal = TMP * Binormal;