Merge pull request #10 from orange-cpp/u/orange_cpp/fix

Small fixes
This commit is contained in:
2024-10-20 00:28:25 +03:00
committed by GitHub
10 changed files with 64 additions and 90 deletions

View File

@@ -2,6 +2,7 @@
// Created by vlad on 9/29/2024. // Created by vlad on 9/29/2024.
// //
#pragma once #pragma once
#include <algorithm>
#include <array> #include <array>
#include <sstream> #include <sstream>
#include <utility> #include <utility>
@@ -59,13 +60,13 @@ namespace omath
} }
[[nodiscard]] [[nodiscard]]
static constexpr size_t RowCount() noexcept { return Rows; } static consteval size_t RowCount() noexcept { return Rows; }
[[nodiscard]] [[nodiscard]]
static constexpr size_t ColumnsCount() noexcept { return Columns; } static consteval size_t ColumnsCount() noexcept { return Columns; }
[[nodiscard]] [[nodiscard]]
static constexpr std::pair<size_t, size_t> Size() noexcept { return { Rows, Columns }; } static consteval std::pair<size_t, size_t> Size() noexcept { return { Rows, Columns }; }
[[nodiscard]] constexpr const float& At(const size_t rowIndex, const size_t columnIndex) const [[nodiscard]] constexpr const float& At(const size_t rowIndex, const size_t columnIndex) const
@@ -92,11 +93,13 @@ namespace omath
constexpr void Clear() constexpr void Clear()
{ {
for (size_t i = 0; i < Rows; ++i) Set(0.f);
for (size_t j = 0; j < Columns; ++j)
At(i, j) = 0.f;
} }
constexpr void Set(const float value)
{
std::ranges::fill(m_data, value);
}
// Operator overloading for multiplication with another Mat // Operator overloading for multiplication with another Mat
template <size_t OtherColumns> template <size_t OtherColumns>
constexpr Mat<Rows, OtherColumns> operator*(const Mat<Columns, OtherColumns>& other) const constexpr Mat<Rows, OtherColumns> operator*(const Mat<Columns, OtherColumns>& other) const
@@ -280,16 +283,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}
}; };
} }

View File

@@ -114,7 +114,10 @@ namespace omath
return x * vOther.x + y * vOther.y; 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 [[nodiscard]] constexpr float LengthSqr() const
{ {
@@ -167,7 +170,11 @@ namespace omath
} }
// Normalize the vector // 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 // Sum of elements
[[nodiscard]] constexpr float Sum() const [[nodiscard]] constexpr float Sum() const

View File

@@ -92,8 +92,10 @@ namespace omath
return *this; return *this;
} }
[[nodiscard]] [[nodiscard]] constexpr float DistTo(const Vector3& vOther) const
float DistTo(const Vector3& vOther) const; {
return (*this - vOther).Length();
}
constexpr Vector3& Abs() constexpr Vector3& Abs()
{ {
@@ -112,14 +114,21 @@ namespace omath
{ {
return Vector2::Dot(vOther) + z * vOther.z; 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 [[nodiscard]] constexpr float LengthSqr() const
{ {
return Vector2::LengthSqr() + z * z; return Vector2::LengthSqr() + z * z;
} }
[[nodiscard]] float Length2D() const; [[nodiscard]] constexpr float Length2D() const
{
return Vector2::Length();
}
[[nodiscard]] constexpr Vector3 operator-() const [[nodiscard]] constexpr Vector3 operator-() const
{ {
@@ -182,8 +191,12 @@ namespace omath
[[nodiscard]] static Vector3 UpVector(float pitch, float yaw, float roll); [[nodiscard]] static Vector3 UpVector(float pitch, float yaw, float roll);
[[nodiscard]] [[nodiscard]] constexpr Vector3 Normalized() const
Vector3 Normalized() const; {
const float length = this->Length();
return length != 0 ? *this / length : *this;
}
[[nodiscard]] std::tuple<float, float, float> AsTuple() const [[nodiscard]] std::tuple<float, float, float> AsTuple() const
{ {

View File

@@ -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<Vector3, Error> WorldToScreen(const 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;

View File

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

View File

@@ -8,18 +8,4 @@
namespace omath 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);
}
} }

View File

@@ -8,23 +8,6 @@
namespace omath 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 Vector3 Vector3::ViewAngleTo(const Vector3 &other) const
{ {
const float distance = DistTo(other); const float distance = DistTo(other);
@@ -83,12 +66,4 @@ namespace omath
{ {
return RightVector(pitch, yaw, roll).Cross(ForwardVector(pitch, yaw)); return RightVector(pitch, yaw, roll).Cross(ForwardVector(pitch, yaw));
} }
Vector3 Vector3::Normalized() const
{
const float length = this->Length();
return length != 0 ? *this / length : *this;
}
} }

View File

@@ -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,7 @@ 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,27 +31,28 @@ 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<Vector3, Error> Camera::WorldToScreen(const 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);
return Vector2{projected.At(0, 0), projected.At(0, 1)}; return Vector3{projected.At(0, 0), projected.At(0, 1), projected.At(0, 2)};
} }
} }

View File

@@ -184,24 +184,6 @@ TEST_F(UnitTestMat, StaticMethod_OrientationMat)
EXPECT_FLOAT_EQ(orientMat.At(2, 2), forward.z); 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 exception handling in At() method
TEST_F(UnitTestMat, Method_At_OutOfRange) TEST_F(UnitTestMat, Method_At_OutOfRange)
{ {

View File

@@ -7,10 +7,13 @@
#include <print> #include <print>
#include <omath/projection/Camera.h> #include <omath/projection/Camera.h>
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}); const auto projected = camera.WorldToScreen({5000, 0, 0});
EXPECT_TRUE(proj.has_value());
EXPECT_TRUE(projected.has_value());
EXPECT_EQ(projected->z, 1.f);
} }