Skip to content

Commit

Permalink
[Fix] Projection matrix for Vulkan and DX12
Browse files Browse the repository at this point in the history
  • Loading branch information
mrouffet committed Feb 25, 2024
1 parent fcf0a54 commit 9b13e57
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Include/SA/Maths/Matrix/Matrix4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ namespace SA
*
* \return perspective matrix.
*/
static Mat4 MakePerspective(T _fov = T(90.0), T _aspect = T(1.0), T _near = T(0.35), T _far = T(10.0), bool _bInvertedZ = false) noexcept;
static Mat4 MakePerspective(T _fov = T(90.0), T _aspect = T(1.0), T _near = T(0.35), T _far = T(10.0)) noexcept;

//}

Expand Down
38 changes: 13 additions & 25 deletions Include/SA/Maths/Matrix/Matrix4.inl
Original file line number Diff line number Diff line change
Expand Up @@ -551,34 +551,22 @@ namespace SA


template <typename T, MatrixMajor major>
Mat4<T, major> Mat4<T, major>::MakePerspective(T _fov, T _aspect, T _near, T _far, bool _bInvertedZ) noexcept
Mat4<T, major> Mat4<T, major>::MakePerspective(T _fov, T _aspect, T _near, T _far) noexcept
{
const T focalLength = T(1) / std::tan(Maths::DegToRad<T> * _fov / T(2));

if (_bInvertedZ)
{
//const T A = _near / (_far - _near);
//const T B = (_far * _near) / (_far - _near);

return Mat4(
_aspect / focalLength, 0, 0, 0,
0, T(1) / focalLength, 0, 0,
0, 0, 0, (_far - _near) / (_far * _near) /* 1 / B */,
0, 0, -1, (_far - _near) / (_far * _far - _far) /* A / B */
);

}
else
{
// GLM implementation.

return Mat4(
focalLength / _aspect, 0, 0, 0,
0, focalLength, 0, 0,
0, 0, -(_far + _near) / (_far - _near), -(2 * _far * _near) / (_far - _near),
0, 0, -1, 0
);
}
/**
* Vulkan/DX12 matrix
* Sources: https://youtu.be/U0_ONQQ5ZNM
* https://computergraphics.stackexchange.com/questions/12448/vulkan-perspective-matrix-vs-opengl-perspective-matrix
*/

return Mat4(
focalLength / _aspect, 0, 0, 0,
0, focalLength, 0, 0,
0, 0, _far / (_far - _near), -(_far * _near) / (_far - _near),
0, 0, 1, 0
);
}

//}
Expand Down

0 comments on commit 9b13e57

Please sign in to comment.