Computing Tangent in Ogre is very good!

Source: Internet
Author: User
Code

Vector3 Math: calculateTangentSpaceVector (
Const Vector3 & position1, const Vector3 & position2, const Vector3 & position3,
Real u1, Real v1, Real u2, Real v2, Real u3, Real v3)
{
// 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;
// Calculate face normal
Vector3 normal = side1.crossProduct (side0 );
Normal. normalise ();
// Now we use a formula to calculate the tangent.
Real deltaV0 = v1-v2;
Real deltaV1 = v3-v1;
Vector3 tangent = deltaV1 * side0-deltaV0 * side1;
Tangent. normalise ();
// Calculate binormal
Real deltaU0 = u1-u2;
Real deltaU1 = u3-u1;
Vector3 binormal = deltaU1 * side0-deltaU0 * side1;
Binormal. normalise ();
// Now, we take the cross product of the tangents to get a vector which
// Shocould 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. crossProduct (binormal );
If (tangentCross. dotProduct (normal) <0.0f)
{
Tangent =-tangent;
Binormal =-binormal;
}

Return tangent;

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.