Esempio n. 1
0
        /// <summary>
        /// Build a rotation matrix from the specified axis/angle rotation.
        /// </summary>
        /// <param name="axis">The axis to rotate about.</param>
        /// <param name="angle">Angle in radians to rotate counter-clockwise (looking in the direction of the given axis).</param>
        /// <param name="result">A matrix instance.</param>
        public static void CreateFromAxisAngle(Vector3F axis, float angle, out Matrix4F result)
        {
            float cos = (float)System.Math.Cos(-angle);
            float sin = (float)System.Math.Sin(-angle);
            float t   = 1.0f - cos;

            axis.Normalize();

            result = new Matrix4F(t * axis.X * axis.X + cos, t * axis.X * axis.Y - sin * axis.Z, t * axis.X * axis.Z + sin * axis.Y, 0.0f,
                                  t * axis.X * axis.Y + sin * axis.Z, t * axis.Y * axis.Y + cos, t * axis.Y * axis.Z - sin * axis.X, 0.0f,
                                  t * axis.X * axis.Z - sin * axis.Y, t * axis.Y * axis.Z + sin * axis.X, t * axis.Z * axis.Z + cos, 0.0f,
                                  0, 0, 0, 1);
        }
Esempio n. 2
0
        /// <summary>
        /// Build a world space to camera space matrix
        /// </summary>
        /// <param name="eye">Eye (camera) position in world space</param>
        /// <param name="target">Target position in world space</param>
        /// <param name="up">Up vector in world space (should not be parallel to the camera direction, that is target - eye)</param>
        /// <returns>A Matrix4F that transforms world space to camera space</returns>
        public static Matrix4F LookAt(Vector3F eye, Vector3F target, Vector3F up)
        {
            Vector3F z = Vector3F.Normalize(eye - target);
            Vector3F x = Vector3F.Normalize(Vector3F.Cross(up, z));
            Vector3F y = Vector3F.Normalize(Vector3F.Cross(z, x));

            Matrix4F rot = new Matrix4F(new Vector4F(x.X, y.X, z.X, 0.0f),
                                        new Vector4F(x.Y, y.Y, z.Y, 0.0f),
                                        new Vector4F(x.Z, y.Z, z.Z, 0.0f),
                                        Vector4F.UnitW);

            Matrix4F trans = Matrix4F.CreateTranslation(eye * -1.0f);

            return(trans * rot);
        }
Esempio n. 3
0
        /// <summary>
        /// Build a quaternion from the given axis and angle
        /// </summary>
        /// <param name="axis">The axis to rotate about</param>
        /// <param name="angle">The rotation angle in radians</param>
        /// <returns></returns>
        public static Quaternion FromAxisAngle(Vector3F axis, float angle)
        {
            if (axis.LengthSquared == 0.0f)
            {
                return(Identity);
            }

            Quaternion result = Identity;

            angle *= 0.5f;
            axis.Normalize();
            result.Xyz = axis * (float)System.Math.Sin(angle);
            result.W   = (float)System.Math.Cos(angle);

            return(Normalize(result));
        }