# How can compute tangent vector like D3DXComputeTangentFrame

This topic is 5165 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all. Im writing engine for load Quake3 Map with suport pixel light in DirectX So i cant use D3DXComputeTangentFrame function for compute tangent for my maps's vertices. I ve read article by Jakob Gath [http://www.blacksmith-studios.dk/projects/downloads/tangent_matrix_derivation.php] but i ve some problem. - fomula form that site is show how to compute tangent for 1 triangle (3 Vertex) data. But if some vertex share in another triangle. How to do ? Quake3 arrange vertices as a triangle fan so must have share vertex. - What's happen if determinant be "0" - and this is my fuction for compute tangent, It not work, what wrong T_T
VOID ComputeTangent( MYVERTEX &V1, MYVERTEX &V2, MYVERTEX &V3)

D3DXVECTOR3 v2v1 = V2.pos - V1.pos;
D3DXVECTOR3 v3v1 = V3.pos - V1.pos;

// Calculate c2c1_T and c2c1_B
float c2c1_T = V2.TexCoord.u - V1.TexCoord.u;
float c2c1_B = V2.TexCoord.v - V1.TexCoord.v;

// Calculate c3c1_T and c3c1_B
float c3c1_T = V3.TexCoord.u - V1.TexCoord.u;
float c3c1_B = V3.TexCoord.v - V1.TexCoord.v;

float fDeterminant = c2c1_T * c3c1_B - c3c1_T * c2c1_B;
if (fabs(fDeterminant )< EPSILON) // avoid divsion by zero.
{
Tangent		= D3DXVECTOR3( 1.0f, 0.0f, 0.0f );
BiNormal	= D3DXVECTOR3(0.0f, 1.0f, 0.0f);
}
else
{
Tangent	= D3DXVECTOR3((c3c1_B * v2v1.x - c2c1_B * v3v1.x),
(c3c1_B * v2v1.y - c2c1_B * v3v1.y),
(c3c1_B * v2v1.z - c2c1_B * v3v1.z));

BiNormal = D3DXVECTOR3((-c3c1_T * v2v1.x + c2c1_T * v3v1.x),
(-c3c1_T * v2v1.y + c2c1_T * v3v1.y),
(-c3c1_T * v2v1.z + c2c1_T * v3v1.z));

Tangent	  *= 1.0f/fDeterminant;
BiNormal  *= 1.0f/fDeterminant;
}



• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 13
• 9
• 11
• 15