Binormal and Tangent
呃,很久以前拷下来的代码……
//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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//we need to solve the equation
// P = s1*T + t1*B
// Q = s2*T + t2*B
// for T and B
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//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
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//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
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//solve this for the unormalized T and B to get from tangent to object space
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
float tmp = 0.0f;
if(fabsf(s1*t2 - s2*t1) <= 0.0001f)
{
tmp = 1.0f;
}
else
{
tmp = 1.0f/(s1*t2 - s2*t1 );
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
tangent.x = (t2*P.x - t1*Q.x);
tangent.y = (t2*P.y - t1*Q.y);
tangent.z = (t2*P.z - t1*Q.z);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
tangent = tmp * tangent;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
binormal.x = (s1*Q.x - s2*P.x);
binormal.y = (s1*Q.y - s2*P.y);
binormal.z = (s1*Q.z - s2*P.z);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
binormal = tmp * binormal;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)