# Tangent calculation

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

## Recommended Posts

Im trying to work out a calculation of tangent in my ASE file loader. I have used this tutorial http://www.terathon.com/code/tangent.php and this is the result code of mine: void OBJEKT::set_vector_tangent() { BOD v1; BOD v2; BOD v3; BOD w1; BOD w2; BOD w3; VECTOR tan1[vert_count]; VECTOR tan2[vert_count]; for (int i=0;i<face_count;i++) { v1=vertices[faces.vert_index[0]]; v2=vertices[faces.vert_index[1]]; v3=vertices[faces.vert_index[2]]; w1=t_coords[faces.text_A]; w2=t_coords[faces.text_B]; w3=t_coords[faces.text_C]; float x1 = v2.X - v1.X; float x2 = v3.X - v1.X; float y1 = v2.Y - v1.Y; float y2 = v3.Y - v1.Y; float z1 = v2.Z - v1.Z; float z2 = v3.Z - v1.Z; float s1 = w2.X - w1.X; float s2 = w3.X - w1.X; float t1 = w2.Y - w1.Y; float t2 = w3.Y - w1.Y; float r = 1.0f / (s1 * t2 - s2 * t1); VECTOR sdir((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r,(t2 * z1 - t1 * z2) * r); VECTOR tdir((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r,(s1 * z2 - s2 * z1) * r); tan1[faces.vert_index[0]].plus(sdir); tan1[faces.vert_index[1]].plus(sdir); tan1[faces.vert_index[2]].plus(sdir); tan2[faces.vert_index[0]].plus(tdir); tan2[faces.vert_index[1]].plus(tdir); tan2[faces.vert_index[2]].plus(tdir); } for (int j=0; j<vert_count;j++) { VECTOR n=vertices[j].point_normal; VECTOR t=tan1[j]; //Gram-Schmidt orthogonalize; //tangent[a] = (t - n * Dot(n, t)).Normalize(); VECTOR t_n=t.minus(n); // t - n float dot_t_n=n.dot(t); // Dot(n, t) VECTOR t_n_do_t_n=t_n.KxV_ret_V(dot_t_n); // t - n * Dot(n, t) t_n_do_t_n.unit(); //(t - n * Dot(n, t)).Normalize() // Calculate handedness //tangent[a].w = (Dot(Cross(n, t), tan2[a]) < 0.0F) ? -1.0F : 1.0F; VECTOR cross_n_t=n.VxV(t); float dotnt_tan=cross_n_t.dot(tan2[j]); if (dotnt_tan<0) { vertices[j].tangent.x=t_n_do_t_n.x; vertices[j].tangent.y=t_n_do_t_n.y; vertices[j].tangent.z=t_n_do_t_n.z; } else { vertices[j].tangent.x=-t_n_do_t_n.x; vertices[j].tangent.y=-t_n_do_t_n.y; vertices[j].tangent.z=-t_n_do_t_n.z; } } It seems correct but it doesnt calculate tangent corectly. Vhen i use the tangent value as a color value in shader the result looks like this http://hi-park.mnichovice.sweb.cz/color_tang.jpg it is clear that there is some problem. I'have no idea what coul it be. Has anyone got an idea?

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633313
• Total Posts
3011315
• ### Who's Online (See full list)

There are no registered users currently online

×