From 9c0285e353e8f242d64dae4369a4e447ee259fac Mon Sep 17 00:00:00 2001 From: Orange Date: Mon, 2 Dec 2024 10:57:47 +0300 Subject: [PATCH] added check, removed deprecated code --- include/omath/Mat.hpp | 53 +++++------------------------ include/omath/engines/opengl.hpp | 2 +- include/omath/engines/source.hpp | 2 +- include/omath/projection/Camera.hpp | 19 +++++++++-- 4 files changed, 27 insertions(+), 49 deletions(-) diff --git a/include/omath/Mat.hpp b/include/omath/Mat.hpp index 2840212..a3f1807 100644 --- a/include/omath/Mat.hpp +++ b/include/omath/Mat.hpp @@ -7,7 +7,6 @@ #include #include #include -#include "Angles.hpp" #include "Vector3.hpp" @@ -317,42 +316,6 @@ namespace omath }; } - [[nodiscard]] - constexpr static Mat<4, 4> TranslationMat(const Vector3& diff) noexcept - { - return { - {1, 0, 0, diff.x}, - {0, 1, 0, diff.y}, - {0, 0, 1, diff.z}, - {0, 0, 0, 1}, - }; - } - - [[nodiscard]] - constexpr static Mat<4, 4> OrientationMat(const Vector3& forward, const Vector3& right, - const Vector3& up) noexcept - { - return { - {right.x, up.x, forward.x, 0}, - {right.y, up.y, forward.y, 0}, - {right.z, up.z, forward.z, 0}, - {0, 0, 0, 1}, - }; - } - - [[nodiscard]] - constexpr static Mat<4, 4> ProjectionMat(const Type& fieldOfView, const Type& aspectRatio, const Type& near, - const Type& far, const Type& lensZoom) noexcept - { - const Type& fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2); - const Type& frustumHeight = far - near; - - return {{-1 / (aspectRatio * fovHalfTan) * lensZoom, 0, 0, 0}, - {0, -1 / fovHalfTan * lensZoom, 0, 0}, - {0, 0, -far / frustumHeight, -1}, - {0, 0, near * far / frustumHeight, 0}}; - } - private: std::array m_data; }; @@ -368,19 +331,19 @@ namespace omath [[nodiscard]] constexpr static Mat<4, 1, T, St> MatColumnFromVector(const Vector3& vector) noexcept { - return { - {vector.x}, {vector.y}, {vector.z}, {1}}; + return {{vector.x}, {vector.y}, {vector.z}, {1}}; } template [[nodiscard]] constexpr Mat<4, 4, T, St> MatTranslation(const Vector3& diff) noexcept { - return Mat<4, 4, T, St>{ - {1, 0, 0, 0}, - {0, 1, 0, 0}, - {0, 0, 1, 0}, - {diff.x, diff.y, diff.z, 1}, - }.Transposed(); + return + { + {1, 0, 0, diff.x}, + {0, 1, 0, diff.y}, + {0, 0, 1, diff.z}, + {0, 0, 0, 1}, + }; } } // namespace omath diff --git a/include/omath/engines/opengl.hpp b/include/omath/engines/opengl.hpp index bcda994..78d7821 100644 --- a/include/omath/engines/opengl.hpp +++ b/include/omath/engines/opengl.hpp @@ -4,7 +4,7 @@ #pragma once #include "omath/Vector3.hpp" #include "omath/Mat.hpp" - +#include "omath/Angle.hpp" namespace omath::opengl { diff --git a/include/omath/engines/source.hpp b/include/omath/engines/source.hpp index 0aa6e7b..cabbaea 100644 --- a/include/omath/engines/source.hpp +++ b/include/omath/engines/source.hpp @@ -55,7 +55,7 @@ namespace omath::source { constexpr auto kMultiplyFactor = Type(0.75); - const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2) * kMultiplyFactor; + const Type fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2) * kMultiplyFactor; return { {-static_cast(1) / (aspectRatio * fovHalfTan), 0, 0, 0}, diff --git a/include/omath/projection/Camera.hpp b/include/omath/projection/Camera.hpp index 3932677..ba87ba6 100644 --- a/include/omath/projection/Camera.hpp +++ b/include/omath/projection/Camera.hpp @@ -31,6 +31,7 @@ namespace omath::projection std::invoke_result_t> class Camera { + public: Camera(const Vector3& position, const ViewAnglesType& viewAngles, const ViewPort& viewPort, const Angle& fov, const float near, const float far, @@ -56,15 +57,20 @@ namespace omath::projection { return GetProjectionMatrix() * GetViewMatrix(); } - [[nodiscard]] std::expected WorldToScreen([[maybe_unused]] const Vector3& worldPosition) const + + [[nodiscard]] std::expected WorldToScreen(const Vector3& worldPosition) const { using mat = std::invoke_result_t; - Mat<4, 1> projected = GetViewProjectionMatrix() * MatColumnFromVector(worldPosition); + auto 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); + if (IsNdcOutOfBounds(projected)) + return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS); + return Vector3{++projected.At(0,0) / 2 * m_viewPort.m_width , ++projected.At(1,0) / 2 * m_viewPort.m_height, projected.At(2,0)}; } @@ -75,10 +81,19 @@ namespace omath::projection float m_nearPlaneDistance; private: + ViewAnglesType m_viewAngles; Vector3 m_origin; std::function CreateViewMatrix; std::function CreateProjectionMatrix; + + + template + [[nodiscard]] + constexpr static bool IsNdcOutOfBounds(const Type& ndc) + { + return std::ranges::any_of( ndc.RawArray(), [](const auto& val) { return val < -1 || val > 1; }); + } }; } // namespace omath::projection