Jump to content
  • Advertisement
Sign in to follow this  
BenjaPrieto

Vertex Tangent - Transformation in left hand coordinates?

This topic is 4564 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);
 normal.Normalize();
 
 /// Calculate the tangent (UV space)
 float deltaV0 = uv1.v - uv2.v;
 float deltaV1 = uv3.v - uv1.v;
 VECTOR3 tangent = deltaV1 * side0 - deltaV0 * side1;
 tangent.Normalize();

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

 ///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
Advertisement
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 V1 = B - A;
D3DXVECTOR3 V2 = C - B;

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 GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!