patched up matrixes

This commit is contained in:
2024-08-27 13:12:35 +03:00
parent 8359524cdb
commit 8fc9a54958
2 changed files with 24 additions and 17 deletions

View File

@@ -28,26 +28,33 @@ namespace omath::projection
Matrix Camera::GetProjectionMatrix() const
{
const auto aspectRatio = m_viewPort.x / m_viewPort.y;
const auto verticalFov = 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 vFov = angles::DegreesToRadians(m_fieldOfView) * (m_viewPort.y / m_viewPort.x);
const float fTop = std::tan(vFov / 2.f);
const float fBottom = -fTop;
const auto m_fFar = m_farPlaneDistance;
const auto m_fNear = m_nearPlaneDistance;
return Matrix({
{2.f / (fRight - fLeft), 0.f, 0.f, 0.f},
{0.f, 0.f, 2.f / (fTop - fBottom), 0.f},
{0.f, (m_fFar + m_fNear) / (m_fFar - m_fNear), 0.f, 1.f},
{0.f, -2.f * m_fNear * m_fFar / (m_fFar - m_fNear), 0.f, 0.f},
});
return Matrix(
{
{1.f / (aspectRatio * verticalFov), 0.f, 0.f, 0.f},
{0.f, 1.f / verticalFov, 0.f, 0.f},
{0.f, 0.f, m_farPlaneDistance / (m_farPlaneDistance-m_nearPlaneDistance), -m_farPlaneDistance*m_nearPlaneDistance / (m_farPlaneDistance-m_nearPlaneDistance)},
{0.f, 0.f, 1.f, 0.f}
});
}
Matrix Camera::GetTranslationMatrix() const
{
return Matrix(
{
{1.f, 0.f, 0.f, m_origin.x},
{0.f, 1.f, 0.f, m_origin.y},
{0.f, 0.f, 1.f, m_origin.z},
{1.f, 0.f, 0.f, -m_origin.x},
{0.f, 1.f, 0.f, -m_origin.y},
{0.f, 0.f, 1.f, -m_origin.z},
{0.f, 0.f, 0.f, 1.f},
});
}
@@ -61,9 +68,9 @@ namespace omath::projection
return Matrix(
{
{right.x, up.y, right.z, 0.f},
{up.x, up.y, up.y, 0.f},
{forward.x, forward.y, forward.z, 0.f},
{right.x, up.x, forward.x, 0.f},
{right.y, up.y, forward.y, 0.f},
{right.z, up.z, forward.z, 0.f},
{0.f, 0.f, 0.f, 1.f},
});
}

View File

@@ -8,8 +8,8 @@
TEST(UnitTestProjection, IsPointOnScreen)
{
const omath::projection::Camera camera({}, {90, 0.f, 0.f} , {1920.f, 1080.f, 0.f}, 120, 10, 100);
const omath::projection::Camera camera({}, {90, 0.f, 0.f} , {1920.f, 1080.f, 0.f}, 120, 10, 100);
const auto proj = camera.WorldToScreen({0,0,15});
const auto proj = camera.WorldToScreen({0, 0, 20});
EXPECT_TRUE(proj.has_value());
}