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.
This commit is contained in:
2025-08-11 00:32:09 +03:00
parent c7228c9674
commit 17b150499d
22 changed files with 267 additions and 228 deletions

View File

@@ -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<Mat4X4, ViewAngles>
{
public:
Camera(const Vector3<float>& position, const ViewAngles& view_angles, const projection::ViewPort& view_port,
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
void look_at(const Vector3<float>& target) override;
protected:
[[nodiscard]] Mat4X4 calc_view_matrix() const noexcept override;
[[nodiscard]] Mat4X4 calc_projection_matrix() const noexcept override;
};
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>;
} // namespace omath::iw_engine

View File

@@ -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<float>& cam_origin, const Vector3<float>& look_at) noexcept;
[[nodiscard]]
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& 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

View File

@@ -8,27 +8,26 @@
#include "omath/projectile_prediction/target.hpp"
#include <optional>
namespace omath::projectile_prediction::traits
namespace omath::iw_engine
{
class IwEngineTrait final
class PredEngineTrait final
{
public:
constexpr static Vector3<float> predict_projectile_position(const Projectile& projectile, const float pitch,
const float yaw, const float time,
const float gravity) noexcept
constexpr static Vector3<float> 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<float> predict_target_position(const Target& target, const float time,
const float gravity) noexcept
static constexpr Vector3<float> 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<float> calc_viewpoint_from_angles(const Projectile& projectile,
static Vector3<float> calc_viewpoint_from_angles(const projectile_prediction::Projectile& projectile,
Vector3<float> predicted_target_position,
const std::optional<float> 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
} // namespace omath::iw_engine

View File

@@ -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<Mat4X4, ViewAngles>
{
public:
Camera(const Vector3<float>& position, const ViewAngles& view_angles, const projection::ViewPort& view_port,
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
void look_at(const Vector3<float>& target) override;
[[nodiscard]] Mat4X4 calc_view_matrix() const noexcept override;
[[nodiscard]] Mat4X4 calc_projection_matrix() const noexcept override;
};
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>;
} // namespace omath::opengl_engine

View File

@@ -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<float>& cam_origin, const Vector3<float>& look_at) noexcept;
[[nodiscard]]
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& 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

View File

@@ -7,27 +7,26 @@
#include "omath/projectile_prediction/target.hpp"
#include <optional>
namespace omath::projectile_prediction::traits
namespace omath::opengl_engine
{
class OpenGlEngineTrait final
class PredEngineTrait final
{
public:
constexpr static Vector3<float> predict_projectile_position(const Projectile& projectile, const float pitch,
const float yaw, const float time,
const float gravity) noexcept
constexpr static Vector3<float> 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<float> predict_target_position(const Target& target, const float time,
const float gravity) noexcept
static constexpr Vector3<float> 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<float> calc_viewpoint_from_angles(const Projectile& projectile,
static Vector3<float> calc_viewpoint_from_angles(const projectile_prediction::Projectile& projectile,
Vector3<float> predicted_target_position,
const std::optional<float> 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

View File

@@ -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<Mat4X4, ViewAngles>
{
public:
Camera(const Vector3<float>& position, const ViewAngles& view_angles, const projection::ViewPort& view_port,
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
void look_at(const Vector3<float>& target) override;
protected:
[[nodiscard]] Mat4X4 calc_view_matrix() const noexcept override;
[[nodiscard]] Mat4X4 calc_projection_matrix() const noexcept override;
};
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>;
} // namespace omath::source_engine

View File

@@ -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<float>& cam_origin, const Vector3<float>& look_at) noexcept;
[[nodiscard]]
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& 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

View File

@@ -8,27 +8,26 @@
#include "omath/projectile_prediction/target.hpp"
#include <optional>
namespace omath::projectile_prediction::traits
namespace omath::source_engine
{
class SourceEngineTrait final
class PredEngineTrait final
{
public:
constexpr static Vector3<float> predict_projectile_position(const Projectile& projectile, const float pitch,
const float yaw, const float time,
const float gravity) noexcept
constexpr static Vector3<float> 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<float> predict_target_position(const Target& target, const float time,
const float gravity) noexcept
static constexpr Vector3<float> 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<float> calc_viewpoint_from_angles(const Projectile& projectile,
static Vector3<float> calc_viewpoint_from_angles(const projectile_prediction::Projectile& projectile,
Vector3<float> predicted_target_position,
const std::optional<float> 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
} // namespace omath::source_engine

View File

@@ -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<Mat4X4, ViewAngles>
{
public:
Camera(const Vector3<float>& position, const ViewAngles& view_angles, const projection::ViewPort& view_port,
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
void look_at(const Vector3<float>& target) override;
protected:
[[nodiscard]] Mat4X4 calc_view_matrix() const noexcept override;
[[nodiscard]] Mat4X4 calc_projection_matrix() const noexcept override;
};
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>;
} // namespace omath::unity_engine

View File

@@ -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<float>& cam_origin, const Vector3<float>& look_at) noexcept;
[[nodiscard]]
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& 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

View File

@@ -7,27 +7,26 @@
#include "omath/projectile_prediction/target.hpp"
#include <optional>
namespace omath::projectile_prediction::traits
namespace omath::unity_engine
{
class UnityEngineTrait final
class PredEngineTrait final
{
public:
constexpr static Vector3<float> predict_projectile_position(const Projectile& projectile, const float pitch,
const float yaw, const float time,
const float gravity) noexcept
constexpr static Vector3<float> 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<float> predict_target_position(const Target& target, const float time,
const float gravity) noexcept
static constexpr Vector3<float> 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<float> calc_viewpoint_from_angles(const Projectile& projectile,
static Vector3<float> calc_viewpoint_from_angles(const projectile_prediction::Projectile& projectile,
Vector3<float> predicted_target_position,
const std::optional<float> 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

View File

@@ -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<class EngineTrait = traits::SourceEngineTrait>
template<class EngineTrait = source_engine::PredEngineTrait>
class ProjPredEngineLegacy final : public ProjPredEngineInterface
{
public:

View File

@@ -26,7 +26,7 @@ namespace omath::projection
};
using FieldOfView = Angle<float, 0.f, 180.f, AngleFlags::Clamped>;
template<class Mat4X4Type, class ViewAnglesType>
template<class Mat4X4Type, class ViewAnglesType, class TraitClass>
class Camera
{
public:
@@ -39,15 +39,16 @@ namespace omath::projection
}
protected:
virtual void look_at(const Vector3<float>& 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<float>& 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: