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

Commit 707244f

Browse files
committed
Replaced perspective view matrix calculation
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.
1 parent 23bfaa5 commit 707244f

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

Source/SharpDX.Mathematics/Matrix.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,13 +2291,15 @@ public static Matrix PerspectiveRH(float width, float height, float znear, float
22912291
/// <param name="result">When the method completes, contains the created projection matrix.</param>
22922292
public static void PerspectiveFovLH(float fov, float aspect, float znear, float zfar, out Matrix result)
22932293
{
2294-
float yScale = (float)(1.0 / Math.Tan(fov * 0.5f));
2295-
float xScale = yScale / aspect;
2294+
float yScale = (float)(1.0f / Math.Tan(fov * 0.5f));
2295+
float q = zfar / (zfar - znear);
22962296

2297-
float halfWidth = znear / xScale;
2298-
float halfHeight = znear / yScale;
2299-
2300-
PerspectiveOffCenterLH(-halfWidth, halfWidth, -halfHeight, halfHeight, znear, zfar, out result);
2297+
result = new Matrix();
2298+
result.M11 = yScale / aspect;
2299+
result.M22 = yScale;
2300+
result.M33 = q;
2301+
result.M34 = 1.0f;
2302+
result.M43 = -q * znear;
23012303
}
23022304

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

2331-
float halfWidth = znear / xScale;
2332-
float halfHeight = znear / yScale;
2333-
2334-
PerspectiveOffCenterRH(-halfWidth, halfWidth, -halfHeight, halfHeight, znear, zfar, out result);
2333+
result = new Matrix();
2334+
result.M11 = yScale / aspect;
2335+
result.M22 = yScale;
2336+
result.M33 = q;
2337+
result.M34 = -1.0f;
2338+
result.M43 = q * znear;
23352339
}
23362340

23372341
/// <summary>

0 commit comments

Comments
 (0)