From 17b150499d81a7cff7ad111aebca513deee6fc8a Mon Sep 17 00:00:00 2001 From: Orange Date: Mon, 11 Aug 2025 00:32:09 +0300 Subject: [PATCH] Refactors camera and prediction engine traits. Moves camera and prediction engine implementations into traits for each engine, decoupling the engine-specific logic from the core classes, promoting code reuse and maintainability. This change allows for easier addition of new engines and customization of existing ones. --- include/omath/engines/iw_engine/camera.hpp | 13 ++----- .../engines/iw_engine/traits/camera_trait.hpp | 24 +++++++++++++ .../iw_engine/traits/pred_engine_trait.hpp} | 23 ++++++------ .../omath/engines/opengl_engine/camera.hpp | 11 ++---- .../opengl_engine/traits/camera_trait.hpp | 24 +++++++++++++ .../traits/pred_engine_trait.hpp} | 23 ++++++------ .../omath/engines/source_engine/camera.hpp | 14 ++------ .../source_engine/traits/camera_trait.hpp | 24 +++++++++++++ .../traits/pred_engine_trait.hpp} | 23 ++++++------ include/omath/engines/unity_engine/camera.hpp | 13 ++----- .../unity_engine/traits/camera_trait.hpp | 24 +++++++++++++ .../traits/pred_engine_trait.hpp} | 23 ++++++------ .../proj_pred_engine_legacy.hpp | 4 +-- include/omath/projection/camera.hpp | 15 ++++---- source/engines/iw_engine/camera.cpp | 33 ----------------- .../engines/iw_engine/traits/camera_trait.cpp | 27 ++++++++++++++ source/engines/opengl_engine/camera.cpp | 33 ----------------- .../opengl_engine/traits/camera_trait.cpp | 28 +++++++++++++++ source/engines/source_engine/camera.cpp | 35 ------------------- .../source_engine/traits/camera_trait.cpp | 27 ++++++++++++++ source/engines/unity_engine/camera.cpp | 27 -------------- .../unity_engine/traits/camera_trait.cpp | 27 ++++++++++++++ 22 files changed, 267 insertions(+), 228 deletions(-) create mode 100644 include/omath/engines/iw_engine/traits/camera_trait.hpp rename include/omath/{projectile_prediction/engine_traits/iw_engine_trait.hpp => engines/iw_engine/traits/pred_engine_trait.hpp} (79%) create mode 100644 include/omath/engines/opengl_engine/traits/camera_trait.hpp rename include/omath/{projectile_prediction/engine_traits/opengl_engine_trait.hpp => engines/opengl_engine/traits/pred_engine_trait.hpp} (79%) create mode 100644 include/omath/engines/source_engine/traits/camera_trait.hpp rename include/omath/{projectile_prediction/engine_traits/source_engine_trait.hpp => engines/source_engine/traits/pred_engine_trait.hpp} (79%) create mode 100644 include/omath/engines/unity_engine/traits/camera_trait.hpp rename include/omath/{projectile_prediction/engine_traits/unity_engine_trait.hpp => engines/unity_engine/traits/pred_engine_trait.hpp} (79%) delete mode 100644 source/engines/iw_engine/camera.cpp create mode 100644 source/engines/iw_engine/traits/camera_trait.cpp delete mode 100644 source/engines/opengl_engine/camera.cpp create mode 100644 source/engines/opengl_engine/traits/camera_trait.cpp delete mode 100644 source/engines/source_engine/camera.cpp create mode 100644 source/engines/source_engine/traits/camera_trait.cpp delete mode 100644 source/engines/unity_engine/camera.cpp create mode 100644 source/engines/unity_engine/traits/camera_trait.cpp diff --git a/include/omath/engines/iw_engine/camera.hpp b/include/omath/engines/iw_engine/camera.hpp index 2f530e18..2efe29b1 100644 --- a/include/omath/engines/iw_engine/camera.hpp +++ b/include/omath/engines/iw_engine/camera.hpp @@ -5,18 +5,9 @@ #pragma once #include "omath/engines/iw_engine/constants.hpp" #include "omath/projection/camera.hpp" +#include "traits/camera_trait.hpp" namespace omath::iw_engine { - class Camera final : public projection::Camera - { - public: - Camera(const Vector3& position, const ViewAngles& view_angles, const projection::ViewPort& view_port, - const Angle& fov, float near, float far); - void look_at(const Vector3& target) override; - - protected: - [[nodiscard]] Mat4X4 calc_view_matrix() const noexcept override; - [[nodiscard]] Mat4X4 calc_projection_matrix() const noexcept override; - }; + using Camera = projection::Camera; } // namespace omath::iw_engine \ No newline at end of file diff --git a/include/omath/engines/iw_engine/traits/camera_trait.hpp b/include/omath/engines/iw_engine/traits/camera_trait.hpp new file mode 100644 index 00000000..88c21569 --- /dev/null +++ b/include/omath/engines/iw_engine/traits/camera_trait.hpp @@ -0,0 +1,24 @@ +// +// Created by Vlad on 8/10/2025. +// + +#pragma once +#include "omath/engines/iw_engine/constants.hpp" +#include "omath/projection/camera.hpp" + +namespace omath::iw_engine +{ + class CameraTrait final + { + public: + [[nodiscard]] + static ViewAngles calc_look_at_angle(const Vector3& cam_origin, const Vector3& look_at) noexcept; + + [[nodiscard]] + static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3& cam_origin) noexcept; + [[nodiscard]] + static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, + float near, float far) noexcept; + }; + +} // namespace omath::iw_engine \ No newline at end of file diff --git a/include/omath/projectile_prediction/engine_traits/iw_engine_trait.hpp b/include/omath/engines/iw_engine/traits/pred_engine_trait.hpp similarity index 79% rename from include/omath/projectile_prediction/engine_traits/iw_engine_trait.hpp rename to include/omath/engines/iw_engine/traits/pred_engine_trait.hpp index 0361e3b9..7961268a 100644 --- a/include/omath/projectile_prediction/engine_traits/iw_engine_trait.hpp +++ b/include/omath/engines/iw_engine/traits/pred_engine_trait.hpp @@ -8,27 +8,26 @@ #include "omath/projectile_prediction/target.hpp" #include -namespace omath::projectile_prediction::traits +namespace omath::iw_engine { - class IwEngineTrait final + class PredEngineTrait final { public: - constexpr static Vector3 predict_projectile_position(const Projectile& projectile, const float pitch, - const float yaw, const float time, - const float gravity) noexcept + constexpr static Vector3 predict_projectile_position(const projectile_prediction::Projectile& projectile, + const float pitch, const float yaw, + const float time, const float gravity) noexcept { auto current_pos = projectile.m_origin - + iw_engine::forward_vector({iw_engine::PitchAngle::from_degrees(-pitch), - iw_engine::YawAngle::from_degrees(yaw), - iw_engine::RollAngle::from_degrees(0)}) + + forward_vector({PitchAngle::from_degrees(-pitch), YawAngle::from_degrees(yaw), + RollAngle::from_degrees(0)}) * projectile.m_launch_speed * time; current_pos.z -= (gravity * projectile.m_gravity_scale) * (time * time) * 0.5f; return current_pos; } [[nodiscard]] - static constexpr Vector3 predict_target_position(const Target& target, const float time, - const float gravity) noexcept + static constexpr Vector3 predict_target_position(const projectile_prediction::Target& target, + const float time, const float gravity) noexcept { auto predicted = target.m_origin + target.m_velocity * time; @@ -50,7 +49,7 @@ namespace omath::projectile_prediction::traits } [[nodiscard]] - static Vector3 calc_viewpoint_from_angles(const Projectile& projectile, + static Vector3 calc_viewpoint_from_angles(const projectile_prediction::Projectile& projectile, Vector3 predicted_target_position, const std::optional projectile_pitch) noexcept { @@ -77,4 +76,4 @@ namespace omath::projectile_prediction::traits return angles::radians_to_degrees(std::atan2(delta.y, delta.x)); }; }; -} // namespace omath::projectile_prediction::traits \ No newline at end of file +} // namespace omath::iw_engine \ No newline at end of file diff --git a/include/omath/engines/opengl_engine/camera.hpp b/include/omath/engines/opengl_engine/camera.hpp index b4cdd210..e69ee340 100644 --- a/include/omath/engines/opengl_engine/camera.hpp +++ b/include/omath/engines/opengl_engine/camera.hpp @@ -4,16 +4,9 @@ #pragma once #include "omath/engines/opengl_engine/constants.hpp" #include "omath/projection/camera.hpp" +#include "traits/camera_trait.hpp" namespace omath::opengl_engine { - class Camera final : public projection::Camera - { - public: - Camera(const Vector3& position, const ViewAngles& view_angles, const projection::ViewPort& view_port, - const Angle& fov, float near, float far); - void look_at(const Vector3& target) override; - [[nodiscard]] Mat4X4 calc_view_matrix() const noexcept override; - [[nodiscard]] Mat4X4 calc_projection_matrix() const noexcept override; - }; + using Camera = projection::Camera; } // namespace omath::opengl_engine \ No newline at end of file diff --git a/include/omath/engines/opengl_engine/traits/camera_trait.hpp b/include/omath/engines/opengl_engine/traits/camera_trait.hpp new file mode 100644 index 00000000..3fb57c07 --- /dev/null +++ b/include/omath/engines/opengl_engine/traits/camera_trait.hpp @@ -0,0 +1,24 @@ +// +// Created by Vlad on 8/10/2025. +// + +#pragma once +#include "omath/engines/opengl_engine/constants.hpp" +#include "omath/projection/camera.hpp" + +namespace omath::opengl_engine +{ + class CameraTrait final + { + public: + [[nodiscard]] + static ViewAngles calc_look_at_angle(const Vector3& cam_origin, const Vector3& look_at) noexcept; + + [[nodiscard]] + static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3& cam_origin) noexcept; + [[nodiscard]] + static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, + float near, float far) noexcept; + }; + +} // namespace omath::opengl_engine \ No newline at end of file diff --git a/include/omath/projectile_prediction/engine_traits/opengl_engine_trait.hpp b/include/omath/engines/opengl_engine/traits/pred_engine_trait.hpp similarity index 79% rename from include/omath/projectile_prediction/engine_traits/opengl_engine_trait.hpp rename to include/omath/engines/opengl_engine/traits/pred_engine_trait.hpp index 59e30dfe..9c014ff8 100644 --- a/include/omath/projectile_prediction/engine_traits/opengl_engine_trait.hpp +++ b/include/omath/engines/opengl_engine/traits/pred_engine_trait.hpp @@ -7,27 +7,26 @@ #include "omath/projectile_prediction/target.hpp" #include -namespace omath::projectile_prediction::traits +namespace omath::opengl_engine { - class OpenGlEngineTrait final + class PredEngineTrait final { public: - constexpr static Vector3 predict_projectile_position(const Projectile& projectile, const float pitch, - const float yaw, const float time, - const float gravity) noexcept + constexpr static Vector3 predict_projectile_position(const projectile_prediction::Projectile& projectile, + const float pitch, const float yaw, + const float time, const float gravity) noexcept { auto current_pos = projectile.m_origin - + opengl_engine::forward_vector({opengl_engine::PitchAngle::from_degrees(-pitch), - opengl_engine::YawAngle::from_degrees(yaw), - opengl_engine::RollAngle::from_degrees(0)}) + + forward_vector({PitchAngle::from_degrees(-pitch), YawAngle::from_degrees(yaw), + RollAngle::from_degrees(0)}) * projectile.m_launch_speed * time; current_pos.y -= (gravity * projectile.m_gravity_scale) * (time * time) * 0.5f; return current_pos; } [[nodiscard]] - static constexpr Vector3 predict_target_position(const Target& target, const float time, - const float gravity) noexcept + static constexpr Vector3 predict_target_position(const projectile_prediction::Target& target, + const float time, const float gravity) noexcept { auto predicted = target.m_origin + target.m_velocity * time; @@ -49,7 +48,7 @@ namespace omath::projectile_prediction::traits } [[nodiscard]] - static Vector3 calc_viewpoint_from_angles(const Projectile& projectile, + static Vector3 calc_viewpoint_from_angles(const projectile_prediction::Projectile& projectile, Vector3 predicted_target_position, const std::optional projectile_pitch) noexcept { @@ -76,4 +75,4 @@ namespace omath::projectile_prediction::traits return angles::radians_to_degrees(std::atan2(delta.z, delta.x)); }; }; -} // namespace omath::projectile_prediction::traits +} // namespace omath::opengl_engine diff --git a/include/omath/engines/source_engine/camera.hpp b/include/omath/engines/source_engine/camera.hpp index cba5c434..6769d40d 100644 --- a/include/omath/engines/source_engine/camera.hpp +++ b/include/omath/engines/source_engine/camera.hpp @@ -4,18 +4,8 @@ #pragma once #include "omath/engines/source_engine/constants.hpp" #include "omath/projection/camera.hpp" - +#include "traits/camera_trait.hpp" namespace omath::source_engine { - class Camera final : public projection::Camera - { - public: - Camera(const Vector3& position, const ViewAngles& view_angles, const projection::ViewPort& view_port, - const Angle& fov, float near, float far); - void look_at(const Vector3& target) override; - - protected: - [[nodiscard]] Mat4X4 calc_view_matrix() const noexcept override; - [[nodiscard]] Mat4X4 calc_projection_matrix() const noexcept override; - }; + using Camera = projection::Camera; } // namespace omath::source_engine \ No newline at end of file diff --git a/include/omath/engines/source_engine/traits/camera_trait.hpp b/include/omath/engines/source_engine/traits/camera_trait.hpp new file mode 100644 index 00000000..d027d25e --- /dev/null +++ b/include/omath/engines/source_engine/traits/camera_trait.hpp @@ -0,0 +1,24 @@ +// +// Created by Vlad on 8/10/2025. +// + +#pragma once +#include "omath/engines/source_engine/constants.hpp" +#include "omath/projection/camera.hpp" + +namespace omath::source_engine +{ + class CameraTrait final + { + public: + [[nodiscard]] + static ViewAngles calc_look_at_angle(const Vector3& cam_origin, const Vector3& look_at) noexcept; + + [[nodiscard]] + static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3& cam_origin) noexcept; + [[nodiscard]] + static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, + float near, float far) noexcept; + }; + +} // namespace omath::source_engine \ No newline at end of file diff --git a/include/omath/projectile_prediction/engine_traits/source_engine_trait.hpp b/include/omath/engines/source_engine/traits/pred_engine_trait.hpp similarity index 79% rename from include/omath/projectile_prediction/engine_traits/source_engine_trait.hpp rename to include/omath/engines/source_engine/traits/pred_engine_trait.hpp index d124a924..ca9771e1 100644 --- a/include/omath/projectile_prediction/engine_traits/source_engine_trait.hpp +++ b/include/omath/engines/source_engine/traits/pred_engine_trait.hpp @@ -8,27 +8,26 @@ #include "omath/projectile_prediction/target.hpp" #include -namespace omath::projectile_prediction::traits +namespace omath::source_engine { - class SourceEngineTrait final + class PredEngineTrait final { public: - constexpr static Vector3 predict_projectile_position(const Projectile& projectile, const float pitch, - const float yaw, const float time, - const float gravity) noexcept + constexpr static Vector3 predict_projectile_position(const projectile_prediction::Projectile& projectile, + const float pitch, const float yaw, + const float time, const float gravity) noexcept { auto current_pos = projectile.m_origin - + source_engine::forward_vector({source_engine::PitchAngle::from_degrees(-pitch), - source_engine::YawAngle::from_degrees(yaw), - source_engine::RollAngle::from_degrees(0)}) + + forward_vector({PitchAngle::from_degrees(-pitch), YawAngle::from_degrees(yaw), + RollAngle::from_degrees(0)}) * projectile.m_launch_speed * time; current_pos.z -= (gravity * projectile.m_gravity_scale) * (time * time) * 0.5f; return current_pos; } [[nodiscard]] - static constexpr Vector3 predict_target_position(const Target& target, const float time, - const float gravity) noexcept + static constexpr Vector3 predict_target_position(const projectile_prediction::Target& target, + const float time, const float gravity) noexcept { auto predicted = target.m_origin + target.m_velocity * time; @@ -50,7 +49,7 @@ namespace omath::projectile_prediction::traits } [[nodiscard]] - static Vector3 calc_viewpoint_from_angles(const Projectile& projectile, + static Vector3 calc_viewpoint_from_angles(const projectile_prediction::Projectile& projectile, Vector3 predicted_target_position, const std::optional projectile_pitch) noexcept { @@ -77,4 +76,4 @@ namespace omath::projectile_prediction::traits return angles::radians_to_degrees(std::atan2(delta.y, delta.x)); }; }; -} // namespace omath::projectile_prediction::traits \ No newline at end of file +} // namespace omath::source_engine \ No newline at end of file diff --git a/include/omath/engines/unity_engine/camera.hpp b/include/omath/engines/unity_engine/camera.hpp index 568b2a75..642290a3 100644 --- a/include/omath/engines/unity_engine/camera.hpp +++ b/include/omath/engines/unity_engine/camera.hpp @@ -5,18 +5,9 @@ #pragma once #include "omath/engines/unity_engine/constants.hpp" #include "omath/projection/camera.hpp" +#include "traits/camera_trait.hpp" namespace omath::unity_engine { - class Camera final : public projection::Camera - { - public: - Camera(const Vector3& position, const ViewAngles& view_angles, const projection::ViewPort& view_port, - const Angle& fov, float near, float far); - void look_at(const Vector3& target) override; - - protected: - [[nodiscard]] Mat4X4 calc_view_matrix() const noexcept override; - [[nodiscard]] Mat4X4 calc_projection_matrix() const noexcept override; - }; + using Camera = projection::Camera; } // namespace omath::unity_engine \ No newline at end of file diff --git a/include/omath/engines/unity_engine/traits/camera_trait.hpp b/include/omath/engines/unity_engine/traits/camera_trait.hpp new file mode 100644 index 00000000..2d98b9db --- /dev/null +++ b/include/omath/engines/unity_engine/traits/camera_trait.hpp @@ -0,0 +1,24 @@ +// +// Created by Vlad on 8/10/2025. +// + +#pragma once +#include "omath/engines/unity_engine/formulas.hpp" +#include "omath/projection/camera.hpp" + +namespace omath::unity_engine +{ + class CameraTrait final + { + public: + [[nodiscard]] + static ViewAngles calc_look_at_angle(const Vector3& cam_origin, const Vector3& look_at) noexcept; + + [[nodiscard]] + static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3& cam_origin) noexcept; + [[nodiscard]] + static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port, + float near, float far) noexcept; + }; + +} // namespace omath::unity_engine \ No newline at end of file diff --git a/include/omath/projectile_prediction/engine_traits/unity_engine_trait.hpp b/include/omath/engines/unity_engine/traits/pred_engine_trait.hpp similarity index 79% rename from include/omath/projectile_prediction/engine_traits/unity_engine_trait.hpp rename to include/omath/engines/unity_engine/traits/pred_engine_trait.hpp index c70caf84..5851e4c6 100644 --- a/include/omath/projectile_prediction/engine_traits/unity_engine_trait.hpp +++ b/include/omath/engines/unity_engine/traits/pred_engine_trait.hpp @@ -7,27 +7,26 @@ #include "omath/projectile_prediction/target.hpp" #include -namespace omath::projectile_prediction::traits +namespace omath::unity_engine { - class UnityEngineTrait final + class PredEngineTrait final { public: - constexpr static Vector3 predict_projectile_position(const Projectile& projectile, const float pitch, - const float yaw, const float time, - const float gravity) noexcept + constexpr static Vector3 predict_projectile_position(const projectile_prediction::Projectile& projectile, + const float pitch, const float yaw, + const float time, const float gravity) noexcept { auto current_pos = projectile.m_origin - + unity_engine::forward_vector({unity_engine::PitchAngle::from_degrees(-pitch), - unity_engine::YawAngle::from_degrees(yaw), - unity_engine::RollAngle::from_degrees(0)}) + + forward_vector({PitchAngle::from_degrees(-pitch), YawAngle::from_degrees(yaw), + RollAngle::from_degrees(0)}) * projectile.m_launch_speed * time; current_pos.y -= (gravity * projectile.m_gravity_scale) * (time * time) * 0.5f; return current_pos; } [[nodiscard]] - static constexpr Vector3 predict_target_position(const Target& target, const float time, - const float gravity) noexcept + static constexpr Vector3 predict_target_position(const projectile_prediction::Target& target, + const float time, const float gravity) noexcept { auto predicted = target.m_origin + target.m_velocity * time; @@ -49,7 +48,7 @@ namespace omath::projectile_prediction::traits } [[nodiscard]] - static Vector3 calc_viewpoint_from_angles(const Projectile& projectile, + static Vector3 calc_viewpoint_from_angles(const projectile_prediction::Projectile& projectile, Vector3 predicted_target_position, const std::optional projectile_pitch) noexcept { @@ -76,4 +75,4 @@ namespace omath::projectile_prediction::traits return angles::radians_to_degrees(std::atan2(delta.z, delta.x)); }; }; -} // namespace omath::projectile_prediction::traits +} // namespace omath::unity_engine diff --git a/include/omath/projectile_prediction/proj_pred_engine_legacy.hpp b/include/omath/projectile_prediction/proj_pred_engine_legacy.hpp index 801f7196..8d44255e 100644 --- a/include/omath/projectile_prediction/proj_pred_engine_legacy.hpp +++ b/include/omath/projectile_prediction/proj_pred_engine_legacy.hpp @@ -4,7 +4,7 @@ #pragma once -#include "engine_traits/source_engine_trait.hpp" +#include "omath/engines/source_engine/traits/pred_engine_trait.hpp" #include "omath/projectile_prediction/proj_pred_engine.hpp" #include "omath/projectile_prediction/projectile.hpp" #include "omath/projectile_prediction/target.hpp" @@ -13,7 +13,7 @@ namespace omath::projectile_prediction { - template + template class ProjPredEngineLegacy final : public ProjPredEngineInterface { public: diff --git a/include/omath/projection/camera.hpp b/include/omath/projection/camera.hpp index cf93a78f..b56879e9 100644 --- a/include/omath/projection/camera.hpp +++ b/include/omath/projection/camera.hpp @@ -26,7 +26,7 @@ namespace omath::projection }; using FieldOfView = Angle; - template + template class Camera { public: @@ -39,15 +39,16 @@ namespace omath::projection } protected: - virtual void look_at(const Vector3& target) = 0; - - [[nodiscard]] virtual Mat4X4Type calc_view_matrix() const noexcept = 0; - - [[nodiscard]] virtual Mat4X4Type calc_projection_matrix() const noexcept = 0; + void look_at(const Vector3& target) + { + m_view_angles = TraitClass::calc_look_at_angle(m_origin, target); + } [[nodiscard]] Mat4X4Type calc_view_projection_matrix() const noexcept { - return calc_projection_matrix() * calc_view_matrix(); + return TraitClass::calc_projection_matrix(m_field_of_view, m_view_port, m_near_plane_distance, + m_far_plane_distance) + * TraitClass::calc_view_matrix(m_view_angles, m_origin); } public: diff --git a/source/engines/iw_engine/camera.cpp b/source/engines/iw_engine/camera.cpp deleted file mode 100644 index 27d8ae78..00000000 --- a/source/engines/iw_engine/camera.cpp +++ /dev/null @@ -1,33 +0,0 @@ -// -// Created by Vlad on 3/17/2025. -// -#include "omath/engines/iw_engine/camera.hpp" -#include "omath/engines/iw_engine/formulas.hpp" - -namespace omath::iw_engine -{ - - Camera::Camera(const Vector3& position, const ViewAngles& view_angles, const projection::ViewPort& view_port, - const Angle& fov, const float near, const float far) - : projection::Camera(position, view_angles, view_port, fov, near, far) - { - } - void Camera::look_at([[maybe_unused]] const Vector3& target) - { - const float distance = m_origin.distance_to(target); - const auto delta = target - m_origin; - - m_view_angles.pitch = PitchAngle::from_radians(std::asin(delta.z / distance)); - m_view_angles.yaw = -YawAngle::from_radians(std::atan2(delta.y, delta.x)); - m_view_angles.roll = RollAngle::from_radians(0.f); - } - Mat4X4 Camera::calc_view_matrix() const noexcept - { - return iw_engine::calc_view_matrix(m_view_angles, m_origin); - } - Mat4X4 Camera::calc_projection_matrix() const noexcept - { - return calc_perspective_projection_matrix(m_field_of_view.as_degrees(), m_view_port.aspect_ratio(), - m_near_plane_distance, m_far_plane_distance); - } -} // namespace omath::iw_engine \ No newline at end of file diff --git a/source/engines/iw_engine/traits/camera_trait.cpp b/source/engines/iw_engine/traits/camera_trait.cpp new file mode 100644 index 00000000..087f7b09 --- /dev/null +++ b/source/engines/iw_engine/traits/camera_trait.cpp @@ -0,0 +1,27 @@ +// +// Created by Vlad on 8/11/2025. +// +#include "omath/engines/iw_engine/traits/camera_trait.hpp" + +namespace omath::iw_engine +{ + + ViewAngles CameraTrait::calc_look_at_angle(const Vector3& cam_origin, const Vector3& look_at) noexcept + { + const auto distance = cam_origin.distance_to(look_at); + const auto delta = cam_origin - look_at; + + return {PitchAngle::from_radians(-std::asin(delta.z / distance)), + YawAngle::from_radians(std::atan2(delta.y, delta.x)), RollAngle::from_radians(0.f)}; + } + Mat4X4 CameraTrait::calc_view_matrix(const ViewAngles& angles, const Vector3& 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) noexcept + { + return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far); + } +} // namespace omath::iw_engine \ No newline at end of file diff --git a/source/engines/opengl_engine/camera.cpp b/source/engines/opengl_engine/camera.cpp deleted file mode 100644 index 269974cc..00000000 --- a/source/engines/opengl_engine/camera.cpp +++ /dev/null @@ -1,33 +0,0 @@ -// -// Created by Orange on 12/23/2024. -// -#include "omath/engines/opengl_engine/camera.hpp" -#include "omath/engines/opengl_engine/formulas.hpp" - -namespace omath::opengl_engine -{ - - Camera::Camera(const Vector3& position, const ViewAngles& view_angles, const projection::ViewPort& view_port, - const Angle& fov, const float near, const float far) - : projection::Camera(position, view_angles, view_port, fov, near, far) - { - } - void Camera::look_at([[maybe_unused]] const Vector3& target) - { - const float distance = m_origin.distance_to(target); - const auto delta = target - m_origin; - - m_view_angles.pitch = PitchAngle::from_radians(std::asin(delta.z / distance)); - m_view_angles.yaw = -YawAngle::from_radians(std::atan2(delta.y, delta.x)); - m_view_angles.roll = RollAngle::from_radians(0.f); - } - Mat4X4 Camera::calc_view_matrix() const noexcept - { - return opengl_engine::calc_view_matrix(m_view_angles, m_origin); - } - Mat4X4 Camera::calc_projection_matrix() const noexcept - { - return calc_perspective_projection_matrix(m_field_of_view.as_degrees(), m_view_port.aspect_ratio(), - m_near_plane_distance, m_far_plane_distance); - } -} // namespace omath::opengl_engine diff --git a/source/engines/opengl_engine/traits/camera_trait.cpp b/source/engines/opengl_engine/traits/camera_trait.cpp new file mode 100644 index 00000000..9fb644fc --- /dev/null +++ b/source/engines/opengl_engine/traits/camera_trait.cpp @@ -0,0 +1,28 @@ +// +// Created by Vlad on 8/11/2025. +// +#include "omath/engines/opengl_engine/traits/camera_trait.hpp" + + +namespace omath::opengl_engine +{ + + ViewAngles CameraTrait::calc_look_at_angle(const Vector3& cam_origin, const Vector3& look_at) noexcept + { + const auto distance = cam_origin.distance_to(look_at); + const auto delta = cam_origin - look_at; + + return {PitchAngle::from_radians(-std::asin(delta.y / distance)), + YawAngle::from_radians(std::atan2(delta.z, delta.x)), RollAngle::from_radians(0.f)}; + } + Mat4X4 CameraTrait::calc_view_matrix(const ViewAngles& angles, const Vector3& 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) noexcept + { + return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far); + } +} // namespace omath::opengl_engine \ No newline at end of file diff --git a/source/engines/source_engine/camera.cpp b/source/engines/source_engine/camera.cpp deleted file mode 100644 index edd4c529..00000000 --- a/source/engines/source_engine/camera.cpp +++ /dev/null @@ -1,35 +0,0 @@ -// -// Created by Orange on 12/4/2024. -// -#include "omath/engines/source_engine/camera.hpp" -#include "omath/engines/source_engine/formulas.hpp" - -namespace omath::source_engine -{ - - Camera::Camera(const Vector3& position, const ViewAngles& view_angles, const projection::ViewPort& view_port, - const projection::FieldOfView& fov, const float near, const float far) - : projection::Camera(position, view_angles, view_port, fov, near, far) - { - } - void Camera::look_at(const Vector3& target) - { - const float distance = m_origin.distance_to(target); - const auto delta = target - m_origin; - - m_view_angles.pitch = PitchAngle::from_radians(std::asin(delta.z / distance)); - m_view_angles.yaw = -YawAngle::from_radians(std::atan2(delta.y, delta.x)); - m_view_angles.roll = RollAngle::from_radians(0.f); - } - - Mat4X4 Camera::calc_view_matrix() const noexcept - { - return source_engine::calc_view_matrix(m_view_angles, m_origin); - } - - Mat4X4 Camera::calc_projection_matrix() const noexcept - { - return calc_perspective_projection_matrix(m_field_of_view.as_degrees(), m_view_port.aspect_ratio(), - m_near_plane_distance, m_far_plane_distance); - } -} // namespace omath::source_engine diff --git a/source/engines/source_engine/traits/camera_trait.cpp b/source/engines/source_engine/traits/camera_trait.cpp new file mode 100644 index 00000000..bf810b76 --- /dev/null +++ b/source/engines/source_engine/traits/camera_trait.cpp @@ -0,0 +1,27 @@ +// +// Created by Vlad on 8/11/2025. +// +#include "omath/engines/source_engine/traits/camera_trait.hpp" + +namespace omath::source_engine +{ + + ViewAngles CameraTrait::calc_look_at_angle(const Vector3& cam_origin, const Vector3& look_at) noexcept + { + const auto distance = cam_origin.distance_to(look_at); + const auto delta = cam_origin - look_at; + + return {PitchAngle::from_radians(-std::asin(delta.z / distance)), + YawAngle::from_radians(std::atan2(delta.y, delta.x)), RollAngle::from_radians(0.f)}; + } + Mat4X4 CameraTrait::calc_view_matrix(const ViewAngles& angles, const Vector3& 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) noexcept + { + return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far); + } +} // namespace omath::source_engine \ No newline at end of file diff --git a/source/engines/unity_engine/camera.cpp b/source/engines/unity_engine/camera.cpp deleted file mode 100644 index 5455c766..00000000 --- a/source/engines/unity_engine/camera.cpp +++ /dev/null @@ -1,27 +0,0 @@ -// -// Created by Vlad on 3/22/2025. -// -#include -#include - -namespace omath::unity_engine -{ - Camera::Camera(const Vector3& position, const ViewAngles& view_angles, const projection::ViewPort& view_port, - const projection::FieldOfView& fov, const float near, const float far) - : projection::Camera(position, view_angles, view_port, fov, near, far) - { - } - void Camera::look_at([[maybe_unused]] const Vector3& target) - { - throw std::runtime_error("Not implemented"); - } - Mat4X4 Camera::calc_view_matrix() const noexcept - { - return unity_engine::calc_view_matrix(m_view_angles, m_origin); - } - Mat4X4 Camera::calc_projection_matrix() const noexcept - { - return calc_perspective_projection_matrix(m_field_of_view.as_degrees(), m_view_port.aspect_ratio(), - m_near_plane_distance, m_far_plane_distance); - } -} // namespace omath::unity_engine diff --git a/source/engines/unity_engine/traits/camera_trait.cpp b/source/engines/unity_engine/traits/camera_trait.cpp new file mode 100644 index 00000000..d76426b7 --- /dev/null +++ b/source/engines/unity_engine/traits/camera_trait.cpp @@ -0,0 +1,27 @@ +// +// Created by Vlad on 8/11/2025. +// +#include "omath/engines/unity_engine/traits/camera_trait.hpp" + +namespace omath::unity_engine +{ + + ViewAngles CameraTrait::calc_look_at_angle(const Vector3& cam_origin, const Vector3& look_at) noexcept + { + const auto distance = cam_origin.distance_to(look_at); + const auto delta = cam_origin - look_at; + + return {PitchAngle::from_radians(-std::asin(delta.y / distance)), + YawAngle::from_radians(std::atan2(delta.z, delta.x)), RollAngle::from_radians(0.f)}; + } + Mat4X4 CameraTrait::calc_view_matrix(const ViewAngles& angles, const Vector3& 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) noexcept + { + return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far); + } +} // namespace omath::unity_engine \ No newline at end of file