From f1e6031d2e664348fe4f589bf5719d4db42f07c9 Mon Sep 17 00:00:00 2001 From: Cocoa Date: Mon, 19 Sep 2022 14:32:01 +0800 Subject: [PATCH 1/8] Added Transform, adjusted alignment in math files for better readability on smaller screens --- SHADE_Application/SHADE_Application.vcxproj | 2 +- .../src/Application/SBApplication.cpp | 10 +- SHADE_Engine/SHADE_Engine.vcxproj | 5 +- SHADE_Engine/SHADE_Engine.vcxproj.filters | 7 + SHADE_Engine/src/Math/SHMath.h | 5 +- SHADE_Engine/src/Math/SHMathHelpers.h | 22 +-- SHADE_Engine/src/Math/SHMathHelpers.hpp | 24 +++- SHADE_Engine/src/Math/SHMatrix.cpp | 19 --- SHADE_Engine/src/Math/SHMatrix.h | 129 +++++++++--------- SHADE_Engine/src/Math/SHQuaternion.cpp | 30 +++- SHADE_Engine/src/Math/SHQuaternion.h | 84 ++++++------ SHADE_Engine/src/Math/SHTransform.cpp | 60 ++++++++ SHADE_Engine/src/Math/SHTransform.h | 64 +++++++++ SHADE_Engine/src/Math/Vector/SHVec2.h | 64 ++++----- SHADE_Engine/src/Math/Vector/SHVec3.h | 72 +++++----- SHADE_Engine/src/Math/Vector/SHVec4.h | 74 +++++----- SHADE_Engine/src/SHpch.h | 5 + SHADE_Engine/src/Tools/SHLogger.h | 30 ++-- 18 files changed, 445 insertions(+), 261 deletions(-) create mode 100644 SHADE_Engine/src/Math/SHTransform.cpp create mode 100644 SHADE_Engine/src/Math/SHTransform.h diff --git a/SHADE_Application/SHADE_Application.vcxproj b/SHADE_Application/SHADE_Application.vcxproj index 2616224e..9a566252 100644 --- a/SHADE_Application/SHADE_Application.vcxproj +++ b/SHADE_Application/SHADE_Application.vcxproj @@ -66,7 +66,7 @@ false MultiThreadedDebugDLL true - stdcpplatest + stdcpp20 Windows diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 184b9611..4ff1a0c2 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -6,6 +6,9 @@ #include "Scenes/SBEditorScene.h" #endif // SHEDITOR +#include "Math/SHMath.h" +#include "Tools/SHLogger.h" + #include #include #include @@ -17,11 +20,14 @@ namespace Sandbox ( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, - _In_ LPWSTR lpCmdLine, + _In_ LPWSTR lpCmdLine, _In_ INT nCmdShow ) { - + + SHADE::SHQuaternion aroundZ20{ SHADE::SHVec3::UnitZ, SHADE::SHMath::DegreesToRadians(20.0f) }; + SHLOG_INFO("Angle is {}", SHADE::SHMath::RadiansToDegrees(aroundZ20.GetAngle())) + window.Create(hInstance, hPrevInstance, lpCmdLine, nCmdShow); #ifdef SHEDITOR diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index 1c50ecc0..60a09861 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -64,7 +64,7 @@ false MultiThreadedDebugDLL true - stdcpplatest + stdcpp20 Windows @@ -174,8 +174,10 @@ + + @@ -250,6 +252,7 @@ + diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index 7486fad4..42084b70 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -339,6 +339,9 @@ Math + + Math + Math\Vector @@ -382,6 +385,7 @@ Tools + @@ -555,6 +559,9 @@ Math + + Math + Math\Vector diff --git a/SHADE_Engine/src/Math/SHMath.h b/SHADE_Engine/src/Math/SHMath.h index 9763abe2..55bf73a9 100644 --- a/SHADE_Engine/src/Math/SHMath.h +++ b/SHADE_Engine/src/Math/SHMath.h @@ -6,4 +6,7 @@ #include "Vector/SHVec3.h" #include "Vector/SHVec4.h" -#include "SHMatrix.h" \ No newline at end of file +#include "SHQuaternion.h" +#include "SHMatrix.h" + +#include "SHTransform.h" \ No newline at end of file diff --git a/SHADE_Engine/src/Math/SHMathHelpers.h b/SHADE_Engine/src/Math/SHMathHelpers.h index 9135230e..1580ce47 100644 --- a/SHADE_Engine/src/Math/SHMathHelpers.h +++ b/SHADE_Engine/src/Math/SHMathHelpers.h @@ -56,25 +56,31 @@ namespace SHADE /* Static Function Members */ /*---------------------------------------------------------------------------------*/ - static void Initialise (); + static void Initialise (); template - [[nodiscard]] static constexpr T DegreesToRadians (T angleInDeg); + [[nodiscard]] static T Min (T lhs, T rhs); template - [[nodiscard]] static constexpr T RadiansToDegrees (T angleInRad); + [[nodiscard]] static T Max (T lhs, T rhs); template - [[nodiscard]] static T Lerp (T a, T b, T alpha); + [[nodiscard]] static T DegreesToRadians (T angleInDeg); template - [[nodiscard]] static T ClampedLerp (T a, T b, T alpha, T alphaMin, T alphaMax); + [[nodiscard]] static T RadiansToDegrees (T angleInRad); template - [[nodiscard]] static T Wrap (T value, T min, T max); + [[nodiscard]] static T Lerp (T a, T b, T alpha); + + template + [[nodiscard]] static T ClampedLerp (T a, T b, T alpha, T alphaMin, T alphaMax); + + template + [[nodiscard]] static T Wrap (T value, T min, T max); template - [[nodiscard]] static T GenerateRandomNumber (T lowerBound = 0, T upperBound = 1); + [[nodiscard]] static T GenerateRandomNumber (T lowerBound = 0, T upperBound = 1); /** * @brief Compares two floating-point values for equality within given tolerances. @@ -86,7 +92,7 @@ namespace SHADE * @returns True if the values are equal within the specified tolerances. */ template - [[nodiscard]] static bool CompareFloat (T lhs, T rhs, T absTolerance = EPSILON, T relTolerance = EPSILON); + [[nodiscard]] static bool CompareFloat (T lhs, T rhs, T absTolerance = EPSILON, T relTolerance = EPSILON); private: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/SHMathHelpers.hpp b/SHADE_Engine/src/Math/SHMathHelpers.hpp index f0a1de12..4b49a9a5 100644 --- a/SHADE_Engine/src/Math/SHMathHelpers.hpp +++ b/SHADE_Engine/src/Math/SHMathHelpers.hpp @@ -13,11 +13,8 @@ // Primary Header #include "SHMathHelpers.h" -#include #include -// TODOs (Diren): Include pch? - namespace SHADE { /*-----------------------------------------------------------------------------------*/ @@ -25,13 +22,25 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ template - constexpr T SHMath::DegreesToRadians(T angleInDeg) + T SHMath::Min(T lhs, T rhs) + { + return lhs < rhs ? lhs : rhs; + } + + template + T SHMath::Max(T lhs, T rhs) + { + return lhs > rhs ? lhs : rhs; + } + + template + T SHMath::DegreesToRadians(T angleInDeg) { return angleInDeg * static_cast(PI / 180.0f); } template - constexpr T SHMath::RadiansToDegrees(T angleInRad) + T SHMath::RadiansToDegrees(T angleInRad) { return angleInRad * static_cast(180.0f / PI); } @@ -82,9 +91,10 @@ namespace SHADE template - bool CompareFloat(T lhs, T rhs, T absTolerance, T relTolerance) + bool SHMath::CompareFloat(T lhs, T rhs, T absTolerance, T relTolerance) { - return std::fabs(lhs - rhs) <= std::max(absTolerance, relTolerance * std::max(abs(lhs), abs(rhs))); + const T RTOL = relTolerance * Max(std::fabs(lhs), std::fabs(rhs)); + return std::fabs(lhs - rhs) <= MAX(absTolerance, RTOL); } } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/SHMatrix.cpp b/SHADE_Engine/src/Math/SHMatrix.cpp index 8e8281d0..94c17914 100644 --- a/SHADE_Engine/src/Math/SHMatrix.cpp +++ b/SHADE_Engine/src/Math/SHMatrix.cpp @@ -362,7 +362,6 @@ namespace SHADE SHMatrix result; XMStoreFloat4x4(&result, XMMatrixTranslation(x, y, z)); - result.Transpose(); return result; } @@ -371,7 +370,6 @@ namespace SHADE SHMatrix result; XMStoreFloat4x4(&result, XMMatrixTranslation(pos.x, pos.y, pos.z)); - result.Transpose(); return result; } @@ -382,7 +380,6 @@ namespace SHADE const XMVECTOR A = XMLoadFloat3(&axis); XMStoreFloat4x4(&result, XMMatrixRotationAxis(A, angleInRad)); - result.Transpose(); return result; } @@ -391,7 +388,6 @@ namespace SHADE SHMatrix result; XMStoreFloat4x4(&result, XMMatrixRotationRollPitchYaw(pitch, yaw, roll)); - result.Transpose(); return result; } @@ -400,7 +396,6 @@ namespace SHADE SHMatrix result; XMStoreFloat4x4(&result, XMMatrixRotationRollPitchYaw(eulerAngles.x, eulerAngles.y, eulerAngles.z)); - result.Transpose(); return result; } @@ -411,7 +406,6 @@ namespace SHADE const XMVECTOR Q = XMLoadFloat4(&q); XMStoreFloat4x4(&result, XMMatrixRotationQuaternion(Q)); - result.Transpose(); return result; } @@ -420,7 +414,6 @@ namespace SHADE SHMatrix result; XMStoreFloat4x4(&result, XMMatrixRotationX(angleInRad)); - result.Transpose(); return result; } @@ -429,7 +422,6 @@ namespace SHADE SHMatrix result; XMStoreFloat4x4(&result, XMMatrixRotationY(angleInRad)); - result.Transpose(); return result; } @@ -438,7 +430,6 @@ namespace SHADE SHMatrix result; XMStoreFloat4x4(&result, XMMatrixRotationZ(angleInRad)); - result.Transpose(); return result; } @@ -476,7 +467,6 @@ namespace SHADE XMStoreFloat4x4(&result, XMMatrixLookAtRH(EYE, TGT, UP)); - result.Transpose(); return result; } @@ -490,7 +480,6 @@ namespace SHADE XMStoreFloat4x4(&result, XMMatrixLookAtLH(EYE, TGT, UP)); - result.Transpose(); return result; } @@ -512,7 +501,6 @@ namespace SHADE result._42 = pos.y; result._43 = pos.z; - result.Transpose(); return result; } @@ -534,7 +522,6 @@ namespace SHADE result._42 = pos.x; result._43 = pos.x; - result.Transpose(); return result; } @@ -544,7 +531,6 @@ namespace SHADE XMStoreFloat4x4(&result, XMMatrixPerspectiveFovRH(fov, aspectRatio, nearPlane, farPlane)); - result.Transpose(); return result; } @@ -554,7 +540,6 @@ namespace SHADE XMStoreFloat4x4(&result, XMMatrixPerspectiveFovLH(fov, aspectRatio, nearPlane, farPlane)); - result.Transpose(); return result; } @@ -564,7 +549,6 @@ namespace SHADE XMStoreFloat4x4(&result, XMMatrixPerspectiveRH(width, height, nearPlane, farPlane)); - result.Transpose(); return result; } @@ -574,7 +558,6 @@ namespace SHADE XMStoreFloat4x4(&result, XMMatrixPerspectiveLH(width, height, nearPlane, farPlane)); - result.Transpose(); return result; } @@ -584,7 +567,6 @@ namespace SHADE XMStoreFloat4x4(&result, XMMatrixOrthographicRH(width, height, nearPlane, farPlane)); - result.Transpose(); return result; } @@ -594,7 +576,6 @@ namespace SHADE XMStoreFloat4x4(&result, XMMatrixOrthographicLH(width, height, nearPlane, farPlane)); - result.Transpose(); return result; } diff --git a/SHADE_Engine/src/Math/SHMatrix.h b/SHADE_Engine/src/Math/SHMatrix.h index 2aab05ab..f5d7c173 100644 --- a/SHADE_Engine/src/Math/SHMatrix.h +++ b/SHADE_Engine/src/Math/SHMatrix.h @@ -49,96 +49,99 @@ namespace SHADE /* Constructors & Destructor */ /*---------------------------------------------------------------------------------*/ - SHMatrix (const SHMatrix& rhs) = default; - SHMatrix (SHMatrix&& rhs) = default; - ~SHMatrix () = default; + SHMatrix (const SHMatrix& rhs) = default; + SHMatrix (SHMatrix&& rhs) = default; + ~SHMatrix () = default; - SHMatrix () noexcept; - SHMatrix ( const SHVec4& r0, - const SHVec4& r1, - const SHVec4& r2, - const SHVec4& r3 = SHVec4::UnitW - ) noexcept; - SHMatrix ( - float m00, float m01, float m02, float m03, - float m10, float m11, float m12, float m13, - float m20, float m21, float m22, float m23, - float m30 = 0.0f, float m31 = 0.0f, float m32 = 0.0f, float m33 = 1.0f - ) noexcept; + SHMatrix () noexcept; + SHMatrix + ( + const SHVec4& r0, + const SHVec4& r1, + const SHVec4& r2, + const SHVec4& r3 = SHVec4::UnitW + ) noexcept; + SHMatrix + ( + float m00, float m01, float m02, float m03, + float m10, float m11, float m12, float m13, + float m20, float m21, float m22, float m23, + float m30 = 0.0f, float m31 = 0.0f, float m32 = 0.0f, float m33 = 1.0f + ) noexcept; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ /*---------------------------------------------------------------------------------*/ - SHMatrix& operator= (const SHMatrix& rhs) = default; - SHMatrix& operator= (SHMatrix&& rhs) = default; + SHMatrix& operator= (const SHMatrix& rhs) = default; + SHMatrix& operator= (SHMatrix&& rhs) = default; - SHMatrix& operator+= (const SHMatrix& rhs) noexcept; - SHMatrix& operator-= (const SHMatrix& rhs) noexcept; - SHMatrix& operator*= (const SHMatrix& rhs) noexcept; - SHMatrix& operator*= (float rhs) noexcept; - SHMatrix& operator/= (const SHMatrix& rhs) noexcept; - SHMatrix& operator/= (float rhs) noexcept; + SHMatrix& operator+= (const SHMatrix& rhs) noexcept; + SHMatrix& operator-= (const SHMatrix& rhs) noexcept; + SHMatrix& operator*= (const SHMatrix& rhs) noexcept; + SHMatrix& operator*= (float rhs) noexcept; + SHMatrix& operator/= (const SHMatrix& rhs) noexcept; + SHMatrix& operator/= (float rhs) noexcept; - SHMatrix operator+ (const SHMatrix& rhs) const noexcept; - SHMatrix operator- (const SHMatrix& rhs) const noexcept; - SHMatrix operator- () const noexcept; - SHMatrix operator* (const SHMatrix& rhs) const noexcept; - SHVec3 operator* (const SHVec3& rhs) const noexcept; - SHVec4 operator* (const SHVec4& rhs) const noexcept; - SHMatrix operator* (float rhs) const noexcept; - SHMatrix operator/ (const SHMatrix& rhs) const noexcept; - SHMatrix operator/ (float rhs) const noexcept; + [[nodiscard]] SHMatrix operator+ (const SHMatrix& rhs) const noexcept; + [[nodiscard]] SHMatrix operator- (const SHMatrix& rhs) const noexcept; + [[nodiscard]] SHMatrix operator- () const noexcept; + [[nodiscard]] SHMatrix operator* (const SHMatrix& rhs) const noexcept; + [[nodiscard]] SHVec3 operator* (const SHVec3& rhs) const noexcept; + [[nodiscard]] SHVec4 operator* (const SHVec4& rhs) const noexcept; + [[nodiscard]] SHMatrix operator* (float rhs) const noexcept; + [[nodiscard]] SHMatrix operator/ (const SHMatrix& rhs) const noexcept; + [[nodiscard]] SHMatrix operator/ (float rhs) const noexcept; - bool operator== (const SHMatrix& rhs) const noexcept; - bool operator!= (const SHMatrix& rhs) const noexcept; + [[nodiscard]] bool operator== (const SHMatrix& rhs) const noexcept; + [[nodiscard]] bool operator!= (const SHMatrix& rhs) const noexcept; /*---------------------------------------------------------------------------------*/ /* Function Members */ /*---------------------------------------------------------------------------------*/ - void Transpose () noexcept; - void Invert () noexcept; + void Transpose () noexcept; + void Invert () noexcept; - [[nodiscard]] float Determinant () const noexcept; - [[nodiscard]] std::string ToString () const noexcept; + [[nodiscard]] float Determinant () const noexcept; + [[nodiscard]] std::string ToString () const noexcept; /*---------------------------------------------------------------------------------*/ /* Static Function Members */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] static SHMatrix Transpose (const SHMatrix& matrix) noexcept; - [[nodiscard]] static SHMatrix Inverse (const SHMatrix& matrix) noexcept; + [[nodiscard]] static SHMatrix Transpose (const SHMatrix& matrix) noexcept; + [[nodiscard]] static SHMatrix Inverse (const SHMatrix& matrix) noexcept; - [[nodiscard]] static SHMatrix Translate (float x, float y, float z) noexcept; - [[nodiscard]] static SHMatrix Translate (const SHVec3& pos) noexcept; + [[nodiscard]] static SHMatrix Translate (float x, float y, float z) noexcept; + [[nodiscard]] static SHMatrix Translate (const SHVec3& pos) noexcept; - [[nodiscard]] static SHMatrix Rotate (const SHVec3& axis, float angleInRad) noexcept; - [[nodiscard]] static SHMatrix Rotate (float yaw, float pitch, float roll) noexcept; - [[nodiscard]] static SHMatrix Rotate (const SHVec3& eulerAngles) noexcept; - [[nodiscard]] static SHMatrix Rotate (const SHQuaternion& q) noexcept; - [[nodiscard]] static SHMatrix RotateX (float angleInRad) noexcept; - [[nodiscard]] static SHMatrix RotateY (float angleInRad) noexcept; - [[nodiscard]] static SHMatrix RotateZ (float angleInRad) noexcept; + [[nodiscard]] static SHMatrix Rotate (const SHVec3& axis, float angleInRad) noexcept; + [[nodiscard]] static SHMatrix Rotate (float yaw, float pitch, float roll) noexcept; + [[nodiscard]] static SHMatrix Rotate (const SHVec3& eulerAngles) noexcept; + [[nodiscard]] static SHMatrix Rotate (const SHQuaternion& q) noexcept; + [[nodiscard]] static SHMatrix RotateX (float angleInRad) noexcept; + [[nodiscard]] static SHMatrix RotateY (float angleInRad) noexcept; + [[nodiscard]] static SHMatrix RotateZ (float angleInRad) noexcept; - [[nodiscard]] static SHMatrix Scale (float uniformScaleFactor) noexcept; - [[nodiscard]] static SHMatrix Scale (float x, float y, float z) noexcept; - [[nodiscard]] static SHMatrix Scale (const SHVec3& scale) noexcept; + [[nodiscard]] static SHMatrix Scale (float uniformScaleFactor) noexcept; + [[nodiscard]] static SHMatrix Scale (float x, float y, float z) noexcept; + [[nodiscard]] static SHMatrix Scale (const SHVec3& scale) noexcept; - [[nodiscard]] static SHMatrix LookAtRH (const SHVec3& eye, const SHVec3& target, const SHVec3& up) noexcept; - [[nodiscard]] static SHMatrix LookAtLH (const SHVec3& eye, const SHVec3& target, const SHVec3& up) noexcept; - [[nodiscard]] static SHMatrix CamToWorldRH (const SHVec3& pos, const SHVec3& forward, const SHVec3& up) noexcept; - [[nodiscard]] static SHMatrix CamToWorldLH (const SHVec3& pos, const SHVec3& forward, const SHVec3& up) noexcept; - [[nodiscard]] static SHMatrix PerspectiveFovRH (float fov, float aspectRatio, float nearPlane, float farPlane) noexcept; - [[nodiscard]] static SHMatrix PerspectiveFovLH (float fov, float aspectRatio, float nearPlane, float farPlane) noexcept; - [[nodiscard]] static SHMatrix PerspectiveRH (float width, float height, float nearPlane, float farPlane) noexcept; - [[nodiscard]] static SHMatrix PerspectiveLH (float width, float height, float nearPlane, float farPlane) noexcept; - [[nodiscard]] static SHMatrix OrthographicRH (float width, float height, float nearPlane, float farPlane) noexcept; - [[nodiscard]] static SHMatrix OrthographicLH (float width, float height, float nearPlane, float farPlane) noexcept; + [[nodiscard]] static SHMatrix LookAtRH (const SHVec3& eye, const SHVec3& target, const SHVec3& up) noexcept; + [[nodiscard]] static SHMatrix LookAtLH (const SHVec3& eye, const SHVec3& target, const SHVec3& up) noexcept; + [[nodiscard]] static SHMatrix CamToWorldRH (const SHVec3& pos, const SHVec3& forward, const SHVec3& up) noexcept; + [[nodiscard]] static SHMatrix CamToWorldLH (const SHVec3& pos, const SHVec3& forward, const SHVec3& up) noexcept; + [[nodiscard]] static SHMatrix PerspectiveFovRH (float fov, float aspectRatio, float nearPlane, float farPlane) noexcept; + [[nodiscard]] static SHMatrix PerspectiveFovLH (float fov, float aspectRatio, float nearPlane, float farPlane) noexcept; + [[nodiscard]] static SHMatrix PerspectiveRH (float width, float height, float nearPlane, float farPlane) noexcept; + [[nodiscard]] static SHMatrix PerspectiveLH (float width, float height, float nearPlane, float farPlane) noexcept; + [[nodiscard]] static SHMatrix OrthographicRH (float width, float height, float nearPlane, float farPlane) noexcept; + [[nodiscard]] static SHMatrix OrthographicLH (float width, float height, float nearPlane, float farPlane) noexcept; // TODO(Diren): Billboard, Shadow, Projection & Reflection }; - SHMatrix operator*(float lhs, const SHMatrix& rhs) noexcept; + SHMatrix operator*(float lhs, const SHMatrix& rhs) noexcept; } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/SHQuaternion.cpp b/SHADE_Engine/src/Math/SHQuaternion.cpp index 208f131d..36921f3f 100644 --- a/SHADE_Engine/src/Math/SHQuaternion.cpp +++ b/SHADE_Engine/src/Math/SHQuaternion.cpp @@ -180,6 +180,34 @@ namespace SHADE return rhs * lhs; } + /*-----------------------------------------------------------------------------------*/ + /* Getter Function Definitions */ + /*-----------------------------------------------------------------------------------*/ + + float SHQuaternion::GetAngle() const noexcept + { + XMVECTOR axis; + float angle; + + const XMVECTOR Q = XMLoadFloat4(this); + XMQuaternionToAxisAngle(&axis, &angle, Q); + return angle; + } + + SHVec4 SHQuaternion::GetAxisAngle() const noexcept + { + XMVECTOR axis; + float angle; + + const XMVECTOR Q = XMLoadFloat4(this); + XMQuaternionToAxisAngle(&axis, &angle, Q); + + + return SHVec4{XMVectorGetX(axis), XMVectorGetY(axis), XMVectorGetZ(axis), angle}; + } + + + /*-----------------------------------------------------------------------------------*/ /* Function Member Definitions */ /*-----------------------------------------------------------------------------------*/ @@ -230,7 +258,7 @@ namespace SHADE { std::stringstream ss; ss << std::fixed << std::setprecision(3); - ss << "<" << x << ", " << y << ", " << z << ", " << w <<">"; + ss << "<" << w << ", " << x << ", " << y << ", " << z <<">"; return ss.str(); } diff --git a/SHADE_Engine/src/Math/SHQuaternion.h b/SHADE_Engine/src/Math/SHQuaternion.h index 27088284..820392fa 100644 --- a/SHADE_Engine/src/Math/SHQuaternion.h +++ b/SHADE_Engine/src/Math/SHQuaternion.h @@ -20,6 +20,7 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ class SHVec3; + class SHVec4; class SHMatrix; /*-----------------------------------------------------------------------------------*/ @@ -39,68 +40,75 @@ namespace SHADE /* Constructors & Destructor */ /*---------------------------------------------------------------------------------*/ - SHQuaternion (const SHQuaternion& rhs) = default; - SHQuaternion (SHQuaternion&& rhs) = default; + SHQuaternion (const SHQuaternion& rhs) = default; + SHQuaternion (SHQuaternion&& rhs) = default; - SHQuaternion () noexcept; - SHQuaternion (float x, float y, float z, float w) noexcept; - SHQuaternion (float yaw, float pitch, float roll) noexcept; - SHQuaternion (const SHVec3& eulerAngles) noexcept; - SHQuaternion (const SHVec3& axis, float angleInRad) noexcept; - SHQuaternion (const SHMatrix& rotationMatrix) noexcept; + SHQuaternion () noexcept; + SHQuaternion (float x, float y, float z, float w) noexcept; + SHQuaternion (float yaw, float pitch, float roll) noexcept; + SHQuaternion (const SHVec3& eulerAngles) noexcept; + SHQuaternion (const SHVec3& axis, float angleInRad) noexcept; + SHQuaternion (const SHMatrix& rotationMatrix) noexcept; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] SHQuaternion& operator= (const SHQuaternion& rhs) = default; - [[nodiscard]] SHQuaternion& operator= (SHQuaternion&& rhs) = default; + SHQuaternion& operator= (const SHQuaternion& rhs) = default; + SHQuaternion& operator= (SHQuaternion&& rhs) = default; + + SHQuaternion& operator+= (const SHQuaternion& rhs) noexcept; + SHQuaternion& operator-= (const SHQuaternion& rhs) noexcept; + SHQuaternion& operator*= (const SHQuaternion& rhs) noexcept; + SHQuaternion& operator*= (float rhs) noexcept; + SHQuaternion& operator/= (const SHQuaternion& rhs) noexcept; - [[nodiscard]] SHQuaternion& operator+= (const SHQuaternion& rhs) noexcept; - [[nodiscard]] SHQuaternion& operator-= (const SHQuaternion& rhs) noexcept; - [[nodiscard]] SHQuaternion& operator*= (const SHQuaternion& rhs) noexcept; - [[nodiscard]] SHQuaternion& operator*= (float rhs) noexcept; - [[nodiscard]] SHQuaternion& operator/= (const SHQuaternion& rhs) noexcept; + [[nodiscard]] SHQuaternion operator+ (const SHQuaternion& rhs) const noexcept; + [[nodiscard]] SHQuaternion operator- (const SHQuaternion& rhs) const noexcept; + [[nodiscard]] SHQuaternion operator- () const noexcept; + [[nodiscard]] SHQuaternion operator* (const SHQuaternion& rhs) const noexcept; + [[nodiscard]] SHQuaternion operator* (float rhs) const noexcept; + [[nodiscard]] SHQuaternion operator/ (const SHQuaternion& rhs) const noexcept; - [[nodiscard]] SHQuaternion operator+ (const SHQuaternion& rhs) const noexcept; - [[nodiscard]] SHQuaternion operator- (const SHQuaternion& rhs) const noexcept; - [[nodiscard]] SHQuaternion operator- () const noexcept; - [[nodiscard]] SHQuaternion operator* (const SHQuaternion& rhs) const noexcept; - [[nodiscard]] SHQuaternion operator* (float rhs) const noexcept; - [[nodiscard]] SHQuaternion operator/ (const SHQuaternion& rhs) const noexcept; + [[nodiscard]] bool operator== (const SHQuaternion& rhs) const noexcept; + [[nodiscard]] bool operator!= (const SHQuaternion& rhs) const noexcept; - [[nodiscard]] bool operator== (const SHQuaternion& rhs) const noexcept; - [[nodiscard]] bool operator!= (const SHQuaternion& rhs) const noexcept; + /*---------------------------------------------------------------------------------*/ + /* Getter Functions */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] float GetAngle () const noexcept; + [[nodiscard]] SHVec4 GetAxisAngle () const noexcept; /*---------------------------------------------------------------------------------*/ /* Function Members */ /*---------------------------------------------------------------------------------*/ - void Invert () noexcept; + void Invert () noexcept; - [[nodiscard]] float Length () const noexcept; - [[nodiscard]] float LengthSquared () const noexcept; - [[nodiscard]] float Dot (const SHQuaternion& rhs) const noexcept; - [[nodiscard]] SHQuaternion RotateTowards (const SHQuaternion& target, float maxAngleInRad) const noexcept; + [[nodiscard]] float Length () const noexcept; + [[nodiscard]] float LengthSquared () const noexcept; + [[nodiscard]] float Dot (const SHQuaternion& rhs) const noexcept; + [[nodiscard]] SHQuaternion RotateTowards (const SHQuaternion& target, float maxAngleInRad) const noexcept; - [[nodiscard]] SHVec3 ToEuler () const noexcept; - [[nodiscard]] std::string ToString () const noexcept; + [[nodiscard]] SHVec3 ToEuler () const noexcept; + [[nodiscard]] std::string ToString () const noexcept; /*---------------------------------------------------------------------------------*/ /* Static Function Members */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] static SHQuaternion Normalise (const SHQuaternion& q) noexcept; - [[nodiscard]] static SHQuaternion Conjugate (const SHQuaternion& q) noexcept; - [[nodiscard]] static SHQuaternion Inverse (const SHQuaternion& q) noexcept; - [[nodiscard]] static float Angle (const SHQuaternion& q1, const SHQuaternion& q2) noexcept; + [[nodiscard]] static SHQuaternion Normalise (const SHQuaternion& q) noexcept; + [[nodiscard]] static SHQuaternion Conjugate (const SHQuaternion& q) noexcept; + [[nodiscard]] static SHQuaternion Inverse (const SHQuaternion& q) noexcept; + [[nodiscard]] static float Angle (const SHQuaternion& q1, const SHQuaternion& q2) noexcept; - [[nodiscard]] static SHQuaternion Lerp (const SHQuaternion& q1, const SHQuaternion& q2, float t) noexcept; - [[nodiscard]] static SHQuaternion Slerp (const SHQuaternion& q1, const SHQuaternion& q2, float t) noexcept; + [[nodiscard]] static SHQuaternion Lerp (const SHQuaternion& q1, const SHQuaternion& q2, float t) noexcept; + [[nodiscard]] static SHQuaternion Slerp (const SHQuaternion& q1, const SHQuaternion& q2, float t) noexcept; - [[nodiscard]] static SHQuaternion Rotate (const SHVec3& from, const SHVec3& to) noexcept; + [[nodiscard]] static SHQuaternion Rotate (const SHVec3& from, const SHVec3& to) noexcept; }; - SHQuaternion operator*(float lhs, const SHQuaternion& rhs) noexcept; + SHQuaternion operator*(float lhs, const SHQuaternion& rhs) noexcept; } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/SHTransform.cpp b/SHADE_Engine/src/Math/SHTransform.cpp new file mode 100644 index 00000000..757ac5b6 --- /dev/null +++ b/SHADE_Engine/src/Math/SHTransform.cpp @@ -0,0 +1,60 @@ +/**************************************************************************************** + * \file SHTransform.cpp + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for a Transform. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#include + +// Primary Header +#include "SHTransform.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Stai Definitions */ + /*-----------------------------------------------------------------------------------*/ + + + + /*-----------------------------------------------------------------------------------*/ + /* Constructors & Destructor Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHTransform::SHTransform() noexcept + : position { SHVec3::Zero } + , rotation { SHVec3::Zero } + , scale { SHVec3::One } + {} + + /*-----------------------------------------------------------------------------------*/ + /* Getter Function Definitions */ + /*-----------------------------------------------------------------------------------*/ + + const SHMatrix& SHTransform::GetTRS() const + { + return trs; + } + + + /*-----------------------------------------------------------------------------------*/ + /* Public Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + const SHMatrix& SHTransform::ComputeTRS() + { + + const SHMatrix T = SHMatrix::Translate(position); + const SHMatrix R = SHMatrix::Rotate(rotation); + const SHMatrix S = SHMatrix::Scale(scale); + + trs = S * R * T; + + return trs; + } + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/SHTransform.h b/SHADE_Engine/src/Math/SHTransform.h new file mode 100644 index 00000000..a6d9a1b2 --- /dev/null +++ b/SHADE_Engine/src/Math/SHTransform.h @@ -0,0 +1,64 @@ +/**************************************************************************************** + * \file SHTransform.h + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Interface for a Transform. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#pragma once + +#include "SHMath.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + + struct SHTransform + { + public: + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + + static const SHTransform Identity; + + SHVec3 position; + SHVec3 rotation; + SHVec3 scale; + + /*---------------------------------------------------------------------------------*/ + /* Constructors & Destructor */ + /*---------------------------------------------------------------------------------*/ + + SHTransform (const SHTransform&) = default; + SHTransform (SHTransform&&) = default; + + SHTransform& operator= (const SHTransform&) = default; + SHTransform& operator= (SHTransform&&) = default; + + ~SHTransform () = default; + + SHTransform () noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Getter Functions */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] const SHMatrix& GetTRS() const; + + /*---------------------------------------------------------------------------------*/ + /* Function Members */ + /*---------------------------------------------------------------------------------*/ + + const SHMatrix& ComputeTRS(); + + private: + SHMatrix trs; + }; + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Vector/SHVec2.h b/SHADE_Engine/src/Math/Vector/SHVec2.h index a64d4bb0..0b272b40 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec2.h +++ b/SHADE_Engine/src/Math/Vector/SHVec2.h @@ -45,53 +45,53 @@ namespace SHADE /* Constructors & Destructor */ /*---------------------------------------------------------------------------------*/ - SHVec2 (const SHVec2& rhs) = default; - SHVec2 (SHVec2&& rhs) = default; - ~SHVec2 () = default; + SHVec2 (const SHVec2& rhs) = default; + SHVec2 (SHVec2&& rhs) = default; + ~SHVec2 () = default; - SHVec2 () noexcept; - SHVec2 (float x, float y) noexcept; + SHVec2 () noexcept; + SHVec2 (float x, float y) noexcept; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] SHVec2& operator= (const SHVec2& rhs) = default; - [[nodiscard]] SHVec2& operator= (SHVec2&& rhs) = default; + SHVec2& operator= (const SHVec2& rhs) = default; + SHVec2& operator= (SHVec2&& rhs) = default; - [[nodiscard]] SHVec2& operator+= (const SHVec2& rhs) noexcept; - [[nodiscard]] SHVec2& operator-= (const SHVec2& rhs) noexcept; - [[nodiscard]] SHVec2& operator*= (const SHVec2& rhs) noexcept; - [[nodiscard]] SHVec2& operator*= (float rhs) noexcept; - [[nodiscard]] SHVec2& operator/= (const SHVec2& rhs) noexcept; - [[nodiscard]] SHVec2& operator/= (float rhs) noexcept; + SHVec2& operator+= (const SHVec2& rhs) noexcept; + SHVec2& operator-= (const SHVec2& rhs) noexcept; + SHVec2& operator*= (const SHVec2& rhs) noexcept; + SHVec2& operator*= (float rhs) noexcept; + SHVec2& operator/= (const SHVec2& rhs) noexcept; + SHVec2& operator/= (float rhs) noexcept; - [[nodiscard]] SHVec2 operator+ (const SHVec2& rhs) const noexcept; - [[nodiscard]] SHVec2 operator- (const SHVec2& rhs) const noexcept; - [[nodiscard]] SHVec2 operator- () const noexcept; - [[nodiscard]] SHVec2 operator* (const SHVec2& rhs) const noexcept; - [[nodiscard]] SHVec2 operator* (float rhs) const noexcept; - [[nodiscard]] SHVec2 operator/ (const SHVec2& rhs) const noexcept; - [[nodiscard]] SHVec2 operator/ (float rhs) const noexcept; + [[nodiscard]] SHVec2 operator+ (const SHVec2& rhs) const noexcept; + [[nodiscard]] SHVec2 operator- (const SHVec2& rhs) const noexcept; + [[nodiscard]] SHVec2 operator- () const noexcept; + [[nodiscard]] SHVec2 operator* (const SHVec2& rhs) const noexcept; + [[nodiscard]] SHVec2 operator* (float rhs) const noexcept; + [[nodiscard]] SHVec2 operator/ (const SHVec2& rhs) const noexcept; + [[nodiscard]] SHVec2 operator/ (float rhs) const noexcept; - [[nodiscard]] bool operator== (const SHVec2& rhs) const noexcept; - [[nodiscard]] bool operator!= (const SHVec2& rhs) const noexcept; + [[nodiscard]] bool operator== (const SHVec2& rhs) const noexcept; + [[nodiscard]] bool operator!= (const SHVec2& rhs) const noexcept; - [[nodiscard]] float operator[] (int index); - [[nodiscard]] float operator[] (size_t index); - [[nodiscard]] float operator[] (int index) const; - [[nodiscard]] float operator[] (size_t index) const; + [[nodiscard]] float operator[] (int index); + [[nodiscard]] float operator[] (size_t index); + [[nodiscard]] float operator[] (int index) const; + [[nodiscard]] float operator[] (size_t index) const; /*---------------------------------------------------------------------------------*/ /* Function Members */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] float Length () const noexcept; - [[nodiscard]] float LengthSquared () const noexcept; - [[nodiscard]] std::string ToString () const noexcept; + [[nodiscard]] float Length () const noexcept; + [[nodiscard]] float LengthSquared () const noexcept; + [[nodiscard]] std::string ToString () const noexcept; - [[nodiscard]] float Dot (const SHVec2& rhs) const noexcept; - [[nodiscard]] SHVec2 Cross (const SHVec2& rhs) const noexcept; + [[nodiscard]] float Dot (const SHVec2& rhs) const noexcept; + [[nodiscard]] SHVec2 Cross (const SHVec2& rhs) const noexcept; /*---------------------------------------------------------------------------------*/ /* Static Function Members */ /*---------------------------------------------------------------------------------*/ @@ -117,6 +117,6 @@ namespace SHADE [[nodiscard]] static float Cross (const SHVec2& lhs, const SHVec2& rhs) noexcept; }; - SHVec2 operator* (float lhs, const SHVec2& rhs) noexcept; + SHVec2 operator* (float lhs, const SHVec2& rhs) noexcept; } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Vector/SHVec3.h b/SHADE_Engine/src/Math/Vector/SHVec3.h index e172e824..059c5708 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec3.h +++ b/SHADE_Engine/src/Math/Vector/SHVec3.h @@ -50,62 +50,62 @@ namespace SHADE /* Constructors & Destructor */ /*---------------------------------------------------------------------------------*/ - SHVec3 (const SHVec3& rhs) = default; - SHVec3 (SHVec3&& rhs) = default; - ~SHVec3 () = default; + SHVec3 (const SHVec3& rhs) = default; + SHVec3 (SHVec3&& rhs) = default; + ~SHVec3 () = default; - SHVec3 () noexcept; - SHVec3 (float x, float y, float z) noexcept; + SHVec3 () noexcept; + SHVec3 (float x, float y, float z) noexcept; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] SHVec3& operator= (const SHVec3& rhs) = default; - [[nodiscard]] SHVec3& operator= (SHVec3&& rhs) = default; + SHVec3& operator= (const SHVec3& rhs) = default; + SHVec3& operator= (SHVec3&& rhs) = default; - [[nodiscard]] SHVec3& operator+= (const SHVec3& rhs) noexcept; - [[nodiscard]] SHVec3& operator-= (const SHVec3& rhs) noexcept; - [[nodiscard]] SHVec3& operator*= (const SHVec3& rhs) noexcept; - [[nodiscard]] SHVec3& operator*= (float rhs) noexcept; - [[nodiscard]] SHVec3& operator/= (const SHVec3& rhs) noexcept; - [[nodiscard]] SHVec3& operator/= (float rhs) noexcept; + SHVec3& operator+= (const SHVec3& rhs) noexcept; + SHVec3& operator-= (const SHVec3& rhs) noexcept; + SHVec3& operator*= (const SHVec3& rhs) noexcept; + SHVec3& operator*= (float rhs) noexcept; + SHVec3& operator/= (const SHVec3& rhs) noexcept; + SHVec3& operator/= (float rhs) noexcept; - [[nodiscard]] SHVec3 operator+ (const SHVec3& rhs) const noexcept; - [[nodiscard]] SHVec3 operator- (const SHVec3& rhs) const noexcept; - [[nodiscard]] SHVec3 operator- () const noexcept; - [[nodiscard]] SHVec3 operator* (const SHVec3& rhs) const noexcept; - [[nodiscard]] SHVec3 operator* (float rhs) const noexcept; - [[nodiscard]] SHVec3 operator/ (const SHVec3& rhs) const noexcept; - [[nodiscard]] SHVec3 operator/ (float rhs) const noexcept; + [[nodiscard]] SHVec3 operator+ (const SHVec3& rhs) const noexcept; + [[nodiscard]] SHVec3 operator- (const SHVec3& rhs) const noexcept; + [[nodiscard]] SHVec3 operator- () const noexcept; + [[nodiscard]] SHVec3 operator* (const SHVec3& rhs) const noexcept; + [[nodiscard]] SHVec3 operator* (float rhs) const noexcept; + [[nodiscard]] SHVec3 operator/ (const SHVec3& rhs) const noexcept; + [[nodiscard]] SHVec3 operator/ (float rhs) const noexcept; - [[nodiscard]] bool operator== (const SHVec3& rhs) const noexcept; - [[nodiscard]] bool operator!= (const SHVec3& rhs) const noexcept; + [[nodiscard]] bool operator== (const SHVec3& rhs) const noexcept; + [[nodiscard]] bool operator!= (const SHVec3& rhs) const noexcept; - [[nodiscard]] float operator[] (int index); - [[nodiscard]] float operator[] (size_t index); - [[nodiscard]] float operator[] (int index) const; - [[nodiscard]] float operator[] (size_t index) const; + [[nodiscard]] float operator[] (int index); + [[nodiscard]] float operator[] (size_t index); + [[nodiscard]] float operator[] (int index) const; + [[nodiscard]] float operator[] (size_t index) const; /*---------------------------------------------------------------------------------*/ /* Function Members */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] float Length () const noexcept; - [[nodiscard]] float LengthSquared () const noexcept; - [[nodiscard]] std::string ToString () const noexcept; + [[nodiscard]] float Length () const noexcept; + [[nodiscard]] float LengthSquared () const noexcept; + [[nodiscard]] std::string ToString () const noexcept; - [[nodiscard]] float Dot (const SHVec3& rhs) const noexcept; - [[nodiscard]] SHVec3 Cross (const SHVec3& rhs) const noexcept; + [[nodiscard]] float Dot (const SHVec3& rhs) const noexcept; + [[nodiscard]] SHVec3 Cross (const SHVec3& rhs) const noexcept; /*---------------------------------------------------------------------------------*/ /* Static Function Members */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] static SHVec3 Normalise (const SHVec3& v) noexcept; - [[nodiscard]] static SHVec3 Abs (const SHVec3& v) noexcept; - [[nodiscard]] static SHVec3 Min (const std::initializer_list& vs) noexcept; - [[nodiscard]] static SHVec3 Max (const std::initializer_list& vs) noexcept; + [[nodiscard]] static SHVec3 Normalise (const SHVec3& v) noexcept; + [[nodiscard]] static SHVec3 Abs (const SHVec3& v) noexcept; + [[nodiscard]] static SHVec3 Min (const std::initializer_list& vs) noexcept; + [[nodiscard]] static SHVec3 Max (const std::initializer_list& vs) noexcept; [[nodiscard]] static SHVec3 Clamp (const SHVec3& v, const SHVec3& vMin, const SHVec3& vMax) noexcept; [[nodiscard]] static SHVec3 Lerp (const SHVec3& a, const SHVec3& b, float t) noexcept; [[nodiscard]] static SHVec3 ClampedLerp (const SHVec3& a, const SHVec3& b, float t, float tMin = 0.0f, float tMax = 1.0f) noexcept; @@ -124,6 +124,6 @@ namespace SHADE [[nodiscard]] static SHVec3 Transform (const SHVec3& v, const SHMatrix& transformMtx) noexcept; }; - SHVec3 operator* (float lhs, const SHVec3& rhs) noexcept; + SHVec3 operator* (float lhs, const SHVec3& rhs) noexcept; } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Vector/SHVec4.h b/SHADE_Engine/src/Math/Vector/SHVec4.h index c4caf2c8..36793fe0 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec4.h +++ b/SHADE_Engine/src/Math/Vector/SHVec4.h @@ -45,57 +45,57 @@ namespace SHADE /* Constructors & Destructor */ /*---------------------------------------------------------------------------------*/ - SHVec4 (const SHVec4& rhs) = default; - SHVec4 (SHVec4&& rhs) = default; - ~SHVec4 () = default; + SHVec4 (const SHVec4& rhs) = default; + SHVec4 (SHVec4&& rhs) = default; + ~SHVec4 () = default; - SHVec4 () noexcept; - SHVec4 (float x, float y, float z, float w) noexcept; + SHVec4 () noexcept; + SHVec4 (float x, float y, float z, float w) noexcept; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] SHVec4& operator= (const SHVec4& rhs) = default; - [[nodiscard]] SHVec4& operator= (SHVec4&& rhs) = default; - - [[nodiscard]] SHVec4& operator+= (const SHVec4& rhs) noexcept; - [[nodiscard]] SHVec4& operator-= (const SHVec4& rhs) noexcept; - [[nodiscard]] SHVec4& operator*= (const SHVec4& rhs) noexcept; - [[nodiscard]] SHVec4& operator*= (float rhs) noexcept; - [[nodiscard]] SHVec4& operator/= (const SHVec4& rhs) noexcept; - [[nodiscard]] SHVec4& operator/= (float rhs) noexcept; + SHVec4& operator= (const SHVec4& rhs) = default; + SHVec4& operator= (SHVec4&& rhs) = default; - [[nodiscard]] SHVec4 operator+ (const SHVec4& rhs) const noexcept; - [[nodiscard]] SHVec4 operator- (const SHVec4& rhs) const noexcept; - [[nodiscard]] SHVec4 operator- () const noexcept; - [[nodiscard]] SHVec4 operator* (const SHVec4& rhs) const noexcept; - [[nodiscard]] SHVec4 operator* (float rhs) const noexcept; - [[nodiscard]] SHVec4 operator/ (const SHVec4& rhs) const noexcept; - [[nodiscard]] SHVec4 operator/ (float rhs) const noexcept; + SHVec4& operator+= (const SHVec4& rhs) noexcept; + SHVec4& operator-= (const SHVec4& rhs) noexcept; + SHVec4& operator*= (const SHVec4& rhs) noexcept; + SHVec4& operator*= (float rhs) noexcept; + SHVec4& operator/= (const SHVec4& rhs) noexcept; + SHVec4& operator/= (float rhs) noexcept; - [[nodiscard]] bool operator== (const SHVec4& rhs) const noexcept; - [[nodiscard]] bool operator!= (const SHVec4& rhs) const noexcept; + [[nodiscard]] SHVec4 operator+ (const SHVec4& rhs) const noexcept; + [[nodiscard]] SHVec4 operator- (const SHVec4& rhs) const noexcept; + [[nodiscard]] SHVec4 operator- () const noexcept; + [[nodiscard]] SHVec4 operator* (const SHVec4& rhs) const noexcept; + [[nodiscard]] SHVec4 operator* (float rhs) const noexcept; + [[nodiscard]] SHVec4 operator/ (const SHVec4& rhs) const noexcept; + [[nodiscard]] SHVec4 operator/ (float rhs) const noexcept; - [[nodiscard]] float operator[] (int index); - [[nodiscard]] float operator[] (size_t index); - [[nodiscard]] float operator[] (int index) const; - [[nodiscard]] float operator[] (size_t index) const; + [[nodiscard]] bool operator== (const SHVec4& rhs) const noexcept; + [[nodiscard]] bool operator!= (const SHVec4& rhs) const noexcept; + + [[nodiscard]] float operator[] (int index); + [[nodiscard]] float operator[] (size_t index); + [[nodiscard]] float operator[] (int index) const; + [[nodiscard]] float operator[] (size_t index) const; /*---------------------------------------------------------------------------------*/ /* Function Members */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] float Length () const noexcept; - [[nodiscard]] float Length3D () const noexcept; - [[nodiscard]] float LengthSquared () const noexcept; - [[nodiscard]] float LengthSquared3D () const noexcept; - [[nodiscard]] std::string ToString () const noexcept; + [[nodiscard]] float Length () const noexcept; + [[nodiscard]] float Length3D () const noexcept; + [[nodiscard]] float LengthSquared () const noexcept; + [[nodiscard]] float LengthSquared3D () const noexcept; + [[nodiscard]] std::string ToString () const noexcept; - [[nodiscard]] float Dot (const SHVec4& rhs) const noexcept; - [[nodiscard]] float Dot3D (const SHVec4& rhs) const noexcept; - [[nodiscard]] SHVec4 Cross3D (const SHVec4& rhs) const noexcept; - [[nodiscard]] SHVec4 Cross (const SHVec4& v1, const SHVec4& v2) const noexcept; + [[nodiscard]] float Dot (const SHVec4& rhs) const noexcept; + [[nodiscard]] float Dot3D (const SHVec4& rhs) const noexcept; + [[nodiscard]] SHVec4 Cross3D (const SHVec4& rhs) const noexcept; + [[nodiscard]] SHVec4 Cross (const SHVec4& v1, const SHVec4& v2) const noexcept; /*---------------------------------------------------------------------------------*/ /* Static Function Members */ @@ -128,6 +128,6 @@ namespace SHADE }; - SHVec4 operator* (float lhs, const SHVec4& rhs) noexcept; + SHVec4 operator* (float lhs, const SHVec4& rhs) noexcept; } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/SHpch.h b/SHADE_Engine/src/SHpch.h index 0342eedb..1adf7af3 100644 --- a/SHADE_Engine/src/SHpch.h +++ b/SHADE_Engine/src/SHpch.h @@ -9,7 +9,10 @@ #pragma once + + #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers +#define NOMINMAX // Windows Header Files #include // C RunTime Header Files @@ -30,3 +33,5 @@ #include #include #include + +#include "Tools/SHLogger.h" diff --git a/SHADE_Engine/src/Tools/SHLogger.h b/SHADE_Engine/src/Tools/SHLogger.h index ac5f9308..2ba4abaf 100644 --- a/SHADE_Engine/src/Tools/SHLogger.h +++ b/SHADE_Engine/src/Tools/SHLogger.h @@ -70,33 +70,33 @@ namespace SHADE /* Getter Functions */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] static const std::string& GetTrivialPattern () noexcept { return trivialPattern; } - [[nodiscard]] static const std::string& GetVerbosePattern () noexcept { return verbosePattern; } + [[nodiscard]] static const std::string& GetTrivialPattern () noexcept { return trivialPattern; } + [[nodiscard]] static const std::string& GetVerbosePattern () noexcept { return verbosePattern; } /*---------------------------------------------------------------------------------*/ /* Setter Functions */ /*---------------------------------------------------------------------------------*/ - static void SetTrivialPattern (const std::string& pattern) noexcept { trivialPattern = pattern; } - static void SetVerbosePattern (const std::string& pattern) noexcept { verbosePattern = pattern; } + static void SetTrivialPattern (const std::string& pattern) noexcept { trivialPattern = pattern; } + static void SetVerbosePattern (const std::string& pattern) noexcept { verbosePattern = pattern; } - static void SetConfig (const Config& config) noexcept; + static void SetConfig (const Config& config) noexcept; - static void SetShowTime (bool showTime) noexcept; - static void SetShowDate (bool showDate) noexcept; - static void SetShowFunctionFileName (bool showFunctionFileName) noexcept; - static void SetShowFunctionLineNumber (bool showFunctionLineNumber) noexcept; + static void SetShowTime (bool showTime) noexcept; + static void SetShowDate (bool showDate) noexcept; + static void SetShowFunctionFileName (bool showFunctionFileName) noexcept; + static void SetShowFunctionLineNumber (bool showFunctionLineNumber) noexcept; - static void SetClockFormat (ClockFormat newClockFormat) noexcept; - static void SetDateFormat (DateFormat newDateFormat) noexcept; + static void SetClockFormat (ClockFormat newClockFormat) noexcept; + static void SetDateFormat (DateFormat newDateFormat) noexcept; - static void SetFileName (const std::string& logFileName) noexcept; - static void SetDirectoryPath (const std::filesystem::path& logDirectoryPath) noexcept; + static void SetFileName (const std::string& logFileName) noexcept; + static void SetDirectoryPath (const std::filesystem::path& logDirectoryPath) noexcept; - static void SetFlushTime (int seconds) noexcept; - static void SetFlushTime (size_t seconds) noexcept { spdlog::flush_every(std::chrono::seconds(seconds)); } + static void SetFlushTime (int seconds) noexcept; + static void SetFlushTime (size_t seconds) noexcept { spdlog::flush_every(std::chrono::seconds(seconds)); } /*---------------------------------------------------------------------------------*/ /* Function Members */ From 471ec27a6de13c0f77e7713119eebdf83258b2a4 Mon Sep 17 00:00:00 2001 From: Cocoa Date: Mon, 19 Sep 2022 16:50:06 +0800 Subject: [PATCH 2/8] Added Bounding Box --- SHADE_Engine/SHADE_Engine.vcxproj | 4 + SHADE_Engine/SHADE_Engine.vcxproj.filters | 4 + .../src/Math/Geometry/SHBoundingBox.cpp | 267 ++++++++++++++++++ .../src/Math/Geometry/SHBoundingBox.h | 87 ++++++ SHADE_Engine/src/Math/Geometry/SHShape.cpp | 26 ++ SHADE_Engine/src/Math/Geometry/SHShape.h | 78 +++++ SHADE_Engine/src/Math/SHMathHelpers.h | 6 + SHADE_Engine/src/Math/SHMathHelpers.hpp | 26 ++ SHADE_Engine/src/Math/Vector/SHVec2.cpp | 4 + SHADE_Engine/src/Math/Vector/SHVec2.h | 5 +- SHADE_Engine/src/Math/Vector/SHVec3.cpp | 4 + SHADE_Engine/src/Math/Vector/SHVec3.h | 1 + 12 files changed, 510 insertions(+), 2 deletions(-) create mode 100644 SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp create mode 100644 SHADE_Engine/src/Math/Geometry/SHBoundingBox.h create mode 100644 SHADE_Engine/src/Math/Geometry/SHShape.cpp create mode 100644 SHADE_Engine/src/Math/Geometry/SHShape.h diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index 60a09861..e69a8f9c 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -172,6 +172,8 @@ + + @@ -249,6 +251,8 @@ + + diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index 42084b70..747538f2 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -386,6 +386,8 @@ Tools + + @@ -590,5 +592,7 @@ Tools + + \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp new file mode 100644 index 00000000..44f01a4b --- /dev/null +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp @@ -0,0 +1,267 @@ +/**************************************************************************************** + * \file SHBoundingBox.cpp + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for a 3-Dimensional Axis Aligned Bounding Box + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#include + +// Primary Header +#include "SHBoundingBox.h" +// Project Headers +#include "Math/SHMathHelpers.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Constructors & Destructor Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHBoundingBox::SHBoundingBox(const SHVec3& c, SHVec3& hE) noexcept + : SHShape {} + , center { c } + , halfExtents { hE } + { + type = Type::BOUNDING_BOX; + } + + SHBoundingBox::SHBoundingBox(const SHVec3* vertices, size_t numVertices) noexcept + : SHShape {} + { + type = Type::BOUNDING_BOX; + + if (vertices == nullptr || numVertices < 2) + { + SHLOG_ERROR("Insufficient number of vertices passed into bounding box constructor!") + return; + } + + SHVec3 min { std::numeric_limits::max() }; + SHVec3 max { std::numeric_limits::min() }; + + for (size_t i = 0; i < numVertices; ++i) + { + const SHVec3& v = vertices[i]; + + min.x = SHMath::Min(min.x, v.x); + min.y = SHMath::Min(min.y, v.y); + min.z = SHMath::Min(min.z, v.z); + + max.x = SHMath::Max(max.x, v.x); + max.y = SHMath::Max(max.y, v.y); + max.z = SHMath::Max(max.z, v.z); + } + + center = SHVec3::Lerp(min, max, 0.5f); + halfExtents = SHVec3::Abs((max - min) * 0.5f); + } + + SHBoundingBox::SHBoundingBox(const SHBoundingBox* boxes, size_t numBoxes) noexcept + : SHShape {} + { + type = Type::BOUNDING_BOX; + + if (boxes == nullptr || numBoxes == 0) + { + SHLOG_ERROR("Insufficient number of boxes passed into bounding box constructor!") + return; + } + + center = boxes->center; + halfExtents = boxes->halfExtents; + + for (size_t i = 1; i < numBoxes; ++i) + *this = Combine(*this, boxes[i]); + } + + SHBoundingBox::SHBoundingBox(const SHBoundingBox& rhs) noexcept + : SHShape {} + , center { rhs.center } + , halfExtents { rhs.halfExtents } + { + type = Type::BOUNDING_BOX; + } + + SHBoundingBox::SHBoundingBox(SHBoundingBox&& rhs) noexcept + : SHShape {} + , center { rhs.center } + , halfExtents { rhs.halfExtents } + { + type = Type::BOUNDING_BOX; + } + + SHBoundingBox& SHBoundingBox::operator=(const SHBoundingBox& rhs) noexcept + { + if (rhs.type != Type::BOUNDING_BOX) + { + SHLOG_WARNING("Cannot assign a non-bounding box to a bounding box!") + } + else + { + center = rhs.center; + halfExtents = rhs.halfExtents; + } + + return *this; + } + + SHBoundingBox& SHBoundingBox::operator=(SHBoundingBox&& rhs) noexcept + { + if (rhs.type != Type::BOUNDING_BOX) + { + SHLOG_WARNING("Cannot assign a non-bounding box to a bounding box!") + } + else + { + center = rhs.center; + halfExtents = rhs.halfExtents; + } + + return *this; + } + + /*-----------------------------------------------------------------------------------*/ + /* Getter Function Definitions */ + /*-----------------------------------------------------------------------------------*/ + + const SHVec3& SHBoundingBox::GetCenter() const noexcept + { + return center; + } + + const SHVec3& SHBoundingBox::GetHalfExtents() const noexcept + { + return halfExtents; + } + + SHVec3 SHBoundingBox::GetMin() const noexcept + { + return center - halfExtents; + } + + SHVec3 SHBoundingBox::GetMax() const noexcept + { + return center + halfExtents; + } + + /*-----------------------------------------------------------------------------------*/ + /* Setter Function Definitions */ + /*-----------------------------------------------------------------------------------*/ + + void SHBoundingBox::SetCenter(const SHVec3& newCenter) noexcept + { + center = newCenter; + } + + void SHBoundingBox::SetHalfExtents(const SHVec3& newHalfExtents) noexcept + { + halfExtents = newHalfExtents; + } + + void SHBoundingBox::SetMin(const SHVec3& min) noexcept + { + const SHVec3 MAX = center + halfExtents; + + center = SHVec3::Lerp(min, MAX, 0.5f); + halfExtents = SHVec3::Abs((MAX - min) * 0.5f); + } + + void SHBoundingBox::SetMax(const SHVec3& max) noexcept + { + const SHVec3 MIN = center - halfExtents; + + center = SHVec3::Lerp(MIN, max, 0.5f); + halfExtents = SHVec3::Abs((max - MIN) * 0.5f); + } + + void SHBoundingBox::SetMinMax(const SHVec3& min, const SHVec3& max) noexcept + { + center = SHVec3::Lerp(min, max, 0.5f); + halfExtents = SHVec3::Abs((max - min) * 0.5f); + } + + + /*-----------------------------------------------------------------------------------*/ + /* Public Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + bool SHBoundingBox::TestPoint(const SHVec3& point) noexcept + { + const SHVec3 V = SHVec3::Abs(point - center); + for (size_t i = 0; i < SHVec3::SIZE; ++i) + { + if (V[i] > halfExtents[i]) + return false; + } + + return true; + } + + bool SHBoundingBox::Contains(const SHBoundingBox& rhs) const noexcept + { + const SHVec3 V = SHVec3::Abs(rhs.center - center); + for (size_t i = 0; i < SHVec3::SIZE; ++i) + { + if (V[i] > rhs.halfExtents[i]) + return false; + } + + return true; + } + + float SHBoundingBox::Volume() const noexcept + { + return 8.0f * (halfExtents.x * halfExtents.y * halfExtents.z); + } + + float SHBoundingBox::SurfaceArea() const noexcept + { + return 8.0f * ((halfExtents.x * halfExtents.y) + + (halfExtents.x * halfExtents.z) + + (halfExtents.y * halfExtents.z)); + } + + /*-----------------------------------------------------------------------------------*/ + /* Static Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHBoundingBox SHBoundingBox::Combine(const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept + { + if (lhs.Contains(rhs)) + return lhs; + + if (rhs.Contains(lhs)) + return rhs; + + const SHVec3 LHS_MIN = lhs.GetMin(); + const SHVec3 LHS_MAX = lhs.GetMax(); + const SHVec3 RHS_MIN = rhs.GetMin(); + const SHVec3 RHS_MAX = rhs.GetMax(); + + SHVec3 min = SHVec3::Min({ LHS_MIN, RHS_MIN }); + SHVec3 max = SHVec3::Max({ LHS_MAX, RHS_MAX }); + + SHBoundingBox result{ lhs }; + result.SetMinMax(min, max); + return result; + } + + bool SHBoundingBox::Intersect(const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept + { + const SHVec3 V = SHVec3::Abs(lhs.center - rhs.center); + const SHVec3 D = lhs.halfExtents + rhs.halfExtents; + + for (size_t i = 0; i < SHVec3::SIZE; ++i) + { + if (V[i] > D[i]) + return false; + } + + return true; + } + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h new file mode 100644 index 00000000..8c397aff --- /dev/null +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h @@ -0,0 +1,87 @@ +/**************************************************************************************** + * \file SHBoundingBox.h + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Interface for a 3-Dimensional Axis Aligned Bounding Box + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#pragma once + +// Project Headers +#include "SHShape.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + + class SHBoundingBox : public SHShape + { + public: + /*---------------------------------------------------------------------------------*/ + /* Constructors & Destructor */ + /*---------------------------------------------------------------------------------*/ + + SHBoundingBox (const SHVec3& center, SHVec3& halfExtents) noexcept; + SHBoundingBox (const SHVec3* vertices, size_t numVertices) noexcept; + SHBoundingBox (const SHBoundingBox* boxes, size_t numBoxes) noexcept; + + SHBoundingBox (const SHBoundingBox& rhs) noexcept; + SHBoundingBox (SHBoundingBox&& rhs) noexcept; + + SHBoundingBox& operator= (const SHBoundingBox& rhs) noexcept; + SHBoundingBox& operator= (SHBoundingBox&& rhs) noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Getter Functions */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] const SHVec3& GetCenter () const noexcept; + [[nodiscard]] const SHVec3& GetHalfExtents () const noexcept; + [[nodiscard]] SHVec3 GetMin () const noexcept; + [[nodiscard]] SHVec3 GetMax () const noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Setter Functions */ + /*---------------------------------------------------------------------------------*/ + + void SetCenter (const SHVec3& newCenter) noexcept; + void SetHalfExtents (const SHVec3& newHalfExtents) noexcept; + void SetMin (const SHVec3& min) noexcept; + void SetMax (const SHVec3& max) noexcept; + void SetMinMax (const SHVec3& min, const SHVec3& max) noexcept; + + + /*---------------------------------------------------------------------------------*/ + /* Function Members */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] bool TestPoint (const SHVec3& point) noexcept override; + + [[nodiscard]] bool Contains (const SHBoundingBox& rhs) const noexcept; + [[nodiscard]] float Volume () const noexcept; + [[nodiscard]] float SurfaceArea () const noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Static Function Members */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] static SHBoundingBox Combine (const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept; + [[nodiscard]] static bool Intersect (const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept; + + private: + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + + SHVec3 center; + SHVec3 halfExtents; + }; + + +} // namespace SHADE + diff --git a/SHADE_Engine/src/Math/Geometry/SHShape.cpp b/SHADE_Engine/src/Math/Geometry/SHShape.cpp new file mode 100644 index 00000000..02aaff58 --- /dev/null +++ b/SHADE_Engine/src/Math/Geometry/SHShape.cpp @@ -0,0 +1,26 @@ +/**************************************************************************************** + * \file SHShape.cpp + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for a shape. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#include + +// Primary Header +#include "SHShape.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Constructors & Destructor Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHShape::SHShape() + : type { Type::NONE } + {} + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Geometry/SHShape.h b/SHADE_Engine/src/Math/Geometry/SHShape.h new file mode 100644 index 00000000..3dbe47e2 --- /dev/null +++ b/SHADE_Engine/src/Math/Geometry/SHShape.h @@ -0,0 +1,78 @@ +/**************************************************************************************** + * \file SHShape.h + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Interface for a shape. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#pragma once + +// Project Headers +#include "Math/SHTransform.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + + class SHShape + { + public: + /*---------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*---------------------------------------------------------------------------------*/ + + enum class Type + { + BOUNDING_BOX + , RAY + , TRIANGLE + + , COUNT + , NONE = -1 + }; + + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + + bool isIntersecting; + + /*---------------------------------------------------------------------------------*/ + /* Constructors & Destructor */ + /*---------------------------------------------------------------------------------*/ + + virtual ~SHShape () = default; + + SHShape (const SHShape&) = default; + SHShape (SHShape&&) = default; + + SHShape& operator=(const SHShape&) = default; + SHShape& operator=(SHShape&&) = default; + + SHShape(); + + /*---------------------------------------------------------------------------------*/ + /* Getter Functions */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] Type GetType() const; + + /*---------------------------------------------------------------------------------*/ + /* Function Members */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] virtual bool TestPoint (const SHVec3& point) noexcept = 0; + + protected: + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + + Type type; + }; +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/SHMathHelpers.h b/SHADE_Engine/src/Math/SHMathHelpers.h index 1580ce47..88c47fbd 100644 --- a/SHADE_Engine/src/Math/SHMathHelpers.h +++ b/SHADE_Engine/src/Math/SHMathHelpers.h @@ -61,9 +61,15 @@ namespace SHADE template [[nodiscard]] static T Min (T lhs, T rhs); + template + [[nodiscard]] static T Min (const std::initializer_list& values); + template [[nodiscard]] static T Max (T lhs, T rhs); + template + [[nodiscard]] static T Max (const std::initializer_list& values); + template [[nodiscard]] static T DegreesToRadians (T angleInDeg); diff --git a/SHADE_Engine/src/Math/SHMathHelpers.hpp b/SHADE_Engine/src/Math/SHMathHelpers.hpp index 4b49a9a5..0e5fc5fa 100644 --- a/SHADE_Engine/src/Math/SHMathHelpers.hpp +++ b/SHADE_Engine/src/Math/SHMathHelpers.hpp @@ -27,12 +27,38 @@ namespace SHADE return lhs < rhs ? lhs : rhs; } + template + T SHMath::Min(const std::initializer_list& values) + { + T min = *(values.begin()); + + for (auto value : values) + { + min = Min(min, value); + } + + return min; + } + template T SHMath::Max(T lhs, T rhs) { return lhs > rhs ? lhs : rhs; } + template + T SHMath::Max(const std::initializer_list& values) + { + T max = *(values.begin()); + + for (auto value : values) + { + max = Min(max, value); + } + + return max; + } + template T SHMath::DegreesToRadians(T angleInDeg) { diff --git a/SHADE_Engine/src/Math/Vector/SHVec2.cpp b/SHADE_Engine/src/Math/Vector/SHVec2.cpp index 72c80a50..2d2eafd2 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec2.cpp +++ b/SHADE_Engine/src/Math/Vector/SHVec2.cpp @@ -38,6 +38,10 @@ namespace SHADE : XMFLOAT2( 0.0f, 0.0f ) {} + SHVec2::SHVec2(float n) noexcept + : XMFLOAT2( n, n ) + {} + SHVec2::SHVec2(float _x, float _y) noexcept : XMFLOAT2( _x, _y ) {} diff --git a/SHADE_Engine/src/Math/Vector/SHVec2.h b/SHADE_Engine/src/Math/Vector/SHVec2.h index 0b272b40..f57c7b5b 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec2.h +++ b/SHADE_Engine/src/Math/Vector/SHVec2.h @@ -49,8 +49,9 @@ namespace SHADE SHVec2 (SHVec2&& rhs) = default; ~SHVec2 () = default; - SHVec2 () noexcept; - SHVec2 (float x, float y) noexcept; + SHVec2 () noexcept; + SHVec2 (float n) noexcept; + SHVec2 (float x, float y) noexcept; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ diff --git a/SHADE_Engine/src/Math/Vector/SHVec3.cpp b/SHADE_Engine/src/Math/Vector/SHVec3.cpp index 73030f9c..194f7964 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec3.cpp +++ b/SHADE_Engine/src/Math/Vector/SHVec3.cpp @@ -43,6 +43,10 @@ namespace SHADE : XMFLOAT3( 0.0f, 0.0f, 0.0f ) {} + SHVec3::SHVec3(float n) noexcept + : XMFLOAT3( n, n, n ) + {} + SHVec3::SHVec3(float _x, float _y, float _z) noexcept : XMFLOAT3( _x, _y, _z ) {} diff --git a/SHADE_Engine/src/Math/Vector/SHVec3.h b/SHADE_Engine/src/Math/Vector/SHVec3.h index 059c5708..76318499 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec3.h +++ b/SHADE_Engine/src/Math/Vector/SHVec3.h @@ -55,6 +55,7 @@ namespace SHADE ~SHVec3 () = default; SHVec3 () noexcept; + SHVec3 (float n) noexcept; SHVec3 (float x, float y, float z) noexcept; /*---------------------------------------------------------------------------------*/ From 34685e6726fbdd283b4a61e23d0bbd0af16f9e7d Mon Sep 17 00:00:00 2001 From: Cocoa Date: Tue, 20 Sep 2022 00:48:27 +0800 Subject: [PATCH 3/8] Regenerated project files --- SHADE_Application/SHADE_Application.vcxproj | 29 ++-- SHADE_Engine/SHADE_Engine.vcxproj | 109 +++++++++---- SHADE_Engine/SHADE_Engine.vcxproj.filters | 168 ++++++++++++++++++-- 3 files changed, 255 insertions(+), 51 deletions(-) diff --git a/SHADE_Application/SHADE_Application.vcxproj b/SHADE_Application/SHADE_Application.vcxproj index 9a566252..0a7457a0 100644 --- a/SHADE_Application/SHADE_Application.vcxproj +++ b/SHADE_Application/SHADE_Application.vcxproj @@ -42,25 +42,28 @@ true - bin\Debug_x86_64\SHADE_Application\ - bin-int\Debug_x86_64\SHADE_Application\ + ..\bin\Debug\ + ..\bin_int\Debug\SHADE_Application\ SHADE_Application .exe + ..\Dependencies\spdlog\include;$(VULKAN_SDK)\include;..\Dependencies\VMA\include;$(VULKAN_SDK)\Source\SPIRV-Reflect;$(IncludePath) false - bin\Release_x86_64\SHADE_Application\ - bin-int\Release_x86_64\SHADE_Application\ + ..\bin\Release\ + ..\bin_int\Release\SHADE_Application\ SHADE_Application .exe + ..\Dependencies\spdlog\include;$(VULKAN_SDK)\include;..\Dependencies\VMA\include;$(VULKAN_SDK)\Source\SPIRV-Reflect;$(IncludePath) Use SBpch.h Level4 - _DEBUG;%(PreprocessorDefinitions) - ..\Dependencies\spdlog\include;..\SHADE_Engine\src;src;%(AdditionalIncludeDirectories) + 4251;%(DisableSpecificWarnings) + NOMINMAX;_DEBUG;%(PreprocessorDefinitions) + ..\SHADE_Engine\src;src;..\Dependencies\dotnet\include;..\Dependencies\SDL\include;%(AdditionalIncludeDirectories) EditAndContinue Disabled false @@ -71,6 +74,8 @@ Windows true + SDL2.lib;SDL2main.lib;%(AdditionalDependencies) + ..\Dependencies\spdlog\lib;..\Dependencies\SDL\lib;%(AdditionalLibraryDirectories) wWinMainCRTStartup @@ -79,8 +84,9 @@ Use SBpch.h Level4 - _RELEASE;%(PreprocessorDefinitions) - ..\Dependencies\spdlog\include;..\SHADE_Engine\src;src;%(AdditionalIncludeDirectories) + 4251;%(DisableSpecificWarnings) + NOMINMAX;_RELEASE;%(PreprocessorDefinitions) + ..\SHADE_Engine\src;src;..\Dependencies\dotnet\include;..\Dependencies\SDL\include;%(AdditionalIncludeDirectories) Full true true @@ -88,12 +94,14 @@ true MultiThreadedDLL true - stdcpplatest + stdcpp20 Windows true true + SDL2.lib;SDL2main.lib;%(AdditionalDependencies) + ..\Dependencies\spdlog\lib;..\Dependencies\SDL\lib;%(AdditionalLibraryDirectories) wWinMainCRTStartup @@ -114,6 +122,9 @@ {3F92E998-2BF5-783D-D47A-B1F3C0BC44C0} + + {16DB1400-829B-9036-4BD6-D9B3B755D512} + diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index e69a8f9c..03e1e0b3 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -19,13 +19,13 @@ - StaticLibrary + DynamicLibrary true Unicode v142 - StaticLibrary + DynamicLibrary false Unicode v142 @@ -41,24 +41,29 @@ - bin\Debug_x86_64\SHADE_Engine\ - bin-int\Debug_x86_64\SHADE_Engine\ + true + ..\bin\Debug\ + ..\bin_int\Debug\SHADE_Engine\ SHADE_Engine - .lib + .dll + ..\Dependencies\assimp\include;..\Dependencies\imgui;..\Dependencies\imguizmo;..\Dependencies\imnodes;..\Dependencies\msdf;..\Dependencies\msdf\msdfgen;..\Dependencies\spdlog\include;..\Dependencies\tracy;..\Dependencies\VMA\include;..\Dependencies\yamlcpp\include;..\Dependencies\SDL\include;..\Dependencies\RTTR\include;..\Dependencies\reactphysics3d\include;$(VULKAN_SDK)\include;$(VULKAN_SDK)\Source\SPIRV-Reflect;..\Dependencies\dotnet\include;$(IncludePath) - bin\Release_x86_64\SHADE_Engine\ - bin-int\Release_x86_64\SHADE_Engine\ + false + ..\bin\Release\ + ..\bin_int\Release\SHADE_Engine\ SHADE_Engine - .lib + .dll + ..\Dependencies\assimp\include;..\Dependencies\imgui;..\Dependencies\imguizmo;..\Dependencies\imnodes;..\Dependencies\msdf;..\Dependencies\msdf\msdfgen;..\Dependencies\spdlog\include;..\Dependencies\tracy;..\Dependencies\VMA\include;..\Dependencies\yamlcpp\include;..\Dependencies\SDL\include;..\Dependencies\RTTR\include;..\Dependencies\reactphysics3d\include;$(VULKAN_SDK)\include;$(VULKAN_SDK)\Source\SPIRV-Reflect;..\Dependencies\dotnet\include;$(IncludePath) Use SHpch.h Level4 - _LIB;_GLFW_INCLUDE_NONE;MSDFGEN_USE_CPP11;NOMINMAX;_DEBUG;%(PreprocessorDefinitions) - src;..\Dependencies\assimp\include;..\Dependencies\imgui;..\Dependencies\imguizmo;..\Dependencies\imnodes;..\Dependencies\msdf;..\Dependencies\msdf\msdfgen;..\Dependencies\spdlog\include;..\Dependencies\tracy;..\Dependencies\VMA\include;..\Dependencies\yamlcpp\include;..\Dependencies\ktx\include;..\Dependencies\RTTR\include;..\Dependencies\reactphysics3d\include;$(VULKAN_SDK)\include;$(VULKAN_SDK)\Source\SPIRV-Reflect;%(AdditionalIncludeDirectories) + 4251;%(DisableSpecificWarnings) + _LIB;_GLFW_INCLUDE_NONE;MSDFGEN_USE_CPP11;NOMINMAX;SH_API_EXPORT;_DEBUG;%(PreprocessorDefinitions) + src;%(AdditionalIncludeDirectories) EditAndContinue Disabled false @@ -69,19 +74,24 @@ Windows true + vulkan-1.lib;SDL2.lib;SDL2main.lib;shaderc_shared.lib;shlwapi.lib;assimp-vc142-mtd.lib;librttr_core_d.lib;spdlogd.lib;%(AdditionalDependencies) + libs;$(VULKAN_SDK)\Lib;..\Dependencies\assimp\lib\Debug;..\Dependencies\assimp\lib\Release;..\Dependencies\RTTR\lib;..\Dependencies\SDL\lib;..\Dependencies\spdlog\lib;%(AdditionalLibraryDirectories) + ..\bin\Debug\SHADE_Engine.lib - - vulkan-1.lib;shaderc_shared.lib;assimp-vc142-mtd.lib;ktxd.lib;librttr_core_d.lib;%(AdditionalDependencies) - libs;$(VULKAN_SDK)\Lib;..\Dependencies\assimp\lib\Debug;..\Dependencies\assimp\lib\Release;..\Dependencies\RTTR\lib;..\Dependencies\ktx\lib\Debug;..\Dependencies\ktx\lib\Release;%(AdditionalLibraryDirectories) - + + xcopy /s /r /y /q "$(SolutionDir)/Dependencies/spdlog/bin" "$(OutDir)" +xcopy /r /y /q "$(SolutionDir)/Dependencies/SDL/lib/SDL2.dll" "$(OutDir)" +xcopy /s /r /y /q "$(SolutionDir)/Dependencies/dotnet/bin" "$(OutDir)" + Use SHpch.h Level4 - _LIB;_GLFW_INCLUDE_NONE;MSDFGEN_USE_CPP11;NOMINMAX;_RELEASE;%(PreprocessorDefinitions) - src;..\Dependencies\assimp\include;..\Dependencies\imgui;..\Dependencies\imguizmo;..\Dependencies\imnodes;..\Dependencies\msdf;..\Dependencies\msdf\msdfgen;..\Dependencies\spdlog\include;..\Dependencies\tracy;..\Dependencies\VMA\include;..\Dependencies\yamlcpp\include;..\Dependencies\ktx\include;..\Dependencies\RTTR\include;..\Dependencies\reactphysics3d\include;$(VULKAN_SDK)\include;$(VULKAN_SDK)\Source\SPIRV-Reflect;%(AdditionalIncludeDirectories) + 4251;%(DisableSpecificWarnings) + _LIB;_GLFW_INCLUDE_NONE;MSDFGEN_USE_CPP11;NOMINMAX;SH_API_EXPORT;_RELEASE;%(PreprocessorDefinitions) + src;%(AdditionalIncludeDirectories) Full true true @@ -89,33 +99,51 @@ true MultiThreadedDLL true - stdcpplatest + stdcpp20 Windows true true + vulkan-1.lib;SDL2.lib;SDL2main.lib;shaderc_shared.lib;shlwapi.lib;assimp-vc142-mt.lib;librttr_core.lib;spdlog.lib;%(AdditionalDependencies) + libs;$(VULKAN_SDK)\Lib;..\Dependencies\assimp\lib\Debug;..\Dependencies\assimp\lib\Release;..\Dependencies\RTTR\lib;..\Dependencies\SDL\lib;..\Dependencies\spdlog\lib;%(AdditionalLibraryDirectories) + ..\bin\Release\SHADE_Engine.lib - - vulkan-1.lib;shaderc_shared.lib;assimp-vc142-mt.lib;ktx.lib;librttr_core.lib;%(AdditionalDependencies) - libs;$(VULKAN_SDK)\Lib;..\Dependencies\assimp\lib\Debug;..\Dependencies\assimp\lib\Release;..\Dependencies\RTTR\lib;..\Dependencies\ktx\lib\Debug;..\Dependencies\ktx\lib\Release;%(AdditionalLibraryDirectories) - + + xcopy /s /r /y /q "$(SolutionDir)/Dependencies/spdlog/bin" "$(OutDir)" +xcopy /r /y /q "$(SolutionDir)/Dependencies/SDL/lib/SDL2.dll" "$(OutDir)" +xcopy /s /r /y /q "$(SolutionDir)/Dependencies/dotnet/bin" "$(OutDir)" + + + + + + + - - + + - + + + + + + + + + @@ -126,6 +154,7 @@ + @@ -136,6 +165,7 @@ + @@ -185,25 +215,41 @@ + + + + + + + + + + + + - - - + + + + + + + @@ -213,6 +259,7 @@ + @@ -222,6 +269,7 @@ + @@ -266,9 +314,13 @@ + + + + @@ -286,9 +338,6 @@ {C0FF640D-2C14-8DBE-F595-301E616989EF} - - {8EAD431C-7A4F-6EF2-630A-82464F4BF542} - diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index 747538f2..2311e8dd 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -1,6 +1,9 @@ + + {8EEA3EAC-7A8C-6982-6347-7DD64F88F0D2} + {1AB26817-067F-C322-2F98-B1CA1BC4F8B0} @@ -10,15 +13,30 @@ {261D0942-92A8-7606-9BB9-F9FA07C4D206} + + {BEBC9142-2A48-FF06-3359-82FB9F635B07} + {07FEB307-F3F6-D259-1C29-B8DE0881B265} + + {37718753-A387-85E9-ECFE-100B58545671} + {EE037863-5A8F-E527-63A0-681CCFAA4128} + + {078AA1A3-F318-2B6D-9C37-3F6888A53B13} + {DBC7D3B0-C769-FE86-B024-12DB9C6585D7} + + {3A8963B1-262B-8E87-0FE6-A1DBFB2615D8} + + + {80E7870B-ECF0-877C-756C-9E7CE174937C} + {8A8E2B37-7646-6D84-DF4D-46E0CB240875} @@ -100,6 +118,9 @@ {AFF4887C-9B2B-8A0D-4418-7010302E060F} + + {EAD6C33D-5697-3F74-1FD2-88F18B518450} + {F1B75745-5D6D-D03A-E661-CA115216C73E} @@ -112,11 +133,17 @@ {B3F7140E-1F0C-3DBF-E88D-E01E546139F0} + + {985A7358-04C5-27CF-4D03-D974B9AC0524} + {16CF2D0E-82E3-55BF-4B65-F91EB73852F0} + + Common + ECS_Base\Components @@ -126,6 +153,12 @@ ECS_Base\Entity + + ECS_Base\Events + + + ECS_Base\Events + ECS_Base\General @@ -141,24 +174,57 @@ ECS_Base\General + + ECS_Base\Managers + + + ECS_Base\Managers + + + ECS_Base\Managers + ECS_Base - + ECS_Base\System - + ECS_Base\System ECS_Base\System - + ECS_Base\System + + ECS_Base\UnitTesting + + + ECS_Base\UnitTesting + + + ECS_Base\UnitTesting + Engine + + Events + + + Events + + + Events + + + Events + + + FRC + Filesystem @@ -189,6 +255,9 @@ Graphics\Descriptors + + Graphics\Descriptors + Graphics\Descriptors @@ -219,6 +288,9 @@ Graphics\Images + + Graphics\Images + Graphics\Instance @@ -327,12 +399,21 @@ Graphics\Windowing\Surface + + Math\Geometry + + + Math\Geometry + Math Math + + Math + Math @@ -357,12 +438,23 @@ Resource + + Resource + Resource + + Resource + Resource + + Resource + + + Scene @@ -373,21 +465,39 @@ Scene + + Scripting + + + Scripting + + + Scripting + Tools Tools + + Tools + Tools + + Tools + + + Tools + Tools - - - + + Tools + @@ -399,18 +509,30 @@ ECS_Base\Entity - + + ECS_Base\Managers + + + ECS_Base\Managers + + + ECS_Base\Managers + + ECS_Base\System - + ECS_Base\System - - ECS_Base\System + + ECS_Base\UnitTesting Engine + + FRC + Filesystem @@ -438,6 +560,9 @@ Graphics\Descriptors + + Graphics\Descriptors + Graphics\Descriptors @@ -465,6 +590,9 @@ Graphics\Images + + Graphics\Images + Graphics\Instance @@ -552,6 +680,12 @@ Graphics\Windowing\Surface + + Math\Geometry + + + Math\Geometry + Math @@ -583,16 +717,26 @@ Scene + + Scripting + + + Scripting + Tools Tools + + Tools + Tools - - + + Tools + \ No newline at end of file From 7f173b3207cf8e5a073cbd77d9b5c4f84826a8d8 Mon Sep 17 00:00:00 2001 From: Cocoa Date: Tue, 20 Sep 2022 16:10:47 +0800 Subject: [PATCH 4/8] Added SH_API to Math Classes --- SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h | 2 +- SHADE_Engine/src/Math/Geometry/SHBoundingBox.h | 3 ++- SHADE_Engine/src/Math/Geometry/SHShape.h | 3 ++- SHADE_Engine/src/Math/SHMathHelpers.h | 5 ++++- SHADE_Engine/src/Math/SHMatrix.h | 4 +++- SHADE_Engine/src/Math/SHQuaternion.h | 5 ++++- SHADE_Engine/src/Math/SHTransform.h | 4 +++- SHADE_Engine/src/Math/Vector/SHVec2.h | 5 ++++- SHADE_Engine/src/Math/Vector/SHVec3.h | 5 ++++- SHADE_Engine/src/Math/Vector/SHVec4.h | 5 ++++- SHADE_Engine/src/Scene/SHSceneManager.h | 8 ++++++-- 11 files changed, 37 insertions(+), 12 deletions(-) diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h b/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h index 20720760..60625d6a 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h +++ b/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h @@ -36,7 +36,7 @@ namespace SHADE //The Container of all Componentgroups static std::vector componentGroups; - friend struct SHSceneNode; + friend class SHSceneNode; diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h index 8c397aff..b60c6c29 100644 --- a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h @@ -12,6 +12,7 @@ // Project Headers #include "SHShape.h" +#include "SH_API.h" namespace SHADE { @@ -19,7 +20,7 @@ namespace SHADE /* Type Definitions */ /*-----------------------------------------------------------------------------------*/ - class SHBoundingBox : public SHShape + class SH_API SHBoundingBox : public SHShape { public: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/Geometry/SHShape.h b/SHADE_Engine/src/Math/Geometry/SHShape.h index 3dbe47e2..87396c6d 100644 --- a/SHADE_Engine/src/Math/Geometry/SHShape.h +++ b/SHADE_Engine/src/Math/Geometry/SHShape.h @@ -12,6 +12,7 @@ // Project Headers #include "Math/SHTransform.h" +#include "SH_API.h" namespace SHADE { @@ -19,7 +20,7 @@ namespace SHADE /* Type Definitions */ /*-----------------------------------------------------------------------------------*/ - class SHShape + class SH_API SHShape { public: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/SHMathHelpers.h b/SHADE_Engine/src/Math/SHMathHelpers.h index 88c47fbd..427011a6 100644 --- a/SHADE_Engine/src/Math/SHMathHelpers.h +++ b/SHADE_Engine/src/Math/SHMathHelpers.h @@ -15,6 +15,9 @@ #include #include +// Project Headers +#include "SH_API.h" + namespace SHADE { @@ -35,7 +38,7 @@ namespace SHADE /* Type Definitions */ /*-----------------------------------------------------------------------------------*/ - class SHMath + class SH_API SHMath { public: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/SHMatrix.h b/SHADE_Engine/src/Math/SHMatrix.h index f5d7c173..95f87146 100644 --- a/SHADE_Engine/src/Math/SHMatrix.h +++ b/SHADE_Engine/src/Math/SHMatrix.h @@ -13,6 +13,8 @@ #include #include +// Project Headers +#include "SH_API.h" #include "Vector/SHVec4.h" namespace SHADE @@ -32,7 +34,7 @@ namespace SHADE /** * @brief Interface for a Column-Major Row Vector 4x4 Matrix. */ - class SHMatrix : public DirectX::XMFLOAT4X4 + class SH_API SHMatrix : public DirectX::XMFLOAT4X4 { public: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/SHQuaternion.h b/SHADE_Engine/src/Math/SHQuaternion.h index 820392fa..61b55ef9 100644 --- a/SHADE_Engine/src/Math/SHQuaternion.h +++ b/SHADE_Engine/src/Math/SHQuaternion.h @@ -13,6 +13,9 @@ #include #include +// Project Headers +#include "SH_API.h" + namespace SHADE { /*-----------------------------------------------------------------------------------*/ @@ -27,7 +30,7 @@ namespace SHADE /* Type Definitions */ /*-----------------------------------------------------------------------------------*/ - class SHQuaternion : public DirectX::XMFLOAT4 + class SH_API SHQuaternion : public DirectX::XMFLOAT4 { public: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/SHTransform.h b/SHADE_Engine/src/Math/SHTransform.h index a6d9a1b2..8908b8eb 100644 --- a/SHADE_Engine/src/Math/SHTransform.h +++ b/SHADE_Engine/src/Math/SHTransform.h @@ -10,6 +10,8 @@ #pragma once +// Project Headers +#include "SH_API.h" #include "SHMath.h" namespace SHADE @@ -18,7 +20,7 @@ namespace SHADE /* Type Definitions */ /*-----------------------------------------------------------------------------------*/ - struct SHTransform + struct SH_API SHTransform { public: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/Vector/SHVec2.h b/SHADE_Engine/src/Math/Vector/SHVec2.h index f57c7b5b..3e6287aa 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec2.h +++ b/SHADE_Engine/src/Math/Vector/SHVec2.h @@ -14,6 +14,9 @@ #include #include +// Project Headers +#include "SH_API.h" + namespace SHADE { /*-----------------------------------------------------------------------------------*/ @@ -25,7 +28,7 @@ namespace SHADE /* Type Definitions */ /*-----------------------------------------------------------------------------------*/ - class SHVec2 : public DirectX::XMFLOAT2 + class SH_API SHVec2 : public DirectX::XMFLOAT2 { public: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/Vector/SHVec3.h b/SHADE_Engine/src/Math/Vector/SHVec3.h index 76318499..476d7b0f 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec3.h +++ b/SHADE_Engine/src/Math/Vector/SHVec3.h @@ -14,6 +14,9 @@ #include #include +// Project Headers +#include "SH_API.h" + namespace SHADE { /*-----------------------------------------------------------------------------------*/ @@ -25,7 +28,7 @@ namespace SHADE /* Type Definitions */ /*-----------------------------------------------------------------------------------*/ - class SHVec3 : public DirectX::XMFLOAT3 + class SH_API SHVec3 : public DirectX::XMFLOAT3 { public: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/Vector/SHVec4.h b/SHADE_Engine/src/Math/Vector/SHVec4.h index 36793fe0..a4a5208a 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec4.h +++ b/SHADE_Engine/src/Math/Vector/SHVec4.h @@ -14,6 +14,9 @@ #include #include +// Project Headers +#include "SH_API.h" + namespace SHADE { /*-----------------------------------------------------------------------------------*/ @@ -25,7 +28,7 @@ namespace SHADE /* Type Definitions */ /*-----------------------------------------------------------------------------------*/ - class SHVec4 : public DirectX::XMFLOAT4 + class SH_API SHVec4 : public DirectX::XMFLOAT4 { public: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Scene/SHSceneManager.h b/SHADE_Engine/src/Scene/SHSceneManager.h index bdd8f596..83ebab0f 100644 --- a/SHADE_Engine/src/Scene/SHSceneManager.h +++ b/SHADE_Engine/src/Scene/SHSceneManager.h @@ -13,14 +13,18 @@ #define SH_SCENE_MANAGER_H -#include "ECS_Base/General/SHFamily.h" + #include "SHScene.h" #include +//Project Headers +#include "SH_API.h" +#include "ECS_Base/General/SHFamily.h" + namespace SHADE { - class SHSceneManager + class SH_API SHSceneManager { private: //boolean to check if the scene has been changed From cc6d1dd95b231b0afe6a323b5c056ce34a870925 Mon Sep 17 00:00:00 2001 From: Cocoa Date: Tue, 20 Sep 2022 17:20:57 +0800 Subject: [PATCH 5/8] Added Transform Component --- SHADE_Engine/SHADE_Engine.vcxproj | 6 +- SHADE_Engine/SHADE_Engine.vcxproj.filters | 6 +- SHADE_Engine/src/Math/Geometry/SHShape.h | 2 +- .../src/Math/{ => Transform}/SHTransform.cpp | 31 ++++- .../src/Math/{ => Transform}/SHTransform.h | 19 ++- .../Math/Transform/SHTransformComponent.cpp | 129 ++++++++++++++++++ .../src/Math/Transform/SHTransformComponent.h | 115 ++++++++++++++++ 7 files changed, 293 insertions(+), 15 deletions(-) rename SHADE_Engine/src/Math/{ => Transform}/SHTransform.cpp (65%) rename SHADE_Engine/src/Math/{ => Transform}/SHTransform.h (73%) create mode 100644 SHADE_Engine/src/Math/Transform/SHTransformComponent.cpp create mode 100644 SHADE_Engine/src/Math/Transform/SHTransformComponent.h diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index 03e1e0b3..235ce56a 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -209,7 +209,8 @@ xcopy /s /r /y /q "$(SolutionDir)/Dependencies/dotnet/bin" "$(OutDir)" - + + @@ -304,7 +305,8 @@ xcopy /s /r /y /q "$(SolutionDir)/Dependencies/dotnet/bin" "$(OutDir)" - + + diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index 2311e8dd..7e279e59 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -420,7 +420,7 @@ Math - + Math @@ -498,6 +498,7 @@ Tools + @@ -695,7 +696,7 @@ Math - + Math @@ -738,5 +739,6 @@ Tools + \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Geometry/SHShape.h b/SHADE_Engine/src/Math/Geometry/SHShape.h index 87396c6d..84b7bbe7 100644 --- a/SHADE_Engine/src/Math/Geometry/SHShape.h +++ b/SHADE_Engine/src/Math/Geometry/SHShape.h @@ -11,7 +11,7 @@ #pragma once // Project Headers -#include "Math/SHTransform.h" +#include "Math/Transform/SHTransform.h" #include "SH_API.h" namespace SHADE diff --git a/SHADE_Engine/src/Math/SHTransform.cpp b/SHADE_Engine/src/Math/Transform/SHTransform.cpp similarity index 65% rename from SHADE_Engine/src/Math/SHTransform.cpp rename to SHADE_Engine/src/Math/Transform/SHTransform.cpp index 757ac5b6..e1b8cf2b 100644 --- a/SHADE_Engine/src/Math/SHTransform.cpp +++ b/SHADE_Engine/src/Math/Transform/SHTransform.cpp @@ -16,21 +16,41 @@ namespace SHADE { /*-----------------------------------------------------------------------------------*/ - /* Stai Definitions */ + /* Static Data Member Definitions */ /*-----------------------------------------------------------------------------------*/ - + const SHTransform SHTransform::Identity { SHVec3::Zero, SHVec3::Zero, SHVec3::One }; /*-----------------------------------------------------------------------------------*/ /* Constructors & Destructor Definitions */ /*-----------------------------------------------------------------------------------*/ SHTransform::SHTransform() noexcept - : position { SHVec3::Zero } - , rotation { SHVec3::Zero } - , scale { SHVec3::One } + : position { SHVec3::Zero } + , rotation { SHVec3::Zero } + , scale { SHVec3::One } {} + SHTransform::SHTransform(const SHVec3& pos, const SHVec3& rot, const SHVec3& scl) noexcept + : position { pos } + , rotation { rot } + , scale { scl } + {} + + /*-----------------------------------------------------------------------------------*/ + /* Operator Overload Definitions */ + /*-----------------------------------------------------------------------------------*/ + + bool SHTransform::operator==(const SHTransform& rhs) const noexcept + { + return !(position != rhs.position || rotation != rhs.rotation || scale != rhs.scale); + } + + bool SHTransform::operator!=(const SHTransform& rhs) const noexcept + { + return (position != rhs.position || rotation != rhs.rotation || scale != rhs.scale); + } + /*-----------------------------------------------------------------------------------*/ /* Getter Function Definitions */ /*-----------------------------------------------------------------------------------*/ @@ -40,7 +60,6 @@ namespace SHADE return trs; } - /*-----------------------------------------------------------------------------------*/ /* Public Function Member Definitions */ /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/SHTransform.h b/SHADE_Engine/src/Math/Transform/SHTransform.h similarity index 73% rename from SHADE_Engine/src/Math/SHTransform.h rename to SHADE_Engine/src/Math/Transform/SHTransform.h index 8908b8eb..4256bf83 100644 --- a/SHADE_Engine/src/Math/SHTransform.h +++ b/SHADE_Engine/src/Math/Transform/SHTransform.h @@ -12,7 +12,7 @@ // Project Headers #include "SH_API.h" -#include "SHMath.h" +#include "Math/SHMath.h" namespace SHADE { @@ -39,13 +39,20 @@ namespace SHADE SHTransform (const SHTransform&) = default; SHTransform (SHTransform&&) = default; + ~SHTransform () = default; + + SHTransform () noexcept; + SHTransform (const SHVec3& pos, const SHVec3& rot, const SHVec3& scl) noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*---------------------------------------------------------------------------------*/ SHTransform& operator= (const SHTransform&) = default; SHTransform& operator= (SHTransform&&) = default; - ~SHTransform () = default; - - SHTransform () noexcept; + [[nodiscard]] bool operator==(const SHTransform& rhs) const noexcept; + [[nodiscard]] bool operator!=(const SHTransform& rhs) const noexcept; /*---------------------------------------------------------------------------------*/ /* Getter Functions */ @@ -60,6 +67,10 @@ namespace SHADE const SHMatrix& ComputeTRS(); private: + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + SHMatrix trs; }; diff --git a/SHADE_Engine/src/Math/Transform/SHTransformComponent.cpp b/SHADE_Engine/src/Math/Transform/SHTransformComponent.cpp new file mode 100644 index 00000000..29b8b0bb --- /dev/null +++ b/SHADE_Engine/src/Math/Transform/SHTransformComponent.cpp @@ -0,0 +1,129 @@ +/**************************************************************************************** + * \file SHTransformComponent.cpp + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for a Transform Component + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#include + +// Primary Header +#include "SHTransformComponent.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Constructors & Destructor Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHTransformComponent::SHTransformComponent() noexcept + : SHComponent {} + , parent { nullptr } + {} + + /*-----------------------------------------------------------------------------------*/ + /* Getter Function Definitions */ + /*-----------------------------------------------------------------------------------*/ + + const SHTransform* SHTransformComponent::GetParent() const noexcept + { + return parent; + } + + const SHVec3& SHTransformComponent::GetLocalPosition() const noexcept + { + return local.position; + } + + const SHVec3& SHTransformComponent::GetLocalRotation() const noexcept + { + return local.rotation; + } + + const SHVec3& SHTransformComponent::GetLocalScale() const noexcept + { + return local.scale; + } + + const SHVec3& SHTransformComponent::GetWorldPosition() const noexcept + { + return world.position; + } + + const SHVec3& SHTransformComponent::GetWorldRotation() const noexcept + { + return world.rotation; + } + + const SHVec3& SHTransformComponent::GetWorldScale() const noexcept + { + return world.scale; + } + + SHMatrix SHTransformComponent::GetLocalToWorld() const noexcept + { + return parent != nullptr ? parent->GetTRS() : SHMatrix::Identity; + } + + SHMatrix SHTransformComponent::GetWorldToLocal() const noexcept + { + return parent != nullptr ? SHMatrix::Inverse(parent->GetTRS()) : SHMatrix::Identity; + } + + const SHMatrix& SHTransformComponent::GetTRS() const noexcept + { + return world.GetTRS(); + } + + /*-----------------------------------------------------------------------------------*/ + /* Setter Function Definitions */ + /*-----------------------------------------------------------------------------------*/ + + void SHTransformComponent::SetParent(const SHTransform* parentTransform) noexcept + { + if (parent == nullptr) + SHLOG_WARNING("Removing parent transform from Entity {}", GetEID()) + + parent = parentTransform; + } + + void SHTransformComponent::SetLocalPosition(const SHVec3& newLocalPosition) noexcept + { + local.position = newLocalPosition; + updateQueue.push({ UpdateCommandType::LOCAL_POSITION, newLocalPosition }); + } + + void SHTransformComponent::SetLocalRotation(const SHVec3& newLocalRotation) noexcept + { + local.rotation = newLocalRotation; + updateQueue.push({ UpdateCommandType::LOCAL_ROTATION, newLocalRotation }); + } + + void SHTransformComponent::SetLocalScale(const SHVec3& newLocalScale) noexcept + { + local.scale = newLocalScale; + updateQueue.push({ UpdateCommandType::LOCAL_SCALE, newLocalScale }); + } + + void SHTransformComponent::SetWorldPosition(const SHVec3& newWorldPosition) noexcept + { + world.position = newWorldPosition; + updateQueue.push({ UpdateCommandType::WORLD_POSITION, newWorldPosition }); + } + + void SHTransformComponent::SetWorldRotation(const SHVec3& newWorldRotation) noexcept + { + world.rotation = newWorldRotation; + updateQueue.push({ UpdateCommandType::WORLD_ROTATION, newWorldRotation }); + } + + void SHTransformComponent::SetWorldScale(const SHVec3& newWorldScale) noexcept + { + world.scale = newWorldScale; + updateQueue.push({ UpdateCommandType::WORLD_SCALE, newWorldScale }); + } + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Transform/SHTransformComponent.h b/SHADE_Engine/src/Math/Transform/SHTransformComponent.h new file mode 100644 index 00000000..eeaf758f --- /dev/null +++ b/SHADE_Engine/src/Math/Transform/SHTransformComponent.h @@ -0,0 +1,115 @@ +/**************************************************************************************** + * \file SHTransformComponent.h + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Interface for a Transform Component + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#pragma once + +#include + +// Project Headers +#include "SH_API.h" +#include "ECS_Base/Components/SHComponent.h" +#include "SHTransform.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + + class SH_API SHTransformComponent : public SHComponent + { + public: + + /*---------------------------------------------------------------------------------*/ + /* Constructors & Destructor */ + /*---------------------------------------------------------------------------------*/ + + ~SHTransformComponent () override = default; + + SHTransformComponent () noexcept; + + // TODO(Diren): Do I implement these in a specific manner or delete? + SHTransformComponent (const SHTransformComponent&) = delete; + SHTransformComponent (SHTransformComponent&&) = delete; + SHTransformComponent& operator=(const SHTransformComponent&) = delete; + SHTransformComponent& operator=(SHTransformComponent&&) = delete; + + /*---------------------------------------------------------------------------------*/ + /* Getter Functions */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] const SHTransform* GetParent () const noexcept; + + [[nodiscard]] const SHVec3& GetLocalPosition () const noexcept; + [[nodiscard]] const SHVec3& GetLocalRotation () const noexcept; + [[nodiscard]] const SHVec3& GetLocalScale () const noexcept; + [[nodiscard]] const SHVec3& GetWorldPosition () const noexcept; + [[nodiscard]] const SHVec3& GetWorldRotation () const noexcept; + [[nodiscard]] const SHVec3& GetWorldScale () const noexcept; + + [[nodiscard]] SHMatrix GetLocalToWorld () const noexcept; + [[nodiscard]] SHMatrix GetWorldToLocal () const noexcept; + + [[nodiscard]] const SHMatrix& GetTRS () const noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Setter Functions */ + /*---------------------------------------------------------------------------------*/ + + void SetParent (const SHTransform* parentTransform) noexcept; + + void SetLocalPosition (const SHVec3& newLocalPosition) noexcept; + void SetLocalRotation (const SHVec3& newLocalRotation) noexcept; + void SetLocalScale (const SHVec3& newLocalScale) noexcept; + void SetWorldPosition (const SHVec3& newWorldPosition) noexcept; + void SetWorldRotation (const SHVec3& newWorldRotation) noexcept; + void SetWorldScale (const SHVec3& newWorldScale) noexcept; + + private: + /*---------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*---------------------------------------------------------------------------------*/ + + enum class UpdateCommandType + { + LOCAL_POSITION + , LOCAL_ROTATION + , LOCAL_SCALE + , WORLD_POSITION + , WORLD_ROTATION + , WORLD_SCALE + }; + + struct UpdateCommand + { + public: + /*-------------------------------------------------------------------------------*/ + /* Data Members */ + /*-------------------------------------------------------------------------------*/ + + UpdateCommandType type; + SHVec3 data; + }; + + using UpdateQueue = std::queue; + + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + + const SHTransform* parent; + SHTransform local; + SHTransform world; + + UpdateQueue updateQueue; + }; + + +} // namespace SHADE \ No newline at end of file From 9c69d41a4e8e0d870e7c189491b011442b07dc95 Mon Sep 17 00:00:00 2001 From: Cocoa Date: Wed, 21 Sep 2022 00:49:52 +0800 Subject: [PATCH 6/8] Added 90% of transform component & transform system. --- SHADE_Engine/SHADE_Engine.vcxproj | 2 + SHADE_Engine/SHADE_Engine.vcxproj.filters | 2 + .../src/Math/Transform/SHTransform.cpp | 9 -- SHADE_Engine/src/Math/Transform/SHTransform.h | 20 +-- .../Math/Transform/SHTransformComponent.cpp | 53 +++++--- .../src/Math/Transform/SHTransformComponent.h | 30 +++-- .../src/Math/Transform/SHTransformSystem.cpp | 121 ++++++++++++++++++ .../src/Math/Transform/SHTransformSystem.h | 57 +++++++++ 8 files changed, 239 insertions(+), 55 deletions(-) create mode 100644 SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp create mode 100644 SHADE_Engine/src/Math/Transform/SHTransformSystem.h diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index 235ce56a..37ddf8f5 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -211,6 +211,7 @@ xcopy /s /r /y /q "$(SolutionDir)/Dependencies/dotnet/bin" "$(OutDir)" + @@ -307,6 +308,7 @@ xcopy /s /r /y /q "$(SolutionDir)/Dependencies/dotnet/bin" "$(OutDir)" + diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index 7e279e59..f52aebb0 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -499,6 +499,7 @@ Tools + @@ -740,5 +741,6 @@ Tools + \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Transform/SHTransform.cpp b/SHADE_Engine/src/Math/Transform/SHTransform.cpp index e1b8cf2b..f51d73ec 100644 --- a/SHADE_Engine/src/Math/Transform/SHTransform.cpp +++ b/SHADE_Engine/src/Math/Transform/SHTransform.cpp @@ -51,15 +51,6 @@ namespace SHADE return (position != rhs.position || rotation != rhs.rotation || scale != rhs.scale); } - /*-----------------------------------------------------------------------------------*/ - /* Getter Function Definitions */ - /*-----------------------------------------------------------------------------------*/ - - const SHMatrix& SHTransform::GetTRS() const - { - return trs; - } - /*-----------------------------------------------------------------------------------*/ /* Public Function Member Definitions */ /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/Transform/SHTransform.h b/SHADE_Engine/src/Math/Transform/SHTransform.h index 4256bf83..9edd7fdd 100644 --- a/SHADE_Engine/src/Math/Transform/SHTransform.h +++ b/SHADE_Engine/src/Math/Transform/SHTransform.h @@ -29,9 +29,11 @@ namespace SHADE static const SHTransform Identity; - SHVec3 position; - SHVec3 rotation; - SHVec3 scale; + SHVec3 position; + SHVec3 rotation; + SHVec3 scale; + + SHMatrix trs; /*---------------------------------------------------------------------------------*/ /* Constructors & Destructor */ @@ -54,24 +56,12 @@ namespace SHADE [[nodiscard]] bool operator==(const SHTransform& rhs) const noexcept; [[nodiscard]] bool operator!=(const SHTransform& rhs) const noexcept; - /*---------------------------------------------------------------------------------*/ - /* Getter Functions */ - /*---------------------------------------------------------------------------------*/ - - [[nodiscard]] const SHMatrix& GetTRS() const; - /*---------------------------------------------------------------------------------*/ /* Function Members */ /*---------------------------------------------------------------------------------*/ const SHMatrix& ComputeTRS(); - private: - /*---------------------------------------------------------------------------------*/ - /* Data Members */ - /*---------------------------------------------------------------------------------*/ - - SHMatrix trs; }; } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Transform/SHTransformComponent.cpp b/SHADE_Engine/src/Math/Transform/SHTransformComponent.cpp index 29b8b0bb..af56b67a 100644 --- a/SHADE_Engine/src/Math/Transform/SHTransformComponent.cpp +++ b/SHADE_Engine/src/Math/Transform/SHTransformComponent.cpp @@ -21,16 +21,15 @@ namespace SHADE SHTransformComponent::SHTransformComponent() noexcept : SHComponent {} - , parent { nullptr } {} /*-----------------------------------------------------------------------------------*/ /* Getter Function Definitions */ /*-----------------------------------------------------------------------------------*/ - const SHTransform* SHTransformComponent::GetParent() const noexcept + bool SHTransformComponent::HasChanged() const noexcept { - return parent; + return dirty; } const SHVec3& SHTransformComponent::GetLocalPosition() const noexcept @@ -63,65 +62,83 @@ namespace SHADE return world.scale; } - SHMatrix SHTransformComponent::GetLocalToWorld() const noexcept + const SHMatrix& SHTransformComponent::GetLocalToWorld() const noexcept { - return parent != nullptr ? parent->GetTRS() : SHMatrix::Identity; + return local.trs; } SHMatrix SHTransformComponent::GetWorldToLocal() const noexcept { - return parent != nullptr ? SHMatrix::Inverse(parent->GetTRS()) : SHMatrix::Identity; + return SHMatrix::Inverse(local.trs); } const SHMatrix& SHTransformComponent::GetTRS() const noexcept { - return world.GetTRS(); + return world.trs; } /*-----------------------------------------------------------------------------------*/ /* Setter Function Definitions */ /*-----------------------------------------------------------------------------------*/ - void SHTransformComponent::SetParent(const SHTransform* parentTransform) noexcept - { - if (parent == nullptr) - SHLOG_WARNING("Removing parent transform from Entity {}", GetEID()) - - parent = parentTransform; - } - void SHTransformComponent::SetLocalPosition(const SHVec3& newLocalPosition) noexcept { + dirty = true; local.position = newLocalPosition; - updateQueue.push({ UpdateCommandType::LOCAL_POSITION, newLocalPosition }); } void SHTransformComponent::SetLocalRotation(const SHVec3& newLocalRotation) noexcept { + dirty = true; local.rotation = newLocalRotation; - updateQueue.push({ UpdateCommandType::LOCAL_ROTATION, newLocalRotation }); + } + + void SHTransformComponent::SetLocalRotation(float pitch, float yaw, float roll) noexcept + { + dirty = true; + + local.rotation.x = pitch; + local.rotation.y = yaw; + local.rotation.z = roll; } void SHTransformComponent::SetLocalScale(const SHVec3& newLocalScale) noexcept { + dirty = true; local.scale = newLocalScale; - updateQueue.push({ UpdateCommandType::LOCAL_SCALE, newLocalScale }); } void SHTransformComponent::SetWorldPosition(const SHVec3& newWorldPosition) noexcept { + dirty = true; + world.position = newWorldPosition; updateQueue.push({ UpdateCommandType::WORLD_POSITION, newWorldPosition }); } void SHTransformComponent::SetWorldRotation(const SHVec3& newWorldRotation) noexcept { + dirty = true; + world.rotation = newWorldRotation; updateQueue.push({ UpdateCommandType::WORLD_ROTATION, newWorldRotation }); } + void SHTransformComponent::SetWorldRotation(float pitch, float yaw, float roll) noexcept + { + dirty = true; + + world.rotation.x = pitch; + world.rotation.y = yaw; + world.rotation.z = roll; + + updateQueue.push({ UpdateCommandType::WORLD_ROTATION, SHVec3{ pitch, yaw, roll} }); + } + void SHTransformComponent::SetWorldScale(const SHVec3& newWorldScale) noexcept { + dirty = true; + world.scale = newWorldScale; updateQueue.push({ UpdateCommandType::WORLD_SCALE, newWorldScale }); } diff --git a/SHADE_Engine/src/Math/Transform/SHTransformComponent.h b/SHADE_Engine/src/Math/Transform/SHTransformComponent.h index eeaf758f..c3fa8a01 100644 --- a/SHADE_Engine/src/Math/Transform/SHTransformComponent.h +++ b/SHADE_Engine/src/Math/Transform/SHTransformComponent.h @@ -25,8 +25,14 @@ namespace SHADE class SH_API SHTransformComponent : public SHComponent { - public: + private: + /*---------------------------------------------------------------------------------*/ + /* Friends */ + /*---------------------------------------------------------------------------------*/ + friend class SHTransformSystem; + + public: /*---------------------------------------------------------------------------------*/ /* Constructors & Destructor */ /*---------------------------------------------------------------------------------*/ @@ -45,7 +51,7 @@ namespace SHADE /* Getter Functions */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] const SHTransform* GetParent () const noexcept; + [[nodiscard]] bool HasChanged () const noexcept; [[nodiscard]] const SHVec3& GetLocalPosition () const noexcept; [[nodiscard]] const SHVec3& GetLocalRotation () const noexcept; @@ -54,7 +60,7 @@ namespace SHADE [[nodiscard]] const SHVec3& GetWorldRotation () const noexcept; [[nodiscard]] const SHVec3& GetWorldScale () const noexcept; - [[nodiscard]] SHMatrix GetLocalToWorld () const noexcept; + [[nodiscard]] const SHMatrix& GetLocalToWorld () const noexcept; [[nodiscard]] SHMatrix GetWorldToLocal () const noexcept; [[nodiscard]] const SHMatrix& GetTRS () const noexcept; @@ -63,13 +69,13 @@ namespace SHADE /* Setter Functions */ /*---------------------------------------------------------------------------------*/ - void SetParent (const SHTransform* parentTransform) noexcept; - void SetLocalPosition (const SHVec3& newLocalPosition) noexcept; void SetLocalRotation (const SHVec3& newLocalRotation) noexcept; + void SetLocalRotation (float pitch, float yaw, float roll) noexcept; void SetLocalScale (const SHVec3& newLocalScale) noexcept; void SetWorldPosition (const SHVec3& newWorldPosition) noexcept; void SetWorldRotation (const SHVec3& newWorldRotation) noexcept; + void SetWorldRotation (float pitch, float yaw, float roll) noexcept; void SetWorldScale (const SHVec3& newWorldScale) noexcept; private: @@ -79,10 +85,7 @@ namespace SHADE enum class UpdateCommandType { - LOCAL_POSITION - , LOCAL_ROTATION - , LOCAL_SCALE - , WORLD_POSITION + WORLD_POSITION , WORLD_ROTATION , WORLD_SCALE }; @@ -104,11 +107,12 @@ namespace SHADE /* Data Members */ /*---------------------------------------------------------------------------------*/ - const SHTransform* parent; - SHTransform local; - SHTransform world; + bool dirty; - UpdateQueue updateQueue; + SHTransform local; // Local TRS holds Local To World Transform + SHTransform world; + + UpdateQueue updateQueue; }; diff --git a/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp b/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp new file mode 100644 index 00000000..b9701020 --- /dev/null +++ b/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp @@ -0,0 +1,121 @@ +/**************************************************************************************** + * \file SHTransformSystem.cpp + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for the Transform System + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#include + +// Primary Header +#include "SHTransformSystem.h" + +// Project Headers +#include "Scene/SHSceneManager.h" +#include "ECS_Base/Managers/SHComponentManager.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Constructors & Destructor Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHTransformSystem::SHTransformSystem() + : SHSystemRoutine { "Transform Routine", false } + {} + + + /*-----------------------------------------------------------------------------------*/ + /* Public Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + void SHTransformSystem::Execute(double dt) noexcept + { + // Get the current scene graph to traverse and update + auto& sceneGraph = SHSceneManager::GetCurrentSceneGraph(); + UpdateEntity(sceneGraph.GetRoot()); + + // Clear all dirty flags + } + + /*-----------------------------------------------------------------------------------*/ + /* Private Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + void SHTransformSystem::UpdateEntity(const SHSceneNode* node) + { + const auto* NODE_TRANSFORM = SHComponentManager::GetComponent_s(node->GetEntityID()); + const bool HAS_PARENT_CHANGED = NODE_TRANSFORM && NODE_TRANSFORM->dirty; + + for (const auto* child : node->GetChildren()) + { + const bool HAS_TRANSFORM = SHComponentManager::HasComponent(child->GetEntityID()); + if (!HAS_TRANSFORM) + continue; + + auto* childTransform = SHComponentManager::GetComponent(child->GetEntityID()); + + if (childTransform->dirty || HAS_PARENT_CHANGED) + UpdateTransform(*childTransform, NODE_TRANSFORM); + + UpdateEntity(child); + } + } + + void SHTransformSystem::UpdateTransform(SHTransformComponent& tf, const SHTransformComponent* parent) + { + SHMatrix localToWorld = SHMatrix::Identity; + SHMatrix worldToLocal = SHMatrix::Identity; + + if (parent) + { + localToWorld = parent->GetTRS(); + worldToLocal = SHMatrix::Inverse(tf.local.trs); + } + + while (!tf.updateQueue.empty()) + { + const auto& UPDATE_COMMAND = tf.updateQueue.front(); + + switch (UPDATE_COMMAND.type) + { + case SHTransformComponent::UpdateCommandType::WORLD_POSITION: + { + tf.local.position = SHVec3::Transform(UPDATE_COMMAND.data, worldToLocal); + break; + } + case SHTransformComponent::UpdateCommandType::WORLD_ROTATION: + { + tf.local.rotation = tf.world.rotation; + if (parent) + tf.local.rotation -= parent->GetLocalRotation(); + + break; + } + case SHTransformComponent::UpdateCommandType::WORLD_SCALE: + { + tf.local.scale = tf.world.scale; + if (parent) + tf.local.scale /= parent->GetLocalScale(); + + break; + } + default: break; // Redundant + } + + tf.updateQueue.pop(); + } + + tf.local.trs = localToWorld; + + tf.world.position = SHVec3::Transform(tf.local.position, localToWorld); + tf.world.rotation = tf.local.rotation + (parent ? parent->GetLocalRotation() : SHVec3::Zero); + tf.world.scale = tf.local.scale * (parent ? parent->GetLocalScale() : SHVec3::One); + + tf.world.ComputeTRS(); + } + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Transform/SHTransformSystem.h b/SHADE_Engine/src/Math/Transform/SHTransformSystem.h new file mode 100644 index 00000000..516bd209 --- /dev/null +++ b/SHADE_Engine/src/Math/Transform/SHTransformSystem.h @@ -0,0 +1,57 @@ +/**************************************************************************************** + * \file SHTransformSystem.h + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Interface for the Transform System + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#pragma once + +// Project Headers +#include "SHTransformComponent.h" +#include "Scene/SHSceneGraph.h" +#include "ECS_Base/System/SHSystemRoutine.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + + class SH_API SHTransformSystem : public SHSystemRoutine + { + public: + + /*---------------------------------------------------------------------------------*/ + /* Constructors & Destructor */ + /*---------------------------------------------------------------------------------*/ + + SHTransformSystem (); + ~SHTransformSystem () = default; + + SHTransformSystem (const SHTransformSystem&) = delete; + SHTransformSystem (SHTransformSystem&&) = delete; + + SHTransformSystem& operator= (const SHTransformSystem&) = delete; + SHTransformSystem& operator= (SHTransformSystem&&) = delete; + + /*---------------------------------------------------------------------------------*/ + /* Function Members */ + /*---------------------------------------------------------------------------------*/ + + void Execute(double dt) noexcept override; + + private: + /*---------------------------------------------------------------------------------*/ + /* Function Members */ + /*---------------------------------------------------------------------------------*/ + + static void UpdateEntity (const SHSceneNode* node); + static void UpdateTransform(SHTransformComponent& tf, const SHTransformComponent* parent = nullptr); + }; + + +} // namespace SHADE \ No newline at end of file From 415e47780ca2d129df5598fa517148b9556a29a7 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Wed, 21 Sep 2022 14:54:57 +0800 Subject: [PATCH 7/8] Completed Transform System --- .../Math/Transform/SHTransformComponent.cpp | 38 +++++++++++++++++++ .../src/Math/Transform/SHTransformComponent.h | 17 +++++---- .../src/Math/Transform/SHTransformSystem.cpp | 4 +- .../src/Math/Transform/SHTransformSystem.h | 4 ++ 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/SHADE_Engine/src/Math/Transform/SHTransformComponent.cpp b/SHADE_Engine/src/Math/Transform/SHTransformComponent.cpp index af56b67a..cdc5105f 100644 --- a/SHADE_Engine/src/Math/Transform/SHTransformComponent.cpp +++ b/SHADE_Engine/src/Math/Transform/SHTransformComponent.cpp @@ -21,8 +21,46 @@ namespace SHADE SHTransformComponent::SHTransformComponent() noexcept : SHComponent {} + , dirty { true } {} + SHTransformComponent::SHTransformComponent(const SHTransformComponent& rhs) noexcept + : SHComponent {} + , dirty { true } + , local { rhs.local } + , world { rhs.world } + {} + + SHTransformComponent::SHTransformComponent(SHTransformComponent&& rhs) noexcept + : SHComponent {} + , dirty { true } + , local { std::move(rhs.local) } + , world { std::move(rhs.world) } + {} + + /*-----------------------------------------------------------------------------------*/ + /* Operator Overload Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHTransformComponent& SHTransformComponent::operator=(const SHTransformComponent& rhs) noexcept + { + dirty = true; + local = rhs.local; + world = rhs.world; + + return *this; + } + + SHTransformComponent& SHTransformComponent::operator=(SHTransformComponent&& rhs) noexcept + { + dirty = true; + local = std::move(rhs.local); + world = std::move(rhs.world); + + return *this; + } + + /*-----------------------------------------------------------------------------------*/ /* Getter Function Definitions */ /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/Transform/SHTransformComponent.h b/SHADE_Engine/src/Math/Transform/SHTransformComponent.h index c3fa8a01..7f7dd473 100644 --- a/SHADE_Engine/src/Math/Transform/SHTransformComponent.h +++ b/SHADE_Engine/src/Math/Transform/SHTransformComponent.h @@ -37,15 +37,18 @@ namespace SHADE /* Constructors & Destructor */ /*---------------------------------------------------------------------------------*/ - ~SHTransformComponent () override = default; + ~SHTransformComponent () override = default; - SHTransformComponent () noexcept; + SHTransformComponent () noexcept; + SHTransformComponent (const SHTransformComponent& rhs) noexcept; + SHTransformComponent (SHTransformComponent&& rhs) noexcept; - // TODO(Diren): Do I implement these in a specific manner or delete? - SHTransformComponent (const SHTransformComponent&) = delete; - SHTransformComponent (SHTransformComponent&&) = delete; - SHTransformComponent& operator=(const SHTransformComponent&) = delete; - SHTransformComponent& operator=(SHTransformComponent&&) = delete; + /*---------------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*---------------------------------------------------------------------------------*/ + + SHTransformComponent& operator=(const SHTransformComponent& rhs) noexcept; + SHTransformComponent& operator=(SHTransformComponent&& rhs) noexcept; /*---------------------------------------------------------------------------------*/ /* Getter Functions */ diff --git a/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp b/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp index b9701020..6a1daff2 100644 --- a/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp +++ b/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp @@ -37,8 +37,6 @@ namespace SHADE // Get the current scene graph to traverse and update auto& sceneGraph = SHSceneManager::GetCurrentSceneGraph(); UpdateEntity(sceneGraph.GetRoot()); - - // Clear all dirty flags } /*-----------------------------------------------------------------------------------*/ @@ -62,6 +60,8 @@ namespace SHADE UpdateTransform(*childTransform, NODE_TRANSFORM); UpdateEntity(child); + + childTransform->dirty = false; } } diff --git a/SHADE_Engine/src/Math/Transform/SHTransformSystem.h b/SHADE_Engine/src/Math/Transform/SHTransformSystem.h index 516bd209..5eebd292 100644 --- a/SHADE_Engine/src/Math/Transform/SHTransformSystem.h +++ b/SHADE_Engine/src/Math/Transform/SHTransformSystem.h @@ -35,6 +35,10 @@ namespace SHADE SHTransformSystem (const SHTransformSystem&) = delete; SHTransformSystem (SHTransformSystem&&) = delete; + /*---------------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*---------------------------------------------------------------------------------*/ + SHTransformSystem& operator= (const SHTransformSystem&) = delete; SHTransformSystem& operator= (SHTransformSystem&&) = delete; From 14eed4c7260acaa792d48c080f4349a7ffd0a52b Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Wed, 21 Sep 2022 18:30:20 +0800 Subject: [PATCH 8/8] Added missing active check in transform system --- .../src/Math/Transform/SHTransformSystem.cpp | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp b/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp index 6a1daff2..879c2d34 100644 --- a/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp +++ b/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp @@ -16,6 +16,8 @@ // Project Headers #include "Scene/SHSceneManager.h" #include "ECS_Base/Managers/SHComponentManager.h" +#include "ECS_Base/Managers/SHEntityManager.h" +#include "Tools/SHException.h" namespace SHADE { @@ -35,8 +37,8 @@ namespace SHADE void SHTransformSystem::Execute(double dt) noexcept { // Get the current scene graph to traverse and update - auto& sceneGraph = SHSceneManager::GetCurrentSceneGraph(); - UpdateEntity(sceneGraph.GetRoot()); + const auto& SCENE_GRAPH = SHSceneManager::GetCurrentSceneGraph(); + UpdateEntity(SCENE_GRAPH.GetRoot()); } /*-----------------------------------------------------------------------------------*/ @@ -50,6 +52,20 @@ namespace SHADE for (const auto* child : node->GetChildren()) { + // Active states of entities should sync with scene nodes + const bool IS_NODE_ACTIVE = child->isActive; + + #ifdef _DEBUG + const bool IS_ENTITY_ACTIVE = SHEntityManager::GetEntityByID(child->GetEntityID())->GetActive(); + SHASSERT(IS_NODE_ACTIVE == IS_ENTITY_ACTIVE, "Entity and Node active states are not synced!") + #endif + + if (!IS_NODE_ACTIVE) + { + UpdateEntity(child); + continue; + } + const bool HAS_TRANSFORM = SHComponentManager::HasComponent(child->GetEntityID()); if (!HAS_TRANSFORM) continue; @@ -61,13 +77,14 @@ namespace SHADE UpdateEntity(child); + // Clear dirty flag after all children are updated childTransform->dirty = false; } } void SHTransformSystem::UpdateTransform(SHTransformComponent& tf, const SHTransformComponent* parent) { - SHMatrix localToWorld = SHMatrix::Identity; + SHMatrix localToWorld = SHMatrix::Identity; SHMatrix worldToLocal = SHMatrix::Identity; if (parent) @@ -103,7 +120,8 @@ namespace SHADE break; } - default: break; // Redundant + // Redundant + default: break; } tf.updateQueue.pop();