diff --git a/include/omath/Mat.h b/include/omath/Mat.h index e2d73a5..e20410e 100644 --- a/include/omath/Mat.h +++ b/include/omath/Mat.h @@ -2,6 +2,7 @@ // Created by vlad on 9/29/2024. // #pragma once +#include #include #include #include @@ -59,13 +60,13 @@ namespace omath } [[nodiscard]] - static constexpr size_t RowCount() noexcept { return Rows; } + static consteval size_t RowCount() noexcept { return Rows; } [[nodiscard]] - static constexpr size_t ColumnsCount() noexcept { return Columns; } + static consteval size_t ColumnsCount() noexcept { return Columns; } [[nodiscard]] - static constexpr std::pair Size() noexcept { return { Rows, Columns }; } + static consteval std::pair Size() noexcept { return { Rows, Columns }; } [[nodiscard]] constexpr const float& At(const size_t rowIndex, const size_t columnIndex) const @@ -92,11 +93,13 @@ namespace omath constexpr void Clear() { - for (size_t i = 0; i < Rows; ++i) - for (size_t j = 0; j < Columns; ++j) - At(i, j) = 0.f; + Set(0.f); } + constexpr void Set(const float value) + { + std::ranges::fill(m_data, value); + } // Operator overloading for multiplication with another Mat template constexpr Mat operator*(const Mat& other) const @@ -280,16 +283,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} }; } diff --git a/include/omath/Vector2.h b/include/omath/Vector2.h index f04da2d..0bb73e9 100644 --- a/include/omath/Vector2.h +++ b/include/omath/Vector2.h @@ -114,7 +114,10 @@ namespace omath return x * vOther.x + y * vOther.y; } - [[nodiscard]] float Length() const; + [[nodiscard]] constexpr float Length() const + { + return std::hypot(x, y); + } [[nodiscard]] constexpr float LengthSqr() const { @@ -167,7 +170,11 @@ namespace omath } // Normalize the vector - [[nodiscard]] Vector2 Normalized() const; + [[nodiscard]] constexpr Vector2 Normalized() const + { + const float len = Length(); + return len > 0.f ? *this / len : *this; + } // Sum of elements [[nodiscard]] constexpr float Sum() const diff --git a/include/omath/Vector3.h b/include/omath/Vector3.h index 18e2ffd..1b75d39 100644 --- a/include/omath/Vector3.h +++ b/include/omath/Vector3.h @@ -92,8 +92,10 @@ namespace omath return *this; } - [[nodiscard]] - float DistTo(const Vector3& vOther) const; + [[nodiscard]] constexpr float DistTo(const Vector3& vOther) const + { + return (*this - vOther).Length(); + } constexpr Vector3& Abs() { @@ -112,14 +114,21 @@ namespace omath { return Vector2::Dot(vOther) + z * vOther.z; } - [[nodiscard]] float Length() const; + + [[nodiscard]] constexpr float Length() const + { + return std::hypot(x, y, z); + } [[nodiscard]] constexpr float LengthSqr() const { return Vector2::LengthSqr() + z * z; } - [[nodiscard]] float Length2D() const; + [[nodiscard]] constexpr float Length2D() const + { + return Vector2::Length(); + } [[nodiscard]] constexpr Vector3 operator-() const { @@ -182,8 +191,12 @@ namespace omath [[nodiscard]] static Vector3 UpVector(float pitch, float yaw, float roll); - [[nodiscard]] - Vector3 Normalized() const; + [[nodiscard]] constexpr Vector3 Normalized() const + { + const float length = this->Length(); + + return length != 0 ? *this / length : *this; + } [[nodiscard]] std::tuple AsTuple() const { diff --git a/include/omath/projection/Camera.h b/include/omath/projection/Camera.h index 4d37e67..f7020c8 100644 --- a/include/omath/projection/Camera.h +++ b/include/omath/projection/Camera.h @@ -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 WorldToScreen(const Vector3& worldPosition) const; + [[nodiscard]] std::expected WorldToScreen(const Vector3& worldPosition) const; ViewPort m_viewPort{}; float m_fieldOfView; float m_farPlaneDistance; float m_nearPlaneDistance; + float m_lensZoom; private: Vector3 m_viewAngles; diff --git a/include/omath/projection/ErrorCodes.h b/include/omath/projection/ErrorCodes.h index 968a15e..3c5d26c 100644 --- a/include/omath/projection/ErrorCodes.h +++ b/include/omath/projection/ErrorCodes.h @@ -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, }; } \ No newline at end of file diff --git a/source/Vector2.cpp b/source/Vector2.cpp index 66dc4f0..56d8044 100644 --- a/source/Vector2.cpp +++ b/source/Vector2.cpp @@ -8,18 +8,4 @@ namespace omath { - Vector2 Vector2::Normalized() const - { - const float len = Length(); - - if (len > 0.f) - return {x / len, y / len}; - - return {0.f, 0.f}; - } - - float Vector2::Length() const - { - return std::sqrt(x * x + y * y); - } } \ No newline at end of file diff --git a/source/Vector3.cpp b/source/Vector3.cpp index 4d5d756..8da7c68 100644 --- a/source/Vector3.cpp +++ b/source/Vector3.cpp @@ -8,23 +8,6 @@ namespace omath { - - float Vector3::DistTo(const Vector3 &vOther) const - { - return (*this - vOther).Length(); - } - - float Vector3::Length() const - { - return std::sqrt(Vector2::LengthSqr() + z * z); - } - - - float Vector3::Length2D() const - { - return Vector2::Length(); - } - Vector3 Vector3::ViewAngleTo(const Vector3 &other) const { const float distance = DistTo(other); @@ -83,12 +66,4 @@ namespace omath { return RightVector(pitch, yaw, roll).Cross(ForwardVector(pitch, yaw)); } - - - Vector3 Vector3::Normalized() const - { - const float length = this->Length(); - - return length != 0 ? *this / length : *this; - } } \ No newline at end of file diff --git a/source/projection/Camera.cpp b/source/projection/Camera.cpp index 0b49e70..41275b4 100644 --- a/source/projection/Camera.cpp +++ b/source/projection/Camera.cpp @@ -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,7 @@ namespace omath::projection m_fieldOfView = fov; m_nearPlaneDistance = near; m_farPlaneDistance = far; + m_lensZoom = lensZoom; } Mat<4, 4> Camera::GetViewMatrix() const @@ -30,27 +31,28 @@ namespace omath::projection return Mat<4, 4>::TranslationMat(-m_origin) * Mat<4, 4>::OrientationMat(forward, right, up); } - std::expected Camera::WorldToScreen(const Vector3 &worldPosition) const + std::expected Camera::WorldToScreen(const 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); - return Vector2{projected.At(0, 0), projected.At(0, 1)}; + return Vector3{projected.At(0, 0), projected.At(0, 1), projected.At(0, 2)}; } } diff --git a/tests/UnitTestMat.cpp b/tests/UnitTestMat.cpp index 7a0048c..931c72f 100644 --- a/tests/UnitTestMat.cpp +++ b/tests/UnitTestMat.cpp @@ -184,24 +184,6 @@ TEST_F(UnitTestMat, StaticMethod_OrientationMat) EXPECT_FLOAT_EQ(orientMat.At(2, 2), forward.z); } -// Test static method: ProjectionMat -TEST_F(UnitTestMat, StaticMethod_ProjectionMat) -{ - constexpr float fieldOfView = 45.0f; - 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 float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f); - - EXPECT_FLOAT_EQ(projMat.At(0, 0), 1.f / (aspectRatio * fovHalfTan)); - EXPECT_FLOAT_EQ(projMat.At(1, 1), 1.f / fovHalfTan); - EXPECT_FLOAT_EQ(projMat.At(2, 2), (far + near) / (far - near)); - EXPECT_FLOAT_EQ(projMat.At(2, 3), (2.f * near * far) / (far - near)); - EXPECT_FLOAT_EQ(projMat.At(3, 2), -1.f); -} - // Test exception handling in At() method TEST_F(UnitTestMat, Method_At_OutOfRange) { diff --git a/tests/UnitTestProjection.cpp b/tests/UnitTestProjection.cpp index fc2c54d..2bb50f9 100644 --- a/tests/UnitTestProjection.cpp +++ b/tests/UnitTestProjection.cpp @@ -7,10 +7,13 @@ #include #include -TEST(UnitTestProjection, IsPointOnScreen) +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.1f, 500.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, 1.335f); - const auto proj = camera.WorldToScreen({100, 0, 15}); - EXPECT_TRUE(proj.has_value()); + const auto projected = camera.WorldToScreen({5000, 0, 0}); + + + EXPECT_TRUE(projected.has_value()); + EXPECT_EQ(projected->z, 1.f); } \ No newline at end of file