# Calculating tangents gives wrong results

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

## Recommended Posts

I've been working all day on this and I still come up with wrong results.

To calculate the tangent I'm using code from this website http://www.terathon.com/code/tangent.html However the result is far from what I'm getting in 3dsmax.

http://i35.tinypic.com/4ptq9i.jpg
Both situations use a shader with object space normal and tangents as color output. My normals and texture coordinates in 3dsmax and my game match, the tangents don't so the problem has to be somewhere in the tangent calculation. I've tried various solutions and multiple methods but without luck.

This method is called every time a triangle is read, the inputs are the index of the 3 vertices of that face.
void CreateTangents(int vertexIndex1, int vertexIndex2, int vertexIndex3){	Point3 v1,v2,v3;	Point2 w1,w2,w3;	Point3 n1,n2,n3;	m_VertexList-&gt;GetVertexPosition(vertexIndex1,&v1);	m_VertexList-&gt;GetVertexPosition(vertexIndex2,&v2);	m_VertexList-&gt;GetVertexPosition(vertexIndex3,&v3);	m_VertexList-&gt;GetVertexTexCoordinate(vertexIndex1,&w1);	m_VertexList-&gt;GetVertexTexCoordinate(vertexIndex2,&w2);	m_VertexList-&gt;GetVertexTexCoordinate(vertexIndex3,&w3);	m_VertexList-&gt;GetVertexNormal(vertexIndex1,&n1);	m_VertexList-&gt;GetVertexNormal(vertexIndex2,&n2);	m_VertexList-&gt;GetVertexNormal(vertexIndex3,&n3);	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);	Point3 tan1((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r );	Point3 tan2((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r );	// tangent[a] = (t - n * Dot(n, t)).Normalize();	Point3 tangent1;	float n1tan1dot = n1.Dot(tan1); 				// Dot(n,t)	n1.Scale(tangent1,n1tan1dot); 					// n * Dot(n,t)	tan1.Subtract(tangent1,tangent1); 				// t - n * Dot(n,t)	tangent1.Normalize(); 							// tangent[a]		Point3 tangent2;	float n2tan1dot = n2.Dot(tan1);	n2.Scale(tangent2,n2tan1dot);	tan1.Subtract(tangent2,tangent2);	tangent2.Normalize();	Point3 tangent3;	float n3tan1dot = n3.Dot(tan1);	n3.Scale(tangent3,n3tan1dot);	tan1.Subtract(tangent3,tangent3);	tangent3.Normalize();	Point3 existingT1,existingT2,existingT3;	m_VertexList-&gt;GetVertexTangent(vertexIndex1,&existingT1);	m_VertexList-&gt;GetVertexTangent(vertexIndex2,&existingT2);	m_VertexList-&gt;GetVertexTangent(vertexIndex3,&existingT3);		tangent1.Add(tangent1,existingT1);				// new tangent + old tangent	tangent2.Add(tangent2,existingT2);	tangent3.Add(tangent3,existingT3);	m_VertexList-&gt;AddVertexTangent(vertexIndex1,&tangent1);	m_VertexList-&gt;AddVertexTangent(vertexIndex2,&tangent2);	m_VertexList-&gt;AddVertexTangent(vertexIndex3,&tangent3);}

What am I doing wrong? I've been looking all the day but I can't find a problem

[Edited by - Bosduif on August 22, 2010 6:49:34 AM]

##### Share on other sites
Hi, try typing in {SOURCE} (square braces not the curly ones I've shown) rather than {CODE} tags it works alot better and is easier to read. Not sure I can help with your problem yet though but I'll read through it and have a look. Cheers.

##### Share on other sites
Don't be sorry you couldn't have known. I did it for ages before someone showed me.

##### Share on other sites
Hi

Don't know if you've seen these:

http://www.gamedev.net/community/forums/topic.asp?topic_id=560530
http://www.gamedev.net/community/forums/topic.asp?topic_id=539329
http://www.gamedev.net/community/forums/topic.asp?topic_id=535815
http://www.gamedev.net/community/forums/topic.asp?topic_id=516072
http://www.gamedev.net/community/forums/topic.asp?topic_id=500804
http://www.gamasutra.com/view/feature/1515/messing_with_tangent_space.php
http://www.3dkingdoms.com/weekly/weekly.php?a=37

1. 1
2. 2
3. 3
Rutin
25
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633650
• Total Posts
3013121
×