maybe improved matrix

This commit is contained in:
2024-08-27 21:41:21 +03:00
parent 3b897e27a8
commit 8a67b7edbe
3 changed files with 14 additions and 18 deletions

View File

@@ -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;

View File

@@ -2,9 +2,7 @@
// Created by Vlad on 27.08.2024.
//
#include "omath/projection/Camera.h"
#include <complex>
#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;

View File

@@ -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());
}