From 339dbade7e1dfffdaeb7d97f171a78f7df975ff6 Mon Sep 17 00:00:00 2001 From: Orange Date: Sun, 1 Dec 2024 04:10:25 +0300 Subject: [PATCH] patch --- include/omath/engines/source.hpp | 8 ++++---- include/omath/projection/Camera.hpp | 11 ++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/include/omath/engines/source.hpp b/include/omath/engines/source.hpp index 5bb085e..a5e7ac9 100644 --- a/include/omath/engines/source.hpp +++ b/include/omath/engines/source.hpp @@ -29,10 +29,10 @@ namespace omath::source template requires std::is_floating_point_v || std::is_integral_v - [[nodiscard]] constexpr Mat<4, 4, Type, MatStoreType::COLUMN_MAJOR> ViewMatrixFromVecs(const Vector3& forward, const Vector3& right, + [[nodiscard]] constexpr Mat<4, 4, Type, MatStoreType::ROW_MAJOR> ViewMatrixFromVecs(const Vector3& forward, const Vector3& right, const Vector3& up, const Vector3& camera_pos) { - return MatTranslation(-camera_pos) * Mat<4, 4, Type, MatStoreType::COLUMN_MAJOR>{ + return MatTranslation(-camera_pos) * Mat<4, 4, Type, MatStoreType::ROW_MAJOR>{ {right.x, up.x, forward.x, 0}, {right.y, up.y, forward.y, 0}, {right.z, up.z, forward.z, 0}, @@ -43,14 +43,14 @@ namespace omath::source template requires std::is_floating_point_v || std::is_integral_v - [[nodiscard]] Mat<4, 4, Type, MatStoreType::COLUMN_MAJOR> ViewMatrix(const ViewAngles& angles, const Vector3& cam_origin) + [[nodiscard]] Mat<4, 4, Type, MatStoreType::ROW_MAJOR> ViewMatrix(const ViewAngles& angles, const Vector3& cam_origin) { return ViewMatrixFromVecs(ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin); } template requires std::is_floating_point_v || std::is_integral_v - [[nodiscard]] Mat<4, 4, Type, MatStoreType::COLUMN_MAJOR> + [[nodiscard]] Mat<4, 4, Type, MatStoreType::ROW_MAJOR> PerspectiveProjectionMatrix(const Type& fieldOfView, const Type& aspectRatio, const Type& near, const Type& far) { const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2); diff --git a/include/omath/projection/Camera.hpp b/include/omath/projection/Camera.hpp index e190664..0a19dca 100644 --- a/include/omath/projection/Camera.hpp +++ b/include/omath/projection/Camera.hpp @@ -52,13 +52,18 @@ namespace omath::projection return CreateProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance, m_farPlaneDistance); } + [[nodiscard]] auto GetViewProjectionMatrix() const + { + return GetProjectionMatrix() * GetViewMatrix().Transposed(); + } [[nodiscard]] std::expected WorldToScreen([[maybe_unused]] const Vector3& worldPosition) const { using mat = std::invoke_result_t; - const auto vecAsMatrix = MatColumnFromVector(worldPosition); - - const auto projected = GetViewMatrix().Transposed() * vecAsMatrix; + Mat<4, 1> projected = GetViewProjectionMatrix() * MatColumnFromVector(worldPosition); + if (projected.At(3, 0) == 0.0f) + return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS); + projected /= projected.At(3, 0); return Vector3{projected.At(0,0), projected.At(1,0), projected.At(2,0)}; }