From ac79326da80a580be2bc5dfbe53feaf0e3784db2 Mon Sep 17 00:00:00 2001 From: Orange Date: Mon, 30 Sep 2024 11:54:47 -0700 Subject: [PATCH] improved projection class --- include/omath/Mat.h | 6 ++++++ include/omath/projection/Camera.h | 4 ++-- source/projection/Camera.cpp | 12 ++++++------ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/include/omath/Mat.h b/include/omath/Mat.h index 36789cf..b1165c9 100644 --- a/include/omath/Mat.h +++ b/include/omath/Mat.h @@ -121,6 +121,12 @@ namespace omath At(i, j) *= f; return *this; } + + template + constexpr Mat operator*=(const Mat& other) + { + return *this = *this * other; + } constexpr Mat operator*(float f) const { diff --git a/include/omath/projection/Camera.h b/include/omath/projection/Camera.h index a3bfbe1..4d37e67 100644 --- a/include/omath/projection/Camera.h +++ b/include/omath/projection/Camera.h @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include "ErrorCodes.h" @@ -28,7 +28,7 @@ namespace omath::projection Camera(const Vector3& position, const Vector3& viewAngles, const ViewPort& viewPort, float fov, float near, float far); void SetViewAngles(const Vector3& viewAngles); - [[nodiscard]] Matrix GetViewMatrix() const; + [[nodiscard]] Mat<4, 4> GetViewMatrix() const; [[nodiscard]] std::expected WorldToScreen(const Vector3& worldPosition) const; diff --git a/source/projection/Camera.cpp b/source/projection/Camera.cpp index 5f67a6e..0b49e70 100644 --- a/source/projection/Camera.cpp +++ b/source/projection/Camera.cpp @@ -21,24 +21,24 @@ namespace omath::projection m_farPlaneDistance = far; } - Matrix Camera::GetViewMatrix() const + Mat<4, 4> Camera::GetViewMatrix() const { const auto forward = Vector3::ForwardVector(m_viewAngles.x, m_viewAngles.y); const auto right = Vector3::RightVector(m_viewAngles.x, m_viewAngles.y, m_viewAngles.z); const auto up = Vector3::UpVector(m_viewAngles.x, m_viewAngles.y, m_viewAngles.z); - return Matrix::TranslationMatrix(-m_origin) * Matrix::OrientationMatrix(forward, right, up); + return Mat<4, 4>::TranslationMat(-m_origin) * Mat<4, 4>::OrientationMat(forward, right, up); } std::expected Camera::WorldToScreen(const Vector3 &worldPosition) const { - const auto posVecAsMatrix = Matrix({{worldPosition.x, worldPosition.y, worldPosition.z, 1.f}}); + const auto posVecAsMatrix = Mat<1, 4>({{worldPosition.x, worldPosition.y, worldPosition.z, 1.f}}); - const auto projectionMatrix = Matrix::ProjectionMatrix(m_fieldOfView, m_viewPort.AspectRatio(), + const auto projectionMatrix = Mat<4, 4>::ProjectionMat(m_fieldOfView, m_viewPort.AspectRatio(), m_nearPlaneDistance, m_farPlaneDistance); - auto projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix); + Mat<1, 4> projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix); if (projected.At(0, 3) <= 0.f) return std::unexpected(Error::WORLD_POSITION_IS_BEHIND_CAMERA); @@ -49,7 +49,7 @@ namespace omath::projection projected.At(0, 1) < -1.f || projected.At(0, 1) > 1.f) return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS); - projected *= Matrix::ToScreenMatrix(m_viewPort.m_width, m_viewPort.m_height); + projected *= Mat<4, 4>::ToScreenMat(m_viewPort.m_width, m_viewPort.m_height); return Vector2{projected.At(0, 0), projected.At(0, 1)}; }