mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
fixed projection matrix
This commit is contained in:
@@ -280,16 +280,18 @@ namespace omath
|
|||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]]
|
[[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 fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
|
||||||
|
const float frustumHeight = far - near;
|
||||||
|
|
||||||
return
|
return
|
||||||
{
|
{
|
||||||
{1.f / (aspectRatio * fovHalfTan), 0.f, 0.f, 0.f},
|
{-1.f / (aspectRatio * fovHalfTan) * lensZoom, 0.f, 0.f, 0.f},
|
||||||
{0.f, 1.f / fovHalfTan, 0.f, 0.f},
|
{0.f, -1.f / fovHalfTan * lensZoom, 0.f, 0.f},
|
||||||
{0.f, 0.f, (far + near) / (far - near), 2.f * near * far / (far - near)},
|
{0.f, 0.f, -far / frustumHeight, -1},
|
||||||
{0.f, 0.f, -1.f, 0.f}
|
{0.f, 0.f, near * far / frustumHeight, 0.f}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,18 +25,20 @@ namespace omath::projection
|
|||||||
class Camera
|
class Camera
|
||||||
{
|
{
|
||||||
public:
|
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);
|
void SetViewAngles(const Vector3& viewAngles);
|
||||||
|
|
||||||
[[nodiscard]] Mat<4, 4> GetViewMatrix() const;
|
[[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{};
|
ViewPort m_viewPort{};
|
||||||
float m_fieldOfView;
|
float m_fieldOfView;
|
||||||
|
|
||||||
float m_farPlaneDistance;
|
float m_farPlaneDistance;
|
||||||
float m_nearPlaneDistance;
|
float m_nearPlaneDistance;
|
||||||
|
float m_lensZoom;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Vector3 m_viewAngles;
|
Vector3 m_viewAngles;
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ namespace omath::projection
|
|||||||
{
|
{
|
||||||
enum class Error : uint16_t
|
enum class Error : uint16_t
|
||||||
{
|
{
|
||||||
WORLD_POSITION_IS_BEHIND_CAMERA = 0,
|
|
||||||
WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS,
|
WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
namespace omath::projection
|
namespace omath::projection
|
||||||
{
|
{
|
||||||
Camera::Camera(const Vector3 &position, const Vector3 &viewAngles, const ViewPort &viewPort,
|
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_origin = position;
|
||||||
m_viewAngles = viewAngles;
|
m_viewAngles = viewAngles;
|
||||||
@@ -19,6 +19,8 @@ namespace omath::projection
|
|||||||
m_fieldOfView = fov;
|
m_fieldOfView = fov;
|
||||||
m_nearPlaneDistance = near;
|
m_nearPlaneDistance = near;
|
||||||
m_farPlaneDistance = far;
|
m_farPlaneDistance = far;
|
||||||
|
m_lensZoom = lensZoom;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Mat<4, 4> Camera::GetViewMatrix() const
|
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);
|
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 posVecAsMatrix = Mat<1, 4>({{worldPosition.x, worldPosition.y, worldPosition.z, 1.f}});
|
||||||
|
|
||||||
|
|
||||||
const auto projectionMatrix = Mat<4, 4>::ProjectionMat(m_fieldOfView, m_viewPort.AspectRatio(),
|
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);
|
Mat<1, 4> projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix);
|
||||||
|
|
||||||
if (projected.At(0, 3) <= 0.f)
|
if (projected.At(0, 3) == 0.f)
|
||||||
return std::unexpected(Error::WORLD_POSITION_IS_BEHIND_CAMERA);
|
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
|
||||||
|
|
||||||
projected /= projected.At(0, 3);
|
projected /= projected.At(0, 3);
|
||||||
|
|
||||||
if (projected.At(0, 0) < -1.f || projected.At(0, 0) > 1.f ||
|
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);
|
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
|
||||||
|
|
||||||
projected *= Mat<4, 4>::ToScreenMat(m_viewPort.m_width, m_viewPort.m_height);
|
projected *= Mat<4, 4>::ToScreenMat(m_viewPort.m_width, m_viewPort.m_height);
|
||||||
|
|||||||
@@ -191,7 +191,7 @@ TEST_F(UnitTestMat, StaticMethod_ProjectionMat)
|
|||||||
constexpr float aspectRatio = 1.33f;
|
constexpr float aspectRatio = 1.33f;
|
||||||
constexpr float near = 0.1f;
|
constexpr float near = 0.1f;
|
||||||
constexpr float far = 100.0f;
|
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);
|
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
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));
|
camera.WorldToScreen({5000, 0, 0});
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user