Calculating tangents gives wrong results

Started by
4 comments, last by venzon 13 years, 8 months ago
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->GetVertexPosition(vertexIndex1,&v1);	m_VertexList->GetVertexPosition(vertexIndex2,&v2);	m_VertexList->GetVertexPosition(vertexIndex3,&v3);	m_VertexList->GetVertexTexCoordinate(vertexIndex1,&w1);	m_VertexList->GetVertexTexCoordinate(vertexIndex2,&w2);	m_VertexList->GetVertexTexCoordinate(vertexIndex3,&w3);	m_VertexList->GetVertexNormal(vertexIndex1,&n1);	m_VertexList->GetVertexNormal(vertexIndex2,&n2);	m_VertexList->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->GetVertexTangent(vertexIndex1,&existingT1);	m_VertexList->GetVertexTangent(vertexIndex2,&existingT2);	m_VertexList->GetVertexTangent(vertexIndex3,&existingT3);		tangent1.Add(tangent1,existingT1);				// new tangent + old tangent	tangent2.Add(tangent2,existingT2);	tangent3.Add(tangent3,existingT3);	m_VertexList->AddVertexTangent(vertexIndex1,&tangent1);	m_VertexList->AddVertexTangent(vertexIndex2,&tangent2);	m_VertexList->AddVertexTangent(vertexIndex3,&tangent3);}


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

Thanks in advance.

[Edited by - Bosduif on August 22, 2010 6:49:34 AM]
Advertisement
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.
Ok, sorry about that.
Don't be sorry you couldn't have known. I did it for ages before someone showed me.
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
Here is the exact method that 3dsmax uses:
http://area.autodesk.com/blogs/chris/how_the_3ds_max_scanline_renderer_computes_tangent_and_binormal_vectors_for_normal_mapping

This topic is closed to new replies.

Advertisement