From 584fa7c76195ba093f360c01439cc1c37d05309a Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Fri, 29 May 2009 16:44:12 +0000 Subject: [PATCH] Added M11-M44 properties to Matrix4 and Matrix4d. Implemented CreateOrthographic and CreateOrthographicOffCenter static methods. Fixes issue [#676]: "Add Matrix4.Ortho method" (http://www.opentk.com/node/676). Implemented CreateTranslation methods. Obsoleted Translation() in favor of the new methods. --- Source/OpenTK/Math/Matrix4.cs | 278 +++++++++++++++++++++++++++++---- Source/OpenTK/Math/Matrix4d.cs | 278 +++++++++++++++++++++++++++++---- 2 files changed, 498 insertions(+), 58 deletions(-) diff --git a/Source/OpenTK/Math/Matrix4.cs b/Source/OpenTK/Math/Matrix4.cs index 23b066f0..53f4c08c 100644 --- a/Source/OpenTK/Math/Matrix4.cs +++ b/Source/OpenTK/Math/Matrix4.cs @@ -163,6 +163,86 @@ namespace OpenTK.Math get { return new Vector4(Row0.W, Row1.W, Row2.W, Row3.W); } } + /// + /// Gets or sets the value at row 1, column 1 of this instance. + /// + public float M11 { get { return Row0.X; } set { Row0.X = value; } } + + /// + /// Gets or sets the value at row 1, column 2 of this instance. + /// + public float M12 { get { return Row0.Y; } set { Row0.Y = value; } } + + /// + /// Gets or sets the value at row 1, column 3 of this instance. + /// + public float M13 { get { return Row0.Z; } set { Row0.Z = value; } } + + /// + /// Gets or sets the value at row 1, column 4 of this instance. + /// + public float M14 { get { return Row0.W; } set { Row0.W = value; } } + + /// + /// Gets or sets the value at row 2, column 1 of this instance. + /// + public float M21 { get { return Row1.X; } set { Row1.X = value; } } + + /// + /// Gets or sets the value at row 2, column 2 of this instance. + /// + public float M22 { get { return Row1.Y; } set { Row1.Y = value; } } + + /// + /// Gets or sets the value at row 2, column 3 of this instance. + /// + public float M23 { get { return Row1.Z; } set { Row1.Z = value; } } + + /// + /// Gets or sets the value at row 2, column 4 of this instance. + /// + public float M24 { get { return Row1.W; } set { Row1.W = value; } } + + /// + /// Gets or sets the value at row 3, column 1 of this instance. + /// + public float M31 { get { return Row2.X; } set { Row2.X = value; } } + + /// + /// Gets or sets the value at row 3, column 2 of this instance. + /// + public float M32 { get { return Row2.Y; } set { Row2.Y = value; } } + + /// + /// Gets or sets the value at row 3, column 3 of this instance. + /// + public float M33 { get { return Row2.Z; } set { Row2.Z = value; } } + + /// + /// Gets or sets the value at row 3, column 4 of this instance. + /// + public float M34 { get { return Row2.W; } set { Row2.W = value; } } + + /// + /// Gets or sets the value at row 4, column 1 of this instance. + /// + public float M41 { get { return Row3.X; } set { Row3.X = value; } } + + /// + /// Gets or sets the value at row 4, column 3 of this instance. + /// + public float M42 { get { return Row3.Y; } set { Row3.Y = value; } } + + /// + /// Gets or sets the value at row 4, column 3 of this instance. + /// + public float M43 { get { return Row3.Z; } set { Row3.Z = value; } } + + /// + /// Gets or sets the value at row 4, column 4 of this instance. + /// + public float M44 { get { return Row3.W; } set { Row3.W = value; } } + #endregion #region Instance @@ -189,6 +269,174 @@ namespace OpenTK.Math #region Static + #region CreateTranslation + + /// + /// Creates a translation matrix. + /// + /// X translation. + /// Y translation. + /// Z translation. + /// The resulting Matrix4 instance. + public static void CreateTranslation(float x, float y, float z, out Matrix4 result) + { + result = Identity; + result.Row3 = new Vector4(x, y, z, 1); + } + + /// + /// Creates a translation matrix. + /// + /// The translation vector. + /// The resulting Matrix4 instance. + public static void CreateTranslation(ref Vector3 vector, out Matrix4 result) + { + result = Identity; + result.Row3 = new Vector4(vector.X, vector.Y, vector.Z, 1); + } + + /// + /// Creates a translation matrix. + /// + /// X translation. + /// Y translation. + /// Z translation. + /// The resulting Matrix4 instance. + public static Matrix4 CreateTranslation(float x, float y, float z) + { + Matrix4 result; + CreateTranslation(x, y, z, out result); + return result; + } + + /// + /// Creates a translation matrix. + /// + /// The translation vector. + /// The resulting Matrix4 instance. + public static Matrix4 CreateTranslation(Vector3 vector) + { + Matrix4 result; + CreateTranslation(vector.X, vector.Y, vector.Z, out result); + return result; + } + + #endregion + + #region CreateOrthographic + + /// + /// Creates an orthographic projection matrix. + /// + /// The width of the projection volume. + /// The height of the projection volume. + /// The near edge of the projection volume. + /// The far edge of the projection volume. + /// The resulting Matrix4 instance. + public static void CreateOrthographic(float width, float height, float zNear, float zFar, out Matrix4 result) + { + CreateOrthographicOffCenter(-width / 2, width / 2, -height / 2, height / 2, zNear, zFar, out result); + } + + /// + /// Creates an orthographic projection matrix. + /// + /// The width of the projection volume. + /// The height of the projection volume. + /// The near edge of the projection volume. + /// The far edge of the projection volume. + /// The resulting Matrix4 instance. + public static Matrix4 CreateOrthographic(float width, float height, float zNear, float zFar) + { + Matrix4 result; + CreateOrthographicOffCenter(-width / 2, width / 2, -height / 2, height / 2, zNear, zFar, out result); + return result; + } + + #endregion + + #region CreateOrthographicOffCenter + + /// + /// Creates an orthographic projection matrix. + /// + /// The left edge of the projection volume. + /// The right edge of the projection volume. + /// The bottom edge of the projection volume. + /// The top edge of the projection volume. + /// The near edge of the projection volume. + /// The far edge of the projection volume. + /// The resulting Matrix4 instance. + public static void CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNear, float zFar, out Matrix4 result) + { + result = new Matrix4(); + + float invRL = 1 / (right - left); + float invTB = 1 / (top - bottom); + float invFN = 1 / (zFar - zNear); + + result.M11 = 2 * invRL; + result.M22 = 2 * invTB; + result.M33 = -2 * invFN; + + result.M41 = -(right + left) * invRL; + result.M42 = -(top + bottom) * invTB; + result.M43 = -(zFar + zNear) * invFN; + } + + /// + /// Creates an orthographic projection matrix. + /// + /// The left edge of the projection volume. + /// The right edge of the projection volume. + /// The bottom edge of the projection volume. + /// The top edge of the projection volume. + /// The near edge of the projection volume. + /// The far edge of the projection volume. + /// The resulting Matrix4 instance. + public static Matrix4 CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNear, float zFar) + { + Matrix4 result; + CreateOrthographicOffCenter(left, right, bottom, top, zNear, zFar, out result); + return result; + } + + #endregion + + #region Obsolete Functions + + #region Translation Functions + + /// + /// Builds a translation matrix. + /// + /// The translation vector. + /// A new Matrix4 instance. + [Obsolete("Use CreateTranslation instead.")] + public static Matrix4 Translation(Vector3 trans) + { + return Translation(trans.X, trans.Y, trans.Z); + } + + /// + /// Build a translation matrix with the given translation + /// + /// X translation + /// Y translation + /// Z translation + /// A Translation matrix + [Obsolete("Use CreateTranslation instead.")] + public static Matrix4 Translation(float x, float y, float z) + { + Matrix4 result = Identity; + result.Row3 = new Vector4(x, y, z, 1.0f); + return result; + } + + #endregion + + #endregion + #region Scale Functions /// @@ -230,34 +478,6 @@ namespace OpenTK.Math #endregion - #region Translation Functions - - /// - /// Build a translation matrix with the given translation - /// - /// The vector to translate along - /// A Translation matrix - public static Matrix4 Translation(Vector3 trans) - { - return Translation(trans.X, trans.Y, trans.Z); - } - - /// - /// Build a translation matrix with the given translation - /// - /// X translation - /// Y translation - /// Z translation - /// A Translation matrix - public static Matrix4 Translation(float x, float y, float z) - { - Matrix4 result = Identity; - result.Row3 = new Vector4(x, y, z, 1.0f); - return result; - } - - #endregion - #region Rotation Functions /// @@ -371,7 +591,7 @@ namespace OpenTK.Math new Vector4(x.Z, y.Z, z.Z, 0.0f), Vector4.UnitW); - Matrix4 trans = Matrix4.Translation(-eye); + Matrix4 trans = Matrix4.CreateTranslation(-eye); return trans * rot; } diff --git a/Source/OpenTK/Math/Matrix4d.cs b/Source/OpenTK/Math/Matrix4d.cs index 8597425b..50ebe67b 100644 --- a/Source/OpenTK/Math/Matrix4d.cs +++ b/Source/OpenTK/Math/Matrix4d.cs @@ -195,6 +195,86 @@ namespace OpenTK.Math } } + /// + /// Gets or sets the value at row 1, column 1 of this instance. + /// + public double M11 { get { return Row0.X; } set { Row0.X = value; } } + + /// + /// Gets or sets the value at row 1, column 2 of this instance. + /// + public double M12 { get { return Row0.Y; } set { Row0.Y = value; } } + + /// + /// Gets or sets the value at row 1, column 3 of this instance. + /// + public double M13 { get { return Row0.Z; } set { Row0.Z = value; } } + + /// + /// Gets or sets the value at row 1, column 4 of this instance. + /// + public double M14 { get { return Row0.W; } set { Row0.W = value; } } + + /// + /// Gets or sets the value at row 2, column 1 of this instance. + /// + public double M21 { get { return Row1.X; } set { Row1.X = value; } } + + /// + /// Gets or sets the value at row 2, column 2 of this instance. + /// + public double M22 { get { return Row1.Y; } set { Row1.Y = value; } } + + /// + /// Gets or sets the value at row 2, column 3 of this instance. + /// + public double M23 { get { return Row1.Z; } set { Row1.Z = value; } } + + /// + /// Gets or sets the value at row 2, column 4 of this instance. + /// + public double M24 { get { return Row1.W; } set { Row1.W = value; } } + + /// + /// Gets or sets the value at row 3, column 1 of this instance. + /// + public double M31 { get { return Row2.X; } set { Row2.X = value; } } + + /// + /// Gets or sets the value at row 3, column 2 of this instance. + /// + public double M32 { get { return Row2.Y; } set { Row2.Y = value; } } + + /// + /// Gets or sets the value at row 3, column 3 of this instance. + /// + public double M33 { get { return Row2.Z; } set { Row2.Z = value; } } + + /// + /// Gets or sets the value at row 3, column 4 of this instance. + /// + public double M34 { get { return Row2.W; } set { Row2.W = value; } } + + /// + /// Gets or sets the value at row 4, column 1 of this instance. + /// + public double M41 { get { return Row3.X; } set { Row3.X = value; } } + + /// + /// Gets or sets the value at row 4, column 3 of this instance. + /// + public double M42 { get { return Row3.Y; } set { Row3.Y = value; } } + + /// + /// Gets or sets the value at row 4, column 3 of this instance. + /// + public double M43 { get { return Row3.Z; } set { Row3.Z = value; } } + + /// + /// Gets or sets the value at row 4, column 4 of this instance. + /// + public double M44 { get { return Row3.W; } set { Row3.W = value; } } + #endregion #region Instance @@ -221,6 +301,174 @@ namespace OpenTK.Math #region Static + #region CreateTranslation + + /// + /// Creates a translation matrix. + /// + /// X translation. + /// Y translation. + /// Z translation. + /// The resulting Matrix4d instance. + public static void CreateTranslation(double x, double y, double z, out Matrix4d result) + { + result = Identity; + result.Row3 = new Vector4d(x, y, z, 1); + } + + /// + /// Creates a translation matrix. + /// + /// The translation vector. + /// The resulting Matrix4d instance. + public static void CreateTranslation(ref Vector3d vector, out Matrix4d result) + { + result = Identity; + result.Row3 = new Vector4d(vector.X, vector.Y, vector.Z, 1); + } + + /// + /// Creates a translation matrix. + /// + /// X translation. + /// Y translation. + /// Z translation. + /// The resulting Matrix4d instance. + public static Matrix4d CreateTranslation(double x, double y, double z) + { + Matrix4d result; + CreateTranslation(x, y, z, out result); + return result; + } + + /// + /// Creates a translation matrix. + /// + /// The translation vector. + /// The resulting Matrix4d instance. + public static Matrix4d CreateTranslation(Vector3d vector) + { + Matrix4d result; + CreateTranslation(vector.X, vector.Y, vector.Z, out result); + return result; + } + + #endregion + + #region CreateOrthographic + + /// + /// Creates an orthographic projection matrix. + /// + /// The width of the projection volume. + /// The height of the projection volume. + /// The near edge of the projection volume. + /// The far edge of the projection volume. + /// The resulting Matrix4d instance. + public static void CreateOrthographic(double width, double height, double zNear, double zFar, out Matrix4d result) + { + CreateOrthographicOffCenter(-width / 2, width / 2, -height / 2, height / 2, zNear, zFar, out result); + } + + /// + /// Creates an orthographic projection matrix. + /// + /// The width of the projection volume. + /// The height of the projection volume. + /// The near edge of the projection volume. + /// The far edge of the projection volume. + /// The resulting Matrix4d instance. + public static Matrix4d CreateOrthographic(double width, double height, double zNear, double zFar) + { + Matrix4d result; + CreateOrthographicOffCenter(-width / 2, width / 2, -height / 2, height / 2, zNear, zFar, out result); + return result; + } + + #endregion + + #region CreateOrthographicOffCenter + + /// + /// Creates an orthographic projection matrix. + /// + /// The left edge of the projection volume. + /// The right edge of the projection volume. + /// The bottom edge of the projection volume. + /// The top edge of the projection volume. + /// The near edge of the projection volume. + /// The far edge of the projection volume. + /// The resulting Matrix4d instance. + public static void CreateOrthographicOffCenter(double left, double right, double bottom, double top, double zNear, double zFar, out Matrix4d result) + { + result = new Matrix4d(); + + double invRL = 1 / (right - left); + double invTB = 1 / (top - bottom); + double invFN = 1 / (zFar - zNear); + + result.M11 = 2 * invRL; + result.M22 = 2 * invTB; + result.M33 = -2 * invFN; + + result.M41 = -(right + left) * invRL; + result.M42 = -(top + bottom) * invTB; + result.M43 = -(zFar + zNear) * invFN; + } + + /// + /// Creates an orthographic projection matrix. + /// + /// The left edge of the projection volume. + /// The right edge of the projection volume. + /// The bottom edge of the projection volume. + /// The top edge of the projection volume. + /// The near edge of the projection volume. + /// The far edge of the projection volume. + /// The resulting Matrix4d instance. + public static Matrix4d CreateOrthographicOffCenter(double left, double right, double bottom, double top, double zNear, double zFar) + { + Matrix4d result; + CreateOrthographicOffCenter(left, right, bottom, top, zNear, zFar, out result); + return result; + } + + #endregion + + #region Obsolete Functions + + #region Translation Functions + + /// + /// Build a translation matrix with the given translation + /// + /// The vector to translate along + /// A Translation matrix + [Obsolete("Use CreateTranslation instead.")] + public static Matrix4d Translation(Vector3d trans) + { + return Translation(trans.X, trans.Y, trans.Z); + } + + /// + /// Build a translation matrix with the given translation + /// + /// X translation + /// Y translation + /// Z translation + /// A Translation matrix + [Obsolete("Use CreateTranslation instead.")] + public static Matrix4d Translation(double x, double y, double z) + { + Matrix4d result = Identity; + result.Row3 = new Vector4d(x, y, z, 1.0f); + return result; + } + + #endregion + + #endregion + #region Scale Functions /// @@ -262,34 +510,6 @@ namespace OpenTK.Math #endregion - #region Translation Functions - - /// - /// Build a translation matrix with the given translation - /// - /// The vector to translate along - /// A Translation matrix - public static Matrix4d Translation(Vector3d trans) - { - return Translation(trans.X, trans.Y, trans.Z); - } - - /// - /// Build a translation matrix with the given translation - /// - /// X translation - /// Y translation - /// Z translation - /// A Translation matrix - public static Matrix4d Translation(double x, double y, double z) - { - Matrix4d result = Identity; - result.Row3 = new Vector4d (x, y, z, 1.0f); - return result; - } - - #endregion - #region Rotation Functions /// @@ -403,7 +623,7 @@ namespace OpenTK.Math new Vector4d (x.Z, y.Z, z.Z, 0.0f), Vector4d .UnitW); - Matrix4d trans = Matrix4d.Translation(-eye); + Matrix4d trans = Matrix4d.CreateTranslation(-eye); return trans * rot; }