Exemple #1
0
        public static Vector3 operator *(Quaternion q, Vector3 v)
        {
            // nVidia SDK implementation
            Vector3 uv, uuv;
            Vector3 qvec = new Vector3(q.X, q.Y, q.Z);

            uv   = qvec.CrossProduct(v);
            uuv  = qvec.CrossProduct(uv);
            uv  *= (2.0f * q.w);
            uuv *= 2.0f;

            return(v + uv + uuv);
        }
Exemple #2
0
        public static Vector3 CalculateTangentSpaceVector(
            Vector3 position1, Vector3 position2, Vector3 position3,
            float u1, float v1, float u2, float v2, float u3, float 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.
            float   deltaV0 = v1 - v2;
            float   deltaV1 = v3 - v1;
            Vector3 tangent = deltaV1 * side0 - deltaV0 * side1;

            tangent.Normalise();
            //Calculate binormal
            float   deltaU0  = u1 - u2;
            float   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
            //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.CrossProduct(binormal);

            if (tangentCross.DotProduct(normal) < 0.0f)
            {
                tangent  = -tangent;
                binormal = -binormal;
            }

            return(tangent);
        }