mirror of
https://github.com/HandmadeMath/HandmadeMath.git
synced 2025-12-29 16:04:33 +00:00
Compare commits
6 Commits
operators
...
quaternion
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c7bffdccb | ||
|
|
a22d411267 | ||
|
|
2a050bc124 | ||
|
|
0e2bc9be4f | ||
|
|
ec5bc9c0d3 | ||
|
|
baf1a70fc6 |
106
HandmadeMath.h
106
HandmadeMath.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
HandmadeMath.h v1.7.1
|
HandmadeMath.h v1.6.0
|
||||||
|
|
||||||
This is a single header file with a bunch of useful functions for game and
|
This is a single header file with a bunch of useful functions for game and
|
||||||
graphics math operations.
|
graphics math operations.
|
||||||
@@ -172,14 +172,7 @@
|
|||||||
(*) Added array subscript operators for vector and matrix types in
|
(*) Added array subscript operators for vector and matrix types in
|
||||||
C++. This is provided as a convenience, but be aware that it may
|
C++. This is provided as a convenience, but be aware that it may
|
||||||
incur an extra function call in unoptimized builds.
|
incur an extra function call in unoptimized builds.
|
||||||
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.7.1
|
|
||||||
(*) Changed operator[] to take in a const ref int instead of a int.
|
|
||||||
Simple dumb mistake. NOTE: The compiler still wont inline operator[]
|
|
||||||
for some reason
|
|
||||||
|
|
||||||
LICENSE
|
LICENSE
|
||||||
|
|
||||||
@@ -327,7 +320,7 @@ typedef union hmm_vec2
|
|||||||
float Elements[2];
|
float Elements[2];
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
inline float &operator[](const int &Index)
|
inline float &operator[](int Index)
|
||||||
{
|
{
|
||||||
return Elements[Index];
|
return Elements[Index];
|
||||||
}
|
}
|
||||||
@@ -378,7 +371,7 @@ typedef union hmm_vec3
|
|||||||
float Elements[3];
|
float Elements[3];
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
inline float &operator[](const int &Index)
|
inline float &operator[](int Index)
|
||||||
{
|
{
|
||||||
return Elements[Index];
|
return Elements[Index];
|
||||||
}
|
}
|
||||||
@@ -442,7 +435,7 @@ typedef union hmm_vec4
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
inline float &operator[](const int &Index)
|
inline float &operator[](int Index)
|
||||||
{
|
{
|
||||||
return Elements[Index];
|
return Elements[Index];
|
||||||
}
|
}
|
||||||
@@ -454,15 +447,11 @@ typedef union hmm_mat4
|
|||||||
float Elements[4][4];
|
float Elements[4][4];
|
||||||
|
|
||||||
#ifdef HANDMADE_MATH__USE_SSE
|
#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];
|
__m128 Rows[4];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
inline hmm_vec4 operator[](const int &Index)
|
inline hmm_vec4 operator[](const int Index)
|
||||||
{
|
{
|
||||||
float* col = Elements[Index];
|
float* col = Elements[Index];
|
||||||
|
|
||||||
@@ -492,8 +481,17 @@ typedef union hmm_quaternion
|
|||||||
|
|
||||||
float W;
|
float W;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
hmm_vec4 XYZW;
|
||||||
|
};
|
||||||
|
|
||||||
float Elements[4];
|
float Elements[4];
|
||||||
|
|
||||||
|
#if HANDMADE_MATH__USE_SSE
|
||||||
|
_m128 InternalElementsSSE;
|
||||||
|
#endif
|
||||||
} hmm_quaternion;
|
} hmm_quaternion;
|
||||||
|
|
||||||
typedef int32_t hmm_bool;
|
typedef int32_t hmm_bool;
|
||||||
@@ -1140,10 +1138,10 @@ HMM_INLINE hmm_vec4 HMM_NormalizeVec4(hmm_vec4 A)
|
|||||||
HMM_INLINE __m128 HMM_LinearCombineSSE(__m128 Left, hmm_mat4 Right)
|
HMM_INLINE __m128 HMM_LinearCombineSSE(__m128 Left, hmm_mat4 Right)
|
||||||
{
|
{
|
||||||
__m128 Result;
|
__m128 Result;
|
||||||
Result = _mm_mul_ps(_mm_shuffle_ps(Left, Left, 0x00), Right.Columns[0]);
|
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.Columns[1]));
|
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.Columns[2]));
|
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.Columns[3]));
|
Result = _mm_add_ps(Result, _mm_mul_ps(_mm_shuffle_ps(Left, Left, 0xff), Right.Rows[3]));
|
||||||
|
|
||||||
return (Result);
|
return (Result);
|
||||||
}
|
}
|
||||||
@@ -1178,7 +1176,7 @@ HMM_INLINE hmm_mat4 HMM_Transpose(hmm_mat4 Matrix)
|
|||||||
{
|
{
|
||||||
hmm_mat4 Result = 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);
|
return (Result);
|
||||||
}
|
}
|
||||||
@@ -1191,10 +1189,10 @@ HMM_INLINE hmm_mat4 HMM_AddMat4(hmm_mat4 Left, hmm_mat4 Right)
|
|||||||
{
|
{
|
||||||
hmm_mat4 Result;
|
hmm_mat4 Result;
|
||||||
|
|
||||||
Result.Columns[0] = _mm_add_ps(Left.Columns[0], Right.Columns[0]);
|
Result.Rows[0] = _mm_add_ps(Left.Rows[0], Right.Rows[0]);
|
||||||
Result.Columns[1] = _mm_add_ps(Left.Columns[1], Right.Columns[1]);
|
Result.Rows[1] = _mm_add_ps(Left.Rows[1], Right.Rows[1]);
|
||||||
Result.Columns[2] = _mm_add_ps(Left.Columns[2], Right.Columns[2]);
|
Result.Rows[2] = _mm_add_ps(Left.Rows[2], Right.Rows[2]);
|
||||||
Result.Columns[3] = _mm_add_ps(Left.Columns[3], Right.Columns[3]);
|
Result.Rows[3] = _mm_add_ps(Left.Rows[3], Right.Rows[3]);
|
||||||
|
|
||||||
return (Result);
|
return (Result);
|
||||||
}
|
}
|
||||||
@@ -1207,10 +1205,10 @@ HMM_INLINE hmm_mat4 HMM_SubtractMat4(hmm_mat4 Left, hmm_mat4 Right)
|
|||||||
{
|
{
|
||||||
hmm_mat4 Result;
|
hmm_mat4 Result;
|
||||||
|
|
||||||
Result.Columns[0] = _mm_sub_ps(Left.Columns[0], Right.Columns[0]);
|
Result.Rows[0] = _mm_sub_ps(Left.Rows[0], Right.Rows[0]);
|
||||||
Result.Columns[1] = _mm_sub_ps(Left.Columns[1], Right.Columns[1]);
|
Result.Rows[1] = _mm_sub_ps(Left.Rows[1], Right.Rows[1]);
|
||||||
Result.Columns[2] = _mm_sub_ps(Left.Columns[2], Right.Columns[2]);
|
Result.Rows[2] = _mm_sub_ps(Left.Rows[2], Right.Rows[2]);
|
||||||
Result.Columns[3] = _mm_sub_ps(Left.Columns[3], Right.Columns[3]);
|
Result.Rows[3] = _mm_sub_ps(Left.Rows[3], Right.Rows[3]);
|
||||||
|
|
||||||
return (Result);
|
return (Result);
|
||||||
}
|
}
|
||||||
@@ -1226,10 +1224,10 @@ HMM_INLINE hmm_mat4 HMM_MultiplyMat4f(hmm_mat4 Matrix, float Scalar)
|
|||||||
hmm_mat4 Result;
|
hmm_mat4 Result;
|
||||||
|
|
||||||
__m128 SSEScalar = _mm_set1_ps(Scalar);
|
__m128 SSEScalar = _mm_set1_ps(Scalar);
|
||||||
Result.Columns[0] = _mm_mul_ps(Matrix.Columns[0], SSEScalar);
|
Result.Rows[0] = _mm_mul_ps(Matrix.Rows[0], SSEScalar);
|
||||||
Result.Columns[1] = _mm_mul_ps(Matrix.Columns[1], SSEScalar);
|
Result.Rows[1] = _mm_mul_ps(Matrix.Rows[1], SSEScalar);
|
||||||
Result.Columns[2] = _mm_mul_ps(Matrix.Columns[2], SSEScalar);
|
Result.Rows[2] = _mm_mul_ps(Matrix.Rows[2], SSEScalar);
|
||||||
Result.Columns[3] = _mm_mul_ps(Matrix.Columns[3], SSEScalar);
|
Result.Rows[3] = _mm_mul_ps(Matrix.Rows[3], SSEScalar);
|
||||||
|
|
||||||
return (Result);
|
return (Result);
|
||||||
}
|
}
|
||||||
@@ -1245,10 +1243,10 @@ HMM_INLINE hmm_mat4 HMM_DivideMat4f(hmm_mat4 Matrix, float Scalar)
|
|||||||
hmm_mat4 Result;
|
hmm_mat4 Result;
|
||||||
|
|
||||||
__m128 SSEScalar = _mm_set1_ps(Scalar);
|
__m128 SSEScalar = _mm_set1_ps(Scalar);
|
||||||
Result.Columns[0] = _mm_div_ps(Matrix.Columns[0], SSEScalar);
|
Result.Rows[0] = _mm_div_ps(Matrix.Rows[0], SSEScalar);
|
||||||
Result.Columns[1] = _mm_div_ps(Matrix.Columns[1], SSEScalar);
|
Result.Rows[1] = _mm_div_ps(Matrix.Rows[1], SSEScalar);
|
||||||
Result.Columns[2] = _mm_div_ps(Matrix.Columns[2], SSEScalar);
|
Result.Rows[2] = _mm_div_ps(Matrix.Rows[2], SSEScalar);
|
||||||
Result.Columns[3] = _mm_div_ps(Matrix.Columns[3], SSEScalar);
|
Result.Rows[3] = _mm_div_ps(Matrix.Rows[3], SSEScalar);
|
||||||
|
|
||||||
return (Result);
|
return (Result);
|
||||||
}
|
}
|
||||||
@@ -1328,11 +1326,14 @@ HMM_INLINE hmm_quaternion HMM_Quaternion(float X, float Y, float Z, float W)
|
|||||||
{
|
{
|
||||||
hmm_quaternion Result;
|
hmm_quaternion Result;
|
||||||
|
|
||||||
|
#if HANDMADE_MATH__USE_SSE
|
||||||
|
Result.InternalElementsSSE = _mm_setr_ps(X, Y, Z, W);
|
||||||
|
#else
|
||||||
Result.X = X;
|
Result.X = X;
|
||||||
Result.Y = Y;
|
Result.Y = Y;
|
||||||
Result.Z = Z;
|
Result.Z = Z;
|
||||||
Result.W = W;
|
Result.W = W;
|
||||||
|
#endif
|
||||||
return (Result);
|
return (Result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1340,11 +1341,15 @@ HMM_INLINE hmm_quaternion HMM_QuaternionV4(hmm_vec4 Vector)
|
|||||||
{
|
{
|
||||||
hmm_quaternion Result;
|
hmm_quaternion Result;
|
||||||
|
|
||||||
|
#if HANDMADE_MATH__USE_SSE
|
||||||
|
Result.InternalElementsSSE = _mm_setr_ps(Vector.X, Vector.Y, Vector.Z, Vector.W);
|
||||||
|
#else
|
||||||
Result.X = Vector.X;
|
Result.X = Vector.X;
|
||||||
Result.Y = Vector.Y;
|
Result.Y = Vector.Y;
|
||||||
Result.Z = Vector.Z;
|
Result.Z = Vector.Z;
|
||||||
Result.W = Vector.W;
|
Result.W = Vector.W;
|
||||||
|
#endif
|
||||||
|
|
||||||
return (Result);
|
return (Result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1352,11 +1357,14 @@ HMM_INLINE hmm_quaternion HMM_AddQuaternion(hmm_quaternion Left, hmm_quaternion
|
|||||||
{
|
{
|
||||||
hmm_quaternion Result;
|
hmm_quaternion Result;
|
||||||
|
|
||||||
|
#if HANDMADE_MATH__USE_SSE
|
||||||
|
Result.InternalElementsSSE = _mm_add_ps(Left.InternalElementsSSE, Right.InternalElementsSSE);
|
||||||
|
#else
|
||||||
Result.X = Left.X + Right.X;
|
Result.X = Left.X + Right.X;
|
||||||
Result.Y = Left.Y + Right.Y;
|
Result.Y = Left.Y + Right.Y;
|
||||||
Result.Z = Left.Z + Right.Z;
|
Result.Z = Left.Z + Right.Z;
|
||||||
Result.W = Left.W + Right.W;
|
Result.W = Left.W + Right.W;
|
||||||
|
#endif
|
||||||
return (Result);
|
return (Result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1364,11 +1372,15 @@ HMM_INLINE hmm_quaternion HMM_SubtractQuaternion(hmm_quaternion Left, hmm_quater
|
|||||||
{
|
{
|
||||||
hmm_quaternion Result;
|
hmm_quaternion Result;
|
||||||
|
|
||||||
|
#if HANDMADE_MATH__USE_SSE
|
||||||
|
Result.InternalElementsSSE = _mm_sub_ps(Left.InternalElementsSSE, Right.InternalElementsSSE);
|
||||||
|
#else
|
||||||
Result.X = Left.X - Right.X;
|
Result.X = Left.X - Right.X;
|
||||||
Result.Y = Left.Y - Right.Y;
|
Result.Y = Left.Y - Right.Y;
|
||||||
Result.Z = Left.Z - Right.Z;
|
Result.Z = Left.Z - Right.Z;
|
||||||
Result.W = Left.W - Right.W;
|
Result.W = Left.W - Right.W;
|
||||||
|
#endif
|
||||||
|
|
||||||
return (Result);
|
return (Result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2263,10 +2275,10 @@ hmm_mat4 HMM_MultiplyMat4(hmm_mat4 Left, hmm_mat4 Right)
|
|||||||
|
|
||||||
#ifdef HANDMADE_MATH__USE_SSE
|
#ifdef HANDMADE_MATH__USE_SSE
|
||||||
|
|
||||||
Result.Columns[0] = HMM_LinearCombineSSE(Right.Columns[0], Left);
|
Result.Rows[0] = HMM_LinearCombineSSE(Right.Rows[0], Left);
|
||||||
Result.Columns[1] = HMM_LinearCombineSSE(Right.Columns[1], Left);
|
Result.Rows[1] = HMM_LinearCombineSSE(Right.Rows[1], Left);
|
||||||
Result.Columns[2] = HMM_LinearCombineSSE(Right.Columns[2], Left);
|
Result.Rows[2] = HMM_LinearCombineSSE(Right.Rows[2], Left);
|
||||||
Result.Columns[3] = HMM_LinearCombineSSE(Right.Columns[3], Left);
|
Result.Rows[3] = HMM_LinearCombineSSE(Right.Rows[3], Left);
|
||||||
|
|
||||||
#else
|
#else
|
||||||
int Columns;
|
int Columns;
|
||||||
|
|||||||
@@ -10,8 +10,6 @@ To get started, go download [the latest release](https://github.com/HandmadeMath
|
|||||||
|
|
||||||
Version | Changes |
|
Version | Changes |
|
||||||
----------------|----------------|
|
----------------|----------------|
|
||||||
**1.7.1** | Changed operator[] to take in a const ref int instead of a int. Simple mistake
|
|
||||||
**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.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.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.
|
**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.
|
||||||
|
|||||||
@@ -8,10 +8,10 @@ TEST(SSE, LinearCombine)
|
|||||||
hmm_mat4 MatrixTwo = HMM_Mat4d(4.0f);
|
hmm_mat4 MatrixTwo = HMM_Mat4d(4.0f);
|
||||||
hmm_mat4 Result;
|
hmm_mat4 Result;
|
||||||
|
|
||||||
Result.Columns[0] = HMM_LinearCombineSSE(MatrixOne.Columns[0], MatrixTwo);
|
Result.Rows[0] = HMM_LinearCombineSSE(MatrixOne.Rows[0], MatrixTwo);
|
||||||
Result.Columns[1] = HMM_LinearCombineSSE(MatrixOne.Columns[1], MatrixTwo);
|
Result.Rows[1] = HMM_LinearCombineSSE(MatrixOne.Rows[1], MatrixTwo);
|
||||||
Result.Columns[2] = HMM_LinearCombineSSE(MatrixOne.Columns[2], MatrixTwo);
|
Result.Rows[2] = HMM_LinearCombineSSE(MatrixOne.Rows[2], MatrixTwo);
|
||||||
Result.Columns[3] = HMM_LinearCombineSSE(MatrixOne.Columns[3], MatrixTwo);
|
Result.Rows[3] = HMM_LinearCombineSSE(MatrixOne.Rows[3], MatrixTwo);
|
||||||
|
|
||||||
{
|
{
|
||||||
EXPECT_FLOAT_EQ(Result.Elements[0][0], 8.0f);
|
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][1], 8.0f);
|
||||||
EXPECT_FLOAT_EQ(Result.Elements[1][2], 0.0f);
|
EXPECT_FLOAT_EQ(Result.Elements[1][2], 0.0f);
|
||||||
EXPECT_FLOAT_EQ(Result.Elements[1][3], 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][0], 0.0f);
|
||||||
EXPECT_FLOAT_EQ(Result.Elements[2][1], 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][2], 8.0f);
|
||||||
EXPECT_FLOAT_EQ(Result.Elements[2][3], 0.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][0], 0.0f);
|
||||||
EXPECT_FLOAT_EQ(Result.Elements[3][1], 0.0f);
|
EXPECT_FLOAT_EQ(Result.Elements[3][1], 0.0f);
|
||||||
EXPECT_FLOAT_EQ(Result.Elements[3][2], 0.0f);
|
EXPECT_FLOAT_EQ(Result.Elements[3][2], 0.0f);
|
||||||
|
|||||||
Reference in New Issue
Block a user