diff --git a/include/omath/engines/cry_engine/formulas.hpp b/include/omath/engines/cry_engine/formulas.hpp new file mode 100644 index 0000000..46c2b76 --- /dev/null +++ b/include/omath/engines/cry_engine/formulas.hpp @@ -0,0 +1,74 @@ +// +// Created by Vlad on 3/22/2025. +// + +#pragma once +#include "omath/engines/cry_engine/constants.hpp" + +namespace omath::cry_engine +{ + [[nodiscard]] + Vector3 forward_vector(const ViewAngles& angles) noexcept; + + [[nodiscard]] + Vector3 right_vector(const ViewAngles& angles) noexcept; + + [[nodiscard]] + Vector3 up_vector(const ViewAngles& angles) noexcept; + + [[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3& cam_origin) noexcept; + + [[nodiscard]] + Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; + + [[nodiscard]] + Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far) noexcept; + + template + requires std::is_floating_point_v + [[nodiscard]] + constexpr FloatingType units_to_centimeters(const FloatingType& units) + { + return units / static_cast(100); + } + + template + requires std::is_floating_point_v + [[nodiscard]] + constexpr FloatingType units_to_meters(const FloatingType& units) + { + return units; + } + + template + requires std::is_floating_point_v + [[nodiscard]] + constexpr FloatingType units_to_kilometers(const FloatingType& units) + { + return units_to_meters(units) / static_cast(1000); + } + + template + requires std::is_floating_point_v + [[nodiscard]] + constexpr FloatingType centimeters_to_units(const FloatingType& centimeters) + { + return centimeters * static_cast(100); + } + + template + requires std::is_floating_point_v + [[nodiscard]] + constexpr FloatingType meters_to_units(const FloatingType& meters) + { + return meters; + } + + template + requires std::is_floating_point_v + [[nodiscard]] + constexpr FloatingType kilometers_to_units(const FloatingType& kilometers) + { + return meters_to_units(kilometers * static_cast(1000)); + } +} // namespace omath::frostbite_engine diff --git a/source/engines/cry_engine/formulas.cpp b/source/engines/cry_engine/formulas.cpp new file mode 100644 index 0000000..14ad049 --- /dev/null +++ b/source/engines/cry_engine/formulas.cpp @@ -0,0 +1,42 @@ +// +// Created by Vlad on 3/22/2025. +// +#include "omath/engines/cry_engine/formulas.hpp" + +namespace omath::cry_engine +{ + Vector3 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 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 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& cam_origin) noexcept + { + return mat_camera_view(forward_vector(angles), right_vector(angles), + up_vector(angles), cam_origin); + } + Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept + { + return mat_rotation_axis_z(angles.roll) + * mat_rotation_axis_y(angles.yaw) + * mat_rotation_axis_x(angles.pitch); + } + Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near, + const float far) noexcept + { + return mat_perspective_left_handed(field_of_view, aspect_ratio, near, far); + } +} // namespace omath::unity_engine