diff --git a/include/omath/linear_algebra/mat.hpp b/include/omath/linear_algebra/mat.hpp index d8fc0333..d82dea5d 100644 --- a/include/omath/linear_algebra/mat.hpp +++ b/include/omath/linear_algebra/mat.hpp @@ -677,11 +677,13 @@ namespace omath {0.f, 1.f / fov_half_tan, 0.f, 0.f}, {0.f, 0.f, far / (far - near), -(near * far) / (far - near)}, {0.f, 0.f, 1.f, 0.f}}; - else + else if constexpr (DepthRange == NDCDepthRange::NEGATIVE_ONE_TO_ONE) return {{1.f / (aspect_ratio * fov_half_tan), 0.f, 0.f, 0.f}, {0.f, 1.f / fov_half_tan, 0.f, 0.f}, {0.f, 0.f, (far + near) / (far - near), -(2.f * near * far) / (far - near)}, {0.f, 0.f, 1.f, 0.f}}; + else + std::unreachable(); } template @@ -717,7 +721,7 @@ namespace omath { 0.f, 0.f, static_cast(1) / (far - near), -near / (far - near) }, { 0.f, 0.f, 0.f, 1.f } }; - else + else if constexpr (DepthRange == NDCDepthRange::NEGATIVE_ONE_TO_ONE) return { { static_cast(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)}, @@ -725,6 +729,8 @@ namespace omath { 0.f, 0.f, static_cast(2) / (far - near), -(far + near) / (far - near) }, { 0.f, 0.f, 0.f, 1.f } }; + else + std::unreachable(); } template @@ -740,7 +746,7 @@ namespace omath { 0.f, 0.f, -static_cast(1) / (far - near), -near / (far - near) }, { 0.f, 0.f, 0.f, 1.f } }; - else + else if constexpr (DepthRange == NDCDepthRange::NEGATIVE_ONE_TO_ONE) return { { static_cast(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)}, @@ -748,6 +754,8 @@ namespace omath { 0.f, 0.f, -static_cast(2) / (far - near), -(far + near) / (far - near) }, { 0.f, 0.f, 0.f, 1.f } }; + else + std::unreachable(); } template Mat<4, 4, T, St> mat_look_at_left_handed(const Vector3& eye, const Vector3& center, const Vector3& up) diff --git a/source/engines/cry_engine/formulas.cpp b/source/engines/cry_engine/formulas.cpp index 4d5bc94e..301016b9 100644 --- a/source/engines/cry_engine/formulas.cpp +++ b/source/engines/cry_engine/formulas.cpp @@ -41,6 +41,9 @@ namespace omath::cry_engine return mat_perspective_left_handed( field_of_view, aspect_ratio, near, far); - return mat_perspective_left_handed(field_of_view, aspect_ratio, near, far); + if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE) + return mat_perspective_left_handed( + field_of_view, aspect_ratio, near, far); + std::unreachable(); } } // namespace omath::unity_engine diff --git a/source/engines/frostbite_engine/formulas.cpp b/source/engines/frostbite_engine/formulas.cpp index 2c0d6cc4..c3193cb1 100644 --- a/source/engines/frostbite_engine/formulas.cpp +++ b/source/engines/frostbite_engine/formulas.cpp @@ -41,6 +41,10 @@ namespace omath::frostbite_engine return mat_perspective_left_handed( field_of_view, aspect_ratio, near, far); - return mat_perspective_left_handed(field_of_view, aspect_ratio, near, far); + if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE) + return mat_perspective_left_handed( + field_of_view, aspect_ratio, near, far); + + std::unreachable(); } } // namespace omath::unity_engine diff --git a/source/engines/iw_engine/formulas.cpp b/source/engines/iw_engine/formulas.cpp index ca59ecd6..e7ab4b88 100644 --- a/source/engines/iw_engine/formulas.cpp +++ b/source/engines/iw_engine/formulas.cpp @@ -50,12 +50,13 @@ namespace omath::iw_engine {0, 0, far / (far - near), -(near * far) / (far - near)}, {0, 0, 1, 0}, }; - - return { - {1.f / (aspect_ratio * fov_half_tan), 0, 0, 0}, - {0, 1.f / (fov_half_tan), 0, 0}, - {0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)}, - {0, 0, 1, 0}, - }; + if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE) + return { + {1.f / (aspect_ratio * fov_half_tan), 0, 0, 0}, + {0, 1.f / (fov_half_tan), 0, 0}, + {0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)}, + {0, 0, 1, 0}, + }; + std::unreachable(); }; } // namespace omath::iw_engine diff --git a/source/engines/opengl_engine/formulas.cpp b/source/engines/opengl_engine/formulas.cpp index 2f936acc..93eec900 100644 --- a/source/engines/opengl_engine/formulas.cpp +++ b/source/engines/opengl_engine/formulas.cpp @@ -8,15 +8,15 @@ namespace omath::opengl_engine Vector3 forward_vector(const ViewAngles& angles) noexcept { - const auto vec - = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward); + const auto vec = + rotation_matrix(angles) * mat_column_from_vector(k_abs_forward); return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)}; } Vector3 right_vector(const ViewAngles& angles) noexcept { - const auto vec - = rotation_matrix(angles) * mat_column_from_vector(k_abs_right); + const auto vec = + rotation_matrix(angles) * mat_column_from_vector(k_abs_right); return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)}; } @@ -28,7 +28,7 @@ namespace omath::opengl_engine } Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3& cam_origin) noexcept { - return mat_look_at_right_handed(cam_origin, cam_origin+forward_vector(angles), up_vector(angles)); + return mat_look_at_right_handed(cam_origin, cam_origin + forward_vector(angles), up_vector(angles)); } Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept { @@ -39,21 +39,14 @@ namespace omath::opengl_engine Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near, const float far, const NDCDepthRange ndc_depth_range) noexcept { - const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f); + if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE) + return mat_perspective_right_handed( + field_of_view, aspect_ratio, near, far); if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE) - return { - {1.f / (aspect_ratio * fov_half_tan), 0, 0, 0}, - {0, 1.f / (fov_half_tan), 0, 0}, - {0, 0, -far / (far - near), -(near * far) / (far - near)}, - {0, 0, -1, 0}, - }; + return mat_perspective_right_handed( + field_of_view, aspect_ratio, near, far); - return { - {1.f / (aspect_ratio * fov_half_tan), 0, 0, 0}, - {0, 1.f / (fov_half_tan), 0, 0}, - {0, 0, -(far + near) / (far - near), -(2.f * far * near) / (far - near)}, - {0, 0, -1, 0}, - }; + std::unreachable(); } } // namespace omath::opengl_engine diff --git a/source/engines/source_engine/formulas.cpp b/source/engines/source_engine/formulas.cpp index 0cf80842..85f70552 100644 --- a/source/engines/source_engine/formulas.cpp +++ b/source/engines/source_engine/formulas.cpp @@ -50,12 +50,13 @@ namespace omath::source_engine {0, 0, far / (far - near), -(near * far) / (far - near)}, {0, 0, 1, 0}, }; - - return { - {1.f / (aspect_ratio * fov_half_tan), 0, 0, 0}, - {0, 1.f / (fov_half_tan), 0, 0}, - {0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)}, - {0, 0, 1, 0}, - }; + if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE) + return { + {1.f / (aspect_ratio * fov_half_tan), 0, 0, 0}, + {0, 1.f / (fov_half_tan), 0, 0}, + {0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)}, + {0, 0, 1, 0}, + }; + std::unreachable(); } } // namespace omath::source_engine diff --git a/source/engines/unity_engine/formulas.cpp b/source/engines/unity_engine/formulas.cpp index 7e955258..51e4c4c8 100644 --- a/source/engines/unity_engine/formulas.cpp +++ b/source/engines/unity_engine/formulas.cpp @@ -40,7 +40,10 @@ namespace omath::unity_engine if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE) return omath::mat_perspective_right_handed( field_of_view, aspect_ratio, near, far); - - return omath::mat_perspective_right_handed(field_of_view, aspect_ratio, near, far); + if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE) + return omath::mat_perspective_right_handed(field_of_view, aspect_ratio, + near, far); + std::unreachable(); } } // namespace omath::unity_engine