// // Created by Vlad on 3/22/2025. // #pragma once #include "omath/engines/unity_engine/constants.hpp" namespace omath::unity_engine { [[nodiscard("rotation matrix result should not be discarded")]] constexpr Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept; [[nodiscard("forward vector result should not be discarded")]] constexpr 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)}; } [[nodiscard("right vector result should not be discarded")]] constexpr 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)}; } [[nodiscard("up vector result should not be discarded")]] constexpr 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)}; } [[nodiscard("view matrix result should not be discarded")]] constexpr 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); } [[nodiscard("rotation matrix result should not be discarded")]] constexpr 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); } [[nodiscard("origin result should not be discarded")]] constexpr Vector3 extract_origin(const Mat4X4& mat) noexcept { return mat_extract_origin(mat); } [[nodiscard("scale result should not be discarded")]] constexpr Vector3 extract_scale(const Mat4X4& mat) noexcept { return mat_extract_scale(mat); } [[nodiscard("rotation angles result should not be discarded")]] 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("perspective projection matrix result should not be discarded")]] constexpr Mat4X4 calc_perspective_projection_matrix( const float field_of_view, const float aspect_ratio, const float near_plane, const float far_plane, 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( field_of_view, aspect_ratio, near_plane, far_plane); if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE) return omath::mat_perspective_right_handed_vertical_fov( field_of_view, aspect_ratio, near_plane, far_plane); std::unreachable(); } template requires std::is_floating_point_v [[nodiscard("centimeters value should not be discarded")]] constexpr FloatingType units_to_centimeters(const FloatingType& units) { return units / static_cast(100); } template requires std::is_floating_point_v [[nodiscard("meters value should not be discarded")]] constexpr FloatingType units_to_meters(const FloatingType& units) { return units; } template requires std::is_floating_point_v [[nodiscard("kilometers value should not be discarded")]] constexpr FloatingType units_to_kilometers(const FloatingType& units) { return units_to_meters(units) / static_cast(1000); } template requires std::is_floating_point_v [[nodiscard("units value should not be discarded")]] constexpr FloatingType centimeters_to_units(const FloatingType& centimeters) { return centimeters * static_cast(100); } template requires std::is_floating_point_v [[nodiscard("units value should not be discarded")]] constexpr FloatingType meters_to_units(const FloatingType& meters) { return meters; } template requires std::is_floating_point_v [[nodiscard("units value should not be discarded")]] constexpr FloatingType kilometers_to_units(const FloatingType& kilometers) { return meters_to_units(kilometers * static_cast(1000)); } } // namespace omath::unity_engine