- // Gram-Schmidt orthogonalize
- Vector3 calc = t - n * n.Dot(t);
- calc.Normalize();
- tangent[a] = Vector3(calc.x,calc.y,calc.z);
+ Vector3 edge1 = v1 - v0;
+ Vector3 edge2 = v2 - v0;
+
+ const Vector2& w0 = mTextures[mTriangles[a].textureIndex[0]];
+ const Vector2& w1 = mTextures[mTriangles[a].textureIndex[1]];
+ const Vector2& w2 = mTextures[mTriangles[a].textureIndex[2]];
+
+ float deltaU1 = w1.x - w0.x;
+ float deltaV1 = w1.y - w0.y;
+ float deltaU2 = w2.x - w0.x;
+ float deltaV2 = w2.y - w0.y;
+
+ float f = 1.0f / (deltaU1 * deltaV2 - deltaU2 * deltaV1);
+
+ tangentVector.x = f * ( deltaV2 * edge1.x - deltaV1 * edge2.x );
+ tangentVector.y = f * ( deltaV2 * edge1.y - deltaV1 * edge2.y );
+ tangentVector.z = f * ( deltaV2 * edge1.z - deltaV1 * edge2.z );
+
+ mTangents[mTriangles[a].pointIndex[0]] += tangentVector;
+ mTangents[mTriangles[a].pointIndex[1]] += tangentVector;
+ mTangents[mTriangles[a].pointIndex[2]] += tangentVector;