Compare commits

...

4 Commits

Author SHA1 Message Date
29b7ac6450 Merge pull request #177 from orange-cpp/feature/custom_ndc_z_range
Feature/custom ndc z range
2026-03-24 04:20:57 +03:00
89df10b778 specifeid ndc for game engines 2026-03-24 00:08:06 +03:00
8fb96b83db removed dead code 2026-03-23 23:52:41 +03:00
4b6db0c402 updated z range 2026-03-23 23:36:19 +03:00
39 changed files with 592 additions and 86 deletions

View File

@@ -9,5 +9,5 @@
namespace omath::cry_engine namespace omath::cry_engine
{ {
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>; using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, false, NDCDepthRange::ZERO_TO_ONE>;
} // namespace omath::cry_engine } // namespace omath::cry_engine

View File

@@ -22,7 +22,8 @@ namespace omath::cry_engine
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]] [[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<class FloatingType> template<class FloatingType>
requires std::is_floating_point_v<FloatingType> requires std::is_floating_point_v<FloatingType>

View File

@@ -18,7 +18,7 @@ namespace omath::cry_engine
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, 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 } // namespace omath::cry_engine

View File

@@ -9,5 +9,5 @@
namespace omath::frostbite_engine namespace omath::frostbite_engine
{ {
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>; using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, false, NDCDepthRange::ZERO_TO_ONE>;
} // namespace omath::unity_engine } // namespace omath::unity_engine

View File

@@ -22,7 +22,8 @@ namespace omath::frostbite_engine
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]] [[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<class FloatingType> template<class FloatingType>
requires std::is_floating_point_v<FloatingType> requires std::is_floating_point_v<FloatingType>

View File

@@ -18,7 +18,7 @@ namespace omath::frostbite_engine
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, 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 } // namespace omath::unreal_engine

View File

@@ -9,5 +9,5 @@
namespace omath::iw_engine namespace omath::iw_engine
{ {
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>; using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, false, NDCDepthRange::ZERO_TO_ONE>;
} // namespace omath::iw_engine } // namespace omath::iw_engine

View File

@@ -22,7 +22,8 @@ namespace omath::iw_engine
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; [[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
[[nodiscard]] [[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<class FloatingType> template<class FloatingType>
requires std::is_floating_point_v<FloatingType> requires std::is_floating_point_v<FloatingType>

View File

@@ -18,7 +18,7 @@ namespace omath::iw_engine
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, 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 } // namespace omath::iw_engine

View File

@@ -8,5 +8,5 @@
namespace omath::opengl_engine namespace omath::opengl_engine
{ {
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, true>; using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, true, NDCDepthRange::NEGATIVE_ONE_TO_ONE>;
} // namespace omath::opengl_engine } // namespace omath::opengl_engine

View File

@@ -21,7 +21,8 @@ namespace omath::opengl_engine
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]] [[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<class FloatingType> template<class FloatingType>
requires std::is_floating_point_v<FloatingType> requires std::is_floating_point_v<FloatingType>

View File

@@ -18,7 +18,7 @@ namespace omath::opengl_engine
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, 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 } // namespace omath::opengl_engine

View File

@@ -7,5 +7,5 @@
#include "traits/camera_trait.hpp" #include "traits/camera_trait.hpp"
namespace omath::source_engine namespace omath::source_engine
{ {
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>; using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, false, NDCDepthRange::ZERO_TO_ONE>;
} // namespace omath::source_engine } // namespace omath::source_engine

View File

@@ -21,7 +21,8 @@ namespace omath::source_engine
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; [[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
[[nodiscard]] [[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<class FloatingType> template<class FloatingType>
requires std::is_floating_point_v<FloatingType> requires std::is_floating_point_v<FloatingType>

View File

@@ -18,7 +18,7 @@ namespace omath::source_engine
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, 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 } // namespace omath::source_engine

View File

@@ -9,5 +9,5 @@
namespace omath::unity_engine namespace omath::unity_engine
{ {
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>; using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, false, NDCDepthRange::ZERO_TO_ONE>;
} // namespace omath::unity_engine } // namespace omath::unity_engine

View File

@@ -22,7 +22,8 @@ namespace omath::unity_engine
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]] [[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<class FloatingType> template<class FloatingType>
requires std::is_floating_point_v<FloatingType> requires std::is_floating_point_v<FloatingType>

View File

@@ -18,7 +18,7 @@ namespace omath::unity_engine
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, 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 } // namespace omath::unity_engine

View File

@@ -9,5 +9,5 @@
namespace omath::unreal_engine namespace omath::unreal_engine
{ {
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>; using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, false, NDCDepthRange::ZERO_TO_ONE>;
} // namespace omath::unreal_engine } // namespace omath::unreal_engine

View File

@@ -22,7 +22,8 @@ namespace omath::unreal_engine
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]] [[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<class FloatingType> template<class FloatingType>
requires std::is_floating_point_v<FloatingType> requires std::is_floating_point_v<FloatingType>

View File

@@ -18,7 +18,7 @@ namespace omath::unreal_engine
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, 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 } // namespace omath::unreal_engine

View File

@@ -37,6 +37,12 @@ namespace omath
COLUMN_MAJOR 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<typename M1, typename M2> concept MatTemplateEqual template<typename M1, typename M2> concept MatTemplateEqual
= (M1::rows == M2::rows) && (M1::columns == M2::columns) = (M1::rows == M2::rows) && (M1::columns == M2::columns)
&& std::is_same_v<typename M1::value_type, typename M2::value_type> && (M1::store_type == M2::store_type); && std::is_same_v<typename M1::value_type, typename M2::value_type> && (M1::store_type == M2::store_type);
@@ -658,56 +664,90 @@ namespace omath
} * mat_translation<Type, St>(-camera_origin); } * mat_translation<Type, St>(-camera_origin);
} }
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR> template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR,
NDCDepthRange DepthRange = NDCDepthRange::NEGATIVE_ONE_TO_ONE>
[[nodiscard]] [[nodiscard]]
Mat<4, 4, Type, St> mat_perspective_left_handed(const float field_of_view, const float aspect_ratio, 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 near, const float far) noexcept
{ {
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f); 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}, if constexpr (DepthRange == NDCDepthRange::ZERO_TO_ONE)
{0.f, 1.f / fov_half_tan, 0.f, 0.f}, return {{1.f / (aspect_ratio * fov_half_tan), 0.f, 0.f, 0.f},
{0.f, 0.f, (far + near) / (far - near), -(2.f * near * far) / (far - near)}, {0.f, 1.f / fov_half_tan, 0.f, 0.f},
{0.f, 0.f, 1.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<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR> template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR,
NDCDepthRange DepthRange = NDCDepthRange::NEGATIVE_ONE_TO_ONE>
[[nodiscard]] [[nodiscard]]
Mat<4, 4, Type, St> mat_perspective_right_handed(const float field_of_view, const float aspect_ratio, 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 near, const float far) noexcept
{ {
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f); 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}, if constexpr (DepthRange == NDCDepthRange::ZERO_TO_ONE)
{0.f, 1.f / fov_half_tan, 0.f, 0.f}, return {{1.f / (aspect_ratio * fov_half_tan), 0.f, 0.f, 0.f},
{0.f, 0.f, -(far + near) / (far - near), -(2.f * near * far) / (far - near)}, {0.f, 1.f / fov_half_tan, 0.f, 0.f},
{0.f, 0.f, -1.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<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR> template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR,
NDCDepthRange DepthRange = NDCDepthRange::NEGATIVE_ONE_TO_ONE>
[[nodiscard]] [[nodiscard]]
Mat<4, 4, Type, St> mat_ortho_left_handed(const Type left, const Type right, const Type bottom, const Type top, 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 const Type near, const Type far) noexcept
{ {
return if constexpr (DepthRange == NDCDepthRange::ZERO_TO_ONE)
{ return
{ static_cast<Type>(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)}, {
{ 0.f, static_cast<Type>(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)}, { static_cast<Type>(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)},
{ 0.f, 0.f, static_cast<Type>(2) / (far - near), -(far + near) / (far - near) }, { 0.f, static_cast<Type>(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)},
{ 0.f, 0.f, 0.f, 1.f } { 0.f, 0.f, static_cast<Type>(1) / (far - near), -near / (far - near) },
}; { 0.f, 0.f, 0.f, 1.f }
};
else
return
{
{ static_cast<Type>(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)},
{ 0.f, static_cast<Type>(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)},
{ 0.f, 0.f, static_cast<Type>(2) / (far - near), -(far + near) / (far - near) },
{ 0.f, 0.f, 0.f, 1.f }
};
} }
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR> template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR,
NDCDepthRange DepthRange = NDCDepthRange::NEGATIVE_ONE_TO_ONE>
[[nodiscard]] [[nodiscard]]
Mat<4, 4, Type, St> mat_ortho_right_handed(const Type left, const Type right, const Type bottom, const Type top, 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 const Type near, const Type far) noexcept
{ {
return if constexpr (DepthRange == NDCDepthRange::ZERO_TO_ONE)
{ return
{ static_cast<Type>(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)}, {
{ 0.f, static_cast<Type>(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)}, { static_cast<Type>(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)},
{ 0.f, 0.f, -static_cast<Type>(2) / (far - near), -(far + near) / (far - near) }, { 0.f, static_cast<Type>(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)},
{ 0.f, 0.f, 0.f, 1.f } { 0.f, 0.f, -static_cast<Type>(1) / (far - near), -near / (far - near) },
}; { 0.f, 0.f, 0.f, 1.f }
};
else
return
{
{ static_cast<Type>(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)},
{ 0.f, static_cast<Type>(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)},
{ 0.f, 0.f, -static_cast<Type>(2) / (far - near), -(far + near) / (far - near) },
{ 0.f, 0.f, 0.f, 1.f }
};
} }
template<class T = float, MatStoreType St = MatStoreType::COLUMN_MAJOR> template<class T = float, MatStoreType St = MatStoreType::COLUMN_MAJOR>
Mat<4, 4, T, St> mat_look_at_left_handed(const Vector3<T>& eye, const Vector3<T>& center, const Vector3<T>& up) Mat<4, 4, T, St> mat_look_at_left_handed(const Vector3<T>& eye, const Vector3<T>& center, const Vector3<T>& up)

View File

@@ -44,19 +44,21 @@ namespace omath::projection
template<class T, class MatType, class ViewAnglesType> template<class T, class MatType, class ViewAnglesType>
concept CameraEngineConcept = concept CameraEngineConcept =
requires(const Vector3<float>& cam_origin, const Vector3<float>& look_at, const ViewAnglesType& angles, requires(const Vector3<float>& cam_origin, const Vector3<float>& 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 // Presence + return types
{ T::calc_look_at_angle(cam_origin, look_at) } -> std::same_as<ViewAnglesType>; { T::calc_look_at_angle(cam_origin, look_at) } -> std::same_as<ViewAnglesType>;
{ T::calc_view_matrix(angles, cam_origin) } -> std::same_as<MatType>; { T::calc_view_matrix(angles, cam_origin) } -> std::same_as<MatType>;
{ T::calc_projection_matrix(fov, viewport, znear, zfar) } -> std::same_as<MatType>; { T::calc_projection_matrix(fov, viewport, znear, zfar, ndc_depth_range) } -> std::same_as<MatType>;
// Enforce noexcept as in the trait declaration // Enforce noexcept as in the trait declaration
requires noexcept(T::calc_look_at_angle(cam_origin, look_at)); requires noexcept(T::calc_look_at_angle(cam_origin, look_at));
requires noexcept(T::calc_view_matrix(angles, cam_origin)); 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<class Mat4X4Type, class ViewAnglesType, class TraitClass, bool inverted_z = false> template<class Mat4X4Type, class ViewAnglesType, class TraitClass, bool inverted_z = false,
NDCDepthRange depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE>
requires CameraEngineConcept<TraitClass, Mat4X4Type, ViewAnglesType> requires CameraEngineConcept<TraitClass, Mat4X4Type, ViewAnglesType>
class Camera final class Camera final
{ {
@@ -135,7 +137,8 @@ namespace omath::projection
{ {
if (!m_projection_matrix.has_value()) if (!m_projection_matrix.has_value())
m_projection_matrix = TraitClass::calc_projection_matrix(m_field_of_view, m_view_port, 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(); 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]; 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 <= x <= w
// -w <= y <= 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)) 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)) 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; return false;
} }
@@ -306,7 +328,8 @@ namespace omath::projection
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS); return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
// ReSharper disable once CppTooWideScope // 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); || projected.at(2, 0) > 1.0f + eps);
if (clipped_manually) if (clipped_manually)
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS); return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
@@ -368,8 +391,27 @@ namespace omath::projection
[[nodiscard]] constexpr static bool is_ndc_out_of_bounds(const Type& ndc) noexcept [[nodiscard]] constexpr static bool is_ndc_out_of_bounds(const Type& ndc) noexcept
{ {
constexpr auto eps = std::numeric_limits<float>::epsilon(); constexpr auto eps = std::numeric_limits<float>::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;
}
return false;
} }
// NDC REPRESENTATION: // NDC REPRESENTATION:

View File

@@ -35,8 +35,12 @@ namespace omath::cry_engine
* mat_rotation_axis_x<float, MatStoreType::ROW_MAJOR>(angles.pitch); * mat_rotation_axis_x<float, MatStoreType::ROW_MAJOR>(angles.pitch);
} }
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near, 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<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near, far);
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 } // namespace omath::unity_engine

View File

@@ -19,8 +19,9 @@ namespace omath::cry_engine
} }
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov, Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near, 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 } // namespace omath::unity_engine

View File

@@ -35,8 +35,12 @@ namespace omath::frostbite_engine
* mat_rotation_axis_x<float, MatStoreType::ROW_MAJOR>(angles.pitch); * mat_rotation_axis_x<float, MatStoreType::ROW_MAJOR>(angles.pitch);
} }
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near, 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<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near, far);
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 } // namespace omath::unity_engine

View File

@@ -19,8 +19,9 @@ namespace omath::frostbite_engine
} }
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov, Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near, 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 } // namespace omath::unity_engine

View File

@@ -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, 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 // NOTE: Need magic number to fix fov calculation, since IW engine inherit Quake proj matrix calculation
constexpr auto k_multiply_factor = 0.75f; 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; 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 { return {
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0}, {1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
{0, 1.f / (fov_half_tan), 0, 0}, {0, 1.f / (fov_half_tan), 0, 0},

View File

@@ -19,8 +19,9 @@ namespace omath::iw_engine
} }
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov, Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near, 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 } // namespace omath::iw_engine

View File

@@ -37,10 +37,18 @@ namespace omath::opengl_engine
* mat_rotation_axis_x<float, MatStoreType::COLUMN_MAJOR>(angles.pitch); * mat_rotation_axis_x<float, MatStoreType::COLUMN_MAJOR>(angles.pitch);
} }
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near, 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); 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 { return {
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0}, {1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
{0, 1.f / (fov_half_tan), 0, 0}, {0, 1.f / (fov_half_tan), 0, 0},

View File

@@ -20,8 +20,9 @@ namespace omath::opengl_engine
} }
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov, Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near, 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 } // namespace omath::opengl_engine

View File

@@ -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, 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 // NOTE: Need magic number to fix fov calculation, since source inherit Quake proj matrix calculation
constexpr auto k_multiply_factor = 0.75f; 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; 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 { return {
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0}, {1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
{0, 1.f / (fov_half_tan), 0, 0}, {0, 1.f / (fov_half_tan), 0, 0},

View File

@@ -20,8 +20,9 @@ namespace omath::source_engine
} }
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov, Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near, 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 } // namespace omath::source_engine

View File

@@ -35,8 +35,12 @@ namespace omath::unity_engine
* mat_rotation_axis_x<float, MatStoreType::ROW_MAJOR>(angles.pitch); * mat_rotation_axis_x<float, MatStoreType::ROW_MAJOR>(angles.pitch);
} }
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near, 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<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near, far);
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 } // namespace omath::unity_engine

View File

@@ -19,8 +19,9 @@ namespace omath::unity_engine
} }
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov, Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near, 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 } // namespace omath::unity_engine

View File

@@ -35,8 +35,12 @@ namespace omath::unreal_engine
* mat_rotation_axis_y<float, MatStoreType::ROW_MAJOR>(angles.pitch); * mat_rotation_axis_y<float, MatStoreType::ROW_MAJOR>(angles.pitch);
} }
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near, 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<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near, far);
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 } // namespace omath::unreal_engine

View File

@@ -19,8 +19,9 @@ namespace omath::unreal_engine
} }
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov, Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near, 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 } // namespace omath::unreal_engine

View File

@@ -21,6 +21,9 @@
#include <omath/engines/unreal_engine/traits/camera_trait.hpp> #include <omath/engines/unreal_engine/traits/camera_trait.hpp>
#include <omath/engines/source_engine/traits/pred_engine_trait.hpp> #include <omath/engines/source_engine/traits/pred_engine_trait.hpp>
#include <omath/engines/source_engine/traits/camera_trait.hpp>
#include <omath/engines/cry_engine/traits/camera_trait.hpp>
#include <omath/projectile_prediction/projectile.hpp> #include <omath/projectile_prediction/projectile.hpp>
#include <omath/projectile_prediction/target.hpp> #include <omath/projectile_prediction/target.hpp>
@@ -218,9 +221,14 @@ TEST(TraitTests, Frostbite_Pred_And_Mesh_And_Camera)
// CameraTrait look at should be callable // CameraTrait look at should be callable
const auto angles = e::CameraTrait::calc_look_at_angle({0, 0, 0}, {0, 1, 1}); const auto angles = e::CameraTrait::calc_look_at_angle({0, 0, 0}, {0, 1, 1});
(void)angles; (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); const auto expected = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f);
expect_matrix_near(proj, expected); 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) TEST(TraitTests, IW_Pred_And_Mesh_And_Camera)
@@ -264,10 +272,15 @@ TEST(TraitTests, IW_Pred_And_Mesh_And_Camera)
e::ViewAngles va; e::ViewAngles va;
expect_matrix_near(e::MeshTrait::rotation_matrix(va), e::rotation_matrix(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); const auto expected = e::calc_perspective_projection_matrix(45.f, 1920.f / 1080.f, 0.1f, 1000.f);
expect_matrix_near(proj, expected); 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 // non-airborne
t.m_is_airborne = false; t.m_is_airborne = false;
const auto pred_ground_iw = e::PredEngineTrait::predict_target_position(t, 2.f, 9.81f); 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; e::ViewAngles va;
expect_matrix_near(e::MeshTrait::rotation_matrix(va), e::rotation_matrix(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); const auto expected = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f);
expect_matrix_near(proj, expected); 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 // non-airborne
t.m_is_airborne = false; t.m_is_airborne = false;
const auto pred_ground_gl = e::PredEngineTrait::predict_target_position(t, 2.f, 9.81f); 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; e::ViewAngles va;
expect_matrix_near(e::MeshTrait::rotation_matrix(va), e::rotation_matrix(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); const auto expected = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f);
expect_matrix_near(proj, expected); 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 // non-airborne
t.m_is_airborne = false; t.m_is_airborne = false;
const auto pred_ground_unity = e::PredEngineTrait::predict_target_position(t, 2.f, 9.81f); 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; e::ViewAngles va;
expect_matrix_near(e::MeshTrait::rotation_matrix(va), e::rotation_matrix(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); const auto expected = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f);
expect_matrix_near(proj, expected); 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 // non-airborne
t.m_is_airborne = false; t.m_is_airborne = false;
const auto pred_ground_unreal = e::PredEngineTrait::predict_target_position(t, 2.f, 9.81f); 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); 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<float>({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<float, MatStoreType::COLUMN_MAJOR>({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<float>({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<float, MatStoreType::COLUMN_MAJOR>({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<float>({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);
}

View File

@@ -240,4 +240,126 @@ TEST(UnitTestMatStandalone, MatPerspectiveLeftHanded)
projected /= projected.at(3, 0); projected /= projected.at(3, 0);
EXPECT_TRUE(projected.at(2, 0) > -1.0f && projected.at(2, 0) < 0.f); EXPECT_TRUE(projected.at(2, 0) > -1.0f && projected.at(2, 0) < 0.f);
}
TEST(UnitTestMatStandalone, MatPerspectiveLeftHandedZeroToOne)
{
const auto proj = mat_perspective_left_handed<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
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<float>({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<float>({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<float>({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<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
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<float>({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<float>({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<float>({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<float, MatStoreType::ROW_MAJOR,
NDCDepthRange::NEGATIVE_ONE_TO_ONE>(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<float>({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<float>({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<float> left_handed = {0, 2, 10};
constexpr omath::Vector3<float> right_handed = {0, 2, -10};
const auto proj_lh = mat_perspective_left_handed<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
90.f, 16.f / 9.f, 0.1f, 1000.f);
const auto proj_rh = mat_perspective_right_handed<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
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<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
-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<float>({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<float>({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<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
-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<float>({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<float>({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<float, MatStoreType::ROW_MAJOR,
NDCDepthRange::NEGATIVE_ONE_TO_ONE>(-1.f, 1.f, -1.f, 1.f, 0.1f, 100.f);
EXPECT_EQ(ortho_default, ortho_explicit);
} }