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 "Camera/SHCameraComponent.h"
#include "Math/SHColour.h"
#include "Resource/SHResourceManager.h"
#include "Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h"
@ -160,7 +161,7 @@ namespace Sandbox
SHComponentManager::RemoveComponent <SHColliderComponent>(0);
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)->SetType(SH_LIGHT_TYPE::AMBIENT);
}

View File

@ -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<float>(r) / 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
: SHVec4
: XMFLOAT4
{
static_cast<float>(r) / 255.0f,
static_cast<float>(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 */
/*-----------------------------------------------------------------------------------*/

View File

@ -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

View File

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

View File

@ -191,7 +191,7 @@ namespace SHADE
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

View File

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

View File

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

View File

@ -14,6 +14,7 @@
#include "SHVec4.h"
// Project Headers
#include "Math/SHMatrix.h"
#include "Math/SHColour.h"
#include "Tools/SHLogger.h"
using namespace DirectX;
@ -460,13 +461,19 @@ namespace SHADE
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 result;
const XMMATRIX TF = XMLoadFloat4x4(&transformMtx);
XMStoreFloat4(&result, XMVector3TransformCoord(v, TF));
XMStoreFloat4(&result, XMVector3TransformCoord(v, transformMtx));
return result;
}
}

View File

@ -23,7 +23,9 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/
/* Forward Declarations */
/*-----------------------------------------------------------------------------------*/
class SHMatrix;
class SHColour;
/*-----------------------------------------------------------------------------------*/
/* Type Definitions */
@ -134,6 +136,7 @@ namespace SHADE
[[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 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;
};