fixed projection matrix

This commit is contained in:
2024-10-18 17:20:23 +03:00
parent dc26ed23f5
commit 3e6cabb6c7
6 changed files with 22 additions and 24 deletions

View File

@@ -280,16 +280,18 @@ namespace omath
}
[[nodiscard]]
constexpr static Mat<4, 4> ProjectionMat(const float fieldOfView, const float aspectRatio, const float near, const float far)
constexpr static Mat<4, 4> ProjectionMat(const float fieldOfView, const float aspectRatio,
const float near, const float far, const float lensZoom)
{
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
const float frustumHeight = far - near;
return
{
{1.f / (aspectRatio * fovHalfTan), 0.f, 0.f, 0.f},
{0.f, 1.f / fovHalfTan, 0.f, 0.f},
{0.f, 0.f, (far + near) / (far - near), 2.f * near * far / (far - near)},
{0.f, 0.f, -1.f, 0.f}
{-1.f / (aspectRatio * fovHalfTan) * lensZoom, 0.f, 0.f, 0.f},
{0.f, -1.f / fovHalfTan * lensZoom, 0.f, 0.f},
{0.f, 0.f, -far / frustumHeight, -1},
{0.f, 0.f, near * far / frustumHeight, 0.f}
};
}

View File

@@ -25,18 +25,20 @@ namespace omath::projection
class Camera
{
public:
Camera(const Vector3& position, const Vector3& viewAngles, const ViewPort& viewPort, float fov, float near, float far);
Camera(const Vector3& position, const Vector3& viewAngles, const ViewPort& viewPort,
float fov, float near, float far, float lensZoom);
void SetViewAngles(const Vector3& viewAngles);
[[nodiscard]] Mat<4, 4> GetViewMatrix() const;
[[nodiscard]] std::expected<Vector2, Error> WorldToScreen(const Vector3& worldPosition) const;
[[nodiscard]] std::expected<Vector2, Error> WorldToScreen(Vector3 worldPosition) const;
ViewPort m_viewPort{};
float m_fieldOfView;
float m_farPlaneDistance;
float m_nearPlaneDistance;
float m_lensZoom;
private:
Vector3 m_viewAngles;

View File

@@ -10,7 +10,6 @@ namespace omath::projection
{
enum class Error : uint16_t
{
WORLD_POSITION_IS_BEHIND_CAMERA = 0,
WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS,
};
}

View File

@@ -11,7 +11,7 @@
namespace omath::projection
{
Camera::Camera(const Vector3 &position, const Vector3 &viewAngles, const ViewPort &viewPort,
const float fov, const float near, const float far)
const float fov, const float near, const float far, const float lensZoom)
{
m_origin = position;
m_viewAngles = viewAngles;
@@ -19,6 +19,8 @@ namespace omath::projection
m_fieldOfView = fov;
m_nearPlaneDistance = near;
m_farPlaneDistance = far;
m_lensZoom = lensZoom;
}
Mat<4, 4> Camera::GetViewMatrix() const
@@ -30,23 +32,24 @@ namespace omath::projection
return Mat<4, 4>::TranslationMat(-m_origin) * Mat<4, 4>::OrientationMat(forward, right, up);
}
std::expected<Vector2, Error> Camera::WorldToScreen(const Vector3 &worldPosition) const
std::expected<Vector2, Error> Camera::WorldToScreen(Vector3 worldPosition) const
{
const auto posVecAsMatrix = Mat<1, 4>({{worldPosition.x, worldPosition.y, worldPosition.z, 1.f}});
const auto projectionMatrix = Mat<4, 4>::ProjectionMat(m_fieldOfView, m_viewPort.AspectRatio(),
m_nearPlaneDistance, m_farPlaneDistance);
m_nearPlaneDistance, m_farPlaneDistance, 1.335f);
Mat<1, 4> projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix);
if (projected.At(0, 3) <= 0.f)
return std::unexpected(Error::WORLD_POSITION_IS_BEHIND_CAMERA);
if (projected.At(0, 3) == 0.f)
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
projected /= projected.At(0, 3);
if (projected.At(0, 0) < -1.f || projected.At(0, 0) > 1.f ||
projected.At(0, 1) < -1.f || projected.At(0, 1) > 1.f)
projected.At(0, 1) < -1.f || projected.At(0, 1) > 1.f ||
projected.At(0, 2) < -1.f || projected.At(0, 2) > 1.f)
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
projected *= Mat<4, 4>::ToScreenMat(m_viewPort.m_width, m_viewPort.m_height);

View File

@@ -191,7 +191,7 @@ TEST_F(UnitTestMat, StaticMethod_ProjectionMat)
constexpr float aspectRatio = 1.33f;
constexpr float near = 0.1f;
constexpr float far = 100.0f;
const Mat<4, 4> projMat = Mat<4, 4>::ProjectionMat(fieldOfView, aspectRatio, near, far);
const Mat<4, 4> projMat = Mat<4, 4>::ProjectionMat(fieldOfView, aspectRatio, near, far, 1.335f);
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);

View File

@@ -11,13 +11,5 @@ TEST(UnitTestProjection, Projection)
{
const omath::projection::Camera camera({0.f, 0.f, 0.f}, {0, 0.f, 0.f} , {1920.f, 1080.f}, 110.f, 0.375f, 5000.f);
EXPECT_EQ(camera.WorldToScreen({100, 0, 0}).value(), omath::Vector2(960, 540));
EXPECT_EQ(camera.WorldToScreen({49.23, 0, 0}).value(), omath::Vector2(960, 540));
const auto proj = camera.WorldToScreen({100, 50, -69});
std::print("{} {}", proj->x, proj->y);
EXPECT_EQ(camera.WorldToScreen({100, 10, 0}).value(), omath::Vector2(909.58887, 540));
EXPECT_EQ(camera.WorldToScreen({100, 10, 8}).value(), omath::Vector2(909.58887, 499.67108));
EXPECT_EQ(camera.WorldToScreen({100, 50, -69}).value(), omath::Vector2(707.9442, 887.83704));
EXPECT_FALSE(camera.WorldToScreen({-10,0, 0}).has_value());
camera.WorldToScreen({5000, 0, 0});
}