# andykorth/opentk

More work on matrices.

`Fixed some static fields in the math classes that weren't also readonly.`
1 parent 84a03e9 commit 5438f941dca32683c097ab2deed87b5781b92688 Robmaister committed Jan 19, 2013
61 Source/OpenTK/Math/Matrix2.cs
 @@ -44,6 +44,16 @@ public struct Matrix2 : IEquatable /// public Vector2 Row1; + /// + /// The identity matrix. + /// + public static Matrix2 Identity = new Matrix2(Vector2.UnitX, Vector2.UnitY); + + /// + /// The zero matrix. + /// + public static readonly Matrix2 Zero = new Matrix2(Vector2.Zero, Vector2.Zero); + #endregion #region Constructors @@ -281,6 +291,33 @@ public static Matrix2 CreateScale(float x, float y) #region Multiply Functions /// + /// Multiplies and instance by a scalar. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix2 left, float right, out Matrix2 result) + { + result.Row0.X = left.Row0.X * right; + result.Row0.Y = left.Row0.Y * right; + result.Row1.X = left.Row1.X * right; + result.Row1.Y = left.Row1.Y * right; + } + + /// + /// Multiplies and instance by a scalar. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix2 Mult(Matrix2 left, float right) + { + Matrix2 result; + Mult(ref left, right, out result); + return result; + } + + /// /// Multiplies two instances. /// /// The left operand of the multiplication. @@ -486,6 +523,28 @@ public static Matrix2 Transpose(Matrix2 mat) #region Operators /// + /// Scalar multiplication. + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2 which holds the result of the multiplication + public static Matrix2 operator *(float left, Matrix2 right) + { + return Matrix2.Mult(right, left); + } + + /// + /// Scalar multiplication. + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2 which holds the result of the multiplication + public static Matrix2 operator *(Matrix2 left, float right) + { + return Matrix2.Mult(left, right); + } + + /// /// Matrix multiplication /// /// left-hand operand @@ -564,7 +623,7 @@ public static Matrix2 Transpose(Matrix2 mat) #endregion - #region Overloads + #region Overrides #region public override string ToString()
14 Source/OpenTK/Math/Matrix2x3.cs
 @@ -44,6 +44,11 @@ public struct Matrix2x3 : IEquatable /// public Vector3 Row1; + /// + /// The zero matrix. + /// + public static Matrix2x3 Zero = new Matrix2x3(Vector3.Zero, Vector3.Zero); + #endregion #region Constructors @@ -164,6 +169,15 @@ public Vector2 Column2 #endregion + #region Instance + #endregion + + #region Static + + + + #endregion + #region Operators ///
5 Source/OpenTK/Math/Matrix2x4.cs
 @@ -34,6 +34,11 @@ public struct Matrix2x4 : IEquatable public Vector4 Row0; public Vector4 Row1; + /// + /// The zero matrix. + /// + public static Matrix2x4 Zero = new Matrix2x4(Vector4.Zero, Vector4.Zero); + #endregion #region Constructors
7 Source/OpenTK/Math/Matrix3.cs
 @@ -54,7 +54,12 @@ public struct Matrix3 : IEquatable /// /// The identity matrix. /// - public static Matrix3 Identity = new Matrix3(Vector3.UnitX, Vector3.UnitY, Vector3.UnitZ); + public static readonly Matrix3 Identity = new Matrix3(Vector3.UnitX, Vector3.UnitY, Vector3.UnitZ); + + /// + /// The zero matrix. + /// + public static readonly Matrix3 Zero = new Matrix3(Vector3.Zero, Vector3.Zero, Vector3.Zero); #endregion
72 Source/OpenTK/Math/Matrix3x2.cs
 @@ -31,10 +31,26 @@ public struct Matrix3x2 : IEquatable { #region Fields + /// + /// Top row of the matrix. + /// public Vector2 Row0; + + /// + /// Second row of the matrix. + /// public Vector2 Row1; + + /// + /// Bottom row of the matrix. + /// public Vector2 Row2; + /// + /// The zero matrix. + /// + public static Matrix3x2 Zero = new Matrix3x2(Vector2.Zero, Vector2.Zero, Vector2.Zero); + #endregion #region Constructors @@ -108,6 +124,62 @@ public Vector3 Column1 #endregion + #region Instance + #endregion + + #region Static + #endregion + + #region Operators + #endregion + + #region Overrides + + #region public override string ToString() + + /// + /// Returns a System.String that represents the current Matrix3d. + /// + /// The string representation of the matrix. + public override string ToString() + { + return String.Format("{0}\n{1}\n{2}", Row0, Row1, Row2); + } + + #endregion + + #region public override int GetHashCode() + + /// + /// Returns the hashcode for this instance. + /// + /// A System.Int32 containing the unique hashcode for this instance. + public override int GetHashCode() + { + return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); + } + + #endregion + + #region public override bool Equals(object obj) + + /// + /// Indicates whether this instance and a specified object are equal. + /// + /// The object to compare to. + /// True if the instances are equal; false otherwise. + public override bool Equals(object obj) + { + if (!(obj is Matrix3x2)) + return false; + + return this.Equals((Matrix3x2)obj); + } + + #endregion + + #endregion + #endregion #region IEquatable Members
62 Source/OpenTK/Math/Matrix3x4.cs
 @@ -52,9 +52,9 @@ public struct Matrix3x4 : IEquatable public Vector4 Row2; /// - /// The identity matrix + /// The zero matrix /// - public static Matrix3x4 Identity = new Matrix3x4(Vector4.UnitX, Vector4.UnitY, Vector4.UnitZ); + public static Matrix3x4 Zero = new Matrix3x4(Vector4.Zero, Vector4.Zero, Vector4.Zero); #endregion @@ -355,11 +355,18 @@ public static void CreateRotationX(float angle, out Matrix3x4 result) float cos = (float)System.Math.Cos(angle); float sin = (float)System.Math.Sin(angle); - result = Identity; + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row0.W = 0; + result.Row1.X = 0; result.Row1.Y = cos; result.Row1.Z = sin; + result.Row1.W = 0; + result.Row2.X = 0; result.Row2.Y = -sin; result.Row2.Z = cos; + result.Row2.W = 0; } /// @@ -384,11 +391,18 @@ public static void CreateRotationY(float angle, out Matrix3x4 result) float cos = (float)System.Math.Cos(angle); float sin = (float)System.Math.Sin(angle); - result = Identity; result.Row0.X = cos; + result.Row0.Y = 0; result.Row0.Z = -sin; + result.Row0.W = 0; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; + result.Row1.W = 0; result.Row2.X = sin; + result.Row2.Y = 0; result.Row2.Z = cos; + result.Row2.W = 0; } /// @@ -413,11 +427,18 @@ public static void CreateRotationZ(float angle, out Matrix3x4 result) float cos = (float)System.Math.Cos(angle); float sin = (float)System.Math.Sin(angle); - result = Identity; result.Row0.X = cos; result.Row0.Y = sin; + result.Row0.Z = 0; + result.Row0.W = 0; result.Row1.X = -sin; result.Row1.Y = cos; + result.Row1.Z = 0; + result.Row1.W = 0; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; + result.Row2.W = 0; } /// @@ -445,9 +466,17 @@ public static Matrix3x4 CreateRotationZ(float angle) /// The resulting Matrix4 instance. public static void CreateTranslation(float x, float y, float z, out Matrix3x4 result) { - result = Identity; + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; result.Row0.W = x; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; result.Row1.W = y; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; result.Row2.W = z; } @@ -458,9 +487,17 @@ public static void CreateTranslation(float x, float y, float z, out Matrix3x4 re /// The resulting Matrix4 instance. public static void CreateTranslation(ref Vector3 vector, out Matrix3x4 result) { - result = Identity; + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; result.Row0.W = vector.X; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; result.Row1.W = vector.Y; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; result.Row2.W = vector.Z; } @@ -523,10 +560,19 @@ public static Matrix3x4 CreateScale(Vector3 scale) /// A scaling matrix public static Matrix3x4 CreateScale(float x, float y, float z) { - Matrix3x4 result = Identity; + Matrix3x4 result; result.Row0.X = x; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row0.W = 0; + result.Row1.X = 0; result.Row1.Y = y; + result.Row1.Z = 0; + result.Row1.W = 0; + result.Row2.X = 0; + result.Row2.Y = 0; result.Row2.Z = z; + result.Row2.W = 0; return result; }
7 Source/OpenTK/Math/Matrix4.cs
 @@ -59,7 +59,12 @@ public struct Matrix4 : IEquatable /// /// The identity matrix. /// - public static Matrix4 Identity = new Matrix4(Vector4.UnitX, Vector4.UnitY, Vector4.UnitZ, Vector4.UnitW); + public static readonly Matrix4 Identity = new Matrix4(Vector4.UnitX, Vector4.UnitY, Vector4.UnitZ, Vector4.UnitW); + + /// + /// The zero matrix. + /// + public static readonly Matrix4 Zero = new Matrix4(Vector4.Zero, Vector4.Zero, Vector4.Zero, Vector4.Zero); #endregion
5 Source/OpenTK/Math/Matrix4x2.cs
 @@ -36,6 +36,11 @@ public struct Matrix4x2 : IEquatable public Vector2 Row2; public Vector2 Row3; + /// + /// The zero matrix. + /// + public static Matrix4x2 Zero = new Matrix4x2(Vector2.Zero, Vector2.Zero, Vector2.Zero, Vector2.Zero); + #endregion #region Constructors
62 Source/OpenTK/Math/Matrix4x3.cs
 @@ -57,9 +57,9 @@ public struct Matrix4x3 : IEquatable public Vector3 Row3; /// - /// The identity matrix + /// The zero matrix /// - public static Matrix4x3 Identity = new Matrix4x3(Vector3.UnitX, Vector3.UnitY, Vector3.UnitZ, Vector3.Zero); + public static Matrix4x3 Zero = new Matrix4x3(Vector3.Zero, Vector3.Zero, Vector3.Zero, Vector3.Zero); #endregion @@ -358,11 +358,18 @@ public static void CreateRotationX(float angle, out Matrix4x3 result) float cos = (float)System.Math.Cos(angle); float sin = (float)System.Math.Sin(angle); - result = Identity; + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row1.X = 0; result.Row1.Y = cos; result.Row1.Z = sin; + result.Row2.X = 0; result.Row2.Y = -sin; result.Row2.Z = cos; + result.Row3.X = 0; + result.Row3.Y = 0; + result.Row3.Z = 0; } /// @@ -387,11 +394,18 @@ public static void CreateRotationY(float angle, out Matrix4x3 result) float cos = (float)System.Math.Cos(angle); float sin = (float)System.Math.Sin(angle); - result = Identity; result.Row0.X = cos; + result.Row0.Y = 0; result.Row0.Z = -sin; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; result.Row2.X = sin; + result.Row2.Y = 0; result.Row2.Z = cos; + result.Row3.X = 0; + result.Row3.Y = 0; + result.Row3.Z = 0; } /// @@ -416,11 +430,18 @@ public static void CreateRotationZ(float angle, out Matrix4x3 result) float cos = (float)System.Math.Cos(angle); float sin = (float)System.Math.Sin(angle); - result = Identity; result.Row0.X = cos; result.Row0.Y = sin; + result.Row0.Z = 0; result.Row1.X = -sin; result.Row1.Y = cos; + result.Row1.Z = 0; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; + result.Row3.X = 0; + result.Row3.Y = 0; + result.Row3.Z = 0; } /// @@ -448,7 +469,15 @@ public static Matrix4x3 CreateRotationZ(float angle) /// The resulting Matrix4 instance. public static void CreateTranslation(float x, float y, float z, out Matrix4x3 result) { - result = Identity; + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; result.Row3.X = x; result.Row3.Y = y; result.Row3.Z = z; @@ -461,7 +490,15 @@ public static void CreateTranslation(float x, float y, float z, out Matrix4x3 re /// The resulting Matrix4 instance. public static void CreateTranslation(ref Vector3 vector, out Matrix4x3 result) { - result = Identity; + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; result.Row3.X = vector.X; result.Row3.Y = vector.Y; result.Row3.Z = vector.Z; @@ -526,10 +563,19 @@ public static Matrix4x3 CreateScale(Vector3 scale) /// A scaling matrix public static Matrix4x3 CreateScale(float x, float y, float z) { - Matrix4x3 result = Identity; + Matrix4x3 result; result.Row0.X = x; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row1.X = 0; result.Row1.Y = y; + result.Row1.Z = 0; + result.Row2.X = 0; + result.Row2.Y = 0; result.Row2.Z = z; + result.Row3.X = 0; + result.Row3.Y = 0; + result.Row3.Z = 0; return result; }
6 Source/OpenTK/Math/Vector2d.cs
 @@ -44,17 +44,17 @@ public struct Vector2d : IEquatable /// /// Defines a unit-length Vector2d that points towards the X-axis. /// - public static Vector2d UnitX = new Vector2d(1, 0); + public static readonly Vector2d UnitX = new Vector2d(1, 0); /// /// Defines a unit-length Vector2d that points towards the Y-axis. /// - public static Vector2d UnitY = new Vector2d(0, 1); + public static readonly Vector2d UnitY = new Vector2d(0, 1); /// /// Defines a zero-length Vector2d. /// - public static Vector2d Zero = new Vector2d(0, 0); + public static readonly Vector2d Zero = new Vector2d(0, 0); /// /// Defines an instance with all components set to 1.
10 Source/OpenTK/Math/Vector4.cs
 @@ -60,27 +60,27 @@ public struct Vector4 : IEquatable /// /// Defines a unit-length Vector4 that points towards the X-axis. /// - public static Vector4 UnitX = new Vector4(1, 0, 0, 0); + public static readonly Vector4 UnitX = new Vector4(1, 0, 0, 0); /// /// Defines a unit-length Vector4 that points towards the Y-axis. /// - public static Vector4 UnitY = new Vector4(0, 1, 0, 0); + public static readonly Vector4 UnitY = new Vector4(0, 1, 0, 0); /// /// Defines a unit-length Vector4 that points towards the Z-axis. /// - public static Vector4 UnitZ = new Vector4(0, 0, 1, 0); + public static readonly Vector4 UnitZ = new Vector4(0, 0, 1, 0); /// /// Defines a unit-length Vector4 that points towards the W-axis. /// - public static Vector4 UnitW = new Vector4(0, 0, 0, 1); + public static readonly Vector4 UnitW = new Vector4(0, 0, 0, 1); /// /// Defines a zero-length Vector4. /// - public static Vector4 Zero = new Vector4(0, 0, 0, 0); + public static readonly Vector4 Zero = new Vector4(0, 0, 0, 0); /// /// Defines an instance with all components set to 1.
10 Source/OpenTK/Math/Vector4d.cs
 @@ -58,27 +58,27 @@ public struct Vector4d : IEquatable /// /// Defines a unit-length Vector4d that points towards the X-axis. /// - public static Vector4d UnitX = new Vector4d(1, 0, 0, 0); + public static readonly Vector4d UnitX = new Vector4d(1, 0, 0, 0); /// /// Defines a unit-length Vector4d that points towards the Y-axis. /// - public static Vector4d UnitY = new Vector4d(0, 1, 0, 0); + public static readonly Vector4d UnitY = new Vector4d(0, 1, 0, 0); /// /// Defines a unit-length Vector4d that points towards the Z-axis. /// - public static Vector4d UnitZ = new Vector4d(0, 0, 1, 0); + public static readonly Vector4d UnitZ = new Vector4d(0, 0, 1, 0); /// /// Defines a unit-length Vector4d that points towards the W-axis. /// - public static Vector4d UnitW = new Vector4d(0, 0, 0, 1); + public static readonly Vector4d UnitW = new Vector4d(0, 0, 0, 1); /// /// Defines a zero-length Vector4d. /// - public static Vector4d Zero = new Vector4d(0, 0, 0, 0); + public static readonly Vector4d Zero = new Vector4d(0, 0, 0, 0); /// /// Defines an instance with all components set to 1.