Added Transform, adjusted alignment in math files for better readability on smaller screens

This commit is contained in:
Cocoa 2022-09-19 14:32:01 +08:00
parent 95440b8d26
commit f1e6031d2e
18 changed files with 445 additions and 261 deletions

View File

@ -66,7 +66,7 @@
<MinimalRebuild>false</MinimalRebuild>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpplatest</LanguageStandard>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>

View File

@ -6,6 +6,9 @@
#include "Scenes/SBEditorScene.h"
#endif // SHEDITOR
#include "Math/SHMath.h"
#include "Tools/SHLogger.h"
#include <chrono>
#include <ratio>
#include <ctime>
@ -22,6 +25,9 @@ namespace Sandbox
)
{
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

View File

@ -64,7 +64,7 @@
<MinimalRebuild>false</MinimalRebuild>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpplatest</LanguageStandard>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
@ -174,8 +174,10 @@
<ClInclude Include="src\Graphics\Windowing\Surface\SHVkSurface.h" />
<ClInclude Include="src\Math\SHMath.h" />
<ClInclude Include="src\Math\SHMathHelpers.h" />
<ClInclude Include="src\Math\SHMathHelpers.hpp" />
<ClInclude Include="src\Math\SHMatrix.h" />
<ClInclude Include="src\Math\SHQuaternion.h" />
<ClInclude Include="src\Math\SHTransform.h" />
<ClInclude Include="src\Math\Vector\SHVec2.h" />
<ClInclude Include="src\Math\Vector\SHVec3.h" />
<ClInclude Include="src\Math\Vector\SHVec4.h" />
@ -250,6 +252,7 @@
<ClCompile Include="src\Math\SHMathHelpers.cpp" />
<ClCompile Include="src\Math\SHMatrix.cpp" />
<ClCompile Include="src\Math\SHQuaternion.cpp" />
<ClCompile Include="src\Math\SHTransform.cpp" />
<ClCompile Include="src\Math\Vector\SHVec2.cpp" />
<ClCompile Include="src\Math\Vector\SHVec3.cpp" />
<ClCompile Include="src\Math\Vector\SHVec4.cpp" />

View File

@ -339,6 +339,9 @@
<ClInclude Include="src\Math\SHQuaternion.h">
<Filter>Math</Filter>
</ClInclude>
<ClInclude Include="src\Math\SHTransform.h">
<Filter>Math</Filter>
</ClInclude>
<ClInclude Include="src\Math\Vector\SHVec2.h">
<Filter>Math\Vector</Filter>
</ClInclude>
@ -382,6 +385,7 @@
<ClInclude Include="src\Tools\SHUtilities.h">
<Filter>Tools</Filter>
</ClInclude>
<ClInclude Include="src\Math\SHMathHelpers.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\ECS_Base\Components\SHComponent.cpp">
@ -555,6 +559,9 @@
<ClCompile Include="src\Math\SHQuaternion.cpp">
<Filter>Math</Filter>
</ClCompile>
<ClCompile Include="src\Math\SHTransform.cpp">
<Filter>Math</Filter>
</ClCompile>
<ClCompile Include="src\Math\Vector\SHVec2.cpp">
<Filter>Math\Vector</Filter>
</ClCompile>

View File

@ -6,4 +6,7 @@
#include "Vector/SHVec3.h"
#include "Vector/SHVec4.h"
#include "SHQuaternion.h"
#include "SHMatrix.h"
#include "SHTransform.h"

View File

@ -59,10 +59,16 @@ namespace SHADE
static void Initialise ();
template <IsArithmetic T>
[[nodiscard]] static constexpr T DegreesToRadians (T angleInDeg);
[[nodiscard]] static T Min (T lhs, T rhs);
template <IsArithmetic T>
[[nodiscard]] static constexpr T RadiansToDegrees (T angleInRad);
[[nodiscard]] static T Max (T lhs, T rhs);
template <IsArithmetic T>
[[nodiscard]] static T DegreesToRadians (T angleInDeg);
template <IsArithmetic T>
[[nodiscard]] static T RadiansToDegrees (T angleInRad);
template <IsArithmetic T>
[[nodiscard]] static T Lerp (T a, T b, T alpha);

View File

@ -13,11 +13,8 @@
// Primary Header
#include "SHMathHelpers.h"
#include <cmath>
#include <algorithm>
// TODOs (Diren): Include pch?
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
@ -25,13 +22,25 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/
template <IsArithmetic T>
constexpr T SHMath::DegreesToRadians(T angleInDeg)
T SHMath::Min(T lhs, T rhs)
{
return lhs < rhs ? lhs : rhs;
}
template <IsArithmetic T>
T SHMath::Max(T lhs, T rhs)
{
return lhs > rhs ? lhs : rhs;
}
template <IsArithmetic T>
T SHMath::DegreesToRadians(T angleInDeg)
{
return angleInDeg * static_cast<T>(PI / 180.0f);
}
template <IsArithmetic T>
constexpr T SHMath::RadiansToDegrees(T angleInRad)
T SHMath::RadiansToDegrees(T angleInRad)
{
return angleInRad * static_cast<T>(180.0f / PI);
}
@ -82,9 +91,10 @@ namespace SHADE
template <IsFloatingPoint T>
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

View File

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

View File

@ -54,12 +54,15 @@ namespace SHADE
~SHMatrix () = default;
SHMatrix () noexcept;
SHMatrix ( const SHVec4& r0,
SHMatrix
(
const SHVec4& r0,
const SHVec4& r1,
const SHVec4& r2,
const SHVec4& r3 = SHVec4::UnitW
) noexcept;
SHMatrix (
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,
@ -80,18 +83,18 @@ namespace SHADE
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 */

View File

@ -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();
}

View File

@ -20,6 +20,7 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/
class SHVec3;
class SHVec4;
class SHMatrix;
/*-----------------------------------------------------------------------------------*/
@ -53,14 +54,14 @@ namespace SHADE
/* 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;
[[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;
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) const noexcept;
[[nodiscard]] SHQuaternion operator- (const SHQuaternion& rhs) const noexcept;
@ -72,6 +73,13 @@ namespace SHADE
[[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 */
/*---------------------------------------------------------------------------------*/

View File

@ -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 <SHpch.h>
// 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

View File

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

View File

@ -56,15 +56,15 @@ namespace SHADE
/* 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;

View File

@ -61,15 +61,15 @@ namespace SHADE
/* 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;

View File

@ -56,15 +56,15 @@ namespace SHADE
/* Operator Overloads */
/*---------------------------------------------------------------------------------*/
[[nodiscard]] SHVec4& operator= (const SHVec4& rhs) = default;
[[nodiscard]] SHVec4& operator= (SHVec4&& rhs) = default;
SHVec4& operator= (const SHVec4& rhs) = default;
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) 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]] SHVec4 operator+ (const SHVec4& rhs) const noexcept;
[[nodiscard]] SHVec4 operator- (const SHVec4& rhs) const noexcept;

View File

@ -9,7 +9,10 @@
#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#define NOMINMAX
// Windows Header Files
#include <Windows.h>
// C RunTime Header Files
@ -30,3 +33,5 @@
#include <functional>
#include <sstream>
#include <iomanip>
#include "Tools/SHLogger.h"