From b9ada6a5be49d3e696d20b990bd662a3fcba61c1 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Wed, 2 Nov 2022 00:47:19 +0800 Subject: [PATCH] Reworked SHColour to be compatible with SHVec4 --- SHADE_Application/src/Scenes/SBTestScene.cpp | 3 +- SHADE_Engine/src/Math/SHColour.cpp | 172 ++++++++++++++++++- SHADE_Engine/src/Math/SHColour.h | 50 ++++-- SHADE_Engine/src/Math/SHMath.h | 2 + SHADE_Engine/src/Math/Vector/SHVec4.cpp | 1 + SHADE_Engine/src/Math/Vector/SHVec4.h | 2 + 6 files changed, 206 insertions(+), 24 deletions(-) diff --git a/SHADE_Application/src/Scenes/SBTestScene.cpp b/SHADE_Application/src/Scenes/SBTestScene.cpp index 52f2dc7b..db58c21e 100644 --- a/SHADE_Application/src/Scenes/SBTestScene.cpp +++ b/SHADE_Application/src/Scenes/SBTestScene.cpp @@ -16,6 +16,7 @@ #include "Assets/SHAssetManager.h" #include "Camera/SHCameraComponent.h" +#include "Math/SHColour.h" #include "Resource/SHResourceManager.h" using namespace SHADE; @@ -159,7 +160,7 @@ namespace Sandbox SHComponentManager::RemoveComponent (0); auto ambientLight = SHEntityManager::CreateEntity(); - SHComponentManager::GetComponent(ambientLight)->SetColor(SHVec4(1.0f, 1.0f, 1.0f, 1.0f)); + SHComponentManager::GetComponent(ambientLight)->SetColor(SHColour::WHITE); SHComponentManager::GetComponent(ambientLight)->SetStrength(0.25f); SHComponentManager::GetComponent(ambientLight)->SetType(SH_LIGHT_TYPE::AMBIENT); } diff --git a/SHADE_Engine/src/Math/SHColour.cpp b/SHADE_Engine/src/Math/SHColour.cpp index 8aae2cb3..944c37cb 100644 --- a/SHADE_Engine/src/Math/SHColour.cpp +++ b/SHADE_Engine/src/Math/SHColour.cpp @@ -87,19 +87,19 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ SHColour::SHColour() noexcept - : SHVec4 { 0.0f, 0.0f, 0.0f, 1.0f } + : XMFLOAT4 { 0.0f, 0.0f, 0.0f, 1.0f } {} SHColour::SHColour(float r, float g, float b) noexcept - : SHVec4 { r, g, b, 1.0f } + : XMFLOAT4 { r, g, b, 1.0f } {} SHColour::SHColour(float r, float g, float b, float a) noexcept - : SHVec4 { r, g, b, a } + : XMFLOAT4 { r, g, b, a } {} SHColour::SHColour(uint8_t r, uint8_t g, uint8_t b) noexcept - : SHVec4 + : XMFLOAT4 { static_cast(r) / 255.0f, static_cast(g) / 255.0f, @@ -109,7 +109,7 @@ namespace SHADE {} SHColour::SHColour(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept - : SHVec4 + : XMFLOAT4 { static_cast(r) / 255.0f, static_cast(g) / 255.0f, @@ -118,12 +118,24 @@ namespace SHADE } {} - SHColour::SHColour(const DirectX::XMFLOAT3& colour) noexcept - : SHVec4 { colour.x, colour.y, colour.z, 1.0f } + SHColour::SHColour(const SHVec3& rgb) noexcept + : XMFLOAT4 { rgb.x, rgb.y, rgb.z, 1.0f } + {} + + SHColour::SHColour(const SHVec4& rgba) noexcept + : XMFLOAT4 { rgba.x, rgba.y, rgba.z, rgba.w } + {} + + SHColour::SHColour(const DirectX::XMFLOAT3& rgb) noexcept + : XMFLOAT4 { rgb.x, rgb.y, rgb.z, 1.0f } + {} + + SHColour::SHColour(const DirectX::XMFLOAT4& rgba) noexcept + : XMFLOAT4 { rgba.x, rgba.y, rgba.z, rgba.w } {} SHColour::SHColour(const DirectX::XMVECTORF32& colour) noexcept - : SHVec4 + : XMFLOAT4 { XMVectorGetX(colour), XMVectorGetY(colour), @@ -136,6 +148,86 @@ namespace SHADE /* Operator Overload Definitions */ /*-----------------------------------------------------------------------------------*/ + SHColour::operator XMVECTOR() const noexcept + { + return XMLoadFloat4(this); + } + + SHColour::operator SHVec4() const noexcept + { + return SHVec4{ *this }; + } + + SHColour& SHColour::operator+=(const SHColour& rhs) noexcept + { + return *this = *this + rhs; + } + + SHColour& SHColour::operator-=(const SHColour& rhs) noexcept + { + return *this = *this - rhs; + } + + SHColour& SHColour::operator*=(const SHColour& rhs) noexcept + { + return *this = *this * rhs; + } + + SHColour& SHColour::operator*=(float rhs) noexcept + { + return *this = *this * rhs; + } + + SHColour& SHColour::operator/=(const SHColour& rhs) noexcept + { + return *this = *this / rhs; + } + + SHColour SHColour::operator+(const SHColour& rhs) const noexcept + { + SHColour result; + + XMStoreFloat4(&result, XMVectorAdd(*this, rhs)); + return result; + } + + SHColour SHColour::operator-(const SHColour& rhs) const noexcept + { + SHColour result; + + XMStoreFloat4(&result, XMVectorSubtract(*this, rhs)); + return result; + } + + SHColour SHColour::operator-() const noexcept + { + return SHColour{ -x, -y, -z, -w }; + } + + SHColour SHColour::operator*(const SHColour& rhs) const noexcept + { + SHColour result; + + XMStoreFloat4(&result, XMVectorMultiply(*this, rhs)); + return result; + } + + SHColour SHColour::operator*(float rhs) const noexcept + { + SHColour result; + + XMStoreFloat4(&result, XMVectorScale(*this, rhs)); + return result; + } + + SHColour SHColour::operator/(const SHColour& rhs) const noexcept + { + SHColour result; + + XMStoreFloat4(&result, XMVectorDivide(*this, rhs)); + return result; + } + bool SHColour::operator==(const SHColour& rhs) const noexcept { return XMColorEqual(*this, rhs); @@ -146,6 +238,70 @@ namespace SHADE return XMColorNotEqual(*this, rhs); } + float& SHColour::operator[](int index) + { + if (index >= SIZE || index < 0) + throw std::invalid_argument("Index out of range!"); + + switch (index) + { + case 0: return x; + case 1: return y; + case 2: return z; + case 3: return w; + } + } + + float& SHColour::operator[](size_t index) + { + if (index >= SIZE || index < 0) + throw std::invalid_argument("Index out of range!"); + + switch (index) + { + case 0: return x; + case 1: return y; + case 2: return z; + case 3: return w; + } + } + + float SHColour::operator[](int index) const + { + if (index >= SIZE || index < 0) + throw std::invalid_argument("Index out of range!"); + + switch (index) + { + case 0: return x; + case 1: return y; + case 2: return z; + case 3: return w; + } + } + + float SHColour::operator[](size_t index) const + { + if (index >= SIZE || index < 0) + throw std::invalid_argument("Index out of range!"); + + switch (index) + { + case 0: return x; + case 1: return y; + case 2: return z; + case 3: return w; + } + } + + SHColour operator* (float lhs, const SHColour& rhs) noexcept + { + SHColour result; + + XMStoreFloat4(&result, XMVectorScale(rhs, lhs)); + return result; + } + /*-----------------------------------------------------------------------------------*/ /* Function Member Definitions */ /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/SHColour.h b/SHADE_Engine/src/Math/SHColour.h index bd2bc9e7..a6adf7bb 100644 --- a/SHADE_Engine/src/Math/SHColour.h +++ b/SHADE_Engine/src/Math/SHColour.h @@ -19,12 +19,6 @@ namespace SHADE { - /*-----------------------------------------------------------------------------------*/ - /* Forward Declarations */ - /*-----------------------------------------------------------------------------------*/ - - class SHColour; - /*-----------------------------------------------------------------------------------*/ /* Type Definitions */ /*-----------------------------------------------------------------------------------*/ @@ -40,7 +34,7 @@ namespace SHADE float v = 0.0f; }; - class SH_API SHColour : private SHVec4 + class SH_API SHColour : public DirectX::XMFLOAT4 { public: /*---------------------------------------------------------------------------------*/ @@ -53,9 +47,11 @@ namespace SHADE SHColour (uint8_t r, uint8_t g, uint8_t b) noexcept; SHColour (uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept; - SHColour (const SHVec3& colour) noexcept; - SHColour (const DirectX::XMFLOAT3& colour) noexcept; - SHColour (const DirectX::XMVECTORF32& colour) noexcept; + SHColour (const SHVec3& rgb) noexcept; + SHColour (const SHVec4& rgba) noexcept; + SHColour (const DirectX::XMFLOAT3& rgb) noexcept; + SHColour (const DirectX::XMFLOAT4& rgba) noexcept; + SHColour (const DirectX::XMVECTORF32& rgba) noexcept; SHColour (const SHColour&) = default; SHColour (SHColour&&) = default; @@ -66,11 +62,32 @@ namespace SHADE /* Operator Overloads */ /*---------------------------------------------------------------------------------*/ - SHColour& operator= (const SHColour&) = default; - SHColour& operator= (SHColour&&) = default; + SHColour& operator= (const SHColour&) = default; + SHColour& operator= (SHColour&&) = default; - bool operator== (const SHColour& rhs) const noexcept; - bool operator!= (const SHColour& rhs) const noexcept; + operator DirectX::XMVECTOR () const noexcept; + operator SHVec4 () const noexcept; + + SHColour& operator+= (const SHColour& rhs) noexcept; + SHColour& operator-= (const SHColour& rhs) noexcept; + SHColour& operator*= (const SHColour& rhs) noexcept; + SHColour& operator*= (float rhs) noexcept; + SHColour& operator/= (const SHColour& rhs) noexcept; + + [[nodiscard]] SHColour operator+ (const SHColour& rhs) const noexcept; + [[nodiscard]] SHColour operator- (const SHColour& rhs) const noexcept; + [[nodiscard]] SHColour operator- () const noexcept; + [[nodiscard]] SHColour operator* (const SHColour& rhs) const noexcept; + [[nodiscard]] SHColour operator* (float rhs) const noexcept; + [[nodiscard]] SHColour operator/ (const SHColour& rhs) const noexcept; + + [[nodiscard]] bool operator== (const SHColour& rhs) const noexcept; + [[nodiscard]] bool operator!= (const SHColour& 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; /*---------------------------------------------------------------------------------*/ /* Properties */ @@ -105,6 +122,8 @@ namespace SHADE /* Static Data Members */ /*---------------------------------------------------------------------------------*/ + static constexpr size_t SIZE = 4U; + static const SHColour BEIGE ; static const SHColour BLACK ; static const SHColour BLUE ; @@ -159,8 +178,9 @@ namespace SHADE static const SHColour TURQUOISE ; static const SHColour VIOLET ; static const SHColour WHITE ; - static const SHColour YELLOW; + static const SHColour YELLOW ; }; + SHColour operator* (float lhs, const SHColour& rhs) noexcept; } // namespace SHADE diff --git a/SHADE_Engine/src/Math/SHMath.h b/SHADE_Engine/src/Math/SHMath.h index 5fcea9fc..3a24d6ef 100644 --- a/SHADE_Engine/src/Math/SHMath.h +++ b/SHADE_Engine/src/Math/SHMath.h @@ -9,4 +9,6 @@ #include "SHQuaternion.h" #include "SHMatrix.h" +#include "SHColour.h" + #include "Transform/SHTransform.h" \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Vector/SHVec4.cpp b/SHADE_Engine/src/Math/Vector/SHVec4.cpp index 88e7f5c9..943d540e 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec4.cpp +++ b/SHADE_Engine/src/Math/Vector/SHVec4.cpp @@ -14,6 +14,7 @@ #include "SHVec4.h" // Project Headers #include "Math/SHMatrix.h" +#include "Math/SHColour.h" #include "Tools/SHLogger.h" using namespace DirectX; diff --git a/SHADE_Engine/src/Math/Vector/SHVec4.h b/SHADE_Engine/src/Math/Vector/SHVec4.h index db8ef860..3c509039 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec4.h +++ b/SHADE_Engine/src/Math/Vector/SHVec4.h @@ -23,7 +23,9 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ /* Forward Declarations */ /*-----------------------------------------------------------------------------------*/ + class SHMatrix; + class SHColour; /*-----------------------------------------------------------------------------------*/ /* Type Definitions */