mirror of
https://github.com/HandmadeMath/HandmadeMath.git
synced 2025-12-29 08:04:31 +00:00
Compare commits
3 Commits
1.9.0
...
mat4toquat
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
05a474eb76 | ||
|
|
3a5a5320c1 | ||
|
|
bb6b315c37 |
324
HandmadeMath.h
324
HandmadeMath.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
HandmadeMath.h v1.9.0
|
||||
HandmadeMath.h v1.6.0
|
||||
|
||||
This is a single header file with a bunch of useful functions for game and
|
||||
graphics math operations.
|
||||
@@ -65,6 +65,114 @@
|
||||
versions of these functions that are provided by the CRT.
|
||||
|
||||
=============================================================================
|
||||
|
||||
Version History:
|
||||
0.2 (*) Updated documentation
|
||||
(*) Better C compliance
|
||||
(*) Prefix all handmade math functions
|
||||
(*) Better operator overloading
|
||||
0.2a
|
||||
(*) Prefixed Macros
|
||||
0.2b
|
||||
(*) Disabled warning 4201 on MSVC as it is legal is C11
|
||||
(*) Removed the f at the end of HMM_PI to get 64bit precision
|
||||
0.3
|
||||
(*) Added +=, -=, *=, /= for hmm_vec2, hmm_vec3, hmm_vec4
|
||||
0.4
|
||||
(*) SSE Optimized HMM_SqrtF
|
||||
(*) SSE Optimized HMM_RSqrtF
|
||||
(*) Removed CRT
|
||||
0.5
|
||||
(*) Added scalar multiplication and division for vectors
|
||||
and matrices
|
||||
(*) Added matrix subtraction and += for hmm_mat4
|
||||
(*) Reconciled all headers and implementations
|
||||
(*) Tidied up, and filled in a few missing operators
|
||||
0.5.1
|
||||
(*) Ensured column-major order for matrices throughout
|
||||
(*) Fixed HMM_Translate producing row-major matrices
|
||||
0.5.2
|
||||
(*) Fixed SSE code in HMM_SqrtF
|
||||
(*) Fixed SSE code in HMM_RSqrtF
|
||||
0.6
|
||||
(*) Added Unit testing
|
||||
(*) Made HMM_Power faster
|
||||
(*) Fixed possible efficiency problem with HMM_Normalize
|
||||
(*) RENAMED HMM_LengthSquareRoot to HMM_LengthSquared
|
||||
(*) RENAMED HMM_RSqrtF to HMM_RSquareRootF
|
||||
(*) RENAMED HMM_SqrtF to HMM_SquareRootF
|
||||
(*) REMOVED Inner function (user should use Dot now)
|
||||
(*) REMOVED HMM_FastInverseSquareRoot function declaration
|
||||
0.7
|
||||
(*) REMOVED HMM_LengthSquared in HANDMADE_MATH_IMPLEMENTATION (should
|
||||
use HMM_LengthSquaredVec3, or HANDMADE_MATH_CPP_MODE for function
|
||||
overloaded version)
|
||||
(*) REMOVED HMM_Length in HANDMADE_MATH_IMPLEMENTATION (should use
|
||||
HMM_LengthVec3, HANDMADE_MATH_CPP_MODE for function
|
||||
overloaded version)
|
||||
(*) REMOVED HMM_Normalize in HANDMADE_MATH_IMPLEMENTATION (should use
|
||||
HMM_NormalizeVec3, or HANDMADE_MATH_CPP_MODE for function
|
||||
overloaded version)
|
||||
(*) Added HMM_LengthSquaredVec2
|
||||
(*) Added HMM_LengthSquaredVec4
|
||||
(*) Addd HMM_LengthVec2
|
||||
(*) Added HMM_LengthVec4
|
||||
(*) Added HMM_NormalizeVec2
|
||||
(*) Added HMM_NormalizeVec4
|
||||
1.0
|
||||
(*) Lots of testing!
|
||||
1.1
|
||||
(*) Quaternion support
|
||||
(*) Added type hmm_quaternion
|
||||
(*) Added HMM_Quaternion
|
||||
(*) Added HMM_QuaternionV4
|
||||
(*) Added HMM_AddQuaternion
|
||||
(*) Added HMM_SubtractQuaternion
|
||||
(*) Added HMM_MultiplyQuaternion
|
||||
(*) Added HMM_MultiplyQuaternionF
|
||||
(*) Added HMM_DivideQuaternionF
|
||||
(*) Added HMM_InverseQuaternion
|
||||
(*) Added HMM_DotQuaternion
|
||||
(*) Added HMM_NormalizeQuaternion
|
||||
(*) Added HMM_Slerp
|
||||
(*) Added HMM_QuaternionToMat4
|
||||
(*) Added HMM_QuaternionFromAxisAngle
|
||||
1.1.1
|
||||
(*) Resolved compiler warnings on gcc and g++
|
||||
1.1.2
|
||||
(*) Fixed invalid HMMDEF's in the function definitions
|
||||
1.1.3
|
||||
(*) Fixed compile error in C mode
|
||||
1.1.4
|
||||
(*) Fixed SSE being included on platforms that don't support it
|
||||
(*) Fixed divide-by-zero errors when normalizing zero vectors.
|
||||
1.1.5
|
||||
(*) Add Width and Height to HMM_Vec2
|
||||
(*) Made it so you can supply your own SqrtF
|
||||
1.2.0
|
||||
(*) Added equality functions for HMM_Vec2, HMM_Vec3, and HMM_Vec4.
|
||||
(*) Added HMM_EqualsVec2, HMM_EqualsVec3, and HMM_EqualsVec4
|
||||
(*) Added C++ overloaded HMM_Equals for all three
|
||||
(*) Added C++ == and != operators for all three
|
||||
(*) SSE'd HMM_MultiplyMat4 (this is _WAY_ faster)
|
||||
(*) SSE'd HMM_Transpose
|
||||
1.3.0
|
||||
(*) Remove need to #define HANDMADE_MATH_CPP_MODE
|
||||
1.4.0
|
||||
(*) Fixed bug when using HandmadeMath in C mode
|
||||
(*) SSEd all vec4 operations
|
||||
(*) Removed all zero-ing
|
||||
1.5.0
|
||||
(*) Changed internal structure for better performance and inlining.
|
||||
(*) As a result, HANDMADE_MATH_NO_INLINE has been removed and no
|
||||
longer has any effect.
|
||||
1.5.1
|
||||
(*) Fixed a bug with uninitialized elements in HMM_LookAt.
|
||||
1.6.0
|
||||
(*) Added array subscript operators for vector and matrix types in
|
||||
C++. This is provided as a convenience, but be aware that it may
|
||||
incur an extra function call in unoptimized builds.
|
||||
|
||||
|
||||
LICENSE
|
||||
|
||||
@@ -83,7 +191,6 @@
|
||||
Gingerbill (@TheGingerBill)
|
||||
Ben Visness (@bvisness)
|
||||
Trinton Bullard (@Peliex_Dev)
|
||||
@AntonDan
|
||||
|
||||
Fixes:
|
||||
Jeroen van Rijn (@J_vanRijn)
|
||||
@@ -213,7 +320,7 @@ typedef union hmm_vec2
|
||||
float Elements[2];
|
||||
|
||||
#ifdef __cplusplus
|
||||
inline float &operator[](const int &Index)
|
||||
inline float &operator[](int Index)
|
||||
{
|
||||
return Elements[Index];
|
||||
}
|
||||
@@ -264,7 +371,7 @@ typedef union hmm_vec3
|
||||
float Elements[3];
|
||||
|
||||
#ifdef __cplusplus
|
||||
inline float &operator[](const int &Index)
|
||||
inline float &operator[](int Index)
|
||||
{
|
||||
return Elements[Index];
|
||||
}
|
||||
@@ -328,7 +435,7 @@ typedef union hmm_vec4
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
inline float &operator[](const int &Index)
|
||||
inline float &operator[](int Index)
|
||||
{
|
||||
return Elements[Index];
|
||||
}
|
||||
@@ -340,15 +447,11 @@ typedef union hmm_mat4
|
||||
float Elements[4][4];
|
||||
|
||||
#ifdef HANDMADE_MATH__USE_SSE
|
||||
__m128 Columns[4];
|
||||
|
||||
// DEPRECATED. Our matrices are column-major, so this was named
|
||||
// incorrectly. Use Columns instead.
|
||||
__m128 Rows[4];
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
inline hmm_vec4 operator[](const int &Index)
|
||||
inline hmm_vec4 operator[](const int Index)
|
||||
{
|
||||
float* col = Elements[Index];
|
||||
|
||||
@@ -380,10 +483,6 @@ typedef union hmm_quaternion
|
||||
};
|
||||
|
||||
float Elements[4];
|
||||
|
||||
#ifdef HANDMADE_MATH__USE_SSE
|
||||
__m128 InternalElementsSSE;
|
||||
#endif
|
||||
} hmm_quaternion;
|
||||
|
||||
typedef int32_t hmm_bool;
|
||||
@@ -1021,21 +1120,6 @@ HMM_INLINE hmm_vec4 HMM_NormalizeVec4(hmm_vec4 A)
|
||||
return (Result);
|
||||
}
|
||||
|
||||
HMM_INLINE hmm_vec2 HMM_FastNormalizeVec2(hmm_vec2 A)
|
||||
{
|
||||
return HMM_MultiplyVec2f(A, HMM_RSquareRootF(HMM_DotVec2(A, A)));
|
||||
}
|
||||
|
||||
HMM_INLINE hmm_vec3 HMM_FastNormalizeVec3(hmm_vec3 A)
|
||||
{
|
||||
return HMM_MultiplyVec3f(A, HMM_RSquareRootF(HMM_DotVec3(A, A)));
|
||||
}
|
||||
|
||||
HMM_INLINE hmm_vec4 HMM_FastNormalizeVec4(hmm_vec4 A)
|
||||
{
|
||||
return HMM_MultiplyVec4f(A, HMM_RSquareRootF(HMM_DotVec4(A, A)));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* SSE stuff
|
||||
@@ -1045,10 +1129,10 @@ HMM_INLINE hmm_vec4 HMM_FastNormalizeVec4(hmm_vec4 A)
|
||||
HMM_INLINE __m128 HMM_LinearCombineSSE(__m128 Left, hmm_mat4 Right)
|
||||
{
|
||||
__m128 Result;
|
||||
Result = _mm_mul_ps(_mm_shuffle_ps(Left, Left, 0x00), Right.Columns[0]);
|
||||
Result = _mm_add_ps(Result, _mm_mul_ps(_mm_shuffle_ps(Left, Left, 0x55), Right.Columns[1]));
|
||||
Result = _mm_add_ps(Result, _mm_mul_ps(_mm_shuffle_ps(Left, Left, 0xaa), Right.Columns[2]));
|
||||
Result = _mm_add_ps(Result, _mm_mul_ps(_mm_shuffle_ps(Left, Left, 0xff), Right.Columns[3]));
|
||||
Result = _mm_mul_ps(_mm_shuffle_ps(Left, Left, 0x00), Right.Rows[0]);
|
||||
Result = _mm_add_ps(Result, _mm_mul_ps(_mm_shuffle_ps(Left, Left, 0x55), Right.Rows[1]));
|
||||
Result = _mm_add_ps(Result, _mm_mul_ps(_mm_shuffle_ps(Left, Left, 0xaa), Right.Rows[2]));
|
||||
Result = _mm_add_ps(Result, _mm_mul_ps(_mm_shuffle_ps(Left, Left, 0xff), Right.Rows[3]));
|
||||
|
||||
return (Result);
|
||||
}
|
||||
@@ -1083,7 +1167,7 @@ HMM_INLINE hmm_mat4 HMM_Transpose(hmm_mat4 Matrix)
|
||||
{
|
||||
hmm_mat4 Result = Matrix;
|
||||
|
||||
_MM_TRANSPOSE4_PS(Result.Columns[0], Result.Columns[1], Result.Columns[2], Result.Columns[3]);
|
||||
_MM_TRANSPOSE4_PS(Result.Rows[0], Result.Rows[1], Result.Rows[2], Result.Rows[3]);
|
||||
|
||||
return (Result);
|
||||
}
|
||||
@@ -1096,10 +1180,10 @@ HMM_INLINE hmm_mat4 HMM_AddMat4(hmm_mat4 Left, hmm_mat4 Right)
|
||||
{
|
||||
hmm_mat4 Result;
|
||||
|
||||
Result.Columns[0] = _mm_add_ps(Left.Columns[0], Right.Columns[0]);
|
||||
Result.Columns[1] = _mm_add_ps(Left.Columns[1], Right.Columns[1]);
|
||||
Result.Columns[2] = _mm_add_ps(Left.Columns[2], Right.Columns[2]);
|
||||
Result.Columns[3] = _mm_add_ps(Left.Columns[3], Right.Columns[3]);
|
||||
Result.Rows[0] = _mm_add_ps(Left.Rows[0], Right.Rows[0]);
|
||||
Result.Rows[1] = _mm_add_ps(Left.Rows[1], Right.Rows[1]);
|
||||
Result.Rows[2] = _mm_add_ps(Left.Rows[2], Right.Rows[2]);
|
||||
Result.Rows[3] = _mm_add_ps(Left.Rows[3], Right.Rows[3]);
|
||||
|
||||
return (Result);
|
||||
}
|
||||
@@ -1112,10 +1196,10 @@ HMM_INLINE hmm_mat4 HMM_SubtractMat4(hmm_mat4 Left, hmm_mat4 Right)
|
||||
{
|
||||
hmm_mat4 Result;
|
||||
|
||||
Result.Columns[0] = _mm_sub_ps(Left.Columns[0], Right.Columns[0]);
|
||||
Result.Columns[1] = _mm_sub_ps(Left.Columns[1], Right.Columns[1]);
|
||||
Result.Columns[2] = _mm_sub_ps(Left.Columns[2], Right.Columns[2]);
|
||||
Result.Columns[3] = _mm_sub_ps(Left.Columns[3], Right.Columns[3]);
|
||||
Result.Rows[0] = _mm_sub_ps(Left.Rows[0], Right.Rows[0]);
|
||||
Result.Rows[1] = _mm_sub_ps(Left.Rows[1], Right.Rows[1]);
|
||||
Result.Rows[2] = _mm_sub_ps(Left.Rows[2], Right.Rows[2]);
|
||||
Result.Rows[3] = _mm_sub_ps(Left.Rows[3], Right.Rows[3]);
|
||||
|
||||
return (Result);
|
||||
}
|
||||
@@ -1131,10 +1215,10 @@ HMM_INLINE hmm_mat4 HMM_MultiplyMat4f(hmm_mat4 Matrix, float Scalar)
|
||||
hmm_mat4 Result;
|
||||
|
||||
__m128 SSEScalar = _mm_set1_ps(Scalar);
|
||||
Result.Columns[0] = _mm_mul_ps(Matrix.Columns[0], SSEScalar);
|
||||
Result.Columns[1] = _mm_mul_ps(Matrix.Columns[1], SSEScalar);
|
||||
Result.Columns[2] = _mm_mul_ps(Matrix.Columns[2], SSEScalar);
|
||||
Result.Columns[3] = _mm_mul_ps(Matrix.Columns[3], SSEScalar);
|
||||
Result.Rows[0] = _mm_mul_ps(Matrix.Rows[0], SSEScalar);
|
||||
Result.Rows[1] = _mm_mul_ps(Matrix.Rows[1], SSEScalar);
|
||||
Result.Rows[2] = _mm_mul_ps(Matrix.Rows[2], SSEScalar);
|
||||
Result.Rows[3] = _mm_mul_ps(Matrix.Rows[3], SSEScalar);
|
||||
|
||||
return (Result);
|
||||
}
|
||||
@@ -1150,10 +1234,10 @@ HMM_INLINE hmm_mat4 HMM_DivideMat4f(hmm_mat4 Matrix, float Scalar)
|
||||
hmm_mat4 Result;
|
||||
|
||||
__m128 SSEScalar = _mm_set1_ps(Scalar);
|
||||
Result.Columns[0] = _mm_div_ps(Matrix.Columns[0], SSEScalar);
|
||||
Result.Columns[1] = _mm_div_ps(Matrix.Columns[1], SSEScalar);
|
||||
Result.Columns[2] = _mm_div_ps(Matrix.Columns[2], SSEScalar);
|
||||
Result.Columns[3] = _mm_div_ps(Matrix.Columns[3], SSEScalar);
|
||||
Result.Rows[0] = _mm_div_ps(Matrix.Rows[0], SSEScalar);
|
||||
Result.Rows[1] = _mm_div_ps(Matrix.Rows[1], SSEScalar);
|
||||
Result.Rows[2] = _mm_div_ps(Matrix.Rows[2], SSEScalar);
|
||||
Result.Rows[3] = _mm_div_ps(Matrix.Rows[3], SSEScalar);
|
||||
|
||||
return (Result);
|
||||
}
|
||||
@@ -1161,6 +1245,8 @@ HMM_INLINE hmm_mat4 HMM_DivideMat4f(hmm_mat4 Matrix, float Scalar)
|
||||
HMM_EXTERN hmm_mat4 HMM_DivideMat4f(hmm_mat4 Matrix, float Scalar);
|
||||
#endif
|
||||
|
||||
HMM_EXTERN hmm_quaternion HMM_Mat4ToQuaternion(hmm_mat4 Matrix);
|
||||
|
||||
|
||||
/*
|
||||
* Common graphics transformations
|
||||
@@ -1233,14 +1319,10 @@ HMM_INLINE hmm_quaternion HMM_Quaternion(float X, float Y, float Z, float W)
|
||||
{
|
||||
hmm_quaternion Result;
|
||||
|
||||
#ifdef HANDMADE_MATH__USE_SSE
|
||||
Result.InternalElementsSSE = _mm_setr_ps(X, Y, Z, W);
|
||||
#else
|
||||
Result.X = X;
|
||||
Result.Y = Y;
|
||||
Result.Z = Z;
|
||||
Result.W = W;
|
||||
#endif
|
||||
|
||||
return (Result);
|
||||
}
|
||||
@@ -1249,14 +1331,10 @@ HMM_INLINE hmm_quaternion HMM_QuaternionV4(hmm_vec4 Vector)
|
||||
{
|
||||
hmm_quaternion Result;
|
||||
|
||||
#ifdef HANDMADE_MATH__USE_SSE
|
||||
Result.InternalElementsSSE = Vector.InternalElementsSSE;
|
||||
#else
|
||||
Result.X = Vector.X;
|
||||
Result.Y = Vector.Y;
|
||||
Result.Z = Vector.Z;
|
||||
Result.W = Vector.W;
|
||||
#endif
|
||||
|
||||
return (Result);
|
||||
}
|
||||
@@ -1265,15 +1343,10 @@ HMM_INLINE hmm_quaternion HMM_AddQuaternion(hmm_quaternion Left, hmm_quaternion
|
||||
{
|
||||
hmm_quaternion Result;
|
||||
|
||||
#ifdef HANDMADE_MATH__USE_SSE
|
||||
Result.InternalElementsSSE = _mm_add_ps(Left.InternalElementsSSE, Right.InternalElementsSSE);
|
||||
#else
|
||||
|
||||
Result.X = Left.X + Right.X;
|
||||
Result.Y = Left.Y + Right.Y;
|
||||
Result.Z = Left.Z + Right.Z;
|
||||
Result.W = Left.W + Right.W;
|
||||
#endif
|
||||
|
||||
return (Result);
|
||||
}
|
||||
@@ -1282,15 +1355,10 @@ HMM_INLINE hmm_quaternion HMM_SubtractQuaternion(hmm_quaternion Left, hmm_quater
|
||||
{
|
||||
hmm_quaternion Result;
|
||||
|
||||
#ifdef HANDMADE_MATH__USE_SSE
|
||||
Result.InternalElementsSSE = _mm_sub_ps(Left.InternalElementsSSE, Right.InternalElementsSSE);
|
||||
#else
|
||||
|
||||
Result.X = Left.X - Right.X;
|
||||
Result.Y = Left.Y - Right.Y;
|
||||
Result.Z = Left.Z - Right.Z;
|
||||
Result.W = Left.W - Right.W;
|
||||
#endif
|
||||
|
||||
return (Result);
|
||||
}
|
||||
@@ -1299,28 +1367,10 @@ HMM_INLINE hmm_quaternion HMM_MultiplyQuaternion(hmm_quaternion Left, hmm_quater
|
||||
{
|
||||
hmm_quaternion Result;
|
||||
|
||||
#ifdef HANDMADE_MATH__USE_SSE
|
||||
__m128 SSEResultOne = _mm_xor_ps(_mm_shuffle_ps(Left.InternalElementsSSE, Left.InternalElementsSSE, _MM_SHUFFLE(0, 0, 0, 0)), _mm_setr_ps(0.f, -0.f, 0.f, -0.f));
|
||||
__m128 SSEResultTwo = _mm_shuffle_ps(Right.InternalElementsSSE, Right.InternalElementsSSE, _MM_SHUFFLE(0, 1, 2, 3));
|
||||
__m128 SSEResultThree = _mm_mul_ps(SSEResultTwo, SSEResultOne);
|
||||
|
||||
SSEResultOne = _mm_xor_ps(_mm_shuffle_ps(Left.InternalElementsSSE, Left.InternalElementsSSE, _MM_SHUFFLE(1, 1, 1, 1)) , _mm_setr_ps(0.f, 0.f, -0.f, -0.f));
|
||||
SSEResultTwo = _mm_shuffle_ps(Right.InternalElementsSSE, Right.InternalElementsSSE, _MM_SHUFFLE(1, 0, 3, 2));
|
||||
SSEResultThree = _mm_add_ps(SSEResultThree, _mm_mul_ps(SSEResultTwo, SSEResultOne));
|
||||
|
||||
SSEResultOne = _mm_xor_ps(_mm_shuffle_ps(Left.InternalElementsSSE, Left.InternalElementsSSE, _MM_SHUFFLE(2, 2, 2, 2)), _mm_setr_ps(-0.f, 0.f, 0.f, -0.f));
|
||||
SSEResultTwo = _mm_shuffle_ps(Right.InternalElementsSSE, Right.InternalElementsSSE, _MM_SHUFFLE(2, 3, 0, 1));
|
||||
SSEResultThree = _mm_add_ps(SSEResultThree, _mm_mul_ps(SSEResultTwo, SSEResultOne));
|
||||
|
||||
SSEResultOne = _mm_shuffle_ps(Left.InternalElementsSSE, Left.InternalElementsSSE, _MM_SHUFFLE(3, 3, 3, 3));
|
||||
SSEResultTwo = _mm_shuffle_ps(Right.InternalElementsSSE, Right.InternalElementsSSE, _MM_SHUFFLE(3, 2, 1, 0));
|
||||
Result.InternalElementsSSE = _mm_add_ps(SSEResultThree, _mm_mul_ps(SSEResultTwo, SSEResultOne));
|
||||
#else
|
||||
Result.X = (Left.X * Right.W) + (Left.Y * Right.Z) - (Left.Z * Right.Y) + (Left.W * Right.X);
|
||||
Result.Y = (-Left.X * Right.Z) + (Left.Y * Right.W) + (Left.Z * Right.X) + (Left.W * Right.Y);
|
||||
Result.Z = (Left.X * Right.Y) - (Left.Y * Right.X) + (Left.Z * Right.W) + (Left.W * Right.Z);
|
||||
Result.W = (-Left.X * Right.X) - (Left.Y * Right.Y) - (Left.Z * Right.Z) + (Left.W * Right.W);
|
||||
#endif
|
||||
|
||||
return (Result);
|
||||
}
|
||||
@@ -1329,15 +1379,10 @@ HMM_INLINE hmm_quaternion HMM_MultiplyQuaternionF(hmm_quaternion Left, float Mul
|
||||
{
|
||||
hmm_quaternion Result;
|
||||
|
||||
#ifdef HANDMADE_MATH__USE_SSE
|
||||
__m128 Scalar = _mm_set1_ps(Multiplicative);
|
||||
Result.InternalElementsSSE = _mm_mul_ps(Left.InternalElementsSSE, Scalar);
|
||||
#else
|
||||
Result.X = Left.X * Multiplicative;
|
||||
Result.Y = Left.Y * Multiplicative;
|
||||
Result.Z = Left.Z * Multiplicative;
|
||||
Result.W = Left.W * Multiplicative;
|
||||
#endif
|
||||
|
||||
return (Result);
|
||||
}
|
||||
@@ -1346,15 +1391,10 @@ HMM_INLINE hmm_quaternion HMM_DivideQuaternionF(hmm_quaternion Left, float Divid
|
||||
{
|
||||
hmm_quaternion Result;
|
||||
|
||||
#ifdef HANDMADE_MATH__USE_SSE
|
||||
__m128 Scalar = _mm_set1_ps(Dividend);
|
||||
Result.InternalElementsSSE = _mm_div_ps(Left.InternalElementsSSE, Scalar);
|
||||
#else
|
||||
Result.X = Left.X / Dividend;
|
||||
Result.Y = Left.Y / Dividend;
|
||||
Result.Z = Left.Z / Dividend;
|
||||
Result.W = Left.W / Dividend;
|
||||
#endif
|
||||
|
||||
return (Result);
|
||||
}
|
||||
@@ -1363,18 +1403,7 @@ HMM_EXTERN hmm_quaternion HMM_InverseQuaternion(hmm_quaternion Left);
|
||||
|
||||
HMM_INLINE float HMM_DotQuaternion(hmm_quaternion Left, hmm_quaternion Right)
|
||||
{
|
||||
float Result;
|
||||
|
||||
#ifdef HANDMADE_MATH__USE_SSE
|
||||
__m128 SSEResultOne = _mm_mul_ps(Left.InternalElementsSSE, Right.InternalElementsSSE);
|
||||
__m128 SSEResultTwo = _mm_shuffle_ps(SSEResultOne, SSEResultOne, _MM_SHUFFLE(2, 3, 0, 1));
|
||||
SSEResultOne = _mm_add_ps(SSEResultOne, SSEResultTwo);
|
||||
SSEResultTwo = _mm_shuffle_ps(SSEResultOne, SSEResultOne, _MM_SHUFFLE(0, 1, 2, 3));
|
||||
SSEResultOne = _mm_add_ps(SSEResultOne, SSEResultTwo);
|
||||
_mm_store_ss(&Result, SSEResultOne);
|
||||
#else
|
||||
Result = (Left.X * Right.X) + (Left.Y * Right.Y) + (Left.Z * Right.Z) + (Left.W * Right.W);
|
||||
#endif
|
||||
float Result = (Left.X * Right.X) + (Left.Y * Right.Y) + (Left.Z * Right.Z) + (Left.W * Right.W);
|
||||
|
||||
return (Result);
|
||||
}
|
||||
@@ -1393,18 +1422,11 @@ HMM_INLINE hmm_quaternion HMM_NLerp(hmm_quaternion Left, float Time, hmm_quatern
|
||||
{
|
||||
hmm_quaternion Result;
|
||||
|
||||
#ifdef HANDMADE_MATH__USE_SSE
|
||||
__m128 ScalarLeft = _mm_set1_ps(1.0f - Time);
|
||||
__m128 ScalarRight = _mm_set1_ps(Time);
|
||||
__m128 SSEResultOne = _mm_mul_ps(Left.InternalElementsSSE, ScalarLeft);
|
||||
__m128 SSEResultTwo = _mm_mul_ps(Right.InternalElementsSSE, ScalarRight);
|
||||
Result.InternalElementsSSE = _mm_add_ps(SSEResultOne, SSEResultTwo);
|
||||
#else
|
||||
Result.X = HMM_Lerp(Left.X, Time, Right.X);
|
||||
Result.Y = HMM_Lerp(Left.Y, Time, Right.Y);
|
||||
Result.Z = HMM_Lerp(Left.Z, Time, Right.Z);
|
||||
Result.W = HMM_Lerp(Left.W, Time, Right.W);
|
||||
#endif
|
||||
|
||||
Result = HMM_NormalizeQuaternion(Result);
|
||||
|
||||
return (Result);
|
||||
@@ -1483,27 +1505,6 @@ HMM_INLINE hmm_vec4 HMM_Normalize(hmm_vec4 A)
|
||||
return (Result);
|
||||
}
|
||||
|
||||
HMM_INLINE hmm_vec2 HMM_FastNormalize(hmm_vec2 A)
|
||||
{
|
||||
hmm_vec2 Result = HMM_FastNormalizeVec2(A);
|
||||
|
||||
return (Result);
|
||||
}
|
||||
|
||||
HMM_INLINE hmm_vec3 HMM_FastNormalize(hmm_vec3 A)
|
||||
{
|
||||
hmm_vec3 Result = HMM_FastNormalizeVec3(A);
|
||||
|
||||
return (Result);
|
||||
}
|
||||
|
||||
HMM_INLINE hmm_vec4 HMM_FastNormalize(hmm_vec4 A)
|
||||
{
|
||||
hmm_vec4 Result = HMM_FastNormalizeVec4(A);
|
||||
|
||||
return (Result);
|
||||
}
|
||||
|
||||
HMM_INLINE hmm_quaternion HMM_Normalize(hmm_quaternion A)
|
||||
{
|
||||
hmm_quaternion Result = HMM_NormalizeQuaternion(A);
|
||||
@@ -2253,10 +2254,10 @@ hmm_mat4 HMM_MultiplyMat4(hmm_mat4 Left, hmm_mat4 Right)
|
||||
|
||||
#ifdef HANDMADE_MATH__USE_SSE
|
||||
|
||||
Result.Columns[0] = HMM_LinearCombineSSE(Right.Columns[0], Left);
|
||||
Result.Columns[1] = HMM_LinearCombineSSE(Right.Columns[1], Left);
|
||||
Result.Columns[2] = HMM_LinearCombineSSE(Right.Columns[2], Left);
|
||||
Result.Columns[3] = HMM_LinearCombineSSE(Right.Columns[3], Left);
|
||||
Result.Rows[0] = HMM_LinearCombineSSE(Right.Rows[0], Left);
|
||||
Result.Rows[1] = HMM_LinearCombineSSE(Right.Rows[1], Left);
|
||||
Result.Rows[2] = HMM_LinearCombineSSE(Right.Rows[2], Left);
|
||||
Result.Rows[3] = HMM_LinearCombineSSE(Right.Rows[3], Left);
|
||||
|
||||
#else
|
||||
int Columns;
|
||||
@@ -2341,6 +2342,42 @@ hmm_mat4 HMM_DivideMat4f(hmm_mat4 Matrix, float Scalar)
|
||||
}
|
||||
#endif
|
||||
|
||||
hmm_quaternion HMM_Mat4ToQuaternion(hmm_mat4 m)
|
||||
{
|
||||
hmm_quaternion q;
|
||||
|
||||
float trace = m.Elements[0][0] + m.Elements[1][1] + m.Elements[2][2];
|
||||
if (trace > 0) {
|
||||
float s = 0.5f / HMM_SquareRootF(trace + 1.0f);
|
||||
q.X = (m.Elements[1][2] - m.Elements[2][1] ) * s;
|
||||
q.Y = (m.Elements[2][0] - m.Elements[0][2] ) * s;
|
||||
q.Z = (m.Elements[0][1] - m.Elements[1][0] ) * s;
|
||||
q.W = 0.25f / s;
|
||||
} else {
|
||||
if (m.Elements[0][0] > m.Elements[1][1] && m.Elements[0][0] > m.Elements[2][2]) {
|
||||
float s = 2.0f * HMM_SquareRootF(1.0f + m.Elements[0][0] - m.Elements[1][1] - m.Elements[2][2]);
|
||||
q.X = 0.25f * s;
|
||||
q.Y = (m.Elements[1][0] + m.Elements[0][1]) / s;
|
||||
q.Z = (m.Elements[2][0] + m.Elements[0][2]) / s;
|
||||
q.W = (m.Elements[1][2] - m.Elements[2][1]) / s;
|
||||
} else if (m.Elements[1][1] > m.Elements[2][2]) {
|
||||
float s = 2.0f * HMM_SquareRootF(1.0f + m.Elements[1][1] - m.Elements[0][0] - m.Elements[2][2]);
|
||||
q.X = (m.Elements[1][0] + m.Elements[0][1]) / s;
|
||||
q.Y = 0.25f * s;
|
||||
q.Z = (m.Elements[2][1] + m.Elements[1][2]) / s;
|
||||
q.W = (m.Elements[2][0] - m.Elements[0][2]) / s;
|
||||
} else {
|
||||
float s = 2.0f * HMM_SquareRootF(1.0f + m.Elements[2][2] - m.Elements[0][0] - m.Elements[1][1]);
|
||||
q.X = (m.Elements[2][0] + m.Elements[0][2]) / s;
|
||||
q.Y = (m.Elements[2][1] + m.Elements[1][2]) / s;
|
||||
q.Z = 0.25f * s;
|
||||
q.W = (m.Elements[0][1] - m.Elements[1][0]) / s;
|
||||
}
|
||||
}
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
hmm_mat4 HMM_Rotate(float Angle, hmm_vec3 Axis)
|
||||
{
|
||||
hmm_mat4 Result = HMM_Mat4d(1.0f);
|
||||
@@ -2412,7 +2449,10 @@ hmm_quaternion HMM_InverseQuaternion(hmm_quaternion Left)
|
||||
Norm = HMM_SquareRootF(HMM_DotQuaternion(Left, Left));
|
||||
NormSquared = Norm * Norm;
|
||||
|
||||
Result = HMM_DivideQuaternionF(Conjugate, NormSquared);
|
||||
Result.X = Conjugate.X / NormSquared;
|
||||
Result.Y = Conjugate.Y / NormSquared;
|
||||
Result.Z = Conjugate.Z / NormSquared;
|
||||
Result.W = Conjugate.W / NormSquared;
|
||||
|
||||
return (Result);
|
||||
}
|
||||
|
||||
@@ -10,10 +10,6 @@ To get started, go download [the latest release](https://github.com/HandmadeMath
|
||||
|
||||
Version | Changes |
|
||||
----------------|----------------|
|
||||
**1.9.0** | Added SSE versions of quaternion operations. |
|
||||
**1.8.0** | Added fast vector normalization routines that use fast inverse square roots.
|
||||
**1.7.1** | Changed operator[] to take a const ref int instead of an int.
|
||||
**1.7.0** | Renamed the 'Rows' member of hmm_mat4 to 'Columns'. Since our matrices are column-major, this should have been named 'Columns' from the start. 'Rows' is still present, but has been deprecated.
|
||||
**1.6.0** | Added array subscript operators for vector and matrix types in C++. This is provided as a convenience, but be aware that it may incur an extra function call in unoptimized builds.
|
||||
**1.5.1** | Fixed a bug with uninitialized elements in HMM_LookAt.
|
||||
**1.5.0** | Changed internal structure for better performance and inlining. As a result, `HANDMADE_MATH_NO_INLINE` has been removed and no longer has any effect.
|
||||
|
||||
154
test/categories/MatrixOps.h
Normal file
154
test/categories/MatrixOps.h
Normal file
@@ -0,0 +1,154 @@
|
||||
#include "../HandmadeTest.h"
|
||||
|
||||
void printQuat(hmm_quaternion quat) {
|
||||
printf("\n%f %f %f %f", quat.X, quat.Y, quat.Z, quat.W);
|
||||
}
|
||||
|
||||
TEST(MatrixOps, Transpose)
|
||||
{
|
||||
hmm_mat4 m4 = HMM_Mat4(); // will have 1 - 16
|
||||
|
||||
// Fill the matrix
|
||||
int Counter = 1;
|
||||
for (int Column = 0; Column < 4; ++Column)
|
||||
{
|
||||
for (int Row = 0; Row < 4; ++Row)
|
||||
{
|
||||
m4.Elements[Column][Row] = Counter;
|
||||
++Counter;
|
||||
}
|
||||
}
|
||||
|
||||
// Test the matrix
|
||||
hmm_mat4 result = HMM_Transpose(m4);
|
||||
EXPECT_FLOAT_EQ(result.Elements[0][0], 1.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[0][1], 5.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[0][2], 9.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[0][3], 13.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[1][0], 2.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[1][1], 6.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[1][2], 10.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[1][3], 14.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[2][0], 3.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[2][1], 7.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[2][2], 11.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[2][3], 15.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[3][0], 4.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[3][1], 8.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[3][2], 12.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[3][3], 16.0f);
|
||||
}
|
||||
|
||||
TEST(MatrixOps, ToQuaternion)
|
||||
{
|
||||
{ // Test 90 degree rotation about X axis
|
||||
hmm_mat4 rot = {
|
||||
1.0f, 0.0f, 0.0f, 0.0f, // first column (X)
|
||||
0.0f, 0.0f, 1.0f, 0.0f, // second column (Y)
|
||||
0.0f, -1.0f, 0.0f, 0.0f, // third column (Z)
|
||||
0.0f, 0.0f, 0.0f, 0.0f
|
||||
};
|
||||
|
||||
hmm_quaternion expected = HMM_QuaternionFromAxisAngle(HMM_Vec3(1.0f, 0.0f, 0.0f), HMM_ToRadians(90.0f));
|
||||
hmm_quaternion actualResult = HMM_Mat4ToQuaternion(rot);
|
||||
|
||||
EXPECT_FLOAT_EQ(actualResult.X, expected.X);
|
||||
EXPECT_FLOAT_EQ(actualResult.Y, expected.Y);
|
||||
EXPECT_FLOAT_EQ(actualResult.Z, expected.Z);
|
||||
EXPECT_FLOAT_EQ(actualResult.W, expected.W);
|
||||
}
|
||||
|
||||
{ // Test 90 degree rotation about Y axis
|
||||
hmm_mat4 rot = {
|
||||
0.0f, 0.0f, -1.0f, 0.0f, // first column (X)
|
||||
0.0f, 1.0f, 0.0f, 0.0f, // second column (Y)
|
||||
1.0f, 0.0f, 0.0f, 0.0f, // third column (Z)
|
||||
0.0f, 0.0f, 0.0f, 0.0f
|
||||
};
|
||||
|
||||
hmm_quaternion expected = HMM_QuaternionFromAxisAngle(HMM_Vec3(0.0f, 1.0f, 0.0f), HMM_ToRadians(90.0f));
|
||||
hmm_quaternion actualResult = HMM_Mat4ToQuaternion(rot);
|
||||
|
||||
EXPECT_FLOAT_EQ(actualResult.X, expected.X);
|
||||
EXPECT_FLOAT_EQ(actualResult.Y, expected.Y);
|
||||
EXPECT_FLOAT_EQ(actualResult.Z, expected.Z);
|
||||
EXPECT_FLOAT_EQ(actualResult.W, expected.W);
|
||||
}
|
||||
|
||||
{ // Test 90 degree rotation about Z axis
|
||||
hmm_mat4 rot = {
|
||||
0.0f, 1.0f, 0.0f, 0.0f, // first column (X)
|
||||
-1.0f, 0.0f, 0.0f, 0.0f, // second column (Y)
|
||||
0.0f, 0.0f, 1.0f, 0.0f, // third column (Z)
|
||||
0.0f, 0.0f, 0.0f, 0.0f
|
||||
};
|
||||
|
||||
hmm_quaternion expected = HMM_QuaternionFromAxisAngle(HMM_Vec3(0.0f, 0.0f, 1.0f), HMM_ToRadians(90.0f));
|
||||
hmm_quaternion actualResult = HMM_Mat4ToQuaternion(rot);
|
||||
|
||||
EXPECT_FLOAT_EQ(actualResult.X, expected.X);
|
||||
EXPECT_FLOAT_EQ(actualResult.Y, expected.Y);
|
||||
EXPECT_FLOAT_EQ(actualResult.Z, expected.Z);
|
||||
EXPECT_FLOAT_EQ(actualResult.W, expected.W);
|
||||
}
|
||||
|
||||
{ // Test 180 degree rotation about X axis
|
||||
hmm_mat4 rot = {
|
||||
1.0f, 0.0f, 0.0f, 0.0f, // first column (X)
|
||||
0.0f, -1.0f, 1.0f, 0.0f, // second column (Y)
|
||||
0.0f, 0.0f, -1.0f, 0.0f, // third column (Z)
|
||||
0.0f, 0.0f, 0.0f, 0.0f
|
||||
};
|
||||
|
||||
hmm_quaternion expected = HMM_QuaternionFromAxisAngle(HMM_Vec3(1.0f, 0.0f, 0.0f), HMM_ToRadians(180.0f));
|
||||
hmm_quaternion actualResult = HMM_Mat4ToQuaternion(rot);
|
||||
|
||||
printQuat(expected);
|
||||
printQuat(actualResult);
|
||||
|
||||
EXPECT_FLOAT_EQ(actualResult.X, expected.X);
|
||||
EXPECT_FLOAT_EQ(actualResult.Y, expected.Y);
|
||||
EXPECT_FLOAT_EQ(actualResult.Z, expected.Z);
|
||||
EXPECT_FLOAT_EQ(actualResult.W, expected.W);
|
||||
}
|
||||
|
||||
{ // Test 180 degree rotation about Y axis
|
||||
hmm_mat4 rot = {
|
||||
-1.0f, 0.0f, 0.0f, 0.0f, // first column (X)
|
||||
0.0f, 1.0f, 1.0f, 0.0f, // second column (Y)
|
||||
0.0f, 0.0f, -1.0f, 0.0f, // third column (Z)
|
||||
0.0f, 0.0f, 0.0f, 0.0f
|
||||
};
|
||||
|
||||
hmm_quaternion expected = HMM_QuaternionFromAxisAngle(HMM_Vec3(0.0f, 1.0f, 0.0f), HMM_ToRadians(180.0f));
|
||||
hmm_quaternion actualResult = HMM_Mat4ToQuaternion(rot);
|
||||
|
||||
printQuat(expected);
|
||||
printQuat(actualResult);
|
||||
|
||||
EXPECT_FLOAT_EQ(actualResult.X, expected.X);
|
||||
EXPECT_FLOAT_EQ(actualResult.Y, expected.Y);
|
||||
EXPECT_FLOAT_EQ(actualResult.Z, expected.Z);
|
||||
EXPECT_FLOAT_EQ(actualResult.W, expected.W);
|
||||
}
|
||||
|
||||
{ // Test 180 degree rotation about Z axis
|
||||
hmm_mat4 rot = {
|
||||
-1.0f, 0.0f, 0.0f, 0.0f, // first column (X)
|
||||
0.0f, -1.0f, 1.0f, 0.0f, // second column (Y)
|
||||
0.0f, 0.0f, 1.0f, 0.0f, // third column (Z)
|
||||
0.0f, 0.0f, 0.0f, 0.0f
|
||||
};
|
||||
|
||||
hmm_quaternion expected = HMM_QuaternionFromAxisAngle(HMM_Vec3(0.0f, 0.0f, 1.0f), HMM_ToRadians(180.0f));
|
||||
hmm_quaternion actualResult = HMM_Mat4ToQuaternion(rot);
|
||||
|
||||
printQuat(expected);
|
||||
printQuat(actualResult);
|
||||
|
||||
EXPECT_FLOAT_EQ(actualResult.X, expected.X);
|
||||
EXPECT_FLOAT_EQ(actualResult.Y, expected.Y);
|
||||
EXPECT_FLOAT_EQ(actualResult.Z, expected.Z);
|
||||
EXPECT_FLOAT_EQ(actualResult.W, expected.W);
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,10 @@ TEST(SSE, LinearCombine)
|
||||
hmm_mat4 MatrixTwo = HMM_Mat4d(4.0f);
|
||||
hmm_mat4 Result;
|
||||
|
||||
Result.Columns[0] = HMM_LinearCombineSSE(MatrixOne.Columns[0], MatrixTwo);
|
||||
Result.Columns[1] = HMM_LinearCombineSSE(MatrixOne.Columns[1], MatrixTwo);
|
||||
Result.Columns[2] = HMM_LinearCombineSSE(MatrixOne.Columns[2], MatrixTwo);
|
||||
Result.Columns[3] = HMM_LinearCombineSSE(MatrixOne.Columns[3], MatrixTwo);
|
||||
Result.Rows[0] = HMM_LinearCombineSSE(MatrixOne.Rows[0], MatrixTwo);
|
||||
Result.Rows[1] = HMM_LinearCombineSSE(MatrixOne.Rows[1], MatrixTwo);
|
||||
Result.Rows[2] = HMM_LinearCombineSSE(MatrixOne.Rows[2], MatrixTwo);
|
||||
Result.Rows[3] = HMM_LinearCombineSSE(MatrixOne.Rows[3], MatrixTwo);
|
||||
|
||||
{
|
||||
EXPECT_FLOAT_EQ(Result.Elements[0][0], 8.0f);
|
||||
@@ -23,12 +23,14 @@ TEST(SSE, LinearCombine)
|
||||
EXPECT_FLOAT_EQ(Result.Elements[1][1], 8.0f);
|
||||
EXPECT_FLOAT_EQ(Result.Elements[1][2], 0.0f);
|
||||
EXPECT_FLOAT_EQ(Result.Elements[1][3], 0.0f);
|
||||
|
||||
|
||||
EXPECT_FLOAT_EQ(Result.Elements[2][0], 0.0f);
|
||||
EXPECT_FLOAT_EQ(Result.Elements[2][1], 0.0f);
|
||||
EXPECT_FLOAT_EQ(Result.Elements[2][2], 8.0f);
|
||||
EXPECT_FLOAT_EQ(Result.Elements[2][3], 0.0f);
|
||||
|
||||
|
||||
EXPECT_FLOAT_EQ(Result.Elements[3][0], 0.0f);
|
||||
EXPECT_FLOAT_EQ(Result.Elements[3][1], 0.0f);
|
||||
EXPECT_FLOAT_EQ(Result.Elements[3][2], 0.0f);
|
||||
|
||||
@@ -134,106 +134,6 @@ TEST(VectorOps, NormalizeZero)
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(VectorOps, FastNormalize)
|
||||
{
|
||||
hmm_vec2 v2 = HMM_Vec2(1.0f, -2.0f);
|
||||
hmm_vec3 v3 = HMM_Vec3(1.0f, -2.0f, 3.0f);
|
||||
hmm_vec4 v4 = HMM_Vec4(1.0f, -2.0f, 3.0f, -1.0f);
|
||||
|
||||
{
|
||||
hmm_vec2 result = HMM_FastNormalizeVec2(v2);
|
||||
EXPECT_NEAR(HMM_LengthVec2(result), 1.0f, 0.001f);
|
||||
EXPECT_GT(result.X, 0.0f);
|
||||
EXPECT_LT(result.Y, 0.0f);
|
||||
}
|
||||
{
|
||||
hmm_vec3 result = HMM_FastNormalizeVec3(v3);
|
||||
EXPECT_NEAR(HMM_LengthVec3(result), 1.0f, 0.001f);
|
||||
EXPECT_GT(result.X, 0.0f);
|
||||
EXPECT_LT(result.Y, 0.0f);
|
||||
EXPECT_GT(result.Z, 0.0f);
|
||||
}
|
||||
{
|
||||
hmm_vec4 result = HMM_FastNormalizeVec4(v4);
|
||||
EXPECT_NEAR(HMM_LengthVec4(result), 1.0f, 0.001f);
|
||||
EXPECT_GT(result.X, 0.0f);
|
||||
EXPECT_LT(result.Y, 0.0f);
|
||||
EXPECT_GT(result.Z, 0.0f);
|
||||
EXPECT_LT(result.W, 0.0f);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
{
|
||||
hmm_vec2 result = HMM_FastNormalize(v2);
|
||||
EXPECT_NEAR(HMM_LengthVec2(result), 1.0f, 0.001f);
|
||||
EXPECT_GT(result.X, 0.0f);
|
||||
EXPECT_LT(result.Y, 0.0f);
|
||||
}
|
||||
{
|
||||
hmm_vec3 result = HMM_FastNormalize(v3);
|
||||
EXPECT_NEAR(HMM_LengthVec3(result), 1.0f, 0.001f);
|
||||
EXPECT_GT(result.X, 0.0f);
|
||||
EXPECT_LT(result.Y, 0.0f);
|
||||
EXPECT_GT(result.Z, 0.0f);
|
||||
}
|
||||
{
|
||||
hmm_vec4 result = HMM_FastNormalize(v4);
|
||||
EXPECT_NEAR(HMM_LengthVec4(result), 1.0f, 0.001f);
|
||||
EXPECT_GT(result.X, 0.0f);
|
||||
EXPECT_LT(result.Y, 0.0f);
|
||||
EXPECT_GT(result.Z, 0.0f);
|
||||
EXPECT_LT(result.W, 0.0f);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(VectorOps, FastNormalizeZero)
|
||||
{
|
||||
hmm_vec2 v2 = HMM_Vec2(0.0f, 0.0f);
|
||||
hmm_vec3 v3 = HMM_Vec3(0.0f, 0.0f, 0.0f);
|
||||
hmm_vec4 v4 = HMM_Vec4(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
|
||||
{
|
||||
hmm_vec2 result = HMM_FastNormalizeVec2(v2);
|
||||
EXPECT_FLOAT_EQ(result.X, 0.0f);
|
||||
EXPECT_FLOAT_EQ(result.Y, 0.0f);
|
||||
}
|
||||
{
|
||||
hmm_vec3 result = HMM_FastNormalizeVec3(v3);
|
||||
EXPECT_FLOAT_EQ(result.X, 0.0f);
|
||||
EXPECT_FLOAT_EQ(result.Y, 0.0f);
|
||||
EXPECT_FLOAT_EQ(result.Z, 0.0f);
|
||||
}
|
||||
{
|
||||
hmm_vec4 result = HMM_FastNormalizeVec4(v4);
|
||||
EXPECT_FLOAT_EQ(result.X, 0.0f);
|
||||
EXPECT_FLOAT_EQ(result.Y, 0.0f);
|
||||
EXPECT_FLOAT_EQ(result.Z, 0.0f);
|
||||
EXPECT_FLOAT_EQ(result.W, 0.0f);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
{
|
||||
hmm_vec2 result = HMM_FastNormalize(v2);
|
||||
EXPECT_FLOAT_EQ(result.X, 0.0f);
|
||||
EXPECT_FLOAT_EQ(result.Y, 0.0f);
|
||||
}
|
||||
{
|
||||
hmm_vec3 result = HMM_FastNormalize(v3);
|
||||
EXPECT_FLOAT_EQ(result.X, 0.0f);
|
||||
EXPECT_FLOAT_EQ(result.Y, 0.0f);
|
||||
EXPECT_FLOAT_EQ(result.Z, 0.0f);
|
||||
}
|
||||
{
|
||||
hmm_vec4 result = HMM_FastNormalize(v4);
|
||||
EXPECT_FLOAT_EQ(result.X, 0.0f);
|
||||
EXPECT_FLOAT_EQ(result.Y, 0.0f);
|
||||
EXPECT_FLOAT_EQ(result.Z, 0.0f);
|
||||
EXPECT_FLOAT_EQ(result.W, 0.0f);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(VectorOps, Cross)
|
||||
{
|
||||
hmm_vec3 v1 = HMM_Vec3(1.0f, 2.0f, 3.0f);
|
||||
@@ -278,43 +178,3 @@ TEST(VectorOps, DotVec4)
|
||||
EXPECT_FLOAT_EQ(HMM_Dot(v1, v2), 70.0f);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* MatrixOps tests
|
||||
*/
|
||||
|
||||
TEST(MatrixOps, Transpose)
|
||||
{
|
||||
hmm_mat4 m4 = HMM_Mat4(); // will have 1 - 16
|
||||
|
||||
// Fill the matrix
|
||||
int Counter = 1;
|
||||
for (int Column = 0; Column < 4; ++Column)
|
||||
{
|
||||
for (int Row = 0; Row < 4; ++Row)
|
||||
{
|
||||
m4.Elements[Column][Row] = Counter;
|
||||
++Counter;
|
||||
}
|
||||
}
|
||||
|
||||
// Test the matrix
|
||||
hmm_mat4 result = HMM_Transpose(m4);
|
||||
EXPECT_FLOAT_EQ(result.Elements[0][0], 1.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[0][1], 5.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[0][2], 9.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[0][3], 13.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[1][0], 2.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[1][1], 6.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[1][2], 10.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[1][3], 14.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[2][0], 3.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[2][1], 7.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[2][2], 11.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[2][3], 15.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[3][0], 4.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[3][1], 8.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[3][2], 12.0f);
|
||||
EXPECT_FLOAT_EQ(result.Elements[3][3], 16.0f);
|
||||
}
|
||||
|
||||
@@ -3,15 +3,16 @@
|
||||
#include "HandmadeTest.h"
|
||||
#include "../HandmadeMath.h"
|
||||
|
||||
#include "categories/ScalarMath.h"
|
||||
#include "categories/Initialization.h"
|
||||
#include "categories/VectorOps.h"
|
||||
#include "categories/QuaternionOps.h"
|
||||
#include "categories/Addition.h"
|
||||
#include "categories/Subtraction.h"
|
||||
#include "categories/Multiplication.h"
|
||||
#include "categories/Division.h"
|
||||
#include "categories/Equality.h"
|
||||
#include "categories/Initialization.h"
|
||||
#include "categories/MatrixOps.h"
|
||||
#include "categories/Multiplication.h"
|
||||
#include "categories/Projection.h"
|
||||
#include "categories/Transformation.h"
|
||||
#include "categories/QuaternionOps.h"
|
||||
#include "categories/ScalarMath.h"
|
||||
#include "categories/SSE.h"
|
||||
#include "categories/Subtraction.h"
|
||||
#include "categories/Transformation.h"
|
||||
#include "categories/VectorOps.h"
|
||||
|
||||
Reference in New Issue
Block a user