From 42374a73d4caae0fe2b526e96a10dea3baa7648e Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Thu, 25 Jun 2009 22:47:01 +0000 Subject: [PATCH] Obsoleted Frustum and Perspective methods in favor of CreatePerspectiveOffCenter and CreatePerspectiveFieldOfView, respectively. --- Source/OpenTK/Math/Matrix4.cs | 139 ++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/Source/OpenTK/Math/Matrix4.cs b/Source/OpenTK/Math/Matrix4.cs index 8c896df3..81a56f4e 100644 --- a/Source/OpenTK/Math/Matrix4.cs +++ b/Source/OpenTK/Math/Matrix4.cs @@ -403,6 +403,143 @@ namespace OpenTK } #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"); + if (zNear >= zFar) + throw new ArgumentOutOfRangeException("zNear"); + + 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 @@ -607,6 +744,7 @@ namespace OpenTK /// 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); @@ -626,6 +764,7 @@ namespace OpenTK /// 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);