Advertisement Jump to content
Sign in to follow this  

Vertex Tangent - Transformation in left hand coordinates?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working in left handed corrdinates and calculating the tangent to do normal mapping transforming lights to tangent space The result is working wrong as it seems that one axis is flipped (the left side of a mesh is being iluminated as should be the right side). So I am guessing that I have a mix with the coordinate system in calculating the tangent for the vertex or in the tangent trasnformation matrix. 1) Calculate tangent in left handed coordinates?
VECTOR3 GetTangentSpaceVector( const VECTOR3& position1, const VECTOR3& position2, const VECTOR3& position3,
    const VECTOR2& uv1, const VECTOR2& uv2, const VECTOR2& uv3)
 /// Calculate face normal
 /// side0 is the vector along one side of the triangle of vertices passed in, 
 /// and side1 is the vector along another side. Taking the cross product of these returns the normal.
 VECTOR3 side0 = position1 - position2;
 VECTOR3 side1 = position3 - position1;
 VECTOR3 normal = side1.Cross(side0);
 /// Calculate the tangent (UV space)
 float deltaV0 = uv1.v - uv2.v;
 float deltaV1 = uv3.v - uv1.v;
 VECTOR3 tangent = deltaV1 * side0 - deltaV0 * side1;

 /// Calculate binormal (UV Space)
 float deltaU0 = uv1.u - uv2.u;
 float deltaU1 = uv3.u - uv1.u;
 VECTOR3 binormal = deltaU1 * side0 - deltaU0 * side1;

 ///Now, take the cross product of the tangents to get a vector which 
 ///should point in the same direction as our normal calculated above. 
 ///If it points in the opposite direction (the dot product between the normals is less than zero), 
 ///then we need to reverse the s and t tangents. 
 ///This is because the triangle has been mirrored when going from tangent space to object space.
 ///reverse tangents if necessary
 VECTOR3 tangentCross = tangent.Cross(binormal);
 if (tangentCross.Dot(normal) < 0.0f)
  tangent = -1.0f * tangent;
  binormal = -1.0f * binormal;

    return tangent;
2) Tangent transformation for left handed?
float3x3 worldToTangent;

worldToTangent[0] = normalize(mul (IN.tangent, matWorld));

worldToTangent[1] = normalize(mul (cross(IN.tangent, IN.normal),matWorld));

worldToTangent[2] = normalize(mul (IN.normal, matWorld));
EDIT: Using [ source ] and [ code ] tags makes your code a bit easier to read. [Edited by - jollyjeffers on January 21, 2006 6:23:41 AM]

Share this post

Link to post
Share on other sites
In a left handed system where you have clockwise winding for vertices, to calculate the normal yourself from a face with vertices A, B, and C:


D3DXVECTOR3 normal;
D3DXVec3Cross(&normal, &V1, &V2);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!