• Advertisement
Sign in to follow this  

Calculating vertex tangent and tangent matrix in left handed

This topic is 4473 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));

Share this post


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

  • Advertisement