From 8a67b7edbe96cc85a27eb8482068c3f400b15e8c Mon Sep 17 00:00:00 2001 From: Orange Date: Tue, 27 Aug 2024 21:41:21 +0300 Subject: [PATCH] maybe improved matrix --- include/omath/projection/Camera.h | 2 +- source/projection/Camera.cpp | 24 ++++++++++-------------- tests/UnitTestProjection.cpp | 6 +++--- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/include/omath/projection/Camera.h b/include/omath/projection/Camera.h index 7b2f864..c96dd16 100644 --- a/include/omath/projection/Camera.h +++ b/include/omath/projection/Camera.h @@ -23,7 +23,7 @@ namespace omath::projection static float& GetFloat2(); [[nodiscard]] Matrix GetViewMatrix() const; - [[nodiscard]] Matrix GetProjectionMatrix(float scaleX, float scaleY) const; + [[nodiscard]] Matrix GetProjectionMatrix() const; [[nodiscard]] Matrix GetTranslationMatrix() const; [[nodiscard]] Matrix GetOrientationMatrix() const; diff --git a/source/projection/Camera.cpp b/source/projection/Camera.cpp index 47540a0..fe88929 100644 --- a/source/projection/Camera.cpp +++ b/source/projection/Camera.cpp @@ -2,9 +2,7 @@ // Created by Vlad on 27.08.2024. // #include "omath/projection/Camera.h" - #include - #include "omath/angles.h" @@ -36,24 +34,22 @@ namespace omath::projection return GetTranslationMatrix() * GetOrientationMatrix(); } - Matrix Camera::GetProjectionMatrix(const float scaleX, const float scaleY) const + Matrix Camera::GetProjectionMatrix() const { - const float right = std::tan(angles::DegreesToRadians(m_fieldOfView) / 2.f); + const float right = m_nearPlaneDistance * std::tan(angles::DegreesToRadians(m_fieldOfView) / 2.f); const float left = -right; - const float verticalFov = angles::DegreesToRadians(m_fieldOfView) * (m_viewPort.y / m_viewPort.x); - - const float top = std::tan(verticalFov / 2.f); + const float top = right * (m_viewPort.y / m_viewPort.x); const float bottom = -top; - const auto far = m_farPlaneDistance; - const auto near = m_nearPlaneDistance; + const float near = m_nearPlaneDistance; + const float far = m_farPlaneDistance; return Matrix({ - {scaleX / (right - left), 0.f, 0.f, 0.f}, - {0.f, scaleY / (top - bottom), 0.f, 0.f}, - {0.f, 0.f, (far + near) / (far - near), 1.f}, - {0.f, 0.f, -near * far / (far - near), 0.f}, + {2 * near / (right - left), 0.f, (right + left) / (right - left), 0.f}, + {0.f, 2 * near / (top - bottom), (top + bottom) / (top - bottom), 0.f}, + {0.f, 0.f, (far + near) / (far - near), 2 * near * far / (far - near)}, + {0.f, 0.f, -1.f, 0.f}, }); } @@ -88,7 +84,7 @@ namespace omath::projection { const auto posVecAsMatrix = Matrix({{worldPosition.x, worldPosition.y, worldPosition.z, 1.f}}); - const auto viewProjectionMatrix = GetViewMatrix() * GetProjectionMatrix(GetFloat1(), GetFloat2()); + const auto viewProjectionMatrix = GetViewMatrix() * GetProjectionMatrix(); auto projected = posVecAsMatrix * viewProjectionMatrix; diff --git a/tests/UnitTestProjection.cpp b/tests/UnitTestProjection.cpp index a989c9e..dcd5d4c 100644 --- a/tests/UnitTestProjection.cpp +++ b/tests/UnitTestProjection.cpp @@ -9,10 +9,10 @@ TEST(UnitTestProjection, IsPointOnScreen) { - const omath::projection::Camera camera({5, 0, 0}, {0, 0.f, 0.f} , {1920.f, 1080.f, 0.f}, 110, 0.1, 500); + const omath::projection::Camera camera({0, 0, 0}, {0, 0.f, 0.f} , {1920.f, 1080.f, 0.f}, 110, 0.1, 500); - const auto proj = camera.WorldToScreen({10, 0, 0}); + const auto proj = camera.WorldToScreen({100, 0, 15}); if (proj) - std::print("{} {} {}", proj->x, proj->y, proj->z); + std::print("{} {}", proj->x, proj->y); EXPECT_TRUE(proj.has_value()); } \ No newline at end of file