Merge branch 'SP3-16-Math' into SP3-1-DebugDraw2

This commit is contained in:
Kah Wei 2022-11-02 01:05:29 +08:00
commit ed446002aa
9 changed files with 219 additions and 32 deletions

View File

@ -16,6 +16,7 @@
#include "Assets/SHAssetManager.h" #include "Assets/SHAssetManager.h"
#include "Camera/SHCameraComponent.h" #include "Camera/SHCameraComponent.h"
#include "Math/SHColour.h"
#include "Resource/SHResourceManager.h" #include "Resource/SHResourceManager.h"
#include "Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h" #include "Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h"
@ -160,7 +161,7 @@ namespace Sandbox
SHComponentManager::RemoveComponent <SHColliderComponent>(0); SHComponentManager::RemoveComponent <SHColliderComponent>(0);
auto ambientLight = SHEntityManager::CreateEntity<SHLightComponent>(); auto ambientLight = SHEntityManager::CreateEntity<SHLightComponent>();
SHComponentManager::GetComponent<SHLightComponent>(ambientLight)->SetColor(SHVec4(1.0f, 1.0f, 1.0f, 1.0f)); SHComponentManager::GetComponent<SHLightComponent>(ambientLight)->SetColor(SHColour::WHITE);
SHComponentManager::GetComponent<SHLightComponent>(ambientLight)->SetStrength(0.25f); SHComponentManager::GetComponent<SHLightComponent>(ambientLight)->SetStrength(0.25f);
SHComponentManager::GetComponent<SHLightComponent>(ambientLight)->SetType(SH_LIGHT_TYPE::AMBIENT); SHComponentManager::GetComponent<SHLightComponent>(ambientLight)->SetType(SH_LIGHT_TYPE::AMBIENT);
} }

View File

@ -87,19 +87,19 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHColour::SHColour() noexcept 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 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 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 SHColour::SHColour(uint8_t r, uint8_t g, uint8_t b) noexcept
: SHVec4 : XMFLOAT4
{ {
static_cast<float>(r) / 255.0f, static_cast<float>(r) / 255.0f,
static_cast<float>(g) / 255.0f, static_cast<float>(g) / 255.0f,
@ -109,7 +109,7 @@ namespace SHADE
{} {}
SHColour::SHColour(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept SHColour::SHColour(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
: SHVec4 : XMFLOAT4
{ {
static_cast<float>(r) / 255.0f, static_cast<float>(r) / 255.0f,
static_cast<float>(g) / 255.0f, static_cast<float>(g) / 255.0f,
@ -118,12 +118,24 @@ namespace SHADE
} }
{} {}
SHColour::SHColour(const DirectX::XMFLOAT3& colour) noexcept SHColour::SHColour(const SHVec3& rgb) noexcept
: SHVec4 { colour.x, colour.y, colour.z, 1.0f } : 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 SHColour::SHColour(const DirectX::XMVECTORF32& colour) noexcept
: SHVec4 : XMFLOAT4
{ {
XMVectorGetX(colour), XMVectorGetX(colour),
XMVectorGetY(colour), XMVectorGetY(colour),
@ -136,6 +148,86 @@ namespace SHADE
/* Operator Overload Definitions */ /* 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 bool SHColour::operator==(const SHColour& rhs) const noexcept
{ {
return XMColorEqual(*this, rhs); return XMColorEqual(*this, rhs);
@ -146,6 +238,70 @@ namespace SHADE
return XMColorNotEqual(*this, rhs); 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 */ /* Function Member Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/

View File

@ -19,12 +19,6 @@
namespace SHADE namespace SHADE
{ {
/*-----------------------------------------------------------------------------------*/
/* Forward Declarations */
/*-----------------------------------------------------------------------------------*/
class SHColour;
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Type Definitions */ /* Type Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -40,7 +34,7 @@ namespace SHADE
float v = 0.0f; float v = 0.0f;
}; };
class SH_API SHColour : private SHVec4 class SH_API SHColour : public DirectX::XMFLOAT4
{ {
public: 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) noexcept;
SHColour (uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept; SHColour (uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
SHColour (const SHVec3& colour) noexcept; SHColour (const SHVec3& rgb) noexcept;
SHColour (const DirectX::XMFLOAT3& colour) noexcept; SHColour (const SHVec4& rgba) noexcept;
SHColour (const DirectX::XMVECTORF32& colour) noexcept; SHColour (const DirectX::XMFLOAT3& rgb) noexcept;
SHColour (const DirectX::XMFLOAT4& rgba) noexcept;
SHColour (const DirectX::XMVECTORF32& rgba) noexcept;
SHColour (const SHColour&) = default; SHColour (const SHColour&) = default;
SHColour (SHColour&&) = default; SHColour (SHColour&&) = default;
@ -66,11 +62,32 @@ namespace SHADE
/* Operator Overloads */ /* Operator Overloads */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
SHColour& operator= (const SHColour&) = default; SHColour& operator= (const SHColour&) = default;
SHColour& operator= (SHColour&&) = default; SHColour& operator= (SHColour&&) = default;
bool operator== (const SHColour& rhs) const noexcept; operator DirectX::XMVECTOR () const noexcept;
bool operator!= (const SHColour& rhs) 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 */ /* Properties */
@ -105,6 +122,8 @@ namespace SHADE
/* Static Data Members */ /* Static Data Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
static constexpr size_t SIZE = 4U;
static const SHColour BEIGE ; static const SHColour BEIGE ;
static const SHColour BLACK ; static const SHColour BLACK ;
static const SHColour BLUE ; static const SHColour BLUE ;
@ -159,8 +178,9 @@ namespace SHADE
static const SHColour TURQUOISE ; static const SHColour TURQUOISE ;
static const SHColour VIOLET ; static const SHColour VIOLET ;
static const SHColour WHITE ; static const SHColour WHITE ;
static const SHColour YELLOW; static const SHColour YELLOW ;
}; };
SHColour operator* (float lhs, const SHColour& rhs) noexcept;
} // namespace SHADE } // namespace SHADE

View File

@ -9,4 +9,6 @@
#include "SHQuaternion.h" #include "SHQuaternion.h"
#include "SHMatrix.h" #include "SHMatrix.h"
#include "SHColour.h"
#include "Transform/SHTransform.h" #include "Transform/SHTransform.h"

View File

@ -191,7 +191,7 @@ namespace SHADE
SHVec4 SHMatrix::operator*(const SHVec4& rhs) const noexcept SHVec4 SHMatrix::operator*(const SHVec4& rhs) const noexcept
{ {
return SHVec4::Transform3D(rhs, *this); return SHVec4::Transform(rhs, *this);
} }
SHMatrix SHMatrix::operator*(float rhs) const noexcept SHMatrix SHMatrix::operator*(float rhs) const noexcept

View File

@ -261,7 +261,7 @@ namespace SHADE
auto* node = EVENT_DATA->data->node; auto* node = EVENT_DATA->data->node;
auto* tf = SHComponentManager::GetComponent_s<SHTransformComponent>(node->GetEntityID()); auto* tf = SHComponentManager::GetComponent_s<SHTransformComponent>(node->GetEntityID());
if(tf == nullptr) if (tf == nullptr)
return EVENT_DATA->handle; return EVENT_DATA->handle;
// Recompute local transform and store localToWorld Matrix // Recompute local transform and store localToWorld Matrix

View File

@ -445,9 +445,7 @@ namespace SHADE
{ {
SHVec3 result; SHVec3 result;
const XMMATRIX TF = XMLoadFloat4x4(&transformMtx); XMStoreFloat3(&result, XMVector3TransformCoord(v, transformMtx));
XMStoreFloat3(&result, XMVector3TransformCoord(v, TF));
return result; return result;
} }
} }

View File

@ -14,6 +14,7 @@
#include "SHVec4.h" #include "SHVec4.h"
// Project Headers // Project Headers
#include "Math/SHMatrix.h" #include "Math/SHMatrix.h"
#include "Math/SHColour.h"
#include "Tools/SHLogger.h" #include "Tools/SHLogger.h"
using namespace DirectX; using namespace DirectX;
@ -460,13 +461,19 @@ namespace SHADE
return result; return result;
} }
SHVec4 SHVec4::Transform(const SHVec4& v, const SHMatrix& transformMtx) noexcept
{
SHVec4 result;
XMStoreFloat4(&result, XMVector4Transform(v, transformMtx));
return result;
}
SHVec4 SHVec4::Transform3D(const SHVec4& v, const SHMatrix& transformMtx) noexcept SHVec4 SHVec4::Transform3D(const SHVec4& v, const SHMatrix& transformMtx) noexcept
{ {
SHVec4 result; SHVec4 result;
const XMMATRIX TF = XMLoadFloat4x4(&transformMtx); XMStoreFloat4(&result, XMVector3TransformCoord(v, transformMtx));
XMStoreFloat4(&result, XMVector3TransformCoord(v, TF));
return result; return result;
} }
} }

View File

@ -23,7 +23,9 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Forward Declarations */ /* Forward Declarations */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
class SHMatrix; class SHMatrix;
class SHColour;
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Type Definitions */ /* Type Definitions */
@ -134,6 +136,7 @@ namespace SHADE
[[nodiscard]] static SHVec4 Project3D (const SHVec4& v, const SHVec4& u) noexcept; [[nodiscard]] static SHVec4 Project3D (const SHVec4& v, const SHVec4& u) noexcept;
[[nodiscard]] static SHVec4 Reflect (const SHVec4& v, const SHVec4& normal) noexcept; [[nodiscard]] static SHVec4 Reflect (const SHVec4& v, const SHVec4& normal) noexcept;
[[nodiscard]] static SHVec4 Reflect3D (const SHVec4& v, const SHVec4& normal) noexcept; [[nodiscard]] static SHVec4 Reflect3D (const SHVec4& v, const SHVec4& normal) noexcept;
[[nodiscard]] static SHVec4 Transform (const SHVec4& v, const SHMatrix& transformMtx) noexcept;
[[nodiscard]] static SHVec4 Transform3D (const SHVec4& v, const SHMatrix& transformMtx) noexcept; [[nodiscard]] static SHVec4 Transform3D (const SHVec4& v, const SHMatrix& transformMtx) noexcept;
}; };