Vertex Tangent - Transformation in left hand coordinates?

Started by
0 comments, last by DXnut 18 years, 3 months ago
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 tags makes your code a bit easier to read.</small> <!--EDIT--><span class=editedby><!--/EDIT-->[Edited by - jollyjeffers on January 21, 2006 6:23:41 AM]<!--EDIT--></span><!--/EDIT-->
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);

--------------------------Most of what I know came from Frank D. Luna's DirectX books

This topic is closed to new replies.

Advertisement