From 6c5d3a90c3621e59f29a31b2fc7785ce975241f0 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Fri, 4 Sep 2009 13:02:23 +0000 Subject: [PATCH] Added missing Matrix4./Matrix4d.CreateRotation[XYZ] methods. Added missing Matrix4d.CreateFromAxisAngle method. Fixed Matrix4d constructor to take double parameters instead of float. --- Source/OpenTK/Math/Matrix4.cs | 88 ++++++++++++++++++++++ Source/OpenTK/Math/Matrix4d.cs | 133 ++++++++++++++++++++++++++++++++- 2 files changed, 217 insertions(+), 4 deletions(-) diff --git a/Source/OpenTK/Math/Matrix4.cs b/Source/OpenTK/Math/Matrix4.cs index a13c63c7..48cc7fd5 100644 --- a/Source/OpenTK/Math/Matrix4.cs +++ b/Source/OpenTK/Math/Matrix4.cs @@ -309,6 +309,94 @@ namespace OpenTK #endregion + #region CreateRotation[XYZ] + + /// + /// Builds a rotation matrix for a rotation around the x-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static void CreateRotationX(float angle, out Matrix4 result) + { + float cos = (float)System.Math.Cos(angle); + float sin = (float)System.Math.Sin(angle); + + result.Row0 = Vector4.UnitX; + result.Row1 = new Vector4(0.0f, cos, sin, 0.0f); + result.Row2 = new Vector4(0.0f, -sin, cos, 0.0f); + result.Row3 = Vector4.UnitW; + } + + /// + /// Builds a rotation matrix for a rotation around the x-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static Matrix4 CreateRotationX(float angle) + { + Matrix4 result; + CreateRotationX(angle, out result); + return result; + } + + /// + /// Builds a rotation matrix for a rotation around the y-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static void CreateRotationY(float angle, out Matrix4 result) + { + float cos = (float)System.Math.Cos(angle); + float sin = (float)System.Math.Sin(angle); + + result.Row0 = new Vector4(cos, 0.0f, -sin, 0.0f); + result.Row1 = Vector4.UnitY; + result.Row2 = new Vector4(sin, 0.0f, cos, 0.0f); + result.Row3 = Vector4.UnitW; + } + + /// + /// Builds a rotation matrix for a rotation around the y-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static Matrix4 CreateRotationY(float angle) + { + Matrix4 result; + CreateRotationY(angle, out result); + return result; + } + + /// + /// Builds a rotation matrix for a rotation around the z-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static void CreateRotationZ(float angle, out Matrix4 result) + { + float cos = (float)System.Math.Cos(angle); + float sin = (float)System.Math.Sin(angle); + + result.Row0 = new Vector4(cos, sin, 0.0f, 0.0f); + result.Row1 = new Vector4(-sin, cos, 0.0f, 0.0f); + result.Row2 = Vector4.UnitZ; + result.Row3 = Vector4.UnitW; + } + + /// + /// Builds a rotation matrix for a rotation around the z-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static Matrix4 CreateRotationZ(float angle) + { + Matrix4 result; + CreateRotationZ(angle, out result); + return result; + } + + #endregion + #region CreateTranslation /// diff --git a/Source/OpenTK/Math/Matrix4d.cs b/Source/OpenTK/Math/Matrix4d.cs index 1a030653..b2a8bfd3 100644 --- a/Source/OpenTK/Math/Matrix4d.cs +++ b/Source/OpenTK/Math/Matrix4d.cs @@ -97,10 +97,10 @@ namespace OpenTK /// Third item of the fourth row. /// Fourth item of the fourth row. public Matrix4d( - float m00, float m01, float m02, float m03, - float m10, float m11, float m12, float m13, - float m20, float m21, float m22, float m23, - float m30, float m31, float m32, float m33) + double m00, double m01, double m02, double m03, + double m10, double m11, double m12, double m13, + double m20, double m21, double m22, double m23, + double m30, double m31, double m32, double m33) { Row0 = new Vector4d(m00, m01, m02, m03); Row1 = new Vector4d(m10, m11, m12, m13); @@ -301,6 +301,131 @@ namespace OpenTK #region Static + #region CreateFromAxisAngle + + /// + /// Build a rotation matrix from the specified axis/angle rotation. + /// + /// The axis to rotate about. + /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). + /// A matrix instance. + public static void CreateFromAxisAngle(Vector3d axis, double angle, out Matrix4d result) + { + double cos = System.Math.Cos(-angle); + double sin = System.Math.Sin(-angle); + double t = 1.0f - cos; + + axis.Normalize(); + + result = new Matrix4d(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); + } + + /// + /// Build a rotation matrix from the specified axis/angle rotation. + /// + /// The axis to rotate about. + /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). + /// A matrix instance. + public static Matrix4d CreateFromAxisAngle(Vector3d axis, double angle) + { + Matrix4d result; + CreateFromAxisAngle(axis, angle, out result); + return result; + } + + #endregion + + #region CreateRotation[XYZ] + + /// + /// Builds a rotation matrix for a rotation around the x-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static void CreateRotationX(double angle, out Matrix4d result) + { + double cos = System.Math.Cos(angle); + double sin = System.Math.Sin(angle); + + result.Row0 = Vector4d.UnitX; + result.Row1 = new Vector4d(0, cos, sin, 0); + result.Row2 = new Vector4d(0, -sin, cos, 0); + result.Row3 = Vector4d.UnitW; + } + + /// + /// Builds a rotation matrix for a rotation around the x-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static Matrix4d CreateRotationX(double angle) + { + Matrix4d result; + CreateRotationX(angle, out result); + return result; + } + + /// + /// Builds a rotation matrix for a rotation around the y-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static void CreateRotationY(double angle, out Matrix4d result) + { + double cos = System.Math.Cos(angle); + double sin = System.Math.Sin(angle); + + result.Row0 = new Vector4d(cos, 0, -sin, 0); + result.Row1 = Vector4d.UnitY; + result.Row2 = new Vector4d(sin, 0, cos, 0); + result.Row3 = Vector4d.UnitW; + } + + /// + /// Builds a rotation matrix for a rotation around the y-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static Matrix4d CreateRotationY(double angle) + { + Matrix4d result; + CreateRotationY(angle, out result); + return result; + } + + /// + /// Builds a rotation matrix for a rotation around the z-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static void CreateRotationZ(double angle, out Matrix4d result) + { + double cos = System.Math.Cos(angle); + double sin = System.Math.Sin(angle); + + result.Row0 = new Vector4d(cos, sin, 0, 0); + result.Row1 = new Vector4d(-sin, cos, 0, 0); + result.Row2 = Vector4d.UnitZ; + result.Row3 = Vector4d.UnitW; + } + + /// + /// Builds a rotation matrix for a rotation around the z-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static Matrix4d CreateRotationZ(double angle) + { + Matrix4d result; + CreateRotationZ(angle, out result); + return result; + } + + #endregion + #region CreateTranslation ///