added constexpr engine traits

This commit is contained in:
2026-06-14 22:46:24 +03:00
parent 136cd39496
commit 91d9335f83
32 changed files with 879 additions and 859 deletions
@@ -8,31 +8,87 @@
namespace omath::frostbite_engine namespace omath::frostbite_engine
{ {
[[nodiscard]] [[nodiscard]]
Vector3<float> forward_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]] [[nodiscard]]
Vector3<float> right_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Vector3<float> up_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Vector3<float> extract_origin(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
{
return mat_camera_view<float, MatStoreType::ROW_MAJOR>(forward_vector(angles), right_vector(angles),
up_vector(angles), cam_origin);
}
[[nodiscard]] [[nodiscard]]
Vector3<float> extract_scale(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z<float, MatStoreType::ROW_MAJOR>(angles.roll)
* mat_rotation_axis_y<float, MatStoreType::ROW_MAJOR>(angles.yaw)
* mat_rotation_axis_x<float, MatStoreType::ROW_MAJOR>(angles.pitch);
}
[[nodiscard]] [[nodiscard]]
ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
[[nodiscard]] [[nodiscard]]
Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far, inline OMATH_CONSTEXPR Vector3<float> extract_scale(const Mat4X4& mat) noexcept
NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept; {
return mat_extract_scale(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
PitchAngle::from_degrees(angles.x),
YawAngle::from_degrees(angles.y),
RollAngle::from_degrees(angles.z),
};
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_perspective_projection_matrix(
const float field_of_view, const float aspect_ratio, const float near, const float far,
const NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept
{
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return mat_perspective_left_handed_vertical_fov<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near, far);
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return mat_perspective_left_handed_vertical_fov<float, MatStoreType::ROW_MAJOR,
NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
field_of_view, aspect_ratio, near, far);
std::unreachable();
}
template<class FloatingType> template<class FloatingType>
requires std::is_floating_point_v<FloatingType> requires std::is_floating_point_v<FloatingType>
@@ -4,6 +4,7 @@
#pragma once #pragma once
#include "omath/engines/frostbite_engine/formulas.hpp" #include "omath/engines/frostbite_engine/formulas.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/projection/camera.hpp" #include "omath/projection/camera.hpp"
namespace omath::frostbite_engine namespace omath::frostbite_engine
@@ -12,13 +13,29 @@ namespace omath::frostbite_engine
{ {
public: public:
[[nodiscard]] [[nodiscard]]
static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept; OMATH_CONSTEXPR static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_radians(-internal::asin(direction.y)),
YawAngle::from_radians(internal::atan2(direction.x, direction.z)), RollAngle::from_radians(0.f)};
}
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; OMATH_CONSTEXPR static Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
{
return frostbite_engine::calc_view_matrix(angles, cam_origin);
}
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, OMATH_CONSTEXPR static Mat4X4
float near, float far, NDCDepthRange ndc_depth_range) noexcept; calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
const float near, const float far, const NDCDepthRange ndc_depth_range) noexcept
{
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
+65 -11
View File
@@ -8,31 +8,85 @@
namespace omath::iw_engine namespace omath::iw_engine
{ {
[[nodiscard]] [[nodiscard]]
Vector3<float> forward_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]] [[nodiscard]]
Vector3<float> right_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Vector3<float> up_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Vector3<float> extract_origin(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z(angles.yaw) * mat_rotation_axis_y(angles.pitch) * mat_rotation_axis_x(angles.roll);
}
[[nodiscard]] [[nodiscard]]
Vector3<float> extract_scale(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
[[nodiscard]] [[nodiscard]]
ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Vector3<float> extract_scale(const Mat4X4& mat) noexcept
{
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; return mat_extract_scale(mat);
}
[[nodiscard]] [[nodiscard]]
Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far, inline OMATH_CONSTEXPR ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept; {
const auto angles = mat_extract_rotation_zyx(mat);
return {
PitchAngle::from_degrees(angles.y),
YawAngle::from_degrees(angles.z),
RollAngle::from_degrees(angles.x),
};
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
{
return mat_camera_view(forward_vector(angles), right_vector(angles), up_vector(angles), cam_origin);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_perspective_projection_matrix(
const float field_of_view, const float aspect_ratio, const float near, const float far,
const NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept
{
constexpr float k_source_reference_aspect = 4.f / 3.f;
const auto vertical_fov = angles::horizontal_fov_to_vertical(field_of_view, k_source_reference_aspect);
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return mat_perspective_left_handed_vertical_fov<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
vertical_fov, aspect_ratio, near, far);
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return mat_perspective_left_handed_vertical_fov<float, MatStoreType::ROW_MAJOR,
NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
vertical_fov, aspect_ratio, near, far);
std::unreachable();
}
template<class FloatingType> template<class FloatingType>
requires std::is_floating_point_v<FloatingType> requires std::is_floating_point_v<FloatingType>
@@ -3,7 +3,8 @@
// //
#pragma once #pragma once
#include "omath/engines/iw_engine/constants.hpp" #include "omath/engines/iw_engine/formulas.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/projection/camera.hpp" #include "omath/projection/camera.hpp"
namespace omath::iw_engine namespace omath::iw_engine
@@ -12,13 +13,29 @@ namespace omath::iw_engine
{ {
public: public:
[[nodiscard]] [[nodiscard]]
static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept; OMATH_CONSTEXPR static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_radians(-internal::asin(direction.z)),
YawAngle::from_radians(internal::atan2(direction.y, direction.x)), RollAngle::from_radians(0.f)};
}
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; OMATH_CONSTEXPR static Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
{
return iw_engine::calc_view_matrix(angles, cam_origin);
}
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, OMATH_CONSTEXPR static Mat4X4
float near, float far, NDCDepthRange ndc_depth_range) noexcept; calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
const float near, const float far, const NDCDepthRange ndc_depth_range) noexcept
{
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
@@ -7,31 +7,89 @@
namespace omath::opengl_engine namespace omath::opengl_engine
{ {
[[nodiscard]] [[nodiscard]]
Vector3<float> forward_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]] [[nodiscard]]
Vector3<float> right_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec =
rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_forward);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Vector3<float> up_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec =
rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_right);
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_up);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Vector3<float> extract_origin(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
{
return mat_look_at_right_handed(cam_origin, cam_origin + forward_vector(angles), up_vector(angles));
}
[[nodiscard]] [[nodiscard]]
Vector3<float> extract_scale(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z<float, MatStoreType::COLUMN_MAJOR>(angles.roll)
* mat_rotation_axis_y<float, MatStoreType::COLUMN_MAJOR>(angles.yaw)
* mat_rotation_axis_x<float, MatStoreType::COLUMN_MAJOR>(angles.pitch);
}
[[nodiscard]] [[nodiscard]]
ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
[[nodiscard]] [[nodiscard]]
Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far, inline OMATH_CONSTEXPR Vector3<float> extract_scale(const Mat4X4& mat) noexcept
NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept; {
return mat_extract_scale(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
PitchAngle::from_degrees(angles.x),
YawAngle::from_degrees(angles.y),
RollAngle::from_degrees(angles.z),
};
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_perspective_projection_matrix(
const float field_of_view, const float aspect_ratio, const float near, const float far,
const NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept
{
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return mat_perspective_right_handed_vertical_fov<
float, MatStoreType::COLUMN_MAJOR, NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
field_of_view, aspect_ratio, near, far);
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return mat_perspective_right_handed_vertical_fov<
float, MatStoreType::COLUMN_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near, far);
std::unreachable();
}
template<class FloatingType> template<class FloatingType>
requires std::is_floating_point_v<FloatingType> requires std::is_floating_point_v<FloatingType>
@@ -3,7 +3,8 @@
// //
#pragma once #pragma once
#include "omath/engines/opengl_engine/constants.hpp" #include "omath/engines/opengl_engine/formulas.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/projection/camera.hpp" #include "omath/projection/camera.hpp"
namespace omath::opengl_engine namespace omath::opengl_engine
@@ -12,13 +13,30 @@ namespace omath::opengl_engine
{ {
public: public:
[[nodiscard]] [[nodiscard]]
static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept; OMATH_CONSTEXPR static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_radians(internal::asin(direction.y)),
YawAngle::from_radians(-internal::atan2(direction.x, -direction.z)),
RollAngle::from_radians(0.f)};
}
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; OMATH_CONSTEXPR static Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
{
return opengl_engine::calc_view_matrix(angles, cam_origin);
}
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, OMATH_CONSTEXPR static Mat4X4
float near, float far, NDCDepthRange ndc_depth_range) noexcept; calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
const float near, const float far, const NDCDepthRange ndc_depth_range) noexcept
{
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
+65 -10
View File
@@ -9,31 +9,86 @@
namespace omath::rage_engine namespace omath::rage_engine
{ {
[[nodiscard]] [[nodiscard]]
Vector3<float> forward_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]] [[nodiscard]]
Vector3<float> right_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Vector3<float> up_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Vector3<float> extract_origin(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
{
return mat_camera_view<float, MatStoreType::ROW_MAJOR>(forward_vector(angles), right_vector(angles),
up_vector(angles), cam_origin);
}
[[nodiscard]] [[nodiscard]]
Vector3<float> extract_scale(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z<float, MatStoreType::ROW_MAJOR>(angles.yaw)
* mat_rotation_axis_y<float, MatStoreType::ROW_MAJOR>(angles.roll)
* mat_rotation_axis_x<float, MatStoreType::ROW_MAJOR>(angles.pitch);
}
[[nodiscard]] [[nodiscard]]
ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
[[nodiscard]] [[nodiscard]]
Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far, inline OMATH_CONSTEXPR Vector3<float> extract_scale(const Mat4X4& mat) noexcept
NDCDepthRange ndc_depth_range = NDCDepthRange::ZERO_TO_ONE) noexcept; {
return mat_extract_scale(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
PitchAngle::from_degrees(angles.x),
YawAngle::from_degrees(angles.z),
RollAngle::from_degrees(angles.y),
};
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_perspective_projection_matrix(
const float field_of_view, const float aspect_ratio, const float near, const float far,
const NDCDepthRange ndc_depth_range = NDCDepthRange::ZERO_TO_ONE) noexcept
{
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return mat_perspective_left_handed_vertical_fov<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near, far);
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return mat_perspective_left_handed_vertical_fov<float, MatStoreType::ROW_MAJOR,
NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
field_of_view, aspect_ratio, near, far);
std::unreachable();
}
template<class FloatingType> template<class FloatingType>
requires std::is_floating_point_v<FloatingType> requires std::is_floating_point_v<FloatingType>
@@ -4,6 +4,7 @@
#pragma once #pragma once
#include "omath/engines/rage_engine/formulas.hpp" #include "omath/engines/rage_engine/formulas.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/projection/camera.hpp" #include "omath/projection/camera.hpp"
namespace omath::rage_engine namespace omath::rage_engine
@@ -12,13 +13,29 @@ namespace omath::rage_engine
{ {
public: public:
[[nodiscard]] [[nodiscard]]
static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept; OMATH_CONSTEXPR static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_radians(internal::asin(direction.z)),
YawAngle::from_radians(-internal::atan2(direction.x, direction.y)), RollAngle::from_radians(0.f)};
}
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; OMATH_CONSTEXPR static Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
{
return rage_engine::calc_view_matrix(angles, cam_origin);
}
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, OMATH_CONSTEXPR static Mat4X4
float near, float far, NDCDepthRange ndc_depth_range) noexcept; calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
const float near, const float far, const NDCDepthRange ndc_depth_range) noexcept
{
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far,
ndc_depth_range);
}
}; };
} // namespace omath::rage_engine } // namespace omath::rage_engine
@@ -7,31 +7,85 @@
namespace omath::source_engine namespace omath::source_engine
{ {
[[nodiscard]] [[nodiscard]]
Vector3<float> forward_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]] [[nodiscard]]
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Vector3<float> extract_origin(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z(angles.yaw) * mat_rotation_axis_y(angles.pitch) * mat_rotation_axis_x(angles.roll);
}
[[nodiscard]] [[nodiscard]]
Vector3<float> extract_scale(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
[[nodiscard]] [[nodiscard]]
ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Vector3<float> extract_scale(const Mat4X4& mat) noexcept
{
return mat_extract_scale(mat);
}
[[nodiscard]] [[nodiscard]]
Vector3<float> right_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
PitchAngle::from_degrees(angles.y),
YawAngle::from_degrees(angles.z),
RollAngle::from_degrees(angles.x),
};
}
[[nodiscard]] [[nodiscard]]
Vector3<float> up_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far, inline OMATH_CONSTEXPR Vector3<float> up_vector(const ViewAngles& angles) noexcept
NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept; {
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
{
return mat_camera_view(forward_vector(angles), right_vector(angles), up_vector(angles), cam_origin);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_perspective_projection_matrix(
const float field_of_view, const float aspect_ratio, const float near, const float far,
const NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept
{
constexpr float k_source_reference_aspect = 4.f / 3.f;
const auto vertical_fov = angles::horizontal_fov_to_vertical(field_of_view, k_source_reference_aspect);
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return mat_perspective_left_handed_vertical_fov<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
vertical_fov, aspect_ratio, near, far);
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return mat_perspective_left_handed_vertical_fov<float, MatStoreType::ROW_MAJOR,
NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
vertical_fov, aspect_ratio, near, far);
std::unreachable();
}
template<class FloatingType> template<class FloatingType>
requires std::is_floating_point_v<FloatingType> requires std::is_floating_point_v<FloatingType>
@@ -3,7 +3,8 @@
// //
#pragma once #pragma once
#include "omath/engines/source_engine/constants.hpp" #include "omath/engines/source_engine/formulas.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/projection/camera.hpp" #include "omath/projection/camera.hpp"
namespace omath::source_engine namespace omath::source_engine
@@ -12,13 +13,29 @@ namespace omath::source_engine
{ {
public: public:
[[nodiscard]] [[nodiscard]]
static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept; OMATH_CONSTEXPR static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_radians(-internal::asin(direction.z)),
YawAngle::from_radians(internal::atan2(direction.y, direction.x)), RollAngle::from_radians(0.f)};
}
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; OMATH_CONSTEXPR static Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
{
return source_engine::calc_view_matrix(angles, cam_origin);
}
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, OMATH_CONSTEXPR static Mat4X4
float near, float far, NDCDepthRange ndc_depth_range) noexcept; calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
const float near, const float far, const NDCDepthRange ndc_depth_range) noexcept
{
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
+65 -10
View File
@@ -8,31 +8,86 @@
namespace omath::unity_engine namespace omath::unity_engine
{ {
[[nodiscard]] [[nodiscard]]
Vector3<float> forward_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]] [[nodiscard]]
Vector3<float> right_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Vector3<float> up_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Vector3<float> extract_origin(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
{
return mat_camera_view<float, MatStoreType::ROW_MAJOR>(-forward_vector(angles), right_vector(angles),
up_vector(angles), cam_origin);
}
[[nodiscard]] [[nodiscard]]
Vector3<float> extract_scale(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z<float, MatStoreType::ROW_MAJOR>(angles.roll)
* mat_rotation_axis_y<float, MatStoreType::ROW_MAJOR>(angles.yaw)
* mat_rotation_axis_x<float, MatStoreType::ROW_MAJOR>(angles.pitch);
}
[[nodiscard]] [[nodiscard]]
ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
[[nodiscard]] [[nodiscard]]
Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far, inline OMATH_CONSTEXPR Vector3<float> extract_scale(const Mat4X4& mat) noexcept
NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept; {
return mat_extract_scale(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
PitchAngle::from_degrees(angles.x),
YawAngle::from_degrees(angles.y),
RollAngle::from_degrees(angles.z),
};
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_perspective_projection_matrix(
const float field_of_view, const float aspect_ratio, const float near, const float far,
const NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept
{
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return omath::mat_perspective_right_handed_vertical_fov<
float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near, far);
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return omath::mat_perspective_right_handed_vertical_fov<
float, MatStoreType::ROW_MAJOR, NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
field_of_view, aspect_ratio, near, far);
std::unreachable();
}
template<class FloatingType> template<class FloatingType>
requires std::is_floating_point_v<FloatingType> requires std::is_floating_point_v<FloatingType>
@@ -4,6 +4,7 @@
#pragma once #pragma once
#include "omath/engines/unity_engine/formulas.hpp" #include "omath/engines/unity_engine/formulas.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/projection/camera.hpp" #include "omath/projection/camera.hpp"
namespace omath::unity_engine namespace omath::unity_engine
@@ -12,13 +13,29 @@ namespace omath::unity_engine
{ {
public: public:
[[nodiscard]] [[nodiscard]]
static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept; OMATH_CONSTEXPR static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_radians(-internal::asin(direction.y)),
YawAngle::from_radians(internal::atan2(direction.x, direction.z)), RollAngle::from_radians(0.f)};
}
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept; OMATH_CONSTEXPR static Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
{
return unity_engine::calc_view_matrix(angles, cam_origin);
}
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, OMATH_CONSTEXPR static Mat4X4
float near, float far, NDCDepthRange ndc_depth_range) noexcept; calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
const float near, const float far, const NDCDepthRange ndc_depth_range) noexcept
{
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
@@ -8,31 +8,86 @@
namespace omath::unreal_engine namespace omath::unreal_engine
{ {
[[nodiscard]] [[nodiscard]]
Vector3<double> forward_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]] [[nodiscard]]
Vector3<double> right_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<double> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Vector3<double> up_vector(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<double> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<double>& cam_origin) noexcept; return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; inline OMATH_CONSTEXPR Vector3<double> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
[[nodiscard]] [[nodiscard]]
Vector3<double> extract_origin(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<double>& cam_origin) noexcept
{
return mat_camera_view<double, MatStoreType::ROW_MAJOR>(forward_vector(angles), right_vector(angles),
up_vector(angles), cam_origin);
}
[[nodiscard]] [[nodiscard]]
Vector3<double> extract_scale(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z<double, MatStoreType::ROW_MAJOR>(angles.yaw)
* mat_rotation_axis_y<double, MatStoreType::ROW_MAJOR>(-angles.pitch)
* mat_rotation_axis_x<double, MatStoreType::ROW_MAJOR>(-angles.roll);
}
[[nodiscard]] [[nodiscard]]
ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept; inline OMATH_CONSTEXPR Vector3<double> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
[[nodiscard]] [[nodiscard]]
Mat4X4 calc_perspective_projection_matrix(double field_of_view, double aspect_ratio, double near, double far, inline OMATH_CONSTEXPR Vector3<double> extract_scale(const Mat4X4& mat) noexcept
NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept; {
return mat_extract_scale(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
PitchAngle::from_degrees(-angles.y),
YawAngle::from_degrees(angles.z),
RollAngle::from_degrees(-angles.x),
};
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_perspective_projection_matrix(
const double field_of_view, const double aspect_ratio, const double near, const double far,
const NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept
{
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return mat_perspective_left_handed_horizontal_fov<
double, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near, far);
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return mat_perspective_left_handed_horizontal_fov<
double, MatStoreType::ROW_MAJOR, NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
field_of_view, aspect_ratio, near, far);
std::unreachable();
}
template<class FloatingType> template<class FloatingType>
requires std::is_floating_point_v<FloatingType> requires std::is_floating_point_v<FloatingType>
@@ -4,6 +4,7 @@
#pragma once #pragma once
#include "omath/engines/unreal_engine/formulas.hpp" #include "omath/engines/unreal_engine/formulas.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/projection/camera.hpp" #include "omath/projection/camera.hpp"
namespace omath::unreal_engine namespace omath::unreal_engine
@@ -12,13 +13,29 @@ namespace omath::unreal_engine
{ {
public: public:
[[nodiscard]] [[nodiscard]]
static ViewAngles calc_look_at_angle(const Vector3<double>& cam_origin, const Vector3<double>& look_at) noexcept; OMATH_CONSTEXPR static ViewAngles calc_look_at_angle(const Vector3<double>& cam_origin,
const Vector3<double>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_radians(internal::asin(direction.z)),
YawAngle::from_radians(internal::atan2(direction.y, direction.x)), RollAngle::from_radians(0.f)};
}
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<double>& cam_origin) noexcept; OMATH_CONSTEXPR static Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<double>& cam_origin) noexcept
{
return unreal_engine::calc_view_matrix(angles, cam_origin);
}
[[nodiscard]] [[nodiscard]]
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, OMATH_CONSTEXPR static Mat4X4
double near, double far, NDCDepthRange ndc_depth_range) noexcept; calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
const double near, const double far, const NDCDepthRange ndc_depth_range) noexcept
{
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
@@ -16,133 +16,169 @@
namespace omath::internal namespace omath::internal
{ {
template<class Type> template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] [[nodiscard]]
OMATH_CONSTEXPR Type sin(const Type& value) noexcept OMATH_CONSTEXPR Type sin(const Type& value) noexcept
{ {
#ifdef OMATH_USE_GCEM #ifdef OMATH_USE_GCEM
if (std::is_constant_evaluated()) if consteval
{
return gcem::sin(value); return gcem::sin(value);
}
#endif #endif
return std::sin(value); return std::sin(value);
} }
template<class Type> template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] [[nodiscard]]
OMATH_CONSTEXPR Type cos(const Type& value) noexcept OMATH_CONSTEXPR Type cos(const Type& value) noexcept
{ {
#ifdef OMATH_USE_GCEM #ifdef OMATH_USE_GCEM
if (std::is_constant_evaluated()) if consteval
{
return gcem::cos(value); return gcem::cos(value);
}
#endif #endif
return std::cos(value); return std::cos(value);
} }
template<class Type> template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] [[nodiscard]]
OMATH_CONSTEXPR Type tan(const Type& value) noexcept OMATH_CONSTEXPR Type tan(const Type& value) noexcept
{ {
#ifdef OMATH_USE_GCEM #ifdef OMATH_USE_GCEM
if (std::is_constant_evaluated()) if consteval
{
return gcem::tan(value); return gcem::tan(value);
}
#endif #endif
return std::tan(value); return std::tan(value);
} }
template<class Type> template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] [[nodiscard]]
OMATH_CONSTEXPR Type atan(const Type& value) noexcept OMATH_CONSTEXPR Type atan(const Type& value) noexcept
{ {
#ifdef OMATH_USE_GCEM #ifdef OMATH_USE_GCEM
if (std::is_constant_evaluated()) if consteval
{
return gcem::atan(value); return gcem::atan(value);
}
#endif #endif
return std::atan(value); return std::atan(value);
} }
template<class Type> template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] [[nodiscard]]
OMATH_CONSTEXPR Type atan2(const Type& y, const Type& x) noexcept OMATH_CONSTEXPR Type atan2(const Type& y, const Type& x) noexcept
{ {
#ifdef OMATH_USE_GCEM #ifdef OMATH_USE_GCEM
if (std::is_constant_evaluated()) if consteval
{
return gcem::atan2(y, x); return gcem::atan2(y, x);
}
#endif #endif
return std::atan2(y, x); return std::atan2(y, x);
} }
template<class Type> template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] [[nodiscard]]
OMATH_CONSTEXPR Type asin(const Type& value) noexcept OMATH_CONSTEXPR Type asin(const Type& value) noexcept
{ {
#ifdef OMATH_USE_GCEM #ifdef OMATH_USE_GCEM
if (std::is_constant_evaluated()) if consteval
{
return gcem::asin(value); return gcem::asin(value);
}
#endif #endif
return std::asin(value); return std::asin(value);
} }
template<class Type> template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] [[nodiscard]]
OMATH_CONSTEXPR Type acos(const Type& value) noexcept OMATH_CONSTEXPR Type acos(const Type& value) noexcept
{ {
#ifdef OMATH_USE_GCEM #ifdef OMATH_USE_GCEM
if (std::is_constant_evaluated()) if consteval
{
return gcem::acos(value); return gcem::acos(value);
}
#endif #endif
return std::acos(value); return std::acos(value);
} }
template<class Type> template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] [[nodiscard]]
OMATH_CONSTEXPR Type sqrt(const Type& value) noexcept OMATH_CONSTEXPR Type sqrt(const Type& value) noexcept
{ {
#ifdef OMATH_USE_GCEM #ifdef OMATH_USE_GCEM
if (std::is_constant_evaluated()) if consteval
{
return gcem::sqrt(value); return gcem::sqrt(value);
}
#endif #endif
return std::sqrt(value); return std::sqrt(value);
} }
template<class Type> template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] [[nodiscard]]
OMATH_CONSTEXPR Type hypot(const Type& x, const Type& y) noexcept OMATH_CONSTEXPR Type hypot(const Type& x, const Type& y) noexcept
{ {
#ifdef OMATH_USE_GCEM #ifdef OMATH_USE_GCEM
if (std::is_constant_evaluated()) if consteval
{
return gcem::hypot(x, y); return gcem::hypot(x, y);
}
#endif #endif
return std::hypot(x, y); return std::hypot(x, y);
} }
template<class Type> template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] [[nodiscard]]
OMATH_CONSTEXPR Type hypot(const Type& x, const Type& y, const Type& z) noexcept OMATH_CONSTEXPR Type hypot(const Type& x, const Type& y, const Type& z) noexcept
{ {
#ifdef OMATH_USE_GCEM #ifdef OMATH_USE_GCEM
if (std::is_constant_evaluated()) if consteval
{
return gcem::sqrt(x * x + y * y + z * z); return gcem::sqrt(x * x + y * y + z * z);
}
#endif #endif
return std::hypot(x, y, z); return std::hypot(x, y, z);
} }
template<class Type> template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] [[nodiscard]]
OMATH_CONSTEXPR Type abs(const Type& value) noexcept OMATH_CONSTEXPR Type abs(const Type& value) noexcept
{ {
#ifdef OMATH_USE_GCEM #ifdef OMATH_USE_GCEM
if (std::is_constant_evaluated()) if consteval
{
return gcem::abs(value); return gcem::abs(value);
}
#endif #endif
return std::abs(value); return std::abs(value);
} }
template<class Type> template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] [[nodiscard]]
OMATH_CONSTEXPR Type fmod(const Type& dividend, const Type& divisor) noexcept OMATH_CONSTEXPR Type fmod(const Type& dividend, const Type& divisor) noexcept
{ {
#ifdef OMATH_USE_GCEM #ifdef OMATH_USE_GCEM
if (std::is_constant_evaluated()) if consteval
{
return gcem::fmod(dividend, divisor); return gcem::fmod(dividend, divisor);
}
#endif #endif
return std::fmod(dividend, divisor); return std::fmod(dividend, divisor);
} }
+118 -55
View File
@@ -6,6 +6,7 @@
#include "omath/3d_primitives/aabb.hpp" #include "omath/3d_primitives/aabb.hpp"
#include "omath/3d_primitives/obb.hpp" #include "omath/3d_primitives/obb.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/linear_algebra/mat.hpp" #include "omath/linear_algebra/mat.hpp"
#include "omath/linear_algebra/triangle.hpp" #include "omath/linear_algebra/triangle.hpp"
#include "omath/linear_algebra/vector3.hpp" #include "omath/linear_algebra/vector3.hpp"
@@ -84,8 +85,9 @@ namespace omath::projection
}; };
~Camera() = default; ~Camera() = default;
Camera(const Vector3<NumericType>& position, const ViewAnglesType& view_angles, const ViewPort& view_port, constexpr Camera(const Vector3<NumericType>& position, const ViewAnglesType& view_angles,
const FieldOfView& fov, const NumericType near, const NumericType far) noexcept const ViewPort& view_port, const FieldOfView& fov, const NumericType near,
const NumericType far) noexcept
: m_view_port(view_port), m_field_of_view(fov), m_far_plane_distance(far), m_near_plane_distance(near), : m_view_port(view_port), m_field_of_view(fov), m_far_plane_distance(far), m_near_plane_distance(near),
m_view_angles(view_angles), m_origin(position) m_view_angles(view_angles), m_origin(position)
{ {
@@ -102,18 +104,18 @@ namespace omath::projection
// NEGATIVE_ONE_TO_ONE) share the same m[0,0]/m[1,1] layout, so this works // NEGATIVE_ONE_TO_ONE) share the same m[0,0]/m[1,1] layout, so this works
// regardless of the NDC depth range. // regardless of the NDC depth range.
[[nodiscard("You must use extracted projection params")]] [[nodiscard("You must use extracted projection params")]]
static ProjectionParams extract_projection_params(const Mat4X4Type& proj_matrix) noexcept OMATH_CONSTEXPR static ProjectionParams extract_projection_params(const Mat4X4Type& proj_matrix) noexcept
{ {
// m[1,1] == 1 / tan(fov/2) => fov = 2 * atan(1 / m[1,1]) // m[1,1] == 1 / tan(fov/2) => fov = 2 * atan(1 / m[1,1])
const auto f = proj_matrix.at(1, 1); const auto f = proj_matrix.at(1, 1);
// m[0,0] == m[1,1] / aspect_ratio => aspect = m[1,1] / m[0,0] // m[0,0] == m[1,1] / aspect_ratio => aspect = m[1,1] / m[0,0]
const auto fov_radians = NumericType{2} * std::atan(NumericType{1} / f); const auto fov_radians = NumericType{2} * internal::atan(NumericType{1} / f);
return {FieldOfView::from_radians(static_cast<typename FieldOfView::ArithmeticType>(fov_radians)), return {FieldOfView::from_radians(static_cast<typename FieldOfView::ArithmeticType>(fov_radians)),
f / proj_matrix.at(0, 0)}; f / proj_matrix.at(0, 0)};
} }
[[nodiscard("You must use calculated view angles")]] [[nodiscard("You must use calculated view angles")]]
static ViewAnglesType calc_view_angles_from_view_matrix(const Mat4X4Type& view_matrix) noexcept OMATH_CONSTEXPR static ViewAnglesType calc_view_angles_from_view_matrix(const Mat4X4Type& view_matrix) noexcept
{ {
Vector3<NumericType> forward_vector = {view_matrix[2, 0], view_matrix[2, 1], view_matrix[2, 2]}; Vector3<NumericType> forward_vector = {view_matrix[2, 0], view_matrix[2, 1], view_matrix[2, 2]};
if constexpr (axes.inverted_forward) if constexpr (axes.inverted_forward)
@@ -122,7 +124,7 @@ namespace omath::projection
} }
[[nodiscard("You must use calculated origin")]] [[nodiscard("You must use calculated origin")]]
static Vector3<NumericType> calc_origin_from_view_matrix(const Mat4X4Type& view_matrix) noexcept constexpr static Vector3<NumericType> calc_origin_from_view_matrix(const Mat4X4Type& view_matrix) noexcept
{ {
// The view matrix is R * T(-origin), so the last column stores t = -R * origin. // The view matrix is R * T(-origin), so the last column stores t = -R * origin.
// Recovering origin: origin = -R^T * t // Recovering origin: origin = -R^T * t
@@ -136,40 +138,58 @@ namespace omath::projection
}; };
} }
void look_at(const Vector3<NumericType>& target) OMATH_CONSTEXPR void look_at(const Vector3<NumericType>& target)
{ {
m_view_angles = TraitClass::calc_look_at_angle(m_origin, target); m_view_angles = TraitClass::calc_look_at_angle(m_origin, target);
m_view_projection_matrix = std::nullopt; m_view_projection_matrix = std::nullopt;
m_view_matrix = std::nullopt; m_view_matrix = std::nullopt;
} }
[[nodiscard("You must use calculated look-at angles")]] [[nodiscard("You must use calculated look-at angles")]]
ViewAnglesType calc_look_at_angles(const Vector3<NumericType>& look_to) const OMATH_CONSTEXPR ViewAnglesType calc_look_at_angles(const Vector3<NumericType>& look_to) const
{ {
return TraitClass::calc_look_at_angle(m_origin, look_to); return TraitClass::calc_look_at_angle(m_origin, look_to);
} }
[[nodiscard("You must use forward vector")]] [[nodiscard("You must use forward vector")]]
Vector3<NumericType> get_forward() const noexcept OMATH_CONSTEXPR Vector3<NumericType> get_forward() const noexcept
{ {
if consteval
{
const auto view_matrix = calc_view_matrix();
return {view_matrix[2, 0], view_matrix[2, 1], view_matrix[2, 2]};
}
const auto& view_matrix = get_view_matrix(); const auto& view_matrix = get_view_matrix();
return {view_matrix[2, 0], view_matrix[2, 1], view_matrix[2, 2]}; return {view_matrix[2, 0], view_matrix[2, 1], view_matrix[2, 2]};
} }
[[nodiscard("You must use right vector")]] [[nodiscard("You must use right vector")]]
Vector3<NumericType> get_right() const noexcept OMATH_CONSTEXPR Vector3<NumericType> get_right() const noexcept
{ {
if consteval
{
const auto view_matrix = calc_view_matrix();
return {view_matrix[0, 0], view_matrix[0, 1], view_matrix[0, 2]};
}
const auto& view_matrix = get_view_matrix(); const auto& view_matrix = get_view_matrix();
return {view_matrix[0, 0], view_matrix[0, 1], view_matrix[0, 2]}; return {view_matrix[0, 0], view_matrix[0, 1], view_matrix[0, 2]};
} }
[[nodiscard("You must use up vector")]] [[nodiscard("You must use up vector")]]
Vector3<NumericType> get_up() const noexcept OMATH_CONSTEXPR Vector3<NumericType> get_up() const noexcept
{ {
if consteval
{
const auto view_matrix = calc_view_matrix();
return {view_matrix[1, 0], view_matrix[1, 1], view_matrix[1, 2]};
}
const auto& view_matrix = get_view_matrix(); const auto& view_matrix = get_view_matrix();
return {view_matrix[1, 0], view_matrix[1, 1], view_matrix[1, 2]}; return {view_matrix[1, 0], view_matrix[1, 1], view_matrix[1, 2]};
} }
[[nodiscard("You must use absolute forward vector")]] [[nodiscard("You must use absolute forward vector")]]
Vector3<NumericType> get_abs_forward() const noexcept OMATH_CONSTEXPR Vector3<NumericType> get_abs_forward() const noexcept
{ {
if constexpr (axes.inverted_forward) if constexpr (axes.inverted_forward)
return -get_forward(); return -get_forward();
@@ -177,7 +197,7 @@ namespace omath::projection
} }
[[nodiscard("You must use absolute right vector")]] [[nodiscard("You must use absolute right vector")]]
Vector3<NumericType> get_abs_right() const noexcept OMATH_CONSTEXPR Vector3<NumericType> get_abs_right() const noexcept
{ {
if constexpr (axes.inverted_right) if constexpr (axes.inverted_right)
return -get_right(); return -get_right();
@@ -185,13 +205,32 @@ namespace omath::projection
} }
[[nodiscard("You must use absolute up vector")]] [[nodiscard("You must use absolute up vector")]]
Vector3<NumericType> get_abs_up() const noexcept OMATH_CONSTEXPR Vector3<NumericType> get_abs_up() const noexcept
{ {
return get_up(); return get_up();
} }
[[nodiscard("You must use calculated view-projection matrix")]]
OMATH_CONSTEXPR Mat4X4Type calc_view_projection_matrix() const noexcept
{
return calc_projection_matrix() * calc_view_matrix();
}
[[nodiscard("You must use calculated view matrix")]]
OMATH_CONSTEXPR Mat4X4Type calc_view_matrix() const noexcept
{
return TraitClass::calc_view_matrix(m_view_angles, m_origin);
}
[[nodiscard("You must use calculated projection matrix")]]
OMATH_CONSTEXPR Mat4X4Type calc_projection_matrix() const noexcept
{
return TraitClass::calc_projection_matrix(
m_field_of_view, m_view_port, m_near_plane_distance, m_far_plane_distance, depth_range);
}
[[nodiscard("You must use view-projection matrix")]] [[nodiscard("You must use view-projection matrix")]]
const Mat4X4Type& get_view_projection_matrix() const noexcept OMATH_CONSTEXPR const Mat4X4Type& get_view_projection_matrix() const noexcept
{ {
if (!m_view_projection_matrix.has_value()) if (!m_view_projection_matrix.has_value())
m_view_projection_matrix = get_projection_matrix() * get_view_matrix(); m_view_projection_matrix = get_projection_matrix() * get_view_matrix();
@@ -199,90 +238,89 @@ namespace omath::projection
return m_view_projection_matrix.value(); return m_view_projection_matrix.value();
} }
[[nodiscard("You must use view matrix")]] const Mat4X4Type& get_view_matrix() const noexcept [[nodiscard("You must use view matrix")]] OMATH_CONSTEXPR const Mat4X4Type& get_view_matrix() const noexcept
{ {
if (!m_view_matrix.has_value()) if (!m_view_matrix.has_value())
m_view_matrix = TraitClass::calc_view_matrix(m_view_angles, m_origin); m_view_matrix = calc_view_matrix();
return m_view_matrix.value(); return m_view_matrix.value();
} }
[[nodiscard("You must use projection matrix")]] const Mat4X4Type& get_projection_matrix() const noexcept [[nodiscard("You must use projection matrix")]] OMATH_CONSTEXPR const Mat4X4Type& get_projection_matrix() const noexcept
{ {
if (!m_projection_matrix.has_value()) if (!m_projection_matrix.has_value())
m_projection_matrix = TraitClass::calc_projection_matrix( m_projection_matrix = calc_projection_matrix();
m_field_of_view, m_view_port, m_near_plane_distance, m_far_plane_distance, depth_range);
return m_projection_matrix.value(); return m_projection_matrix.value();
} }
void set_field_of_view(const FieldOfView& fov) noexcept constexpr void set_field_of_view(const FieldOfView& fov) noexcept
{ {
m_field_of_view = fov; m_field_of_view = fov;
m_view_projection_matrix = std::nullopt; m_view_projection_matrix = std::nullopt;
m_projection_matrix = std::nullopt; m_projection_matrix = std::nullopt;
} }
void set_near_plane(const NumericType near_plane) noexcept constexpr void set_near_plane(const NumericType near_plane) noexcept
{ {
m_near_plane_distance = near_plane; m_near_plane_distance = near_plane;
m_view_projection_matrix = std::nullopt; m_view_projection_matrix = std::nullopt;
m_projection_matrix = std::nullopt; m_projection_matrix = std::nullopt;
} }
void set_far_plane(const NumericType far_plane) noexcept constexpr void set_far_plane(const NumericType far_plane) noexcept
{ {
m_far_plane_distance = far_plane; m_far_plane_distance = far_plane;
m_view_projection_matrix = std::nullopt; m_view_projection_matrix = std::nullopt;
m_projection_matrix = std::nullopt; m_projection_matrix = std::nullopt;
} }
void set_view_angles(const ViewAnglesType& view_angles) noexcept constexpr void set_view_angles(const ViewAnglesType& view_angles) noexcept
{ {
m_view_angles = view_angles; m_view_angles = view_angles;
m_view_projection_matrix = std::nullopt; m_view_projection_matrix = std::nullopt;
m_view_matrix = std::nullopt; m_view_matrix = std::nullopt;
} }
void set_origin(const Vector3<NumericType>& origin) noexcept constexpr void set_origin(const Vector3<NumericType>& origin) noexcept
{ {
m_origin = origin; m_origin = origin;
m_view_projection_matrix = std::nullopt; m_view_projection_matrix = std::nullopt;
m_view_matrix = std::nullopt; m_view_matrix = std::nullopt;
} }
void set_view_port(const ViewPort& view_port) noexcept constexpr void set_view_port(const ViewPort& view_port) noexcept
{ {
m_view_port = view_port; m_view_port = view_port;
m_view_projection_matrix = std::nullopt; m_view_projection_matrix = std::nullopt;
m_projection_matrix = std::nullopt; m_projection_matrix = std::nullopt;
} }
[[nodiscard("You must use field of view")]] const FieldOfView& get_field_of_view() const noexcept [[nodiscard("You must use field of view")]] constexpr const FieldOfView& get_field_of_view() const noexcept
{ {
return m_field_of_view; return m_field_of_view;
} }
[[nodiscard("You must use near plane")]] const NumericType& get_near_plane() const noexcept [[nodiscard("You must use near plane")]] constexpr const NumericType& get_near_plane() const noexcept
{ {
return m_near_plane_distance; return m_near_plane_distance;
} }
[[nodiscard("You must use far plane")]] const NumericType& get_far_plane() const noexcept [[nodiscard("You must use far plane")]] constexpr const NumericType& get_far_plane() const noexcept
{ {
return m_far_plane_distance; return m_far_plane_distance;
} }
[[nodiscard("You must use view angles")]] const ViewAnglesType& get_view_angles() const noexcept [[nodiscard("You must use view angles")]] constexpr const ViewAnglesType& get_view_angles() const noexcept
{ {
return m_view_angles; return m_view_angles;
} }
[[nodiscard("You must use origin")]] const Vector3<NumericType>& get_origin() const noexcept [[nodiscard("You must use origin")]] constexpr const Vector3<NumericType>& get_origin() const noexcept
{ {
return m_origin; return m_origin;
} }
template<ScreenStart screen_start = ScreenStart::TOP_LEFT_CORNER> template<ScreenStart screen_start = ScreenStart::TOP_LEFT_CORNER>
[[nodiscard("You must use screen position")]] std::expected<Vector3<NumericType>, Error> [[nodiscard("You must use screen position")]] OMATH_CONSTEXPR std::expected<Vector3<NumericType>, Error>
world_to_screen(const Vector3<NumericType>& world_position) const noexcept world_to_screen(const Vector3<NumericType>& world_position) const noexcept
{ {
const auto normalized_cords = world_to_view_port(world_position); const auto normalized_cords = world_to_view_port(world_position);
@@ -298,7 +336,7 @@ namespace omath::projection
std::unreachable(); std::unreachable();
} }
template<ScreenStart screen_start = ScreenStart::TOP_LEFT_CORNER> template<ScreenStart screen_start = ScreenStart::TOP_LEFT_CORNER>
[[nodiscard("You must use unclipped screen position")]] std::expected<Vector3<NumericType>, Error> [[nodiscard("You must use unclipped screen position")]] OMATH_CONSTEXPR std::expected<Vector3<NumericType>, Error>
world_to_screen_unclipped(const Vector3<NumericType>& world_position) const noexcept world_to_screen_unclipped(const Vector3<NumericType>& world_position) const noexcept
{ {
const auto normalized_cords = world_to_view_port(world_position, ViewPortClipping::MANUAL); const auto normalized_cords = world_to_view_port(world_position, ViewPortClipping::MANUAL);
@@ -314,7 +352,7 @@ namespace omath::projection
std::unreachable(); std::unreachable();
} }
[[nodiscard("You must use frustum culling result")]] bool [[nodiscard("You must use frustum culling result")]] OMATH_CONSTEXPR bool
is_culled_by_frustum(const Triangle<Vector3<NumericType>>& triangle) const noexcept is_culled_by_frustum(const Triangle<Vector3<NumericType>>& triangle) const noexcept
{ {
// Transform to clip space (before perspective divide) // Transform to clip space (before perspective divide)
@@ -382,7 +420,7 @@ namespace omath::projection
return false; return false;
} }
[[nodiscard("You must use AABB frustum culling result")]] bool [[nodiscard("You must use AABB frustum culling result")]] OMATH_CONSTEXPR bool
is_aabb_culled_by_frustum(const primitives::Aabb<NumericType>& aabb) const noexcept is_aabb_culled_by_frustum(const primitives::Aabb<NumericType>& aabb) const noexcept
{ {
// For each plane, find the AABB corner most in the direction of the plane normal // For each plane, find the AABB corner most in the direction of the plane normal
@@ -400,7 +438,7 @@ namespace omath::projection
return false; return false;
} }
[[nodiscard("You must use OBB frustum culling result")]] bool [[nodiscard("You must use OBB frustum culling result")]] OMATH_CONSTEXPR bool
is_obb_culled_by_frustum(const primitives::Obb<NumericType>& obb) const noexcept is_obb_culled_by_frustum(const primitives::Obb<NumericType>& obb) const noexcept
{ {
// For each plane, project the OBB extents onto the plane normal to get the // For each plane, project the OBB extents onto the plane normal to get the
@@ -410,9 +448,9 @@ namespace omath::projection
const Vector3<NumericType> normal{a, b, c}; const Vector3<NumericType> normal{a, b, c};
const auto center_distance = normal.dot(obb.center) + d; const auto center_distance = normal.dot(obb.center) + d;
const auto radius = obb.half_extents.x * std::abs(normal.dot(obb.axis_x)) const auto radius = obb.half_extents.x * internal::abs(normal.dot(obb.axis_x))
+ obb.half_extents.y * std::abs(normal.dot(obb.axis_y)) + obb.half_extents.y * internal::abs(normal.dot(obb.axis_y))
+ obb.half_extents.z * std::abs(normal.dot(obb.axis_z)); + obb.half_extents.z * internal::abs(normal.dot(obb.axis_z));
if (center_distance + radius < NumericType{0}) if (center_distance + radius < NumericType{0})
return true; return true;
@@ -421,13 +459,12 @@ namespace omath::projection
return false; return false;
} }
[[nodiscard("You must use view port position")]] std::expected<Vector3<NumericType>, Error> [[nodiscard("You must use view port position")]] OMATH_CONSTEXPR std::expected<Vector3<NumericType>, Error>
world_to_view_port(const Vector3<NumericType>& world_position, world_to_view_port(const Vector3<NumericType>& world_position,
const ViewPortClipping& clipping = ViewPortClipping::AUTO) const noexcept const ViewPortClipping& clipping = ViewPortClipping::AUTO) const noexcept
{ {
auto projected = get_view_projection_matrix() auto project_to_view_port = [this, &clipping](auto projected) -> std::expected<Vector3<NumericType>, Error>
* mat_column_from_vector<NumericType, Mat4X4Type::get_store_ordering()>(world_position); {
const auto& w = projected.at(3, 0); const auto& w = projected.at(3, 0);
constexpr auto eps = std::numeric_limits<NumericType>::epsilon(); constexpr auto eps = std::numeric_limits<NumericType>::epsilon();
if (w <= eps) if (w <= eps)
@@ -436,7 +473,8 @@ namespace omath::projection
projected /= w; projected /= w;
// ReSharper disable once CppTooWideScope // ReSharper disable once CppTooWideScope
const auto clipped_automatically = clipping == ViewPortClipping::AUTO && is_ndc_out_of_bounds(projected); const auto clipped_automatically =
clipping == ViewPortClipping::AUTO && is_ndc_out_of_bounds(projected);
if (clipped_automatically) if (clipped_automatically)
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS); return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
@@ -449,32 +487,56 @@ 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);
return Vector3<NumericType>{projected.at(0, 0), projected.at(1, 0), projected.at(2, 0)}; return Vector3<NumericType>{projected.at(0, 0), projected.at(1, 0), projected.at(2, 0)};
};
if consteval
{
auto projected =
calc_view_projection_matrix()
* mat_column_from_vector<NumericType, Mat4X4Type::get_store_ordering()>(world_position);
return project_to_view_port(projected);
}
auto projected = get_view_projection_matrix()
* mat_column_from_vector<NumericType, Mat4X4Type::get_store_ordering()>(world_position);
return project_to_view_port(projected);
} }
[[nodiscard("You must use world position")]] [[nodiscard("You must use world position")]]
std::expected<Vector3<NumericType>, Error> view_port_to_world(const Vector3<NumericType>& ndc) const noexcept OMATH_CONSTEXPR std::expected<Vector3<NumericType>, Error>
view_port_to_world(const Vector3<NumericType>& ndc) const noexcept
{ {
const auto inv_view_proj = get_view_projection_matrix().inverted(); auto view_port_to_world = [&ndc](const Mat4X4Type& view_projection) -> std::expected<Vector3<NumericType>, Error>
{
const auto inv_view_proj = view_projection.inverted();
if (!inv_view_proj) if (!inv_view_proj)
return std::unexpected(Error::INV_VIEW_PROJ_MAT_DET_EQ_ZERO); return std::unexpected(Error::INV_VIEW_PROJ_MAT_DET_EQ_ZERO);
auto inverted_projection = auto inverted_projection = inv_view_proj.value()
inv_view_proj.value() * mat_column_from_vector<NumericType, Mat4X4Type::get_store_ordering()>(ndc); * mat_column_from_vector<NumericType, Mat4X4Type::get_store_ordering()>(ndc);
const auto& w = inverted_projection.at(3, 0); const auto& w = inverted_projection.at(3, 0);
if (std::abs(w) < std::numeric_limits<NumericType>::epsilon()) if (internal::abs(w) < std::numeric_limits<NumericType>::epsilon())
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS); return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
inverted_projection /= w; inverted_projection /= w;
return Vector3<NumericType>{inverted_projection.at(0, 0), inverted_projection.at(1, 0), return Vector3<NumericType>{inverted_projection.at(0, 0), inverted_projection.at(1, 0),
inverted_projection.at(2, 0)}; inverted_projection.at(2, 0)};
};
if consteval
{
return view_port_to_world(calc_view_projection_matrix());
}
return view_port_to_world(get_view_projection_matrix());
} }
template<ScreenStart screen_start = ScreenStart::TOP_LEFT_CORNER> template<ScreenStart screen_start = ScreenStart::TOP_LEFT_CORNER>
[[nodiscard("You must use world position")]] [[nodiscard("You must use world position")]]
std::expected<Vector3<NumericType>, Error> OMATH_CONSTEXPR std::expected<Vector3<NumericType>, Error>
screen_to_world(const Vector3<NumericType>& screen_pos) const noexcept screen_to_world(const Vector3<NumericType>& screen_pos) const noexcept
{ {
return view_port_to_world(screen_to_ndc<screen_start>(screen_pos)); return view_port_to_world(screen_to_ndc<screen_start>(screen_pos));
@@ -482,7 +544,7 @@ namespace omath::projection
template<ScreenStart screen_start = ScreenStart::TOP_LEFT_CORNER> template<ScreenStart screen_start = ScreenStart::TOP_LEFT_CORNER>
[[nodiscard("You must use world position")]] [[nodiscard("You must use world position")]]
std::expected<Vector3<NumericType>, Error> OMATH_CONSTEXPR std::expected<Vector3<NumericType>, Error>
screen_to_world(const Vector2<NumericType>& screen_pos) const noexcept screen_to_world(const Vector2<NumericType>& screen_pos) const noexcept
{ {
const auto& [x, y] = screen_pos; const auto& [x, y] = screen_pos;
@@ -517,7 +579,8 @@ namespace omath::projection
// Top = r3 - r1 // Top = r3 - r1
// Near = r3 + r2 ([-1,1]) or r2 ([0,1]) // Near = r3 + r2 ([-1,1]) or r2 ([0,1])
// Far = r3 - r2 // Far = r3 - r2
[[nodiscard("You must use frustum planes")]] std::array<FrustumPlane, 6> extract_frustum_planes() const noexcept [[nodiscard("You must use frustum planes")]] OMATH_CONSTEXPR std::array<FrustumPlane, 6>
extract_frustum_planes() const noexcept
{ {
const auto& m = get_view_projection_matrix(); const auto& m = get_view_projection_matrix();
@@ -589,7 +652,7 @@ namespace omath::projection
v v
*/ */
[[nodiscard("You must use screen position")]] Vector3<NumericType> [[nodiscard("You must use screen position")]] constexpr Vector3<NumericType>
ndc_to_screen_position_from_top_left_corner(const Vector3<NumericType>& ndc) const noexcept ndc_to_screen_position_from_top_left_corner(const Vector3<NumericType>& ndc) const noexcept
{ {
/* /*
@@ -607,7 +670,7 @@ namespace omath::projection
(ndc.y / -NumericType{2} + NumericType{0.5}) * m_view_port.m_height, ndc.z}; (ndc.y / -NumericType{2} + NumericType{0.5}) * m_view_port.m_height, ndc.z};
} }
[[nodiscard("You must use screen position")]] Vector3<NumericType> [[nodiscard("You must use screen position")]] constexpr Vector3<NumericType>
ndc_to_screen_position_from_bottom_left_corner(const Vector3<NumericType>& ndc) const noexcept ndc_to_screen_position_from_bottom_left_corner(const Vector3<NumericType>& ndc) const noexcept
{ {
/* /*
@@ -626,7 +689,7 @@ namespace omath::projection
} }
template<ScreenStart screen_start = ScreenStart::TOP_LEFT_CORNER> template<ScreenStart screen_start = ScreenStart::TOP_LEFT_CORNER>
[[nodiscard("You must use NDC position")]] Vector3<NumericType> [[nodiscard("You must use NDC position")]] constexpr Vector3<NumericType>
screen_to_ndc(const Vector3<NumericType>& screen_pos) const noexcept screen_to_ndc(const Vector3<NumericType>& screen_pos) const noexcept
{ {
if constexpr (screen_start == ScreenStart::TOP_LEFT_CORNER) if constexpr (screen_start == ScreenStart::TOP_LEFT_CORNER)
@@ -2,70 +2,3 @@
// Created by Vlad on 3/22/2025. // Created by Vlad on 3/22/2025.
// //
#include "omath/engines/frostbite_engine/formulas.hpp" #include "omath/engines/frostbite_engine/formulas.hpp"
namespace omath::frostbite_engine
{
Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return mat_camera_view<float, MatStoreType::ROW_MAJOR>(forward_vector(angles), right_vector(angles),
up_vector(angles), cam_origin);
}
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z<float, MatStoreType::ROW_MAJOR>(angles.roll)
* mat_rotation_axis_y<float, MatStoreType::ROW_MAJOR>(angles.yaw)
* mat_rotation_axis_x<float, MatStoreType::ROW_MAJOR>(angles.pitch);
}
Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
Vector3<float> extract_scale(const Mat4X4& mat) noexcept
{
return mat_extract_scale(mat);
}
ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
PitchAngle::from_degrees(angles.x),
YawAngle::from_degrees(angles.y),
RollAngle::from_degrees(angles.z),
};
}
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
const float far, const NDCDepthRange ndc_depth_range) noexcept
{
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return mat_perspective_left_handed_vertical_fov<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near, far);
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return mat_perspective_left_handed_vertical_fov<float, MatStoreType::ROW_MAJOR, NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
field_of_view, aspect_ratio, near, far);
std::unreachable();
}
} // namespace omath::unity_engine
@@ -2,26 +2,3 @@
// Created by Vlad on 8/11/2025. // Created by Vlad on 8/11/2025.
// //
#include "omath/engines/frostbite_engine/traits/camera_trait.hpp" #include "omath/engines/frostbite_engine/traits/camera_trait.hpp"
namespace omath::frostbite_engine
{
ViewAngles CameraTrait::calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_radians(-std::asin(direction.y)),
YawAngle::from_radians(std::atan2(direction.x, direction.z)), RollAngle::from_radians(0.f)};
}
Mat4X4 CameraTrait::calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return frostbite_engine::calc_view_matrix(angles, cam_origin);
}
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near,
const float far, const NDCDepthRange ndc_depth_range) noexcept
{
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far,
ndc_depth_range);
}
} // namespace omath::unity_engine
-75
View File
@@ -2,78 +2,3 @@
// Created by Vlad on 3/19/2025. // Created by Vlad on 3/19/2025.
// //
#include "omath/engines/iw_engine/formulas.hpp" #include "omath/engines/iw_engine/formulas.hpp"
namespace omath::iw_engine
{
Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z(angles.yaw) * mat_rotation_axis_y(angles.pitch) * mat_rotation_axis_x(angles.roll);
}
Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
Vector3<float> extract_scale(const Mat4X4& mat) noexcept
{
return mat_extract_scale(mat);
}
ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
PitchAngle::from_degrees(angles.y),
YawAngle::from_degrees(angles.z),
RollAngle::from_degrees(angles.x),
};
}
Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return mat_camera_view(forward_vector(angles), right_vector(angles), up_vector(angles), cam_origin);
}
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
const float far, const NDCDepthRange ndc_depth_range) noexcept
{
// InfinityWard Engine (inherited from Quake) stores FOV as horizontal FOV at a 4:3
// reference aspect. Convert to true vertical FOV, then delegate to the
// standard vertical-FOV left-handed builder with the caller's actual
// aspect ratio.
// vfov = 2 · atan( tan(hfov_4:3 / 2) / (4/3) )
constexpr float k_source_reference_aspect = 4.f / 3.f;
const auto vertical_fov = angles::horizontal_fov_to_vertical(field_of_view, k_source_reference_aspect);
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return mat_perspective_left_handed_vertical_fov<
float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
vertical_fov, aspect_ratio, near, far);
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return mat_perspective_left_handed_vertical_fov<
float, MatStoreType::ROW_MAJOR, NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
vertical_fov, aspect_ratio, near, far);
std::unreachable();
};
} // namespace omath::iw_engine
@@ -2,26 +2,3 @@
// Created by Vlad on 8/11/2025. // Created by Vlad on 8/11/2025.
// //
#include "omath/engines/iw_engine/traits/camera_trait.hpp" #include "omath/engines/iw_engine/traits/camera_trait.hpp"
#include "omath/engines/iw_engine/formulas.hpp"
namespace omath::iw_engine
{
ViewAngles CameraTrait::calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_radians(-std::asin(direction.z)),
YawAngle::from_radians(std::atan2(direction.y, direction.x)), RollAngle::from_radians(0.f)};
}
Mat4X4 CameraTrait::calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return iw_engine::calc_view_matrix(angles, cam_origin);
}
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near,
const float far, const NDCDepthRange ndc_depth_range) noexcept
{
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far,
ndc_depth_range);
}
} // namespace omath::iw_engine
-69
View File
@@ -2,72 +2,3 @@
// Created by Vlad on 3/19/2025. // Created by Vlad on 3/19/2025.
// //
#include "omath/engines/opengl_engine/formulas.hpp" #include "omath/engines/opengl_engine/formulas.hpp"
namespace omath::opengl_engine
{
Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec =
rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_forward);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec =
rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_right);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_up);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return mat_look_at_right_handed(cam_origin, cam_origin + forward_vector(angles), up_vector(angles));
}
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z<float, MatStoreType::COLUMN_MAJOR>(angles.roll)
* mat_rotation_axis_y<float, MatStoreType::COLUMN_MAJOR>(angles.yaw)
* mat_rotation_axis_x<float, MatStoreType::COLUMN_MAJOR>(angles.pitch);
}
Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
Vector3<float> extract_scale(const Mat4X4& mat) noexcept
{
return mat_extract_scale(mat);
}
ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
PitchAngle::from_degrees(angles.x),
YawAngle::from_degrees(angles.y),
RollAngle::from_degrees(angles.z),
};
}
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
const float far, const NDCDepthRange ndc_depth_range) noexcept
{
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return mat_perspective_right_handed_vertical_fov<float, MatStoreType::COLUMN_MAJOR, NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
field_of_view, aspect_ratio, near, far);
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return mat_perspective_right_handed_vertical_fov<float, MatStoreType::COLUMN_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near, far);
std::unreachable();
}
} // namespace omath::opengl_engine
@@ -2,27 +2,3 @@
// Created by Vlad on 8/11/2025. // Created by Vlad on 8/11/2025.
// //
#include "omath/engines/opengl_engine/traits/camera_trait.hpp" #include "omath/engines/opengl_engine/traits/camera_trait.hpp"
#include "omath/engines/opengl_engine/formulas.hpp"
namespace omath::opengl_engine
{
ViewAngles CameraTrait::calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_radians(std::asin(direction.y)),
YawAngle::from_radians(-std::atan2(direction.x, -direction.z)), RollAngle::from_radians(0.f)};
}
Mat4X4 CameraTrait::calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return opengl_engine::calc_view_matrix(angles, cam_origin);
}
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near,
const float far, const NDCDepthRange ndc_depth_range) noexcept
{
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far,
ndc_depth_range);
}
} // namespace omath::opengl_engine
-67
View File
@@ -2,70 +2,3 @@
// Created by Orange on 6/3/2026. // Created by Orange on 6/3/2026.
// //
#include "omath/engines/rage_engine/formulas.hpp" #include "omath/engines/rage_engine/formulas.hpp"
namespace omath::rage_engine
{
Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return mat_camera_view<float, MatStoreType::ROW_MAJOR>(forward_vector(angles), right_vector(angles),
up_vector(angles), cam_origin);
}
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z<float, MatStoreType::ROW_MAJOR>(angles.yaw)
* mat_rotation_axis_y<float, MatStoreType::ROW_MAJOR>(angles.roll)
* mat_rotation_axis_x<float, MatStoreType::ROW_MAJOR>(angles.pitch);
}
Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
Vector3<float> extract_scale(const Mat4X4& mat) noexcept
{
return mat_extract_scale(mat);
}
ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
PitchAngle::from_degrees(angles.x),
YawAngle::from_degrees(angles.z),
RollAngle::from_degrees(angles.y),
};
}
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
const float far, const NDCDepthRange ndc_depth_range) noexcept
{
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return mat_perspective_left_handed_vertical_fov<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near, far);
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return mat_perspective_left_handed_vertical_fov<float, MatStoreType::ROW_MAJOR,
NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
field_of_view, aspect_ratio, near, far);
std::unreachable();
}
} // namespace omath::rage_engine
@@ -2,26 +2,3 @@
// Created by Orange on 6/3/2026. // Created by Orange on 6/3/2026.
// //
#include "omath/engines/rage_engine/traits/camera_trait.hpp" #include "omath/engines/rage_engine/traits/camera_trait.hpp"
namespace omath::rage_engine
{
ViewAngles CameraTrait::calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_radians(std::asin(direction.z)),
YawAngle::from_radians(-std::atan2(direction.x, direction.y)), RollAngle::from_radians(0.f)};
}
Mat4X4 CameraTrait::calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return rage_engine::calc_view_matrix(angles, cam_origin);
}
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near, const float far,
const NDCDepthRange ndc_depth_range) noexcept
{
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far,
ndc_depth_range);
}
} // namespace omath::rage_engine
-75
View File
@@ -2,78 +2,3 @@
// Created by Vlad on 3/19/2025. // Created by Vlad on 3/19/2025.
// //
#include <omath/engines/source_engine/formulas.hpp> #include <omath/engines/source_engine/formulas.hpp>
namespace omath::source_engine
{
Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z(angles.yaw) * mat_rotation_axis_y(angles.pitch) * mat_rotation_axis_x(angles.roll);
}
Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
Vector3<float> extract_scale(const Mat4X4& mat) noexcept
{
return mat_extract_scale(mat);
}
ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
PitchAngle::from_degrees(angles.y),
YawAngle::from_degrees(angles.z),
RollAngle::from_degrees(angles.x),
};
}
Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return mat_camera_view(forward_vector(angles), right_vector(angles), up_vector(angles), cam_origin);
}
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
const float far, const NDCDepthRange ndc_depth_range) noexcept
{
// Source (inherited from Quake) stores FOV as horizontal FOV at a 4:3
// reference aspect. Convert to true vertical FOV, then delegate to the
// standard vertical-FOV left-handed builder with the caller's actual
// aspect ratio.
// vfov = 2 · atan( tan(hfov_4:3 / 2) / (4/3) )
constexpr float k_source_reference_aspect = 4.f / 3.f;
const auto vertical_fov = angles::horizontal_fov_to_vertical(field_of_view, k_source_reference_aspect);
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return mat_perspective_left_handed_vertical_fov<
float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
vertical_fov, aspect_ratio, near, far);
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return mat_perspective_left_handed_vertical_fov<
float, MatStoreType::ROW_MAJOR, NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
vertical_fov, aspect_ratio, near, far);
std::unreachable();
}
} // namespace omath::source_engine
@@ -2,27 +2,3 @@
// Created by Vlad on 8/11/2025. // Created by Vlad on 8/11/2025.
// //
#include "omath/engines/source_engine/traits/camera_trait.hpp" #include "omath/engines/source_engine/traits/camera_trait.hpp"
#include "omath/engines/source_engine/formulas.hpp"
namespace omath::source_engine
{
ViewAngles CameraTrait::calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_radians(-std::asin(direction.z)),
YawAngle::from_radians(std::atan2(direction.y, direction.x)), RollAngle::from_radians(0.f)};
}
Mat4X4 CameraTrait::calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return source_engine::calc_view_matrix(angles, cam_origin);
}
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near,
const float far, const NDCDepthRange ndc_depth_range) noexcept
{
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far,
ndc_depth_range);
}
} // namespace omath::source_engine
-66
View File
@@ -2,69 +2,3 @@
// Created by Vlad on 3/22/2025. // Created by Vlad on 3/22/2025.
// //
#include "omath/engines/unity_engine/formulas.hpp" #include "omath/engines/unity_engine/formulas.hpp"
namespace omath::unity_engine
{
Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return mat_camera_view<float, MatStoreType::ROW_MAJOR>(-forward_vector(angles), right_vector(angles),
up_vector(angles), cam_origin);
}
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z<float, MatStoreType::ROW_MAJOR>(angles.roll)
* mat_rotation_axis_y<float, MatStoreType::ROW_MAJOR>(angles.yaw)
* mat_rotation_axis_x<float, MatStoreType::ROW_MAJOR>(angles.pitch);
}
Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
Vector3<float> extract_scale(const Mat4X4& mat) noexcept
{
return mat_extract_scale(mat);
}
ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
PitchAngle::from_degrees(angles.x),
YawAngle::from_degrees(angles.y),
RollAngle::from_degrees(angles.z),
};
}
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
const float far, const NDCDepthRange ndc_depth_range) noexcept
{
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return omath::mat_perspective_right_handed_vertical_fov<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near, far);
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return omath::mat_perspective_right_handed_vertical_fov<float, MatStoreType::ROW_MAJOR,
NDCDepthRange::NEGATIVE_ONE_TO_ONE>(field_of_view, aspect_ratio,
near, far);
std::unreachable();
}
} // namespace omath::unity_engine
@@ -2,26 +2,3 @@
// Created by Vlad on 8/11/2025. // Created by Vlad on 8/11/2025.
// //
#include "omath/engines/unity_engine/traits/camera_trait.hpp" #include "omath/engines/unity_engine/traits/camera_trait.hpp"
namespace omath::unity_engine
{
ViewAngles CameraTrait::calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_radians(-std::asin(direction.y)),
YawAngle::from_radians(std::atan2(direction.x, direction.z)), RollAngle::from_radians(0.f)};
}
Mat4X4 CameraTrait::calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return unity_engine::calc_view_matrix(angles, cam_origin);
}
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near,
const float far, const NDCDepthRange ndc_depth_range) noexcept
{
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far,
ndc_depth_range);
}
} // namespace omath::unity_engine
-73
View File
@@ -2,76 +2,3 @@
// Created by Vlad on 3/22/2025. // Created by Vlad on 3/22/2025.
// //
#include "omath/engines/unreal_engine/formulas.hpp" #include "omath/engines/unreal_engine/formulas.hpp"
namespace omath::unreal_engine
{
Vector3<double> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<double> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<double> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<double>& cam_origin) noexcept
{
return mat_camera_view<double, MatStoreType::ROW_MAJOR>(forward_vector(angles), right_vector(angles),
up_vector(angles), cam_origin);
}
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
// UE FRotator is intrinsic Z-Y-X (Yaw → Pitch → Roll applied in local
// frame), which for column-vector composition is Rz·Ry·Rx.
// Pitch and roll axes in omath spin opposite to UE's convention, so
// both carry a sign flip.
return mat_rotation_axis_z<double, MatStoreType::ROW_MAJOR>(angles.yaw)
* mat_rotation_axis_y<double, MatStoreType::ROW_MAJOR>(-angles.pitch)
* mat_rotation_axis_x<double, MatStoreType::ROW_MAJOR>(-angles.roll);
}
Vector3<double> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
Vector3<double> extract_scale(const Mat4X4& mat) noexcept
{
return mat_extract_scale(mat);
}
ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
PitchAngle::from_degrees(-angles.y),
YawAngle::from_degrees(angles.z),
RollAngle::from_degrees(-angles.x),
};
}
Mat4X4 calc_perspective_projection_matrix(const double field_of_view, const double aspect_ratio, const double near,
const double far, const NDCDepthRange ndc_depth_range) noexcept
{
// UE stores horizontal FOV in FMinimalViewInfo — use the left-handed
// horizontal-FOV builder directly.
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return mat_perspective_left_handed_horizontal_fov<
double, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near, far);
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return mat_perspective_left_handed_horizontal_fov<
double, MatStoreType::ROW_MAJOR, NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
field_of_view, aspect_ratio, near, far);
std::unreachable();
}
} // namespace omath::unreal_engine
@@ -2,26 +2,3 @@
// Created by Vlad on 8/11/2025. // Created by Vlad on 8/11/2025.
// //
#include "omath/engines/unreal_engine/traits/camera_trait.hpp" #include "omath/engines/unreal_engine/traits/camera_trait.hpp"
namespace omath::unreal_engine
{
ViewAngles CameraTrait::calc_look_at_angle(const Vector3<double>& cam_origin, const Vector3<double>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_radians(std::asin(direction.z)),
YawAngle::from_radians(std::atan2(direction.y, direction.x)), RollAngle::from_radians(0.f)};
}
Mat4X4 CameraTrait::calc_view_matrix(const ViewAngles& angles, const Vector3<double>& cam_origin) noexcept
{
return unreal_engine::calc_view_matrix(angles, cam_origin);
}
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const double near,
const double far, const NDCDepthRange ndc_depth_range) noexcept
{
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far,
ndc_depth_range);
}
} // namespace omath::unreal_engine
@@ -1,30 +1,40 @@
// Tests for engine trait headers to improve header coverage // Tests for engine trait headers to improve header coverage
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <omath/engines/frostbite_engine/camera.hpp>
#include <omath/engines/frostbite_engine/traits/pred_engine_trait.hpp> #include <omath/engines/frostbite_engine/traits/pred_engine_trait.hpp>
#include <omath/engines/frostbite_engine/traits/mesh_trait.hpp> #include <omath/engines/frostbite_engine/traits/mesh_trait.hpp>
#include <omath/engines/frostbite_engine/traits/camera_trait.hpp> #include <omath/engines/frostbite_engine/traits/camera_trait.hpp>
#include <omath/engines/iw_engine/camera.hpp>
#include <omath/engines/iw_engine/traits/pred_engine_trait.hpp> #include <omath/engines/iw_engine/traits/pred_engine_trait.hpp>
#include <omath/engines/iw_engine/traits/mesh_trait.hpp> #include <omath/engines/iw_engine/traits/mesh_trait.hpp>
#include <omath/engines/iw_engine/traits/camera_trait.hpp> #include <omath/engines/iw_engine/traits/camera_trait.hpp>
#include <omath/engines/opengl_engine/camera.hpp>
#include <omath/engines/opengl_engine/traits/pred_engine_trait.hpp> #include <omath/engines/opengl_engine/traits/pred_engine_trait.hpp>
#include <omath/engines/opengl_engine/traits/mesh_trait.hpp> #include <omath/engines/opengl_engine/traits/mesh_trait.hpp>
#include <omath/engines/opengl_engine/traits/camera_trait.hpp> #include <omath/engines/opengl_engine/traits/camera_trait.hpp>
#include <omath/engines/unity_engine/camera.hpp>
#include <omath/engines/unity_engine/traits/pred_engine_trait.hpp> #include <omath/engines/unity_engine/traits/pred_engine_trait.hpp>
#include <omath/engines/unity_engine/traits/mesh_trait.hpp> #include <omath/engines/unity_engine/traits/mesh_trait.hpp>
#include <omath/engines/unity_engine/traits/camera_trait.hpp> #include <omath/engines/unity_engine/traits/camera_trait.hpp>
#include <omath/engines/unreal_engine/camera.hpp>
#include <omath/engines/unreal_engine/traits/pred_engine_trait.hpp> #include <omath/engines/unreal_engine/traits/pred_engine_trait.hpp>
#include <omath/engines/unreal_engine/traits/mesh_trait.hpp> #include <omath/engines/unreal_engine/traits/mesh_trait.hpp>
#include <omath/engines/unreal_engine/traits/camera_trait.hpp> #include <omath/engines/unreal_engine/traits/camera_trait.hpp>
#include <omath/engines/source_engine/camera.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/source_engine/traits/camera_trait.hpp>
#include <omath/engines/cry_engine/camera.hpp>
#include <omath/engines/cry_engine/traits/camera_trait.hpp> #include <omath/engines/cry_engine/traits/camera_trait.hpp>
#include <omath/engines/rage_engine/camera.hpp>
#include <omath/engines/rage_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>
#include <optional> #include <optional>
@@ -41,6 +51,43 @@ static void expect_matrix_near(const MatT& a, const MatT& b, float eps = 1e-5f)
} }
// ── Launch offset tests for all engines ────────────────────────────────────── // ── Launch offset tests for all engines ──────────────────────────────────────
#ifdef OMATH_USE_GCEM
template<class MatType, class NumericType>
constexpr bool matrix_has_non_zero_element(const MatType& mat)
{
for (std::size_t r = 0; r < 4; ++r)
for (std::size_t c = 0; c < 4; ++c)
if (mat.at(r, c) != NumericType{0})
return true;
return false;
}
template<class CameraType, class NumericType>
constexpr bool camera_can_calculate_matrices_at_compile_time()
{
using FieldOfView = projection::FieldOfView;
CameraType camera{{}, {}, {1280.f, 720.f}, FieldOfView::from_degrees(90.f), NumericType{0.01}, NumericType{1000}};
const auto view = camera.get_view_matrix();
const auto projection = camera.get_projection_matrix();
const auto view_projection = camera.get_view_projection_matrix();
return matrix_has_non_zero_element<decltype(view), NumericType>(view)
&& matrix_has_non_zero_element<decltype(projection), NumericType>(projection)
&& matrix_has_non_zero_element<decltype(view_projection), NumericType>(view_projection);
}
static_assert(camera_can_calculate_matrices_at_compile_time<source_engine::Camera, float>());
static_assert(camera_can_calculate_matrices_at_compile_time<iw_engine::Camera, float>());
static_assert(camera_can_calculate_matrices_at_compile_time<frostbite_engine::Camera, float>());
static_assert(camera_can_calculate_matrices_at_compile_time<opengl_engine::Camera, float>());
static_assert(camera_can_calculate_matrices_at_compile_time<unity_engine::Camera, float>());
static_assert(camera_can_calculate_matrices_at_compile_time<cry_engine::Camera, float>());
static_assert(camera_can_calculate_matrices_at_compile_time<rage_engine::Camera, float>());
static_assert(camera_can_calculate_matrices_at_compile_time<unreal_engine::Camera, double>());
#endif
#include <omath/engines/cry_engine/traits/pred_engine_trait.hpp> #include <omath/engines/cry_engine/traits/pred_engine_trait.hpp>
// Helper: verify that zero offset matches default-initialized offset behavior // Helper: verify that zero offset matches default-initialized offset behavior
+26 -4
View File
@@ -60,15 +60,37 @@ static void verify_random_look_at_targets_project_to_screen_center(const omath::
} }
} }
#ifdef OMATH_USE_GCEM
constexpr bool source_camera_constexpr_projection_round_trip()
{
constexpr auto fov = omath::Angle<float, 0.f, 180.f, omath::AngleFlags::Clamped>::from_degrees(90.f);
constexpr auto cam = omath::source_engine::Camera({0, 0, 0}, omath::source_engine::ViewAngles{}, {1920.f, 1080.f},
fov, 0.01f, 1000.f);
constexpr auto projected = cam.world_to_screen({1000.f, 0, 50.f});
constexpr auto result = cam.screen_to_world(projected.value());
constexpr auto result2 = cam.world_to_screen(result.value());
return projected.has_value() && result.has_value() && result2.has_value()
&& static_cast<omath::Vector2<float>>(projected.value())
== static_cast<omath::Vector2<float>>(result2.value())
&& omath::internal::abs(projected->x - 960.f) < 0.001f
&& omath::internal::abs(projected->y - 504.f) < 0.001f
&& omath::internal::abs(projected->z - 1.f) < 0.001f;
}
static_assert(source_camera_constexpr_projection_round_trip());
#endif
TEST(UnitTestProjection, Projection) TEST(UnitTestProjection, Projection)
{ {
constexpr auto fov = omath::Angle<float, 0.f, 180.f, omath::AngleFlags::Clamped>::from_degrees(90.f); constexpr auto fov = omath::Angle<float, 0.f, 180.f, omath::AngleFlags::Clamped>::from_degrees(90.f);
const auto cam = omath::source_engine::Camera({0, 0, 0}, omath::source_engine::ViewAngles{}, {1920.f, 1080.f}, fov, constexpr auto cam = omath::source_engine::Camera({0, 0, 0}, omath::source_engine::ViewAngles{}, {1920.f, 1080.f}, fov,
0.01f, 1000.f); 0.01f, 1000.f);
const auto projected = cam.world_to_screen({1000.f, 0, 50.f}); constexpr auto projected = cam.world_to_screen({1000.f, 0, 50.f});
const auto result = cam.screen_to_world(projected.value()); constexpr auto result = cam.screen_to_world(projected.value());
const auto result2 = cam.world_to_screen(result.value()); constexpr auto result2 = cam.world_to_screen(result.value());
EXPECT_EQ(static_cast<omath::Vector2<float>>(projected.value()), EXPECT_EQ(static_cast<omath::Vector2<float>>(projected.value()),
static_cast<omath::Vector2<float>>(result2.value())); static_cast<omath::Vector2<float>>(result2.value()));