#region --- License ---
/*
Copyright (c) 2006 - 2008 The Open Toolkit library.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#endregion
using System;
using System.Runtime.InteropServices;
namespace OpenTK
{
///
/// Represents a 4x4 Matrix
///
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Matrix4 : IEquatable
{
#region Fields
///
/// Top row of the matrix
///
public Vector4 Row0;
///
/// 2nd row of the matrix
///
public Vector4 Row1;
///
/// 3rd row of the matrix
///
public Vector4 Row2;
///
/// Bottom row of the matrix
///
public Vector4 Row3;
///
/// The identity matrix
///
public static Matrix4 Identity = new Matrix4(Vector4.UnitX, Vector4.UnitY, Vector4.UnitZ, Vector4.UnitW);
#endregion
#region Constructors
///
/// Constructs a new instance.
///
/// Top row of the matrix
/// Second row of the matrix
/// Third row of the matrix
/// Bottom row of the matrix
public Matrix4(Vector4 row0, Vector4 row1, Vector4 row2, Vector4 row3)
{
Row0 = row0;
Row1 = row1;
Row2 = row2;
Row3 = row3;
}
///
/// Constructs a new instance.
///
/// First item of the first row of the matrix.
/// Second item of the first row of the matrix.
/// Third item of the first row of the matrix.
/// Fourth item of the first row of the matrix.
/// First item of the second row of the matrix.
/// Second item of the second row of the matrix.
/// Third item of the second row of the matrix.
/// Fourth item of the second row of the matrix.
/// First item of the third row of the matrix.
/// Second item of the third row of the matrix.
/// Third item of the third row of the matrix.
/// First item of the third row of the matrix.
/// Fourth item of the fourth row of the matrix.
/// Second item of the fourth row of the matrix.
/// Third item of the fourth row of the matrix.
/// Fourth item of the fourth row of the matrix.
public Matrix4(
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)
{
Row0 = new Vector4(m00, m01, m02, m03);
Row1 = new Vector4(m10, m11, m12, m13);
Row2 = new Vector4(m20, m21, m22, m23);
Row3 = new Vector4(m30, m31, m32, m33);
}
#endregion
#region Public Members
#region Properties
///
/// The determinant of this matrix
///
public float Determinant
{
get
{
return
Row0.X * Row1.Y * Row2.Z * Row3.W - Row0.X * Row1.Y * Row2.W * Row3.Z + Row0.X * Row1.Z * Row2.W * Row3.Y - Row0.X * Row1.Z * Row2.Y * Row3.W
+ Row0.X * Row1.W * Row2.Y * Row3.Z - Row0.X * Row1.W * Row2.Z * Row3.Y - Row0.Y * Row1.Z * Row2.W * Row3.X + Row0.Y * Row1.Z * Row2.X * Row3.W
- Row0.Y * Row1.W * Row2.X * Row3.Z + Row0.Y * Row1.W * Row2.Z * Row3.X - Row0.Y * Row1.X * Row2.Z * Row3.W + Row0.Y * Row1.X * Row2.W * Row3.Z
+ Row0.Z * Row1.W * Row2.X * Row3.Y - Row0.Z * Row1.W * Row2.Y * Row3.X + Row0.Z * Row1.X * Row2.Y * Row3.W - Row0.Z * Row1.X * Row2.W * Row3.Y
+ Row0.Z * Row1.Y * Row2.W * Row3.X - Row0.Z * Row1.Y * Row2.X * Row3.W - Row0.W * Row1.X * Row2.Y * Row3.Z + Row0.W * Row1.X * Row2.Z * Row3.Y
- Row0.W * Row1.Y * Row2.Z * Row3.X + Row0.W * Row1.Y * Row2.X * Row3.Z - Row0.W * Row1.Z * Row2.X * Row3.Y + Row0.W * Row1.Z * Row2.Y * Row3.X;
}
}
///
/// The first column of this matrix
///
public Vector4 Column0
{
get { return new Vector4(Row0.X, Row1.X, Row2.X, Row3.X); }
}
///
/// The second column of this matrix
///
public Vector4 Column1
{
get { return new Vector4(Row0.Y, Row1.Y, Row2.Y, Row3.Y); }
}
///
/// The third column of this matrix
///
public Vector4 Column2
{
get { return new Vector4(Row0.Z, Row1.Z, Row2.Z, Row3.Z); }
}
///
/// The fourth column of this matrix
///
public Vector4 Column3
{
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 2 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
#region public void Invert()
///
/// Converts this instance into its inverse.
///
public void Invert()
{
this = Matrix4.Invert(this);
}
#endregion
#region public void Transpose()
///
/// Converts this instance into its transpose.
///
public void Transpose()
{
this = Matrix4.Transpose(this);
}
#endregion
#endregion
#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(Vector3 axis, float angle, out Matrix4 result)
{
float cos = (float)System.Math.Cos(-angle);
float sin = (float)System.Math.Sin(-angle);
float t = 1.0f - cos;
axis.Normalize();
result = new Matrix4(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 Matrix4 CreateFromAxisAngle(Vector3 axis, float angle)
{
Matrix4 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(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
///
/// 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;
result.M44 = 1;
}
///
/// 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 CreatePerspectiveFieldOfView
///
/// Creates a perspective projection matrix.
///
/// Angle of the field of view in the y direction (in radians)
/// Aspect ratio of the view (width / height)
/// Distance to the near clip plane
/// Distance to the far clip plane
/// A projection matrix that transforms camera space to raster space
///
/// Thrown under the following conditions:
///
/// - fovy is zero, less than zero or larger than Math.PI
/// - aspect is negative or zero
/// - zNear is negative or zero
/// - zFar is negative or zero
/// - zNear is larger than zFar
///
///
public static void CreatePerspectiveFieldOfView(float fovy, float aspect, float zNear, float zFar, out Matrix4 result)
{
if (fovy <= 0 || fovy > Math.PI)
throw new ArgumentOutOfRangeException("fovy");
if (aspect <= 0)
throw new ArgumentOutOfRangeException("aspect");
if (zNear <= 0)
throw new ArgumentOutOfRangeException("zNear");
if (zFar <= 0)
throw new ArgumentOutOfRangeException("zFar");
float yMax = zNear * (float)System.Math.Tan(0.5f * fovy);
float yMin = -yMax;
float xMin = yMin * aspect;
float xMax = yMax * aspect;
CreatePerspectiveOffCenter(xMin, xMax, yMin, yMax, zNear, zFar, out result);
}
///
/// Creates a perspective projection matrix.
///
/// Angle of the field of view in the y direction (in radians)
/// Aspect ratio of the view (width / height)
/// Distance to the near clip plane
/// Distance to the far clip plane
/// A projection matrix that transforms camera space to raster space
///
/// Thrown under the following conditions:
///
/// - fovy is zero, less than zero or larger than Math.PI
/// - aspect is negative or zero
/// - zNear is negative or zero
/// - zFar is negative or zero
/// - zNear is larger than zFar
///
///
public static Matrix4 CreatePerspectiveFieldOfView(float fovy, float aspect, float zNear, float zFar)
{
Matrix4 result;
CreatePerspectiveFieldOfView(fovy, aspect, zNear, zFar, out result);
return result;
}
#endregion
#region CreatePerspectiveOffCenter
///
/// Creates an perspective projection matrix.
///
/// Left edge of the view frustum
/// Right edge of the view frustum
/// Bottom edge of the view frustum
/// Top edge of the view frustum
/// Distance to the near clip plane
/// Distance to the far clip plane
/// A projection matrix that transforms camera space to raster space
///
/// Thrown under the following conditions:
///
/// - zNear is negative or zero
/// - zFar is negative or zero
/// - zNear is larger than zFar
///
///
public static void CreatePerspectiveOffCenter(float left, float right, float bottom, float top, float zNear, float zFar, out Matrix4 result)
{
if (zNear <= 0)
throw new ArgumentOutOfRangeException("zNear");
if (zFar <= 0)
throw new ArgumentOutOfRangeException("zFar");
if (zNear >= zFar)
throw new ArgumentOutOfRangeException("zNear");
float x = (2.0f * zNear) / (right - left);
float y = (2.0f * zNear) / (top - bottom);
float a = (right + left) / (right - left);
float b = (top + bottom) / (top - bottom);
float c = -(zFar + zNear) / (zFar - zNear);
float d = -(2.0f * zFar * zNear) / (zFar - zNear);
result = new Matrix4(x, 0, 0, 0,
0, y, 0, 0,
a, b, c, -1,
0, 0, d, 0);
}
///
/// Creates an perspective projection matrix.
///
/// Left edge of the view frustum
/// Right edge of the view frustum
/// Bottom edge of the view frustum
/// Top edge of the view frustum
/// Distance to the near clip plane
/// Distance to the far clip plane
/// A projection matrix that transforms camera space to raster space
///
/// Thrown under the following conditions:
///
/// - zNear is negative or zero
/// - zFar is negative or zero
/// - zNear is larger than zFar
///
///
public static Matrix4 CreatePerspectiveOffCenter(float left, float right, float bottom, float top, float zNear, float zFar)
{
Matrix4 result;
CreatePerspectiveOffCenter(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
///
/// Build a scaling matrix
///
/// Single scale factor for x,y and z axes
/// A scaling matrix
public static Matrix4 Scale(float scale)
{
return Scale(scale, scale, scale);
}
///
/// Build a scaling matrix
///
/// Scale factors for x,y and z axes
/// A scaling matrix
public static Matrix4 Scale(Vector3 scale)
{
return Scale(scale.X, scale.Y, scale.Z);
}
///
/// Build a scaling matrix
///
/// Scale factor for x-axis
/// Scale factor for y-axis
/// Scale factor for z-axis
/// A scaling matrix
public static Matrix4 Scale(float x, float y, float z)
{
Matrix4 result;
result.Row0 = Vector4.UnitX * x;
result.Row1 = Vector4.UnitY * y;
result.Row2 = Vector4.UnitZ * z;
result.Row3 = Vector4.UnitW;
return result;
}
#endregion
#region Rotation Functions
///
/// Build a rotation matrix that rotates about the x-axis
///
/// angle in radians to rotate counter-clockwise around the x-axis
/// A rotation matrix
[Obsolete("Use CreateRotationX instead.")]
public static Matrix4 RotateX(float angle)
{
float cos = (float)System.Math.Cos(angle);
float sin = (float)System.Math.Sin(angle);
Matrix4 result;
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;
return result;
}
///
/// Build a rotation matrix that rotates about the y-axis
///
/// angle in radians to rotate counter-clockwise around the y-axis
/// A rotation matrix
[Obsolete("Use CreateRotationY instead.")]
public static Matrix4 RotateY(float angle)
{
float cos = (float)System.Math.Cos(angle);
float sin = (float)System.Math.Sin(angle);
Matrix4 result;
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;
return result;
}
///
/// Build a rotation matrix that rotates about the z-axis
///
/// angle in radians to rotate counter-clockwise around the z-axis
/// A rotation matrix
[Obsolete("Use CreateRotationZ instead.")]
public static Matrix4 RotateZ(float angle)
{
float cos = (float)System.Math.Cos(angle);
float sin = (float)System.Math.Sin(angle);
Matrix4 result;
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;
return result;
}
///
/// Build a rotation matrix to rotate about the given axis
///
/// the axis to rotate about
/// angle in radians to rotate counter-clockwise (looking in the direction of the given axis)
/// A rotation matrix
[Obsolete("Use CreateFromAxisAngle instead.")]
public static Matrix4 Rotate(Vector3 axis, float angle)
{
float cos = (float)System.Math.Cos(-angle);
float sin = (float)System.Math.Sin(-angle);
float t = 1.0f - cos;
axis.Normalize();
Matrix4 result;
result.Row0 = new Vector4(t * axis.X * axis.X + cos, t * axis.X * axis.Y - sin * axis.Z, t * axis.X * axis.Z + sin * axis.Y, 0.0f);
result.Row1 = new Vector4(t * axis.X * axis.Y + sin * axis.Z, t * axis.Y * axis.Y + cos, t * axis.Y * axis.Z - sin * axis.X, 0.0f);
result.Row2 = new Vector4(t * axis.X * axis.Z - sin * axis.Y, t * axis.Y * axis.Z + sin * axis.X, t * axis.Z * axis.Z + cos, 0.0f);
result.Row3 = Vector4.UnitW;
return result;
}
///
/// Build a rotation matrix from a quaternion
///
/// the quaternion
/// A rotation matrix
public static Matrix4 Rotate(Quaternion q)
{
Vector3 axis;
float angle;
q.ToAxisAngle(out axis, out angle);
return CreateFromAxisAngle(axis, angle);
}
#endregion
#region Camera Helper Functions
///
/// Build a world space to camera space matrix
///
/// Eye (camera) position in world space
/// Target position in world space
/// Up vector in world space (should not be parallel to the camera direction, that is target - eye)
/// A Matrix4 that transforms world space to camera space
public static Matrix4 LookAt(Vector3 eye, Vector3 target, Vector3 up)
{
Vector3 z = Vector3.Normalize(eye - target);
Vector3 x = Vector3.Normalize(Vector3.Cross(up, z));
Vector3 y = Vector3.Normalize(Vector3.Cross(z, x));
Matrix4 rot = new Matrix4(new Vector4(x.X, y.X, z.X, 0.0f),
new Vector4(x.Y, y.Y, z.Y, 0.0f),
new Vector4(x.Z, y.Z, z.Z, 0.0f),
Vector4.UnitW);
Matrix4 trans = Matrix4.CreateTranslation(-eye);
return trans * rot;
}
///
/// Build a world space to camera space matrix
///
/// Eye (camera) position in world space
/// Eye (camera) position in world space
/// Eye (camera) position in world space
/// Target position in world space
/// Target position in world space
/// Target position in world space
/// Up vector in world space (should not be parallel to the camera direction, that is target - eye)
/// Up vector in world space (should not be parallel to the camera direction, that is target - eye)
/// Up vector in world space (should not be parallel to the camera direction, that is target - eye)
/// A Matrix4 that transforms world space to camera space
public static Matrix4 LookAt(float eyeX, float eyeY, float eyeZ, float targetX, float targetY, float targetZ, float upX, float upY, float upZ)
{
return LookAt(new Vector3(eyeX, eyeY, eyeZ), new Vector3(targetX, targetY, targetZ), new Vector3(upX, upY, upZ));
}
///
/// Build a projection matrix
///
/// Left edge of the view frustum
/// Right edge of the view frustum
/// Bottom edge of the view frustum
/// Top edge of the view frustum
/// Distance to the near clip plane
/// Distance to the far clip plane
/// A projection matrix that transforms camera space to raster space
[Obsolete("Use CreatePerspectiveOffCenter instead.")]
public static Matrix4 Frustum(float left, float right, float bottom, float top, float near, float far)
{
float invRL = 1.0f / (right - left);
float invTB = 1.0f / (top - bottom);
float invFN = 1.0f / (far - near);
return new Matrix4(new Vector4(2.0f * near * invRL, 0.0f, 0.0f, 0.0f),
new Vector4(0.0f, 2.0f * near * invTB, 0.0f, 0.0f),
new Vector4((right + left) * invRL, (top + bottom) * invTB, -(far + near) * invFN, -1.0f),
new Vector4(0.0f, 0.0f, -2.0f * far * near * invFN, 0.0f));
}
///
/// Build a projection matrix
///
/// Angle of the field of view in the y direction (in radians)
/// Aspect ratio of the view (width / height)
/// Distance to the near clip plane
/// Distance to the far clip plane
/// A projection matrix that transforms camera space to raster space
[Obsolete("Use CreatePerspectiveFieldOfView instead.")]
public static Matrix4 Perspective(float fovy, float aspect, float near, float far)
{
float yMax = near * (float)System.Math.Tan(0.5f * fovy);
float yMin = -yMax;
float xMin = yMin * aspect;
float xMax = yMax * aspect;
return Frustum(xMin, xMax, yMin, yMax, near, far);
}
#endregion
#region Multiply Functions
///
/// Multiplies two instances.
///
/// The left operand of the multiplication.
/// The right operand of the multiplication.
/// A new instance that is the result of the multiplication
public static Matrix4 Mult(Matrix4 left, Matrix4 right)
{
Matrix4 result;
Mult(ref left, ref right, out result);
return result;
}
///
/// Multiplies two instances.
///
/// 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 Matrix4 left, ref Matrix4 right, out Matrix4 result)
{
result = new Matrix4(
left.M11 * right.M11 + left.M12 * right.M21 + left.M13 * right.M31 + left.M14 * right.M41,
left.M11 * right.M12 + left.M12 * right.M22 + left.M13 * right.M32 + left.M14 * right.M42,
left.M11 * right.M13 + left.M12 * right.M23 + left.M13 * right.M33 + left.M14 * right.M43,
left.M11 * right.M14 + left.M12 * right.M24 + left.M13 * right.M34 + left.M14 * right.M44,
left.M21 * right.M11 + left.M22 * right.M21 + left.M23 * right.M31 + left.M24 * right.M41,
left.M21 * right.M12 + left.M22 * right.M22 + left.M23 * right.M32 + left.M24 * right.M42,
left.M21 * right.M13 + left.M22 * right.M23 + left.M23 * right.M33 + left.M24 * right.M43,
left.M21 * right.M14 + left.M22 * right.M24 + left.M23 * right.M34 + left.M24 * right.M44,
left.M31 * right.M11 + left.M32 * right.M21 + left.M33 * right.M31 + left.M34 * right.M41,
left.M31 * right.M12 + left.M32 * right.M22 + left.M33 * right.M32 + left.M34 * right.M42,
left.M31 * right.M13 + left.M32 * right.M23 + left.M33 * right.M33 + left.M34 * right.M43,
left.M31 * right.M14 + left.M32 * right.M24 + left.M33 * right.M34 + left.M34 * right.M44,
left.M41 * right.M11 + left.M42 * right.M21 + left.M43 * right.M31 + left.M44 * right.M41,
left.M41 * right.M12 + left.M42 * right.M22 + left.M43 * right.M32 + left.M44 * right.M42,
left.M41 * right.M13 + left.M42 * right.M23 + left.M43 * right.M33 + left.M44 * right.M43,
left.M41 * right.M14 + left.M42 * right.M24 + left.M43 * right.M34 + left.M44 * right.M44);
}
#endregion
#region Invert Functions
///
/// Calculate the inverse of the given matrix
///
/// The matrix to invert
/// The inverse of the given matrix if it has one, or the input if it is singular
/// Thrown if the Matrix4 is singular.
public static Matrix4 Invert(Matrix4 mat)
{
int[] colIdx = { 0, 0, 0, 0 };
int[] rowIdx = { 0, 0, 0, 0 };
int[] pivotIdx = { -1, -1, -1, -1 };
// convert the matrix to an array for easy looping
float[,] inverse = {{mat.Row0.X, mat.Row0.Y, mat.Row0.Z, mat.Row0.W},
{mat.Row1.X, mat.Row1.Y, mat.Row1.Z, mat.Row1.W},
{mat.Row2.X, mat.Row2.Y, mat.Row2.Z, mat.Row2.W},
{mat.Row3.X, mat.Row3.Y, mat.Row3.Z, mat.Row3.W} };
int icol = 0;
int irow = 0;
for (int i = 0; i < 4; i++)
{
// Find the largest pivot value
float maxPivot = 0.0f;
for (int j = 0; j < 4; j++)
{
if (pivotIdx[j] != 0)
{
for (int k = 0; k < 4; ++k)
{
if (pivotIdx[k] == -1)
{
float absVal = System.Math.Abs(inverse[j, k]);
if (absVal > maxPivot)
{
maxPivot = absVal;
irow = j;
icol = k;
}
}
else if (pivotIdx[k] > 0)
{
return mat;
}
}
}
}
++(pivotIdx[icol]);
// Swap rows over so pivot is on diagonal
if (irow != icol)
{
for (int k = 0; k < 4; ++k)
{
float f = inverse[irow, k];
inverse[irow, k] = inverse[icol, k];
inverse[icol, k] = f;
}
}
rowIdx[i] = irow;
colIdx[i] = icol;
float pivot = inverse[icol, icol];
// check for singular matrix
if (pivot == 0.0f)
{
throw new InvalidOperationException("Matrix is singular and cannot be inverted.");
//return mat;
}
// Scale row so it has a unit diagonal
float oneOverPivot = 1.0f / pivot;
inverse[icol, icol] = 1.0f;
for (int k = 0; k < 4; ++k)
inverse[icol, k] *= oneOverPivot;
// Do elimination of non-diagonal elements
for (int j = 0; j < 4; ++j)
{
// check this isn't on the diagonal
if (icol != j)
{
float f = inverse[j, icol];
inverse[j, icol] = 0.0f;
for (int k = 0; k < 4; ++k)
inverse[j, k] -= inverse[icol, k] * f;
}
}
}
for (int j = 3; j >= 0; --j)
{
int ir = rowIdx[j];
int ic = colIdx[j];
for (int k = 0; k < 4; ++k)
{
float f = inverse[k, ir];
inverse[k, ir] = inverse[k, ic];
inverse[k, ic] = f;
}
}
mat.Row0 = new Vector4(inverse[0, 0], inverse[0, 1], inverse[0, 2], inverse[0, 3]);
mat.Row1 = new Vector4(inverse[1, 0], inverse[1, 1], inverse[1, 2], inverse[1, 3]);
mat.Row2 = new Vector4(inverse[2, 0], inverse[2, 1], inverse[2, 2], inverse[2, 3]);
mat.Row3 = new Vector4(inverse[3, 0], inverse[3, 1], inverse[3, 2], inverse[3, 3]);
return mat;
}
#endregion
#region Transpose
///
/// Calculate the transpose of the given matrix
///
/// The matrix to transpose
/// The transpose of the given matrix
public static Matrix4 Transpose(Matrix4 mat)
{
return new Matrix4(mat.Column0, mat.Column1, mat.Column2, mat.Column3);
}
///
/// Calculate the transpose of the given matrix
///
/// The matrix to transpose
/// The result of the calculation
public static void Transpose(ref Matrix4 mat, out Matrix4 result)
{
result.Row0 = mat.Column0;
result.Row1 = mat.Column1;
result.Row2 = mat.Column2;
result.Row3 = mat.Column3;
}
#endregion
#endregion
#region Operators
///
/// Matrix multiplication
///
/// left-hand operand
/// right-hand operand
/// A new Matrix44 which holds the result of the multiplication
public static Matrix4 operator *(Matrix4 left, Matrix4 right)
{
return Matrix4.Mult(left, right);
}
///
/// Compares two instances for equality.
///
/// The first instance.
/// The second instance.
/// True, if left equals right; false otherwise.
public static bool operator ==(Matrix4 left, Matrix4 right)
{
return left.Equals(right);
}
///
/// Compares two instances for inequality.
///
/// The first instance.
/// The second instance.
/// True, if left does not equal right; false otherwise.
public static bool operator !=(Matrix4 left, Matrix4 right)
{
return !left.Equals(right);
}
#endregion
#region Overrides
#region public override string ToString()
///
/// Returns a System.String that represents the current Matrix44.
///
///
public override string ToString()
{
return String.Format("{0}\n{1}\n{2}\n{3}", Row0, Row1, Row2, Row3);
}
#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() ^ Row3.GetHashCode();
}
#endregion
#region public override bool Equals(object obj)
///
/// Indicates whether this instance and a specified object are equal.
///
/// The object to compare tresult.
/// True if the instances are equal; false otherwise.
public override bool Equals(object obj)
{
if (!(obj is Matrix4))
return false;
return this.Equals((Matrix4)obj);
}
#endregion
#endregion
#endregion
#region IEquatable Members
/// Indicates whether the current matrix is equal to another matrix.
/// An matrix to compare with this matrix.
/// true if the current matrix is equal to the matrix parameter; otherwise, false.
public bool Equals(Matrix4 other)
{
return
Row0 == other.Row0 &&
Row1 == other.Row1 &&
Row2 == other.Row2 &&
Row3 == other.Row3;
}
#endregion
}
}