From 4b6db0c402c1837f05e2bb04a34c4c27a7938c0b Mon Sep 17 00:00:00 2001 From: Orange Date: Mon, 23 Mar 2026 23:36:19 +0300 Subject: [PATCH 1/3] updated z range --- include/omath/engines/cry_engine/formulas.hpp | 3 +- .../cry_engine/traits/camera_trait.hpp | 2 +- .../engines/frostbite_engine/formulas.hpp | 3 +- .../frostbite_engine/traits/camera_trait.hpp | 2 +- include/omath/engines/iw_engine/formulas.hpp | 3 +- .../engines/iw_engine/traits/camera_trait.hpp | 2 +- .../omath/engines/opengl_engine/formulas.hpp | 3 +- .../opengl_engine/traits/camera_trait.hpp | 2 +- .../omath/engines/source_engine/formulas.hpp | 3 +- .../source_engine/traits/camera_trait.hpp | 2 +- .../omath/engines/unity_engine/formulas.hpp | 3 +- .../unity_engine/traits/camera_trait.hpp | 2 +- .../omath/engines/unreal_engine/formulas.hpp | 3 +- .../unreal_engine/traits/camera_trait.hpp | 2 +- include/omath/linear_algebra/mat.hpp | 92 +++++-- include/omath/projection/camera.hpp | 75 ++++- source/engines/cry_engine/formulas.cpp | 6 +- .../cry_engine/traits/camera_trait.cpp | 5 +- source/engines/frostbite_engine/formulas.cpp | 6 +- .../frostbite_engine/traits/camera_trait.cpp | 5 +- source/engines/iw_engine/formulas.cpp | 10 +- .../engines/iw_engine/traits/camera_trait.cpp | 5 +- source/engines/opengl_engine/formulas.cpp | 10 +- .../opengl_engine/traits/camera_trait.cpp | 5 +- source/engines/source_engine/formulas.cpp | 10 +- .../source_engine/traits/camera_trait.cpp | 5 +- source/engines/unity_engine/formulas.cpp | 6 +- .../unity_engine/traits/camera_trait.cpp | 5 +- source/engines/unreal_engine/formulas.cpp | 6 +- .../unreal_engine/traits/camera_trait.cpp | 5 +- tests/engines/unit_test_traits_engines.cpp | 258 +++++++++++++++++- tests/general/unit_test_mat.cpp | 122 +++++++++ 32 files changed, 592 insertions(+), 79 deletions(-) diff --git a/include/omath/engines/cry_engine/formulas.hpp b/include/omath/engines/cry_engine/formulas.hpp index 7a7c8152..555a5f68 100644 --- a/include/omath/engines/cry_engine/formulas.hpp +++ b/include/omath/engines/cry_engine/formulas.hpp @@ -22,7 +22,8 @@ namespace omath::cry_engine Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; [[nodiscard]] - Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far) noexcept; + Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far, + NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept; template requires std::is_floating_point_v diff --git a/include/omath/engines/cry_engine/traits/camera_trait.hpp b/include/omath/engines/cry_engine/traits/camera_trait.hpp index e59f2a9c..b7770af2 100644 --- a/include/omath/engines/cry_engine/traits/camera_trait.hpp +++ b/include/omath/engines/cry_engine/traits/camera_trait.hpp @@ -18,7 +18,7 @@ namespace omath::cry_engine static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3& cam_origin) noexcept; [[nodiscard]] static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, - float near, float far) noexcept; + float near, float far, NDCDepthRange ndc_depth_range) noexcept; }; } // namespace omath::cry_engine \ No newline at end of file diff --git a/include/omath/engines/frostbite_engine/formulas.hpp b/include/omath/engines/frostbite_engine/formulas.hpp index 49df685d..6e9aa62c 100644 --- a/include/omath/engines/frostbite_engine/formulas.hpp +++ b/include/omath/engines/frostbite_engine/formulas.hpp @@ -22,7 +22,8 @@ namespace omath::frostbite_engine Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; [[nodiscard]] - Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far) noexcept; + Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far, + NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept; template requires std::is_floating_point_v diff --git a/include/omath/engines/frostbite_engine/traits/camera_trait.hpp b/include/omath/engines/frostbite_engine/traits/camera_trait.hpp index 8152178d..99a46aeb 100644 --- a/include/omath/engines/frostbite_engine/traits/camera_trait.hpp +++ b/include/omath/engines/frostbite_engine/traits/camera_trait.hpp @@ -18,7 +18,7 @@ namespace omath::frostbite_engine static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3& cam_origin) noexcept; [[nodiscard]] static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, - float near, float far) noexcept; + float near, float far, NDCDepthRange ndc_depth_range) noexcept; }; } // namespace omath::unreal_engine \ No newline at end of file diff --git a/include/omath/engines/iw_engine/formulas.hpp b/include/omath/engines/iw_engine/formulas.hpp index 849f02cc..cb4ea722 100644 --- a/include/omath/engines/iw_engine/formulas.hpp +++ b/include/omath/engines/iw_engine/formulas.hpp @@ -22,7 +22,8 @@ namespace omath::iw_engine [[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3& cam_origin) noexcept; [[nodiscard]] - Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far) noexcept; + Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far, + NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept; template requires std::is_floating_point_v diff --git a/include/omath/engines/iw_engine/traits/camera_trait.hpp b/include/omath/engines/iw_engine/traits/camera_trait.hpp index 88c21569..964b8c49 100644 --- a/include/omath/engines/iw_engine/traits/camera_trait.hpp +++ b/include/omath/engines/iw_engine/traits/camera_trait.hpp @@ -18,7 +18,7 @@ namespace omath::iw_engine static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3& cam_origin) noexcept; [[nodiscard]] static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, - float near, float far) noexcept; + float near, float far, NDCDepthRange ndc_depth_range) noexcept; }; } // namespace omath::iw_engine \ No newline at end of file diff --git a/include/omath/engines/opengl_engine/formulas.hpp b/include/omath/engines/opengl_engine/formulas.hpp index ed1547f2..4f9691b4 100644 --- a/include/omath/engines/opengl_engine/formulas.hpp +++ b/include/omath/engines/opengl_engine/formulas.hpp @@ -21,7 +21,8 @@ namespace omath::opengl_engine Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; [[nodiscard]] - Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far) noexcept; + Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far, + NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept; template requires std::is_floating_point_v diff --git a/include/omath/engines/opengl_engine/traits/camera_trait.hpp b/include/omath/engines/opengl_engine/traits/camera_trait.hpp index 3fb57c07..e7c1f4bc 100644 --- a/include/omath/engines/opengl_engine/traits/camera_trait.hpp +++ b/include/omath/engines/opengl_engine/traits/camera_trait.hpp @@ -18,7 +18,7 @@ namespace omath::opengl_engine static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3& cam_origin) noexcept; [[nodiscard]] static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, - float near, float far) noexcept; + float near, float far, NDCDepthRange ndc_depth_range) noexcept; }; } // namespace omath::opengl_engine \ No newline at end of file diff --git a/include/omath/engines/source_engine/formulas.hpp b/include/omath/engines/source_engine/formulas.hpp index e6416329..cbf9c67b 100644 --- a/include/omath/engines/source_engine/formulas.hpp +++ b/include/omath/engines/source_engine/formulas.hpp @@ -21,7 +21,8 @@ namespace omath::source_engine [[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3& cam_origin) noexcept; [[nodiscard]] - Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far) noexcept; + Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far, + NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept; template requires std::is_floating_point_v diff --git a/include/omath/engines/source_engine/traits/camera_trait.hpp b/include/omath/engines/source_engine/traits/camera_trait.hpp index d027d25e..5ecb1929 100644 --- a/include/omath/engines/source_engine/traits/camera_trait.hpp +++ b/include/omath/engines/source_engine/traits/camera_trait.hpp @@ -18,7 +18,7 @@ namespace omath::source_engine static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3& cam_origin) noexcept; [[nodiscard]] static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, - float near, float far) noexcept; + float near, float far, NDCDepthRange ndc_depth_range) noexcept; }; } // namespace omath::source_engine \ No newline at end of file diff --git a/include/omath/engines/unity_engine/formulas.hpp b/include/omath/engines/unity_engine/formulas.hpp index 8bdb7b9c..e23f16c2 100644 --- a/include/omath/engines/unity_engine/formulas.hpp +++ b/include/omath/engines/unity_engine/formulas.hpp @@ -22,7 +22,8 @@ namespace omath::unity_engine Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; [[nodiscard]] - Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far) noexcept; + Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far, + NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept; template requires std::is_floating_point_v diff --git a/include/omath/engines/unity_engine/traits/camera_trait.hpp b/include/omath/engines/unity_engine/traits/camera_trait.hpp index 2d98b9db..1f531afe 100644 --- a/include/omath/engines/unity_engine/traits/camera_trait.hpp +++ b/include/omath/engines/unity_engine/traits/camera_trait.hpp @@ -18,7 +18,7 @@ namespace omath::unity_engine static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3& cam_origin) noexcept; [[nodiscard]] static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, - float near, float far) noexcept; + float near, float far, NDCDepthRange ndc_depth_range) noexcept; }; } // namespace omath::unity_engine \ No newline at end of file diff --git a/include/omath/engines/unreal_engine/formulas.hpp b/include/omath/engines/unreal_engine/formulas.hpp index a88f3dcb..650bebd1 100644 --- a/include/omath/engines/unreal_engine/formulas.hpp +++ b/include/omath/engines/unreal_engine/formulas.hpp @@ -22,7 +22,8 @@ namespace omath::unreal_engine Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; [[nodiscard]] - Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far) noexcept; + Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far, + NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept; template requires std::is_floating_point_v diff --git a/include/omath/engines/unreal_engine/traits/camera_trait.hpp b/include/omath/engines/unreal_engine/traits/camera_trait.hpp index f2de27e4..952542c3 100644 --- a/include/omath/engines/unreal_engine/traits/camera_trait.hpp +++ b/include/omath/engines/unreal_engine/traits/camera_trait.hpp @@ -18,7 +18,7 @@ namespace omath::unreal_engine static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3& cam_origin) noexcept; [[nodiscard]] static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, - float near, float far) noexcept; + float near, float far, NDCDepthRange ndc_depth_range) noexcept; }; } // namespace omath::unreal_engine \ No newline at end of file diff --git a/include/omath/linear_algebra/mat.hpp b/include/omath/linear_algebra/mat.hpp index f0a7aa4b..d8fc0333 100644 --- a/include/omath/linear_algebra/mat.hpp +++ b/include/omath/linear_algebra/mat.hpp @@ -37,6 +37,12 @@ namespace omath COLUMN_MAJOR }; + enum class NDCDepthRange : uint8_t + { + NEGATIVE_ONE_TO_ONE = 0, // OpenGL: [-1.0, 1.0] + ZERO_TO_ONE // DirectX / Vulkan: [0.0, 1.0] + }; + template concept MatTemplateEqual = (M1::rows == M2::rows) && (M1::columns == M2::columns) && std::is_same_v && (M1::store_type == M2::store_type); @@ -658,56 +664,90 @@ namespace omath } * mat_translation(-camera_origin); } - template + template [[nodiscard]] Mat<4, 4, Type, St> mat_perspective_left_handed(const float field_of_view, const float aspect_ratio, const float near, const float far) noexcept { const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f); - 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}}; + if constexpr (DepthRange == NDCDepthRange::ZERO_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 / (far - near), -(near * far) / (far - near)}, + {0.f, 0.f, 1.f, 0.f}}; + else + 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}}; } - template + template [[nodiscard]] Mat<4, 4, Type, St> mat_perspective_right_handed(const float field_of_view, const float aspect_ratio, const float near, const float far) noexcept { const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f); - 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}}; + if constexpr (DepthRange == NDCDepthRange::ZERO_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 / (far - near), -(near * far) / (far - near)}, + {0.f, 0.f, -1.f, 0.f}}; + else + 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}}; } - template + template [[nodiscard]] Mat<4, 4, Type, St> mat_ortho_left_handed(const Type left, const Type right, const Type bottom, const Type top, const Type near, const Type far) noexcept { - return - { - { static_cast(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)}, - { 0.f, static_cast(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)}, - { 0.f, 0.f, static_cast(2) / (far - near), -(far + near) / (far - near) }, - { 0.f, 0.f, 0.f, 1.f } - }; + if constexpr (DepthRange == NDCDepthRange::ZERO_TO_ONE) + return + { + { static_cast(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)}, + { 0.f, static_cast(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)}, + { 0.f, 0.f, static_cast(1) / (far - near), -near / (far - near) }, + { 0.f, 0.f, 0.f, 1.f } + }; + else + return + { + { static_cast(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)}, + { 0.f, static_cast(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)}, + { 0.f, 0.f, static_cast(2) / (far - near), -(far + near) / (far - near) }, + { 0.f, 0.f, 0.f, 1.f } + }; } - template + template [[nodiscard]] Mat<4, 4, Type, St> mat_ortho_right_handed(const Type left, const Type right, const Type bottom, const Type top, const Type near, const Type far) noexcept { - return - { - { static_cast(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)}, - { 0.f, static_cast(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)}, - { 0.f, 0.f, -static_cast(2) / (far - near), -(far + near) / (far - near) }, - { 0.f, 0.f, 0.f, 1.f } - }; + if constexpr (DepthRange == NDCDepthRange::ZERO_TO_ONE) + return + { + { static_cast(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)}, + { 0.f, static_cast(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)}, + { 0.f, 0.f, -static_cast(1) / (far - near), -near / (far - near) }, + { 0.f, 0.f, 0.f, 1.f } + }; + else + return + { + { static_cast(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)}, + { 0.f, static_cast(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)}, + { 0.f, 0.f, -static_cast(2) / (far - near), -(far + near) / (far - near) }, + { 0.f, 0.f, 0.f, 1.f } + }; } template Mat<4, 4, T, St> mat_look_at_left_handed(const Vector3& eye, const Vector3& center, const Vector3& up) diff --git a/include/omath/projection/camera.hpp b/include/omath/projection/camera.hpp index ed7d3e5c..ee0c3239 100644 --- a/include/omath/projection/camera.hpp +++ b/include/omath/projection/camera.hpp @@ -44,19 +44,21 @@ namespace omath::projection template concept CameraEngineConcept = requires(const Vector3& cam_origin, const Vector3& look_at, const ViewAnglesType& angles, - const FieldOfView& fov, const ViewPort& viewport, float znear, float zfar) { + const FieldOfView& fov, const ViewPort& viewport, float znear, float zfar, + NDCDepthRange ndc_depth_range) { // Presence + return types { T::calc_look_at_angle(cam_origin, look_at) } -> std::same_as; { T::calc_view_matrix(angles, cam_origin) } -> std::same_as; - { T::calc_projection_matrix(fov, viewport, znear, zfar) } -> std::same_as; + { T::calc_projection_matrix(fov, viewport, znear, zfar, ndc_depth_range) } -> std::same_as; // Enforce noexcept as in the trait declaration requires noexcept(T::calc_look_at_angle(cam_origin, look_at)); requires noexcept(T::calc_view_matrix(angles, cam_origin)); - requires noexcept(T::calc_projection_matrix(fov, viewport, znear, zfar)); + requires noexcept(T::calc_projection_matrix(fov, viewport, znear, zfar, ndc_depth_range)); }; - template + template requires CameraEngineConcept class Camera final { @@ -135,7 +137,8 @@ namespace omath::projection { if (!m_projection_matrix.has_value()) m_projection_matrix = TraitClass::calc_projection_matrix(m_field_of_view, m_view_port, - m_near_plane_distance, m_far_plane_distance); + m_near_plane_distance, m_far_plane_distance, + depth_range); return m_projection_matrix.value(); } @@ -271,17 +274,36 @@ namespace omath::projection return a[axis] < -a[3] && b[axis] < -b[3] && c[axis] < -c[3]; }; - // Clip volume in clip space (OpenGL-style): + // Clip volume in clip space: // -w <= x <= w // -w <= y <= w - // -w <= z <= w + // z_min <= z <= w (z_min = -w for [-1,1], 0 for [0,1]) - for (int i = 0; i < 3; i++) + // x and y planes + for (int i = 0; i < 2; i++) { if (all_outside_plane(i, c0, c1, c2, false)) - return true; // x < -w (left) + return true; if (all_outside_plane(i, c0, c1, c2, true)) - return true; // x > w (right) + return true; + } + + // z far plane: z > w + if (all_outside_plane(2, c0, c1, c2, true)) + return true; + + // z near plane + if constexpr (depth_range == NDCDepthRange::ZERO_TO_ONE) + { + // 0 <= z, so reject if z < 0 for all vertices + if (c0[2] < 0.f && c1[2] < 0.f && c2[2] < 0.f) + return true; + } + else + { + // -w <= z + if (all_outside_plane(2, c0, c1, c2, false)) + return true; } return false; } @@ -306,7 +328,8 @@ namespace omath::projection return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS); // ReSharper disable once CppTooWideScope - const auto clipped_manually = clipping == ViewPortClipping::MANUAL && (projected.at(2, 0) < 0.0f - eps + constexpr auto z_min = depth_range == NDCDepthRange::ZERO_TO_ONE ? 0.0f : -1.0f; + const auto clipped_manually = clipping == ViewPortClipping::MANUAL && (projected.at(2, 0) < z_min - eps || projected.at(2, 0) > 1.0f + eps); if (clipped_manually) return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS); @@ -368,8 +391,34 @@ namespace omath::projection [[nodiscard]] constexpr static bool is_ndc_out_of_bounds(const Type& ndc) noexcept { constexpr auto eps = std::numeric_limits::epsilon(); - return std::ranges::any_of(ndc.raw_array(), - [](const auto& val) { return val < -1.0f - eps || val > 1.0f + eps; }); + + const auto& data = ndc.raw_array(); + // x and y are always in [-1, 1] + if (data[0] < -1.0f - eps || data[0] > 1.0f + eps) + return true; + if (data[1] < -1.0f - eps || data[1] > 1.0f + eps) + return true; + + // z range depends on the NDC depth convention + if constexpr (depth_range == NDCDepthRange::ZERO_TO_ONE) + { + if (data[2] < 0.0f - eps || data[2] > 1.0f + eps) + return true; + } + else + { + if (data[2] < -1.0f - eps || data[2] > 1.0f + eps) + return true; + } + + // w + if constexpr (ndc.size().rows * ndc.size().columns > 3) + { + if (data[3] < -1.0f - eps || data[3] > 1.0f + eps) + return true; + } + + return false; } // NDC REPRESENTATION: diff --git a/source/engines/cry_engine/formulas.cpp b/source/engines/cry_engine/formulas.cpp index 605b3d03..4d5bc94e 100644 --- a/source/engines/cry_engine/formulas.cpp +++ b/source/engines/cry_engine/formulas.cpp @@ -35,8 +35,12 @@ namespace omath::cry_engine * mat_rotation_axis_x(angles.pitch); } Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near, - const float far) noexcept + const float far, const NDCDepthRange ndc_depth_range) noexcept { + if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE) + return mat_perspective_left_handed( + field_of_view, aspect_ratio, near, far); + return mat_perspective_left_handed(field_of_view, aspect_ratio, near, far); } } // namespace omath::unity_engine diff --git a/source/engines/cry_engine/traits/camera_trait.cpp b/source/engines/cry_engine/traits/camera_trait.cpp index ac912f8a..a8ea6180 100644 --- a/source/engines/cry_engine/traits/camera_trait.cpp +++ b/source/engines/cry_engine/traits/camera_trait.cpp @@ -19,8 +19,9 @@ namespace omath::cry_engine } Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, const float near, - const float far) noexcept + const float far, const NDCDepthRange ndc_depth_range) noexcept { - return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far); + return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far, + ndc_depth_range); } } // namespace omath::unity_engine \ No newline at end of file diff --git a/source/engines/frostbite_engine/formulas.cpp b/source/engines/frostbite_engine/formulas.cpp index 021cdc7c..2c0d6cc4 100644 --- a/source/engines/frostbite_engine/formulas.cpp +++ b/source/engines/frostbite_engine/formulas.cpp @@ -35,8 +35,12 @@ namespace omath::frostbite_engine * mat_rotation_axis_x(angles.pitch); } Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near, - const float far) noexcept + const float far, const NDCDepthRange ndc_depth_range) noexcept { + if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE) + return mat_perspective_left_handed( + field_of_view, aspect_ratio, near, far); + return mat_perspective_left_handed(field_of_view, aspect_ratio, near, far); } } // namespace omath::unity_engine diff --git a/source/engines/frostbite_engine/traits/camera_trait.cpp b/source/engines/frostbite_engine/traits/camera_trait.cpp index 8b44d4f4..1d4ac217 100644 --- a/source/engines/frostbite_engine/traits/camera_trait.cpp +++ b/source/engines/frostbite_engine/traits/camera_trait.cpp @@ -19,8 +19,9 @@ namespace omath::frostbite_engine } Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, const float near, - const float far) noexcept + const float far, const NDCDepthRange ndc_depth_range) noexcept { - return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far); + return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far, + ndc_depth_range); } } // namespace omath::unity_engine \ No newline at end of file diff --git a/source/engines/iw_engine/formulas.cpp b/source/engines/iw_engine/formulas.cpp index c346d50c..ca59ecd6 100644 --- a/source/engines/iw_engine/formulas.cpp +++ b/source/engines/iw_engine/formulas.cpp @@ -36,13 +36,21 @@ namespace omath::iw_engine } Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near, - const float far) noexcept + const float far, const NDCDepthRange ndc_depth_range) noexcept { // NOTE: Need magic number to fix fov calculation, since IW engine inherit Quake proj matrix calculation constexpr auto k_multiply_factor = 0.75f; const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f) * k_multiply_factor; + 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 { {1.f / (aspect_ratio * fov_half_tan), 0, 0, 0}, {0, 1.f / (fov_half_tan), 0, 0}, diff --git a/source/engines/iw_engine/traits/camera_trait.cpp b/source/engines/iw_engine/traits/camera_trait.cpp index 427a3c58..14fbd7b1 100644 --- a/source/engines/iw_engine/traits/camera_trait.cpp +++ b/source/engines/iw_engine/traits/camera_trait.cpp @@ -19,8 +19,9 @@ namespace omath::iw_engine } Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, const float near, - const float far) noexcept + const float far, const NDCDepthRange ndc_depth_range) noexcept { - return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far); + return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far, + ndc_depth_range); } } // namespace omath::iw_engine \ No newline at end of file diff --git a/source/engines/opengl_engine/formulas.cpp b/source/engines/opengl_engine/formulas.cpp index da8ab494..2f936acc 100644 --- a/source/engines/opengl_engine/formulas.cpp +++ b/source/engines/opengl_engine/formulas.cpp @@ -37,10 +37,18 @@ namespace omath::opengl_engine * mat_rotation_axis_x(angles.pitch); } Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near, - const float far) noexcept + 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::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 { {1.f / (aspect_ratio * fov_half_tan), 0, 0, 0}, {0, 1.f / (fov_half_tan), 0, 0}, diff --git a/source/engines/opengl_engine/traits/camera_trait.cpp b/source/engines/opengl_engine/traits/camera_trait.cpp index d034bef1..6addb710 100644 --- a/source/engines/opengl_engine/traits/camera_trait.cpp +++ b/source/engines/opengl_engine/traits/camera_trait.cpp @@ -20,8 +20,9 @@ namespace omath::opengl_engine } Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, const float near, - const float far) noexcept + const float far, const NDCDepthRange ndc_depth_range) noexcept { - return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far); + return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far, + ndc_depth_range); } } // namespace omath::opengl_engine \ No newline at end of file diff --git a/source/engines/source_engine/formulas.cpp b/source/engines/source_engine/formulas.cpp index 326507e8..0cf80842 100644 --- a/source/engines/source_engine/formulas.cpp +++ b/source/engines/source_engine/formulas.cpp @@ -36,13 +36,21 @@ namespace omath::source_engine } Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near, - const float far) noexcept + const float far, const NDCDepthRange ndc_depth_range) noexcept { // NOTE: Need magic number to fix fov calculation, since source inherit Quake proj matrix calculation constexpr auto k_multiply_factor = 0.75f; const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f) * k_multiply_factor; + 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 { {1.f / (aspect_ratio * fov_half_tan), 0, 0, 0}, {0, 1.f / (fov_half_tan), 0, 0}, diff --git a/source/engines/source_engine/traits/camera_trait.cpp b/source/engines/source_engine/traits/camera_trait.cpp index 071326e2..2663d349 100644 --- a/source/engines/source_engine/traits/camera_trait.cpp +++ b/source/engines/source_engine/traits/camera_trait.cpp @@ -20,8 +20,9 @@ namespace omath::source_engine } Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, const float near, - const float far) noexcept + const float far, const NDCDepthRange ndc_depth_range) noexcept { - return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far); + return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far, + ndc_depth_range); } } // namespace omath::source_engine \ No newline at end of file diff --git a/source/engines/unity_engine/formulas.cpp b/source/engines/unity_engine/formulas.cpp index f1298260..7e955258 100644 --- a/source/engines/unity_engine/formulas.cpp +++ b/source/engines/unity_engine/formulas.cpp @@ -35,8 +35,12 @@ namespace omath::unity_engine * mat_rotation_axis_x(angles.pitch); } Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near, - const float far) noexcept + const float far, const NDCDepthRange ndc_depth_range) noexcept { + 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); } } // namespace omath::unity_engine diff --git a/source/engines/unity_engine/traits/camera_trait.cpp b/source/engines/unity_engine/traits/camera_trait.cpp index b981befd..1c88cb2a 100644 --- a/source/engines/unity_engine/traits/camera_trait.cpp +++ b/source/engines/unity_engine/traits/camera_trait.cpp @@ -19,8 +19,9 @@ namespace omath::unity_engine } Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, const float near, - const float far) noexcept + const float far, const NDCDepthRange ndc_depth_range) noexcept { - return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far); + return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far, + ndc_depth_range); } } // namespace omath::unity_engine \ No newline at end of file diff --git a/source/engines/unreal_engine/formulas.cpp b/source/engines/unreal_engine/formulas.cpp index 9b401246..0e40b8ab 100644 --- a/source/engines/unreal_engine/formulas.cpp +++ b/source/engines/unreal_engine/formulas.cpp @@ -35,8 +35,12 @@ namespace omath::unreal_engine * mat_rotation_axis_y(angles.pitch); } Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near, - const float far) noexcept + const float far, const NDCDepthRange ndc_depth_range) noexcept { + if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE) + return mat_perspective_left_handed( + field_of_view, aspect_ratio, near, far); + return mat_perspective_left_handed(field_of_view, aspect_ratio, near, far); } } // namespace omath::unreal_engine diff --git a/source/engines/unreal_engine/traits/camera_trait.cpp b/source/engines/unreal_engine/traits/camera_trait.cpp index 9eedce3c..0d1d9e17 100644 --- a/source/engines/unreal_engine/traits/camera_trait.cpp +++ b/source/engines/unreal_engine/traits/camera_trait.cpp @@ -19,8 +19,9 @@ namespace omath::unreal_engine } Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, const float near, - const float far) noexcept + const float far, const NDCDepthRange ndc_depth_range) noexcept { - return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far); + return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far, + ndc_depth_range); } } // namespace omath::unreal_engine \ No newline at end of file diff --git a/tests/engines/unit_test_traits_engines.cpp b/tests/engines/unit_test_traits_engines.cpp index 35d5b6fe..5c08bb4a 100644 --- a/tests/engines/unit_test_traits_engines.cpp +++ b/tests/engines/unit_test_traits_engines.cpp @@ -21,6 +21,9 @@ #include #include +#include + +#include #include #include @@ -218,9 +221,14 @@ TEST(TraitTests, Frostbite_Pred_And_Mesh_And_Camera) // CameraTrait look at should be callable const auto angles = e::CameraTrait::calc_look_at_angle({0, 0, 0}, {0, 1, 1}); (void)angles; - const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f); + const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE); const auto expected = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f); expect_matrix_near(proj, expected); + + const auto proj_zo = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + const auto expected_zo = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + expect_matrix_near(proj_zo, expected_zo); + EXPECT_NE(proj, proj_zo); } TEST(TraitTests, IW_Pred_And_Mesh_And_Camera) @@ -264,10 +272,15 @@ TEST(TraitTests, IW_Pred_And_Mesh_And_Camera) e::ViewAngles va; expect_matrix_near(e::MeshTrait::rotation_matrix(va), e::rotation_matrix(va)); - const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(45.f), {1920.f, 1080.f}, 0.1f, 1000.f); + const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(45.f), {1920.f, 1080.f}, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE); const auto expected = e::calc_perspective_projection_matrix(45.f, 1920.f / 1080.f, 0.1f, 1000.f); expect_matrix_near(proj, expected); + const auto proj_zo = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(45.f), {1920.f, 1080.f}, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + const auto expected_zo = e::calc_perspective_projection_matrix(45.f, 1920.f / 1080.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + expect_matrix_near(proj_zo, expected_zo); + EXPECT_NE(proj, proj_zo); + // non-airborne t.m_is_airborne = false; const auto pred_ground_iw = e::PredEngineTrait::predict_target_position(t, 2.f, 9.81f); @@ -314,10 +327,15 @@ TEST(TraitTests, OpenGL_Pred_And_Mesh_And_Camera) e::ViewAngles va; expect_matrix_near(e::MeshTrait::rotation_matrix(va), e::rotation_matrix(va)); - const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f); + const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE); const auto expected = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f); expect_matrix_near(proj, expected); + const auto proj_zo = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + const auto expected_zo = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + expect_matrix_near(proj_zo, expected_zo); + EXPECT_NE(proj, proj_zo); + // non-airborne t.m_is_airborne = false; const auto pred_ground_gl = e::PredEngineTrait::predict_target_position(t, 2.f, 9.81f); @@ -364,10 +382,15 @@ TEST(TraitTests, Unity_Pred_And_Mesh_And_Camera) e::ViewAngles va; expect_matrix_near(e::MeshTrait::rotation_matrix(va), e::rotation_matrix(va)); - const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f); + const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE); const auto expected = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f); expect_matrix_near(proj, expected); + const auto proj_zo = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + const auto expected_zo = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + expect_matrix_near(proj_zo, expected_zo); + EXPECT_NE(proj, proj_zo); + // non-airborne t.m_is_airborne = false; const auto pred_ground_unity = e::PredEngineTrait::predict_target_position(t, 2.f, 9.81f); @@ -414,12 +437,237 @@ TEST(TraitTests, Unreal_Pred_And_Mesh_And_Camera) e::ViewAngles va; expect_matrix_near(e::MeshTrait::rotation_matrix(va), e::rotation_matrix(va)); - const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f); + const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE); const auto expected = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f); expect_matrix_near(proj, expected); + const auto proj_zo = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + const auto expected_zo = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + expect_matrix_near(proj_zo, expected_zo); + EXPECT_NE(proj, proj_zo); + // non-airborne t.m_is_airborne = false; const auto pred_ground_unreal = e::PredEngineTrait::predict_target_position(t, 2.f, 9.81f); EXPECT_NEAR(pred_ground_unreal.x, 4.f, 1e-6f); } + +// ── NDC Depth Range tests for Source and CryEngine camera traits ──────────── + +TEST(NDCDepthRangeTests, Source_BothDepthRanges) +{ + namespace e = omath::source_engine; + + const auto proj_no = e::CameraTrait::calc_projection_matrix( + projection::FieldOfView::from_degrees(90.f), {1920.f, 1080.f}, 0.1f, 1000.f, + NDCDepthRange::NEGATIVE_ONE_TO_ONE); + const auto expected_no = e::calc_perspective_projection_matrix( + 90.f, 1920.f / 1080.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE); + expect_matrix_near(proj_no, expected_no); + + const auto proj_zo = e::CameraTrait::calc_projection_matrix( + projection::FieldOfView::from_degrees(90.f), {1920.f, 1080.f}, 0.1f, 1000.f, + NDCDepthRange::ZERO_TO_ONE); + const auto expected_zo = e::calc_perspective_projection_matrix( + 90.f, 1920.f / 1080.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + expect_matrix_near(proj_zo, expected_zo); + + EXPECT_NE(proj_no, proj_zo); +} + +TEST(NDCDepthRangeTests, CryEngine_BothDepthRanges) +{ + namespace e = omath::cry_engine; + + const auto proj_no = e::CameraTrait::calc_projection_matrix( + projection::FieldOfView::from_degrees(90.f), {1920.f, 1080.f}, 0.1f, 1000.f, + NDCDepthRange::NEGATIVE_ONE_TO_ONE); + const auto expected_no = e::calc_perspective_projection_matrix( + 90.f, 1920.f / 1080.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE); + expect_matrix_near(proj_no, expected_no); + + const auto proj_zo = e::CameraTrait::calc_projection_matrix( + projection::FieldOfView::from_degrees(90.f), {1920.f, 1080.f}, 0.1f, 1000.f, + NDCDepthRange::ZERO_TO_ONE); + const auto expected_zo = e::calc_perspective_projection_matrix( + 90.f, 1920.f / 1080.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + expect_matrix_near(proj_zo, expected_zo); + + EXPECT_NE(proj_no, proj_zo); +} + +// ── Verify Z mapping for ZERO_TO_ONE across all engines ───────────────────── + +// Helper: projects a point at given z through a left-handed projection matrix and returns NDC z +static float project_z_lh(const Mat<4, 4>& proj, float z) +{ + auto clip = proj * mat_column_from_vector({0, 0, z}); + return clip.at(2, 0) / clip.at(3, 0); +} + +TEST(NDCDepthRangeTests, Source_ZeroToOne_ZRange) +{ + namespace e = omath::source_engine; + // Source is left-handed + const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + + EXPECT_NEAR(project_z_lh(proj, 0.1f), 0.0f, 1e-4f); + EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-4f); + EXPECT_GT(project_z_lh(proj, 500.f), 0.0f); + EXPECT_LT(project_z_lh(proj, 500.f), 1.0f); +} + +TEST(NDCDepthRangeTests, IW_ZeroToOne_ZRange) +{ + namespace e = omath::iw_engine; + // IW is left-handed + const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + + EXPECT_NEAR(project_z_lh(proj, 0.1f), 0.0f, 1e-4f); + EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-4f); + EXPECT_GT(project_z_lh(proj, 500.f), 0.0f); + EXPECT_LT(project_z_lh(proj, 500.f), 1.0f); +} + +TEST(NDCDepthRangeTests, OpenGL_ZeroToOne_ZRange) +{ + namespace e = omath::opengl_engine; + // OpenGL is right-handed (negative z forward), column-major + const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + + // OpenGL engine uses column-major matrices, project manually + auto proj_z = [&](float z) { + auto clip = proj * mat_column_from_vector({0, 0, z}); + return clip.at(2, 0) / clip.at(3, 0); + }; + + EXPECT_NEAR(proj_z(-0.1f), 0.0f, 1e-4f); + EXPECT_NEAR(proj_z(-1000.f), 1.0f, 1e-4f); + EXPECT_GT(proj_z(-500.f), 0.0f); + EXPECT_LT(proj_z(-500.f), 1.0f); +} + +TEST(NDCDepthRangeTests, Frostbite_ZeroToOne_ZRange) +{ + namespace e = omath::frostbite_engine; + const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + + EXPECT_NEAR(project_z_lh(proj, 0.1f), 0.0f, 1e-4f); + EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-4f); + EXPECT_GT(project_z_lh(proj, 500.f), 0.0f); + EXPECT_LT(project_z_lh(proj, 500.f), 1.0f); +} + +TEST(NDCDepthRangeTests, Unity_ZeroToOne_ZRange) +{ + namespace e = omath::unity_engine; + // Unity is right-handed, row-major + const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + + auto proj_z = [&](float z) { + auto clip = proj * mat_column_from_vector({0, 0, z}); + return clip.at(2, 0) / clip.at(3, 0); + }; + + EXPECT_NEAR(proj_z(-0.1f), 0.0f, 1e-4f); + EXPECT_NEAR(proj_z(-1000.f), 1.0f, 1e-4f); + EXPECT_GT(proj_z(-500.f), 0.0f); + EXPECT_LT(proj_z(-500.f), 1.0f); +} + +TEST(NDCDepthRangeTests, Unreal_ZeroToOne_ZRange) +{ + namespace e = omath::unreal_engine; + const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + + EXPECT_NEAR(project_z_lh(proj, 0.1f), 0.0f, 1e-4f); + EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-4f); + EXPECT_GT(project_z_lh(proj, 500.f), 0.0f); + EXPECT_LT(project_z_lh(proj, 500.f), 1.0f); +} + +TEST(NDCDepthRangeTests, CryEngine_ZeroToOne_ZRange) +{ + namespace e = omath::cry_engine; + const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE); + + EXPECT_NEAR(project_z_lh(proj, 0.1f), 0.0f, 1e-4f); + EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-4f); + EXPECT_GT(project_z_lh(proj, 500.f), 0.0f); + EXPECT_LT(project_z_lh(proj, 500.f), 1.0f); +} + +// ── Verify Z mapping for NEGATIVE_ONE_TO_ONE across all engines ───────────── + +TEST(NDCDepthRangeTests, Source_NegativeOneToOne_ZRange) +{ + namespace e = omath::source_engine; + const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE); + + EXPECT_NEAR(project_z_lh(proj, 0.1f), -1.0f, 1e-3f); + EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-3f); +} + +TEST(NDCDepthRangeTests, IW_NegativeOneToOne_ZRange) +{ + namespace e = omath::iw_engine; + const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE); + + EXPECT_NEAR(project_z_lh(proj, 0.1f), -1.0f, 1e-3f); + EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-3f); +} + +TEST(NDCDepthRangeTests, Frostbite_NegativeOneToOne_ZRange) +{ + namespace e = omath::frostbite_engine; + const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE); + + EXPECT_NEAR(project_z_lh(proj, 0.1f), -1.0f, 1e-3f); + EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-3f); +} + +TEST(NDCDepthRangeTests, Unreal_NegativeOneToOne_ZRange) +{ + namespace e = omath::unreal_engine; + const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE); + + EXPECT_NEAR(project_z_lh(proj, 0.1f), -1.0f, 1e-3f); + EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-3f); +} + +TEST(NDCDepthRangeTests, CryEngine_NegativeOneToOne_ZRange) +{ + namespace e = omath::cry_engine; + const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE); + + EXPECT_NEAR(project_z_lh(proj, 0.1f), -1.0f, 1e-3f); + EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-3f); +} + +TEST(NDCDepthRangeTests, OpenGL_NegativeOneToOne_ZRange) +{ + namespace e = omath::opengl_engine; + const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE); + + auto proj_z = [&](float z) { + auto clip = proj * mat_column_from_vector({0, 0, z}); + return clip.at(2, 0) / clip.at(3, 0); + }; + + EXPECT_NEAR(proj_z(-0.1f), -1.0f, 1e-3f); + EXPECT_NEAR(proj_z(-1000.f), 1.0f, 1e-3f); +} + +TEST(NDCDepthRangeTests, Unity_NegativeOneToOne_ZRange) +{ + namespace e = omath::unity_engine; + const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE); + + auto proj_z = [&](float z) { + auto clip = proj * mat_column_from_vector({0, 0, z}); + return clip.at(2, 0) / clip.at(3, 0); + }; + + EXPECT_NEAR(proj_z(-0.1f), -1.0f, 1e-3f); + EXPECT_NEAR(proj_z(-1000.f), 1.0f, 1e-3f); +} diff --git a/tests/general/unit_test_mat.cpp b/tests/general/unit_test_mat.cpp index 7cfc9c1d..57c426d3 100644 --- a/tests/general/unit_test_mat.cpp +++ b/tests/general/unit_test_mat.cpp @@ -240,4 +240,126 @@ TEST(UnitTestMatStandalone, MatPerspectiveLeftHanded) projected /= projected.at(3, 0); EXPECT_TRUE(projected.at(2, 0) > -1.0f && projected.at(2, 0) < 0.f); +} + +TEST(UnitTestMatStandalone, MatPerspectiveLeftHandedZeroToOne) +{ + const auto proj = mat_perspective_left_handed( + 90.f, 16.f / 9.f, 0.1f, 1000.f); + + // Near plane point should map to z ~ 0 + auto near_pt = proj * mat_column_from_vector({0, 0, 0.1f}); + near_pt /= near_pt.at(3, 0); + EXPECT_NEAR(near_pt.at(2, 0), 0.0f, 1e-4f); + + // Far plane point should map to z ~ 1 + auto far_pt = proj * mat_column_from_vector({0, 0, 1000.f}); + far_pt /= far_pt.at(3, 0); + EXPECT_NEAR(far_pt.at(2, 0), 1.0f, 1e-4f); + + // Mid-range point should be in [0, 1] + auto mid_pt = proj * mat_column_from_vector({0, 0, 500.f}); + mid_pt /= mid_pt.at(3, 0); + EXPECT_GT(mid_pt.at(2, 0), 0.0f); + EXPECT_LT(mid_pt.at(2, 0), 1.0f); +} + +TEST(UnitTestMatStandalone, MatPerspectiveRightHandedZeroToOne) +{ + const auto proj = mat_perspective_right_handed( + 90.f, 16.f / 9.f, 0.1f, 1000.f); + + // Near plane point (negative z for right-handed) should map to z ~ 0 + auto near_pt = proj * mat_column_from_vector({0, 0, -0.1f}); + near_pt /= near_pt.at(3, 0); + EXPECT_NEAR(near_pt.at(2, 0), 0.0f, 1e-4f); + + // Far plane point should map to z ~ 1 + auto far_pt = proj * mat_column_from_vector({0, 0, -1000.f}); + far_pt /= far_pt.at(3, 0); + EXPECT_NEAR(far_pt.at(2, 0), 1.0f, 1e-4f); + + // Mid-range point should be in [0, 1] + auto mid_pt = proj * mat_column_from_vector({0, 0, -500.f}); + mid_pt /= mid_pt.at(3, 0); + EXPECT_GT(mid_pt.at(2, 0), 0.0f); + EXPECT_LT(mid_pt.at(2, 0), 1.0f); +} + +TEST(UnitTestMatStandalone, MatPerspectiveNegativeOneToOneRange) +{ + // Verify existing [-1, 1] behavior with explicit template arg matches default + const auto proj_default = mat_perspective_left_handed(90.f, 16.f / 9.f, 0.1f, 1000.f); + const auto proj_explicit = mat_perspective_left_handed(90.f, 16.f / 9.f, 0.1f, 1000.f); + + EXPECT_EQ(proj_default, proj_explicit); + + // Near plane should map to z ~ -1 + auto near_pt = proj_default * mat_column_from_vector({0, 0, 0.1f}); + near_pt /= near_pt.at(3, 0); + EXPECT_NEAR(near_pt.at(2, 0), -1.0f, 1e-3f); + + // Far plane should map to z ~ 1 + auto far_pt = proj_default * mat_column_from_vector({0, 0, 1000.f}); + far_pt /= far_pt.at(3, 0); + EXPECT_NEAR(far_pt.at(2, 0), 1.0f, 1e-3f); +} + +TEST(UnitTestMatStandalone, MatPerspectiveZeroToOneEquanity) +{ + // LH and RH should produce same NDC for mirrored z + constexpr omath::Vector3 left_handed = {0, 2, 10}; + constexpr omath::Vector3 right_handed = {0, 2, -10}; + + const auto proj_lh = mat_perspective_left_handed( + 90.f, 16.f / 9.f, 0.1f, 1000.f); + const auto proj_rh = mat_perspective_right_handed( + 90.f, 16.f / 9.f, 0.1f, 1000.f); + + auto ndc_lh = proj_lh * mat_column_from_vector(left_handed); + auto ndc_rh = proj_rh * mat_column_from_vector(right_handed); + + ndc_lh /= ndc_lh.at(3, 0); + ndc_rh /= ndc_rh.at(3, 0); + + EXPECT_EQ(ndc_lh, ndc_rh); +} + +TEST(UnitTestMatStandalone, MatOrthoLeftHandedZeroToOne) +{ + const auto ortho = mat_ortho_left_handed( + -1.f, 1.f, -1.f, 1.f, 0.1f, 100.f); + + // Near plane should map to z ~ 0 + auto near_pt = ortho * mat_column_from_vector({0, 0, 0.1f}); + EXPECT_NEAR(near_pt.at(2, 0), 0.0f, 1e-4f); + + // Far plane should map to z ~ 1 + auto far_pt = ortho * mat_column_from_vector({0, 0, 100.f}); + EXPECT_NEAR(far_pt.at(2, 0), 1.0f, 1e-4f); +} + +TEST(UnitTestMatStandalone, MatOrthoRightHandedZeroToOne) +{ + const auto ortho = mat_ortho_right_handed( + -1.f, 1.f, -1.f, 1.f, 0.1f, 100.f); + + // Near plane (negative z for RH) should map to z ~ 0 + auto near_pt = ortho * mat_column_from_vector({0, 0, -0.1f}); + EXPECT_NEAR(near_pt.at(2, 0), 0.0f, 1e-4f); + + // Far plane should map to z ~ 1 + auto far_pt = ortho * mat_column_from_vector({0, 0, -100.f}); + EXPECT_NEAR(far_pt.at(2, 0), 1.0f, 1e-4f); +} + +TEST(UnitTestMatStandalone, MatOrthoNegativeOneToOneDefault) +{ + // Verify explicit [-1, 1] matches default + const auto ortho_default = mat_ortho_left_handed(-1.f, 1.f, -1.f, 1.f, 0.1f, 100.f); + const auto ortho_explicit = mat_ortho_left_handed(-1.f, 1.f, -1.f, 1.f, 0.1f, 100.f); + + EXPECT_EQ(ortho_default, ortho_explicit); } \ No newline at end of file From 8fb96b83db8d525fe559def93e346eedb742ff6c Mon Sep 17 00:00:00 2001 From: Orange Date: Mon, 23 Mar 2026 23:52:41 +0300 Subject: [PATCH 2/3] removed dead code --- include/omath/projection/camera.hpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/include/omath/projection/camera.hpp b/include/omath/projection/camera.hpp index ee0c3239..e6db8bf4 100644 --- a/include/omath/projection/camera.hpp +++ b/include/omath/projection/camera.hpp @@ -411,13 +411,6 @@ namespace omath::projection return true; } - // w - if constexpr (ndc.size().rows * ndc.size().columns > 3) - { - if (data[3] < -1.0f - eps || data[3] > 1.0f + eps) - return true; - } - return false; } From 89df10b778a725a11dd70e1bd04f6bb33e0534ce Mon Sep 17 00:00:00 2001 From: Orange Date: Tue, 24 Mar 2026 00:08:06 +0300 Subject: [PATCH 3/3] specifeid ndc for game engines --- include/omath/engines/cry_engine/camera.hpp | 2 +- include/omath/engines/frostbite_engine/camera.hpp | 2 +- include/omath/engines/iw_engine/camera.hpp | 2 +- include/omath/engines/opengl_engine/camera.hpp | 2 +- include/omath/engines/source_engine/camera.hpp | 2 +- include/omath/engines/unity_engine/camera.hpp | 2 +- include/omath/engines/unreal_engine/camera.hpp | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/omath/engines/cry_engine/camera.hpp b/include/omath/engines/cry_engine/camera.hpp index 0db847fe..d02ec6ac 100644 --- a/include/omath/engines/cry_engine/camera.hpp +++ b/include/omath/engines/cry_engine/camera.hpp @@ -9,5 +9,5 @@ namespace omath::cry_engine { - using Camera = projection::Camera; + using Camera = projection::Camera; } // namespace omath::cry_engine \ No newline at end of file diff --git a/include/omath/engines/frostbite_engine/camera.hpp b/include/omath/engines/frostbite_engine/camera.hpp index e16c8b66..977dc42e 100644 --- a/include/omath/engines/frostbite_engine/camera.hpp +++ b/include/omath/engines/frostbite_engine/camera.hpp @@ -9,5 +9,5 @@ namespace omath::frostbite_engine { - using Camera = projection::Camera; + using Camera = projection::Camera; } // namespace omath::unity_engine \ No newline at end of file diff --git a/include/omath/engines/iw_engine/camera.hpp b/include/omath/engines/iw_engine/camera.hpp index 2efe29b1..b7213ad2 100644 --- a/include/omath/engines/iw_engine/camera.hpp +++ b/include/omath/engines/iw_engine/camera.hpp @@ -9,5 +9,5 @@ namespace omath::iw_engine { - using Camera = projection::Camera; + using Camera = projection::Camera; } // namespace omath::iw_engine \ No newline at end of file diff --git a/include/omath/engines/opengl_engine/camera.hpp b/include/omath/engines/opengl_engine/camera.hpp index 145216ce..de01bd56 100644 --- a/include/omath/engines/opengl_engine/camera.hpp +++ b/include/omath/engines/opengl_engine/camera.hpp @@ -8,5 +8,5 @@ namespace omath::opengl_engine { - using Camera = projection::Camera; + using Camera = projection::Camera; } // namespace omath::opengl_engine \ No newline at end of file diff --git a/include/omath/engines/source_engine/camera.hpp b/include/omath/engines/source_engine/camera.hpp index 6769d40d..68871a91 100644 --- a/include/omath/engines/source_engine/camera.hpp +++ b/include/omath/engines/source_engine/camera.hpp @@ -7,5 +7,5 @@ #include "traits/camera_trait.hpp" namespace omath::source_engine { - using Camera = projection::Camera; + using Camera = projection::Camera; } // namespace omath::source_engine \ No newline at end of file diff --git a/include/omath/engines/unity_engine/camera.hpp b/include/omath/engines/unity_engine/camera.hpp index bc3d6d2b..55c82a80 100644 --- a/include/omath/engines/unity_engine/camera.hpp +++ b/include/omath/engines/unity_engine/camera.hpp @@ -9,5 +9,5 @@ namespace omath::unity_engine { - using Camera = projection::Camera; + using Camera = projection::Camera; } // namespace omath::unity_engine \ No newline at end of file diff --git a/include/omath/engines/unreal_engine/camera.hpp b/include/omath/engines/unreal_engine/camera.hpp index 30d5daf5..6565e137 100644 --- a/include/omath/engines/unreal_engine/camera.hpp +++ b/include/omath/engines/unreal_engine/camera.hpp @@ -9,5 +9,5 @@ namespace omath::unreal_engine { - using Camera = projection::Camera; + using Camera = projection::Camera; } // namespace omath::unreal_engine \ No newline at end of file