diff --git a/Source/OpenTK/Math/Matrix4.cs b/Source/OpenTK/Math/Matrix4.cs index 643361bf..45011f6d 100644 --- a/Source/OpenTK/Math/Matrix4.cs +++ b/Source/OpenTK/Math/Matrix4.cs @@ -667,7 +667,82 @@ namespace OpenTK CreatePerspectiveOffCenter(left, right, bottom, top, zNear, zFar, out result); return result; } + + #region CreateFromQuaternion + /// + /// Build a rotation matrix from the specified quaternion. + /// + /// Quaternion to translate. + /// Matrix result. + public static void CreateFromQuaternion(ref Quaternion q,ref Matrix4 m) + { + m = Matrix4.Identity; + + float X = q.X; + float Y = q.Y; + float Z = q.Z; + float W = q.W; + + float xx = X * X; + float xy = X * Y; + float xz = X * Z; + float xw = X * W; + float yy = Y * Y; + float yz = Y * Z; + float yw = Y * W; + float zz = Z * Z; + float zw = Z * W; + + m.M11 = 1 - 2 * (yy + zz); + m.M21 = 2 * (xy - zw); + m.M31 = 2 * (xz + yw); + m.M12 = 2 * (xy + zw); + m.M22 = 1 - 2 * (xx + zz); + m.M32 = 2 * (yz - xw); + m.M13 = 2 * (xz - yw); + m.M23 = 2 * (yz + xw); + m.M33 = 1 - 2 * (xx + yy); + } + /// + /// Build a rotation matrix from the specified quaternion. + /// + /// Quaternion to translate. + /// A matrix instance. + public static Matrix4 CreateFromQuaternion(ref Quaternion q) + { + Matrix4 result = Matrix4.Identity; + + float X = q.X; + float Y = q.Y; + float Z = q.Z; + float W = q.W; + + float xx = X * X; + float xy = X * Y; + float xz = X * Z; + float xw = X * W; + float yy = Y * Y; + float yz = Y * Z; + float yw = Y * W; + float zz = Z * Z; + float zw = Z * W; + + result.M11 = 1 - 2 * (yy + zz); + result.M21 = 2 * (xy - zw); + result.M31 = 2 * (xz + yw); + result.M12 = 2 * (xy + zw); + result.M22 = 1 - 2 * (xx + zz); + result.M32 = 2 * (yz - xw); + result.M13 = 2 * (xz - yw); + result.M23 = 2 * (yz + xw); + result.M33 = 1 - 2 * (xx + yy); + return result; + } + + #endregion + + #endregion #region Obsolete Functions diff --git a/Source/OpenTK/Math/Matrix4d.cs b/Source/OpenTK/Math/Matrix4d.cs index 3a0b7d3b..f8b6e7b3 100644 --- a/Source/OpenTK/Math/Matrix4d.cs +++ b/Source/OpenTK/Math/Matrix4d.cs @@ -670,6 +670,83 @@ namespace OpenTK #endregion + + + #region CreateFromQuaternion + /// + /// Build a rotation matrix from the specified quaternion. + /// + /// Quaternion to translate. + /// Matrix result. + public static void CreateFromQuaternion(ref Quaternion q,ref Matrix4 m) + { + m = Matrix4.Identity; + + float X = q.X; + float Y = q.Y; + float Z = q.Z; + float W = q.W; + + float xx = X * X; + float xy = X * Y; + float xz = X * Z; + float xw = X * W; + float yy = Y * Y; + float yz = Y * Z; + float yw = Y * W; + float zz = Z * Z; + float zw = Z * W; + + m.M11 = 1 - 2 * (yy + zz); + m.M21 = 2 * (xy - zw); + m.M31 = 2 * (xz + yw); + m.M12 = 2 * (xy + zw); + m.M22 = 1 - 2 * (xx + zz); + m.M32 = 2 * (yz - xw); + m.M13 = 2 * (xz - yw); + m.M23 = 2 * (yz + xw); + m.M33 = 1 - 2 * (xx + yy); + } + + /// + /// Build a rotation matrix from the specified quaternion. + /// + /// Quaternion to translate. + /// A matrix instance. + public static Matrix4 CreateFromQuaternion(ref Quaternion q) + { + Matrix4 result = Matrix4.Identity; + + float X = q.X; + float Y = q.Y; + float Z = q.Z; + float W = q.W; + + float xx = X * X; + float xy = X * Y; + float xz = X * Z; + float xw = X * W; + float yy = Y * Y; + float yz = Y * Z; + float yw = Y * W; + float zz = Z * Z; + float zw = Z * W; + + result.M11 = 1 - 2 * (yy + zz); + result.M21 = 2 * (xy - zw); + result.M31 = 2 * (xz + yw); + result.M12 = 2 * (xy + zw); + result.M22 = 1 - 2 * (xx + zz); + result.M32 = 2 * (yz - xw); + result.M13 = 2 * (xz - yw); + result.M23 = 2 * (yz + xw); + result.M33 = 1 - 2 * (xx + yy); + return result; + } + + #endregion + + #region Obsolete Functions #region Translation Functions