Skip to content
This repository has been archived by the owner on Mar 30, 2019. It is now read-only.

Commit

Permalink
Replaced perspective view matrix calculation
Browse files Browse the repository at this point in the history
Replaced perspective view matrix calculation when given FOV and aspect
ratio. The new implementation does not calculate the view rectangle, so
the result matrix is still valid when znear = 0.
  • Loading branch information
sakumamayu committed Mar 14, 2016
1 parent 23bfaa5 commit 707244f
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions Source/SharpDX.Mathematics/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2291,13 +2291,15 @@ public static Matrix PerspectiveRH(float width, float height, float znear, float
/// <param name="result">When the method completes, contains the created projection matrix.</param>
public static void PerspectiveFovLH(float fov, float aspect, float znear, float zfar, out Matrix result)
{
float yScale = (float)(1.0 / Math.Tan(fov * 0.5f));
float xScale = yScale / aspect;
float yScale = (float)(1.0f / Math.Tan(fov * 0.5f));
float q = zfar / (zfar - znear);

float halfWidth = znear / xScale;
float halfHeight = znear / yScale;

PerspectiveOffCenterLH(-halfWidth, halfWidth, -halfHeight, halfHeight, znear, zfar, out result);
result = new Matrix();
result.M11 = yScale / aspect;
result.M22 = yScale;
result.M33 = q;
result.M34 = 1.0f;
result.M43 = -q * znear;
}

/// <summary>
Expand Down Expand Up @@ -2325,13 +2327,15 @@ public static Matrix PerspectiveFovLH(float fov, float aspect, float znear, floa
/// <param name="result">When the method completes, contains the created projection matrix.</param>
public static void PerspectiveFovRH(float fov, float aspect, float znear, float zfar, out Matrix result)
{
float yScale = (float)(1.0 / Math.Tan(fov * 0.5f));
float xScale = yScale / aspect;
float yScale = (float)(1.0f / Math.Tan(fov * 0.5f));
float q = zfar / (znear - zfar);

float halfWidth = znear / xScale;
float halfHeight = znear / yScale;

PerspectiveOffCenterRH(-halfWidth, halfWidth, -halfHeight, halfHeight, znear, zfar, out result);
result = new Matrix();
result.M11 = yScale / aspect;
result.M22 = yScale;
result.M33 = q;
result.M34 = -1.0f;
result.M43 = q * znear;
}

/// <summary>
Expand Down

0 comments on commit 707244f

Please sign in to comment.