Ninja

10-29-2002, 01:25 AM

Hi,

I found this code somewhere and I don't understand all thoery behind it. It's calculating the tangent to use when creating a tangent space for one vertices in a model. It works good for me but could someone explain why? Why is for example that crossproduct needed?

void tangent_basis(float v0[3], float v1[3], float v2[3], float t0[2], float t1[2], float t2[2], float tangent_out[3])

{

float crossproduct[3];

float edge0[3]={ v1[0]-v0[0], t1[0]-t0[0], t1[1]-t0[1] };

float edge1[3]={ v2[0]-v0[0], t2[0]-t0[0], t2[1]-t0[1] };

crossproduct[0]=edge0[1]*edge1[2]-edge0[2]*edge1[1];

crossproduct[1]=edge0[2]*edge1[0]-edge0[0]*edge1[2];

crossproduct[2]=edge0[0]*edge1[1]-edge0[1]*edge1[0];

tangent_out[0]=-crossproduct[1]*(1/crossproduct[0]);

edge0[0]=v1[1]-v0[1];

edge1[0]=v2[1]-v0[1];

crossproduct[0]=edge0[1]*edge1[2]-edge0[2]*edge1[1];

crossproduct[1]=edge0[2]*edge1[0]-edge0[0]*edge1[2];

crossproduct[2]=edge0[0]*edge1[1]-edge0[1]*edge1[0];

tangent_out[1]=-crossproduct[1]*(1/crossproduct[0]);

edge0[0]=v1[2]-v0[2];

edge1[0]=v2[2]-v0[2];

crossproduct[0]=edge0[1]*edge1[2]-edge0[2]*edge1[1];

crossproduct[1]=edge0[2]*edge1[0]-edge0[0]*edge1[2];

crossproduct[2]=edge0[0]*edge1[1]-edge0[1]*edge1[0];

tangent_out[2]=-crossproduct[1]*(1/crossproduct[0]);

}

//Regards, Ninja

I found this code somewhere and I don't understand all thoery behind it. It's calculating the tangent to use when creating a tangent space for one vertices in a model. It works good for me but could someone explain why? Why is for example that crossproduct needed?

void tangent_basis(float v0[3], float v1[3], float v2[3], float t0[2], float t1[2], float t2[2], float tangent_out[3])

{

float crossproduct[3];

float edge0[3]={ v1[0]-v0[0], t1[0]-t0[0], t1[1]-t0[1] };

float edge1[3]={ v2[0]-v0[0], t2[0]-t0[0], t2[1]-t0[1] };

crossproduct[0]=edge0[1]*edge1[2]-edge0[2]*edge1[1];

crossproduct[1]=edge0[2]*edge1[0]-edge0[0]*edge1[2];

crossproduct[2]=edge0[0]*edge1[1]-edge0[1]*edge1[0];

tangent_out[0]=-crossproduct[1]*(1/crossproduct[0]);

edge0[0]=v1[1]-v0[1];

edge1[0]=v2[1]-v0[1];

crossproduct[0]=edge0[1]*edge1[2]-edge0[2]*edge1[1];

crossproduct[1]=edge0[2]*edge1[0]-edge0[0]*edge1[2];

crossproduct[2]=edge0[0]*edge1[1]-edge0[1]*edge1[0];

tangent_out[1]=-crossproduct[1]*(1/crossproduct[0]);

edge0[0]=v1[2]-v0[2];

edge1[0]=v2[2]-v0[2];

crossproduct[0]=edge0[1]*edge1[2]-edge0[2]*edge1[1];

crossproduct[1]=edge0[2]*edge1[0]-edge0[0]*edge1[2];

crossproduct[2]=edge0[0]*edge1[1]-edge0[1]*edge1[0];

tangent_out[2]=-crossproduct[1]*(1/crossproduct[0]);

}

//Regards, Ninja