Skip to content

Commit 2c698b2

Browse files
committed
Minor projection matrix improvements
1 parent 025a1a1 commit 2c698b2

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

Diff for: source/Matrices/TMatrix.inl

+16-8
Original file line numberDiff line numberDiff line change
@@ -854,15 +854,23 @@ namespace Langulus::A
854854
CT::ScalarBased auto far
855855
) {
856856
using T = Lossless<decltype(aspect), decltype(near), decltype(far)>;
857+
// https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/opengl-perspective-projection-matrix.html
858+
const T scale = ::std::tan(T {fieldOfView.GetRadians()} * T {0.5}) * near;
859+
const T r = scale;
860+
const T l = -r;
861+
const T t = scale / aspect;
862+
const T b = -t;
863+
857864
auto result = Math::TMatrix<T, 4>::Null();
858-
const T t = ::std::tan(T {fieldOfView.GetRadians()} * T {0.5});
859-
const T r = far - near;
865+
result.mArray[0] = T {2} * near / (r - l);
866+
result.mArray[5] = T {2} * near / (t - b);
860867

861-
result.mArray[0] = T {1} / t;
862-
result.mArray[5] = aspect / t;
863-
result.mArray[10] = -(far + near) / r;
868+
result.mArray[8] = (r + l) / (r - l);
869+
result.mArray[9] = (t + b) / (t - b);
870+
result.mArray[10] = - (far + near) / (far - near);
864871
result.mArray[11] = T {-1};
865-
result.mArray[14] = T {-2} * far * near / r;
872+
873+
result.mArray[14] = T {-2} * far * near / (far - near);
866874
return result;
867875
}
868876

@@ -884,12 +892,12 @@ namespace Langulus::A
884892
const auto d = T {-2} * far * near / (far - near);
885893

886894
result[ 0] = x;
887-
result[ 8] = a;
888895
result[ 5] = y;
896+
result[ 8] = a;
889897
result[ 9] = b;
890898
result[10] = c;
891-
result[14] = d;
892899
result[11] = -1;
900+
result[14] = d;
893901
return result;
894902
}
895903

0 commit comments

Comments
 (0)