This commit is contained in:
2024-08-27 21:47:08 +03:00
parent e5dbb78b15
commit 6356d06e34
3 changed files with 17 additions and 19 deletions

View File

@@ -5,7 +5,6 @@
#include <utility> #include <utility>
#include <stdexcept> #include <stdexcept>
#include <utility> #include <utility>
#include <cmath>
namespace omath namespace omath

View File

@@ -31,21 +31,20 @@ namespace omath::projection
const float fRight = std::tan(angles::DegreesToRadians(m_fieldOfView) / 2.f); const float fRight = std::tan(angles::DegreesToRadians(m_fieldOfView) / 2.f);
const float fLeft = -fRight; const float fLeft = -fRight;
const float vFov = angles::DegreesToRadians(m_fieldOfView) * (m_viewPort.y / m_viewPort.x); const float verticalFov = angles::DegreesToRadians(m_fieldOfView) * (m_viewPort.y / m_viewPort.x);
const float fTop = std::tan(vFov / 2.f); const float top = std::tan(verticalFov / 2.f);
const float fBottom = -fTop; const float botton = -top;
const auto m_fFar = m_farPlaneDistance; const auto far = m_farPlaneDistance;
const auto m_fNear = m_nearPlaneDistance; const auto near = m_nearPlaneDistance;
return Matrix({ return Matrix({
{2.f / (fRight - fLeft), 0.f, 0.f, 0.f}, {1.555f / (fRight - fLeft), 0.f, 0.f, 0.f},
{0.f, 2.f / (fTop - fBottom), 0.f, 0.f}, {0.f, 1.15f / (top - botton), 0.f, 0.f},
{0.f, 0.f, (m_fFar + m_fNear) / (m_fFar - m_fNear), 1.f}, {0.f, 0.f, (far + near) / (far - near), 1.f},
{0.f, 0.f, -2.f * m_fNear * m_fFar / (m_fFar - m_fNear), 0.f}, {0.f, 0.f, -near * far / (far - near), 0.f},
}); });
} }
Matrix Camera::GetTranslationMatrix() const Matrix Camera::GetTranslationMatrix() const
@@ -68,10 +67,10 @@ namespace omath::projection
return Matrix( return Matrix(
{ {
{right.x, up.x, forward.x, 0.f}, {right.x, up.x, forward.x, 0.f},
{right.y, up.y, forward.y, 0.f}, {right.y, up.y, forward.y, 0.f},
{right.z, up.z, forward.z, 0.f}, {right.z, up.z, forward.z, 0.f},
{0.f, 0.f, 0.f, 1.f}, {0.f, 0.f, 0.f, 1.f},
}); });
} }
@@ -90,6 +89,6 @@ namespace omath::projection
projected *= Matrix::ToScreenMatrix(m_viewPort.x, m_viewPort.y); projected *= Matrix::ToScreenMatrix(m_viewPort.x, m_viewPort.y);
return Vector3{projected.At(0, 0), projected.At(0, 2), projected.At(0, 1)}; return Vector3{projected.At(0, 0), projected.At(0, 1), projected.At(0, 2)};
} }
} }

View File

@@ -9,10 +9,10 @@
TEST(UnitTestProjection, IsPointOnScreen) TEST(UnitTestProjection, IsPointOnScreen)
{ {
const omath::projection::Camera camera({0, 0, 0}, {0, 0.f, 0.f} , {1920.f, 1080.f, 0.f}, 110, 0.1, 500); const omath::projection::Camera camera({5, 0, 0}, {0, 0.f, 0.f} , {1920.f, 1080.f, 0.f}, 110, 0.1, 500);
const auto proj = camera.WorldToScreen({5, 0, 0}); const auto proj = camera.WorldToScreen({10, 0, 0});
if (proj) if (proj)
std::print("{} {} {}", proj->x, proj->z, proj->y); std::print("{} {} {}", proj->x, proj->y, proj->z);
EXPECT_TRUE(proj.has_value()); EXPECT_TRUE(proj.has_value());
} }