diff --git a/CMakePresets.json b/CMakePresets.json index 74671b4..d67436b 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -8,8 +8,8 @@ "binaryDir": "${sourceDir}/cmake-build/build/${presetName}", "installDir": "${sourceDir}/cmake-build/install/${presetName}", "cacheVariables": { - "CMAKE_C_COMPILER": "clang.exe", - "CMAKE_CXX_COMPILER": "clang++.exe" + "CMAKE_C_COMPILER": "cl.exe", + "CMAKE_CXX_COMPILER": "cl.exe" }, "condition": { "type": "equals", diff --git a/extlibs/CMakeLists.txt b/extlibs/CMakeLists.txt index caca17c..bf73c7a 100644 --- a/extlibs/CMakeLists.txt +++ b/extlibs/CMakeLists.txt @@ -1,2 +1 @@ -add_subdirectory(googletest) -add_subdirectory(glm) \ No newline at end of file +add_subdirectory(googletest) \ No newline at end of file diff --git a/include/omath/Angle.hpp b/include/omath/Angle.hpp index 8ddaa2e..9b5c64f 100644 --- a/include/omath/Angle.hpp +++ b/include/omath/Angle.hpp @@ -5,6 +5,8 @@ #pragma once #include "omath/Angles.hpp" +#include + namespace omath { @@ -14,14 +16,12 @@ namespace omath Clamped = 1, }; - template + template requires std::is_arithmetic_v class Angle { Type m_angle; - public: - - constexpr explicit Angle(const Type& degrees) + constexpr Angle(const Type& degrees) { if constexpr (flags == AngleFlags::Normalized) m_angle = angles::WrapAngle(degrees, min, max); @@ -34,17 +34,20 @@ namespace omath std::unreachable(); } } - + public: [[nodiscard]] constexpr static Angle FromDegrees(const Type& degrees) { return {degrees}; } + constexpr Angle() : m_angle(0) + { + } [[nodiscard]] constexpr static Angle FromRadians(const Type& degrees) { - return {angles::RadiansToDegrees(degrees)}; + return {angles::RadiansToDegrees(degrees)}; } [[nodiscard]] @@ -54,23 +57,11 @@ namespace omath } [[nodiscard]] - constexpr Type& operator*() + constexpr Type AsDegrees() const { return m_angle; } - [[nodiscard]] - constexpr const Type& Value() const - { - return **std::as_const(this); - } - - [[nodiscard]] - constexpr Type& Value() - { - return **this; - } - [[nodiscard]] constexpr Type AsRadians() const { @@ -86,7 +77,7 @@ namespace omath [[nodiscard]] Type Cos() const { - return std::sin(AsRadians()); + return std::cos(AsRadians()); } [[nodiscard]] @@ -108,13 +99,13 @@ namespace omath } [[nodiscard]] - constexpr Angle& operator+=(const Type& other) + constexpr Angle& operator+=(const Angle& other) { if constexpr (flags == AngleFlags::Normalized) - m_angle = angles::WrapAngle(m_angle + other, min, max); + m_angle = angles::WrapAngle(m_angle + other.m_angle, min, max); else if constexpr (flags == AngleFlags::Clamped) - m_angle = std::clamp(m_angle + other, min, max); + m_angle = std::clamp(m_angle + other.m_angle, min, max); else { static_assert(false); @@ -125,19 +116,22 @@ namespace omath } [[nodiscard]] - constexpr Angle& operator-=(const Type& other) + constexpr std::partial_ordering operator<=>(const Angle& other) const = default; + + [[nodiscard]] + constexpr Angle& operator-=(const Angle& other) { return operator+=(-other); } [[nodiscard]] - constexpr Angle& operator+(const Type& other) + constexpr Angle& operator+(const Angle& other) { if constexpr (flags == AngleFlags::Normalized) - return {angles::WrapAngle(m_angle + other, min, max)}; + return {angles::WrapAngle(m_angle + other.m_angle, min, max)}; else if constexpr (flags == AngleFlags::Clamped) - return {std::clamp(m_angle + other, min, max)}; + return {std::clamp(m_angle + other.m_angle, min, max)}; else static_assert(false); @@ -146,11 +140,9 @@ namespace omath } [[nodiscard]] - constexpr Angle& operator-(const Type& other) + constexpr Angle& operator-(const Angle& other) { return operator+(-other); } - - }; } diff --git a/include/omath/Angles.hpp b/include/omath/Angles.hpp index 7b0a3d1..47a287a 100644 --- a/include/omath/Angles.hpp +++ b/include/omath/Angles.hpp @@ -11,14 +11,14 @@ namespace omath::angles { template requires std::is_floating_point_v - [[nodiscard]] constexpr float RadiansToDegrees(const Type& radians) + [[nodiscard]] constexpr Type RadiansToDegrees(const Type& radians) { return radians * (Type(180) / std::numbers::pi_v); } template requires std::is_floating_point_v - [[nodiscard]] constexpr float DegreesToRadians(const Type& degrees) + [[nodiscard]] constexpr Type DegreesToRadians(const Type& degrees) { return degrees * (std::numbers::pi_v / Type(180)); } diff --git a/include/omath/Mat.hpp b/include/omath/Mat.hpp index 536ca37..2f25bd5 100644 --- a/include/omath/Mat.hpp +++ b/include/omath/Mat.hpp @@ -5,10 +5,10 @@ #include #include #include -#include -#include "Vector3.hpp" #include +#include #include "Angles.hpp" +#include "Vector3.hpp" namespace omath @@ -33,8 +33,11 @@ namespace omath { Clear(); } - - constexpr Mat(const std::initializer_list >& rows) + constexpr static MatStoreType GetStoreOrdering() + { + return StoreType; + } + constexpr Mat(const std::initializer_list>& rows) { if (rows.size() != Rows) throw std::invalid_argument("Initializer list rows size does not match template parameter Rows"); @@ -44,7 +47,7 @@ namespace omath { if (rowIt->size() != Columns) throw std::invalid_argument( - "All rows must have the same number of columns as template parameter Columns"); + "All rows must have the same number of columns as template parameter Columns"); auto colIt = rowIt->begin(); for (size_t j = 0; j < Columns; ++j, ++colIt) @@ -133,8 +136,8 @@ namespace omath // Operator overloading for multiplication with another Mat template - constexpr Mat operator*( - const Mat& other) const + constexpr Mat + operator*(const Mat& other) const { Mat result; @@ -158,8 +161,8 @@ namespace omath } template - constexpr Mat operator*=( - const Mat& other) + constexpr Mat + operator*=(const Mat& other) { return *this = *this * other; } @@ -306,24 +309,22 @@ namespace omath [[nodiscard]] constexpr static Mat<4, 4> ToScreenMat(const Type& screenWidth, const Type& screenHeight) noexcept { - return - { - {screenWidth / 2, 0, 0, 0}, - {0, -screenHeight / 2, 0, 0}, - {0, 0, 1, 0}, - {screenWidth / 2, screenHeight / 2, 0, 1}, + return { + {screenWidth / 2, 0, 0, 0}, + {0, -screenHeight / 2, 0, 0}, + {0, 0, 1, 0}, + {screenWidth / 2, screenHeight / 2, 0, 1}, }; } [[nodiscard]] constexpr static Mat<4, 4> TranslationMat(const Vector3& diff) noexcept { - return - { - {1, 0, 0, 0}, - {0, 1, 0, 0}, - {0, 0, 1, 0}, - {diff.x, diff.y, diff.z, 1}, + return { + {1, 0, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 1, 0}, + {diff.x, diff.y, diff.z, 1}, }; } @@ -331,52 +332,55 @@ namespace omath constexpr static Mat<4, 4> OrientationMat(const Vector3& forward, const Vector3& right, const Vector3& up) noexcept { - return - { - {right.x, up.x, forward.x, 0}, - {right.y, up.y, forward.y, 0}, - {right.z, up.z, forward.z, 0}, - {0, 0, 0, 1}, + return { + {right.x, up.x, forward.x, 0}, + {right.y, up.y, forward.y, 0}, + {right.z, up.z, forward.z, 0}, + {0, 0, 0, 1}, }; } [[nodiscard]] - constexpr static Mat<4, 4> ProjectionMat(const Type& fieldOfView, const Type& aspectRatio, - const Type& near, const Type& far, const Type& lensZoom) noexcept + constexpr static Mat<4, 4> ProjectionMat(const Type& fieldOfView, const Type& aspectRatio, const Type& near, + const Type& far, const Type& lensZoom) noexcept { const Type& fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2); const Type& frustumHeight = far - near; - return - { - {-1 / (aspectRatio * fovHalfTan) * lensZoom, 0, 0, 0}, - {0, -1 / fovHalfTan * lensZoom, 0, 0}, - {0, 0, -far / frustumHeight, -1}, - {0, 0, near * far / frustumHeight, 0} - }; - } - - [[nodiscard]] - constexpr static Mat<4, 1> MatRowFromVector(const Vector3& vector) noexcept - { - return {{vector.x, vector.y, vector.z, 1}}; - } - - [[nodiscard]] - constexpr static Mat<1, 4> MatColumnFromVector(const Vector3& vector) noexcept - { - return - { - { - vector.x, - vector.y, - vector.z, - 1 - } - }; + return {{-1 / (aspectRatio * fovHalfTan) * lensZoom, 0, 0, 0}, + {0, -1 / fovHalfTan * lensZoom, 0, 0}, + {0, 0, -far / frustumHeight, -1}, + {0, 0, near * far / frustumHeight, 0}}; } private: std::array m_data; }; -} + + template + [[nodiscard]] + constexpr static Mat<1, 4, T, St> MatRowFromVector(const Vector3& vector) noexcept + { + return {{vector.x, vector.y, vector.z, 1}}; + } + + template + [[nodiscard]] + constexpr static Mat<4, 1, T, St> MatColumnFromVector(const Vector3& vector) noexcept + { + return { + {vector.x}, {vector.y}, {vector.z}, {1}}; + } + + template + [[nodiscard]] + constexpr Mat<4, 4, T, St> MatTranslation(const Vector3& diff) noexcept + { + return { + {1, 0, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 1, 0}, + {diff.x, diff.y, diff.z, 1}, + }; + } +} // namespace omath diff --git a/include/omath/Vector3.hpp b/include/omath/Vector3.hpp index a9a687e..e22d815 100644 --- a/include/omath/Vector3.hpp +++ b/include/omath/Vector3.hpp @@ -216,10 +216,6 @@ namespace omath [[nodiscard]] Vector3 ViewAngleTo(const Vector3& other) const; - [[nodiscard]] static Vector3 ForwardVector(float pitch, float yaw); - [[nodiscard]] static Vector3 RightVector(float pitch, float yaw, float roll); - [[nodiscard]] static Vector3 UpVector(float pitch, float yaw, float roll); - [[nodiscard]] std::tuple AsTuple() const { return std::make_tuple(x, y, z); diff --git a/include/omath/ViewAngles.hpp b/include/omath/ViewAngles.hpp index cf36ca2..d744f6b 100644 --- a/include/omath/ViewAngles.hpp +++ b/include/omath/ViewAngles.hpp @@ -2,34 +2,14 @@ // Created by Orange on 11/30/2024. // #pragma once -#include -#include <__algorithm/clamp.h> - -#include "omath/Angles.hpp" - namespace omath { - template - requires std::is_arithmetic_v - class ViewAngles + template + struct ViewAngles { - Type pitch; - Type yaw; - Type roll; - - constexpr void SetPitch(const Type& newPitch) - { - pitch = std::clamp(newPitch, min, max); - } - void SetYaw(const Type& newYaw) - { - yaw = std::clamp(newYaw, min, max); - } - void SetRoll(const Type& newRoll) - { - roll = angles::WrapAngle(newRoll, min, max); - } - + PitchType pitch; + YawType yaw; + RollType roll; }; } diff --git a/include/omath/engines/source.hpp b/include/omath/engines/source.hpp index 61eb1b6..5bb085e 100644 --- a/include/omath/engines/source.hpp +++ b/include/omath/engines/source.hpp @@ -2,8 +2,12 @@ // Created by Orange on 11/24/2024. // #pragma once -#include "omath/Vector3.hpp" #include "omath/Mat.hpp" +#include "omath/Vector3.hpp" + +#include +#include +#include namespace omath::source { @@ -11,22 +15,119 @@ namespace omath::source constexpr Vector3 kAbsRight = {0, -1, 0}; constexpr Vector3 kAbsForward = {1, 0, 0}; + using PitchAngle = Angle; + using YawAngle = Angle; + using RollAngle = Angle; + + using ViewAngles = omath::ViewAngles; - template requires std::is_floating_point_v || std::is_integral_v - [[nodiscard]] Mat<4, 4, Type, MatStoreType::COLUMN_MAJOR> PerspectiveProjectionMatrix( - const float fieldOfView, const Type &aspectRatio, const Type &near, const Type &far) + inline Vector3 ForwardVector(const ViewAngles& angles); + inline Vector3 RightVector(const ViewAngles& angles); + inline Vector3 UpVector(const ViewAngles& angles); + + + template + requires std::is_floating_point_v || std::is_integral_v + [[nodiscard]] constexpr Mat<4, 4, Type, MatStoreType::COLUMN_MAJOR> ViewMatrixFromVecs(const Vector3& forward, const Vector3& right, + const Vector3& up, const Vector3& camera_pos) { - const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2); - - return - { - {static_cast(1) / (aspectRatio * fovHalfTan), 0, 0, 0}, - {0, static_cast(1) / (fovHalfTan), 0, 0}, - {0, 0, (far + near) / (far - near), -(static_cast(2) * far * near) / (far - near)}, - {0, 0, 1, 0}, + return MatTranslation(-camera_pos) * Mat<4, 4, Type, MatStoreType::COLUMN_MAJOR>{ + {right.x, up.x, forward.x, 0}, + {right.y, up.y, forward.y, 0}, + {right.z, up.z, forward.z, 0}, + {0, 0, 0, 1}, }; } -} + template + requires std::is_floating_point_v || std::is_integral_v + [[nodiscard]] Mat<4, 4, Type, MatStoreType::COLUMN_MAJOR> ViewMatrix(const ViewAngles& angles, const Vector3& cam_origin) + { + return ViewMatrixFromVecs(ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin); + } + + template + requires std::is_floating_point_v || std::is_integral_v + [[nodiscard]] Mat<4, 4, Type, MatStoreType::COLUMN_MAJOR> + PerspectiveProjectionMatrix(const Type& fieldOfView, const Type& aspectRatio, const Type& near, const Type& far) + { + const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2); + + return { + {static_cast(1) / (aspectRatio * fovHalfTan), 0, 0, 0}, + {0, static_cast(1) / (fovHalfTan), 0, 0}, + {0, 0, (far + near) / (far - near), -(static_cast(2) * far * near) / (far - near)}, + {0, 0, 1, 0}, + }; + } + // Copied from + // https://github.com/ValveSoftware/source-sdk-2013/blob/0d8dceea4310fde5706b3ce1c70609d72a38efdf/mp/src/mathlib/mathlib_base.cpp#L919 + [[nodiscard]] + inline Vector3 ForwardVector(const ViewAngles& angles) + { + const auto cosPitch = angles.pitch.Cos(); + const auto sinPitch = angles.pitch.Sin(); + + const auto cosYaw = angles.yaw.Cos(); + const auto sinYaw = angles.yaw.Sin(); + + + return {cosPitch * cosYaw, cosPitch * sinYaw, -sinPitch}; + } + + [[nodiscard]] + inline Vector3 RightVector(const ViewAngles& angles) + { + const auto cosPitch = angles.pitch.Cos(); + const auto sinPitch = angles.pitch.Sin(); + + const auto cosYaw = angles.yaw.Cos(); + const auto sinYaw = angles.yaw.Sin(); + + const auto cosRoll = angles.roll.Cos(); + const auto sinRoll = angles.roll.Sin(); + + + return + { + -1 * sinRoll * sinPitch * cosYaw + -1 * cosRoll * -sinYaw, + -1 * sinRoll * sinPitch * sinYaw + -1 * cosRoll * cosYaw, + -1 * sinRoll * cosPitch + }; + } + + [[nodiscard]] + inline Vector3 UpVector(const ViewAngles& angles) + { + const auto cosPitch = angles.pitch.Cos(); + const auto sinPitch = angles.pitch.Sin(); + + const auto cosYaw = angles.yaw.Cos(); + const auto sinYaw = angles.yaw.Sin(); + + const auto cosRoll = angles.roll.Cos(); + const auto sinRoll = angles.roll.Sin(); + + + return + { + cosRoll * sinPitch * cosYaw + - sinRoll * -sinYaw, + cosRoll * sinPitch * sinYaw + - sinRoll * cosYaw, + cosRoll * cosPitch, + }; + } + using Camera = omath::projection::Camera), decltype(PerspectiveProjectionMatrix)>; + + + // Camera(const Vector3& position, const ViewAnglesType& viewAngles, const ViewPort& viewPort, + // const Angle& fov, const float near, const float far, + // const std::function& viewMatFunc, const std::function& projFunc) + + inline Camera CreateCamera(const Vector3& position, const auto& viewAngles, const projection::ViewPort& viewPort, + const Angle& fov, const float near, const float far) + { + return Camera(position, viewAngles, viewPort, fov, near, far, ViewMatrix, PerspectiveProjectionMatrix); + } +} // namespace omath::source diff --git a/include/omath/projection/Camera.hpp b/include/omath/projection/Camera.hpp index ac5a241..e190664 100644 --- a/include/omath/projection/Camera.hpp +++ b/include/omath/projection/Camera.hpp @@ -5,9 +5,11 @@ #pragma once #include -#include #include +#include #include "ErrorCodes.hpp" +#include +#include namespace omath::projection @@ -18,29 +20,60 @@ namespace omath::projection float m_width; float m_height; - [[nodiscard]] constexpr float AspectRatio() const {return m_width / m_height;} + [[nodiscard]] constexpr float AspectRatio() const + { + return m_width / m_height; + } }; + template + requires std::is_same_v, + std::invoke_result_t> class Camera { public: - Camera(const Vector3& position, const Vector3& viewAngles, const ViewPort& viewPort, - float fov, float near, float far, float lensZoom); - void SetViewAngles(const Vector3& viewAngles); + Camera(const Vector3& position, const ViewAnglesType& viewAngles, const ViewPort& viewPort, + const Angle& fov, const float near, const float far, + const std::function& viewMatFunc, const std::function& projFunc) : + m_viewPort(viewPort), m_fieldOfView(fov), m_farPlaneDistance(far), m_nearPlaneDistance(near), + m_viewAngles(viewAngles), m_origin(position), CreateViewMatrix(viewMatFunc), CreateProjectionMatrix(projFunc) + { + } - [[nodiscard]] Mat<4, 4> GetViewMatrix() const; + void LookAt(const Vector3& target); - [[nodiscard]] std::expected WorldToScreen(const Vector3& worldPosition) const; + [[nodiscard]] auto GetViewMatrix() const + { + return CreateViewMatrix(m_viewAngles, m_origin); + } + + [[nodiscard]] auto GetProjectionMatrix() const + { + return CreateProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance, m_farPlaneDistance); + } + + [[nodiscard]] std::expected WorldToScreen([[maybe_unused]] const Vector3& worldPosition) const + { + using mat = std::invoke_result_t; + const auto vecAsMatrix = MatColumnFromVector(worldPosition); + + const auto projected = GetViewMatrix().Transposed() * vecAsMatrix; + + + return Vector3{projected.At(0,0), projected.At(1,0), projected.At(2,0)}; + } ViewPort m_viewPort{}; - float m_fieldOfView; + Angle m_fieldOfView; float m_farPlaneDistance; float m_nearPlaneDistance; - float m_lensZoom; private: - Vector3 m_viewAngles; + ViewAnglesType m_viewAngles; Vector3 m_origin; + + std::function CreateViewMatrix; + std::function CreateProjectionMatrix; }; -} +} // namespace omath::projection diff --git a/source/Vector3.cpp b/source/Vector3.cpp index acd2b02..45b9f1e 100644 --- a/source/Vector3.cpp +++ b/source/Vector3.cpp @@ -20,50 +20,4 @@ namespace omath 0.f }; } - - Vector3 Vector3::ForwardVector(const float pitch, const float yaw) - { - const auto cosPitch = std::cos(angles::DegreesToRadians(pitch)); - const auto sinPitch = std::sin(angles::DegreesToRadians(pitch)); - - const auto cosYaw = std::cos(angles::DegreesToRadians(yaw)); - const auto sinYaw = std::sin(angles::DegreesToRadians(yaw)); - - - return - { - cosPitch*cosYaw, - cosPitch*sinYaw, - sinPitch - }; - } - - Vector3 Vector3::RightVector(const float pitch, const float yaw, const float roll) - { - const auto radPitch = angles::DegreesToRadians(pitch); - const auto radYaw = angles::DegreesToRadians(yaw); - const auto radRoll = angles::DegreesToRadians(roll); - - const auto cosPitch = std::cos(radPitch); - const auto sinPitch = std::sin(radPitch); - - const auto cosYaw = std::cos(radYaw); - const auto sinYaw = std::sin(radYaw); - - const auto cosRoll = std::cos(radRoll); - const auto sinRoll = std::sin(radRoll); - - - return - { - sinRoll*sinPitch*cosYaw + cosRoll*sinYaw, - sinRoll*sinPitch*sinYaw - cosRoll*cosYaw, - -sinRoll*cosPitch - }; - } - - Vector3 Vector3::UpVector(float pitch, float yaw, float roll) - { - return RightVector(pitch, yaw, roll).Cross(ForwardVector(pitch, yaw)); - } } \ No newline at end of file diff --git a/source/prediction/Projectile.cpp b/source/prediction/Projectile.cpp index 26cfd8e..3ceb951 100644 --- a/source/prediction/Projectile.cpp +++ b/source/prediction/Projectile.cpp @@ -4,13 +4,13 @@ #include "omath/prediction/Projectile.hpp" #include - +#include namespace omath::prediction { Vector3 Projectile::PredictPosition(const float pitch, const float yaw, const float time, const float gravity) const { - auto currentPos = m_origin + Vector3::ForwardVector(pitch, yaw) * m_launchSpeed * time; + auto currentPos = m_origin + omath::source::ForwardVector({source::PitchAngle::FromDegrees(pitch), source::YawAngle::FromDegrees(yaw), source::RollAngle::FromDegrees(0)}) * m_launchSpeed * time; currentPos.z -= (gravity * m_gravityScale) * std::pow(time, 2.f) * 0.5f; return currentPos; diff --git a/source/projection/Camera.cpp b/source/projection/Camera.cpp index af9782b..dc0e070 100644 --- a/source/projection/Camera.cpp +++ b/source/projection/Camera.cpp @@ -6,49 +6,4 @@ 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 lensZoom) - { - m_origin = position; - m_viewAngles = viewAngles; - m_viewPort = viewPort; - m_fieldOfView = fov; - m_nearPlaneDistance = near; - m_farPlaneDistance = far; - m_lensZoom = lensZoom; - } - - Mat<4, 4> Camera::GetViewMatrix() const - { - const auto forward = Vector3::ForwardVector(m_viewAngles.x, m_viewAngles.y); - const auto right = Vector3::RightVector(m_viewAngles.x, m_viewAngles.y, m_viewAngles.z); - const auto up = Vector3::UpVector(m_viewAngles.x, m_viewAngles.y, m_viewAngles.z); - - return Mat<>::TranslationMat(-m_origin) * Mat<>::OrientationMat(forward, right, up); - } - - std::expected Camera::WorldToScreen(const Vector3& worldPosition) const - { - const auto posVecAsMatrix = Mat<>::MatColumnFromVector(worldPosition); - - - const auto projectionMatrix = Mat<>::ProjectionMat(m_fieldOfView, m_viewPort.AspectRatio(), - m_nearPlaneDistance, m_farPlaneDistance, m_lensZoom); - - Mat<1, 4> projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix); - - 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, 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 Vector3{projected.At(0, 0), projected.At(0, 1), projected.At(0, 2)}; - } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index dbde66f..77c9508 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -17,6 +17,7 @@ add_executable(unit-tests general/UnitTestLineTrace.cpp general/UnitTestAngles.cpp general/UnitTestViewAngles.cpp + general/UnitTestAngle.cpp engines/UnitTestOpenGL.cpp engines/UnitTestUnityEngine.cpp @@ -24,6 +25,6 @@ add_executable(unit-tests ) -target_link_libraries(unit-tests PRIVATE gtest gtest_main omath glm) +target_link_libraries(unit-tests PRIVATE gtest gtest_main omath) gtest_discover_tests(unit-tests) \ No newline at end of file diff --git a/tests/engines/UnitTestOpenGL.cpp b/tests/engines/UnitTestOpenGL.cpp index 2cb6889..cbe252b 100644 --- a/tests/engines/UnitTestOpenGL.cpp +++ b/tests/engines/UnitTestOpenGL.cpp @@ -7,17 +7,17 @@ #include #include #include -#include +// #include -#include "glm/ext/matrix_clip_space.hpp" -#include "glm/ext/matrix_transform.hpp" +// #include "glm/ext/matrix_clip_space.hpp" +// #include "glm/ext/matrix_transform.hpp" TEST(UnitTestOpenGL, Projection) { - const auto proj_glm = glm::perspective(glm::radians(90.f), 16.f / 9.f, 0.1f, 1000.f); - const auto proj_glm2 = glm::perspectiveLH_NO(glm::radians(90.f), 16.f / 9.f, 0.1f, 1000.f); + /*const auto proj_glm = glm::perspective(glm::radians(90.f), 16.f / 9.f, 0.1f, 1000.f); + // const auto proj_glm2 = glm::perspectiveLH_NO(glm::radians(90.f), 16.f / 9.f, 0.1f, 1000.f); // const auto proj_omath = omath::Mat<4, 4, float, omath::MatStoreType::COLUMN_MAJOR>((const float*)&proj_glm); // EXPECT_EQ(omath::opengl::PerspectiveProjectionMatrix(90, 16.f / 9.f, 0.1f, 1000.f), proj_omath); @@ -34,7 +34,7 @@ TEST(UnitTestOpenGL, Projection) //auto ndc_omath = proj_omath * cords_omath; // ndc_omath /= ndc_omath.At(3, 0); - + */ } TEST(UnitTestOpenGL, Projection2) { diff --git a/tests/engines/UnitTestSourceEngine.cpp b/tests/engines/UnitTestSourceEngine.cpp index 4f6b72b..9c1861f 100644 --- a/tests/engines/UnitTestSourceEngine.cpp +++ b/tests/engines/UnitTestSourceEngine.cpp @@ -1,3 +1,29 @@ // -// Created by Orange on 11/27/2024. +// Created by Orange on 11/23/2024. // +#include +#include + + + + + +TEST(UnitTestSourceEngine, ForwardVector) +{ + const auto forward = omath::source::ForwardVector({}); + + EXPECT_EQ(forward, omath::source::kAbsForward); +} + +TEST(UnitTestSourceEngine, RightVector) +{ + const auto right = omath::source::RightVector({}); + + EXPECT_EQ(right, omath::source::kAbsRight); +} + +TEST(UnitTestSourceEngine, UpVector) +{ + const auto up = omath::source::UpVector({}); + EXPECT_EQ(up, omath::source::kAbsUp); +} \ No newline at end of file diff --git a/tests/general/UnitTestAngle.cpp b/tests/general/UnitTestAngle.cpp new file mode 100644 index 0000000..66bc0d1 --- /dev/null +++ b/tests/general/UnitTestAngle.cpp @@ -0,0 +1,3 @@ +// +// Created by Orange on 11/30/2024. +// diff --git a/tests/general/UnitTestProjection.cpp b/tests/general/UnitTestProjection.cpp index 3424633..fc0ced4 100644 --- a/tests/general/UnitTestProjection.cpp +++ b/tests/general/UnitTestProjection.cpp @@ -4,16 +4,15 @@ #include #include #include -#include +#include #include +#include 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, 1.335f); + auto x = omath::Angle::FromDegrees(90.f); + auto cam = omath::source::CreateCamera({-10, 0, 0}, omath::source::ViewAngles{}, {1920.f, 1080.f}, x, 0.1f, 1000.f); - const auto projected = camera.WorldToScreen({5000, 0, 0}); - - - EXPECT_TRUE(projected.has_value()); - EXPECT_EQ(projected->z, 1.f); + const auto projected = cam.WorldToScreen({10, 0, 0}); + std::print("{} {} {}", projected->x, projected->y, projected->z); } \ No newline at end of file