From bcf7937eec0c182cdd781e4b93a49dd5ba5c8746 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 25 Aug 2016 14:08:46 -0500 Subject: [PATCH 1/5] Ensure column-major order for matrices and fix HMM_Translate Most of the changes in this patch are cosmetic (such as looping by columns first.) HMM_Translate was incorrectly producing row-major matrices, which should now be fixed. Fixes #22 --- HandmadeMath.h | 77 +++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/HandmadeMath.h b/HandmadeMath.h index 860bea6..33ed44e 100644 --- a/HandmadeMath.h +++ b/HandmadeMath.h @@ -1,5 +1,5 @@ /* - HandmadeMath.h v0.5 + HandmadeMath.h v0.5.1 This is a single header file with a bunch of useful functions for basic game math operations. @@ -116,6 +116,9 @@ (*) 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 @@ -987,16 +990,6 @@ HMM_Mat4d(float Diagonal) { hmm_mat4 Result = HMM_Mat4(); - int Rows; - for(Rows = 0; Rows < 4; ++Rows) - { - int Columns; - for(Columns = 0; Columns < 4; ++Columns) - { - Result.Elements[Rows][Columns] = 0.0f; - } - } - Result.Elements[0][0] = Diagonal; Result.Elements[1][1] = Diagonal; Result.Elements[2][2] = Diagonal; @@ -1010,13 +1003,13 @@ HMM_AddMat4(hmm_mat4 Left, hmm_mat4 Right) { hmm_mat4 Result = HMM_Mat4(); - int Rows; - for (Rows = 0; Rows < 4; ++Rows) + int Columns; + for(Columns = 0; Columns < 4; ++Columns) { - int Columns; - for (Columns = 0; Columns < 4; ++Columns) + int Rows; + for(Rows = 0; Rows < 4; ++Rows) { - Result.Elements[Rows][Columns] = Left.Elements[Rows][Columns] + Right.Elements[Rows][Columns]; + Result.Elements[Columns][Rows] = Left.Elements[Columns][Rows] + Right.Elements[Columns][Rows]; } } @@ -1028,13 +1021,13 @@ HMM_SubtractMat4(hmm_mat4 Left, hmm_mat4 Right) { hmm_mat4 Result = HMM_Mat4(); - int Rows; - for (Rows = 0; Rows < 4; ++Rows) + int Columns; + for(Columns = 0; Columns < 4; ++Columns) { - int Columns; - for (Columns = 0; Columns < 4; ++Columns) + int Rows; + for(Rows = 0; Rows < 4; ++Rows) { - Result.Elements[Rows][Columns] = Left.Elements[Rows][Columns] - Right.Elements[Rows][Columns]; + Result.Elements[Columns][Rows] = Left.Elements[Columns][Rows] - Right.Elements[Columns][Rows]; } } @@ -1046,20 +1039,20 @@ HMM_MultiplyMat4(hmm_mat4 Left, hmm_mat4 Right) { hmm_mat4 Result = HMM_Mat4(); - int Rows; - for(Rows = 0; Rows < 4; ++Rows) + int Columns; + for(Columns = 0; Columns < 4; ++Columns) { - int Columns; - for(Columns = 0; Columns < 4; ++Columns) + int Rows; + for(Rows = 0; Rows < 4; ++Rows) { float Sum = 0; int CurrentMatrice; for(CurrentMatrice = 0; CurrentMatrice < 4; ++CurrentMatrice) { - Sum += Right.Elements[Rows][CurrentMatrice] * Left.Elements[CurrentMatrice][Columns]; + Sum += Left.Elements[CurrentMatrice][Rows] * Right.Elements[Columns][CurrentMatrice]; } - Result.Elements[Rows][Columns] = Sum; + Result.Elements[Columns][Rows] = Sum; } } @@ -1071,13 +1064,13 @@ HMM_MultiplyMat4f(hmm_mat4 Matrix, float Scalar) { hmm_mat4 Result = HMM_Mat4(); - int Rows; - for (Rows = 0; Rows < 4; ++Rows) + int Columns; + for(Columns = 0; Columns < 4; ++Columns) { - int Columns; - for (Columns = 0; Columns < 4; ++Columns) + int Rows; + for(Rows = 0; Rows < 4; ++Rows) { - Result.Elements[Rows][Columns] = Matrix.Elements[Rows][Columns] * Scalar; + Result.Elements[Columns][Rows] = Matrix.Elements[Columns][Rows] * Scalar; } } @@ -1089,13 +1082,13 @@ HMM_MultiplyMat4ByVec4(hmm_mat4 Matrix, hmm_vec4 Vector) { hmm_vec4 Result = {0}; - int Rows, Columns; + int Columns, Rows; for(Rows = 0; Rows < 4; ++Rows) { float Sum = 0; for(Columns = 0; Columns < 4; ++Columns) { - Sum += Matrix.Elements[Rows][Columns] * Vector.Elements[Columns]; + Sum += Matrix.Elements[Columns][Rows] * Vector.Elements[Columns]; } Result.Elements[Rows] = Sum; @@ -1109,13 +1102,13 @@ HMM_DivideMat4f(hmm_mat4 Matrix, float Scalar) { hmm_mat4 Result = HMM_Mat4(); - int Rows; - for (Rows = 0; Rows < 4; ++Rows) + int Columns; + for(Columns = 0; Columns < 4; ++Columns) { - int Columns; - for (Columns = 0; Columns < 4; ++Columns) + int Rows; + for(Rows = 0; Rows < 4; ++Rows) { - Result.Elements[Rows][Columns] = Matrix.Elements[Rows][Columns] / Scalar; + Result.Elements[Columns][Rows] = Matrix.Elements[Columns][Rows] / Scalar; } } @@ -1160,9 +1153,9 @@ HMM_Translate(hmm_vec3 Translation) { hmm_mat4 Result = HMM_Mat4d(1.0f); - Result.Elements[0][3] = Translation.X; - Result.Elements[1][3] = Translation.Y; - Result.Elements[2][3] = Translation.Z; + Result.Elements[3][0] = Translation.X; + Result.Elements[3][1] = Translation.Y; + Result.Elements[3][2] = Translation.Z; return (Result); } From fa9f40184496a6bb1d06add89db0cbb5d5d81bc8 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 25 Aug 2016 14:22:46 -0500 Subject: [PATCH 2/5] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2427b3f..d9dd98e 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ Single-file public domain game math library for C/C++ Version | Changes | ----------------|----------------| +**0.5.1** | Fixed HMM_Translate producing row-major matrices, ensured column-major order for matrices throughout | **0.5** | Added scalar operations on vectors and matrices, added += and -= for hmm_mat4, reconciled headers and implementations, tidied up in general | **0.4** | Added SSE Optimized HMM_SqrtF, HMM_RSqrtF, Removed use of CRT | **0.3** | Added +=,-=, *=, /= for hmm_vec2, hmm_vec3, hmm_vec4 | From b638056bcd7c2af1c864257766d47a06f51d41d3 Mon Sep 17 00:00:00 2001 From: Zak Strange Date: Thu, 25 Aug 2016 16:11:03 -0700 Subject: [PATCH 3/5] Updated README.md and Fixed SSE code --- HandmadeMath.h | 14 ++++++++------ README.md | 14 +++++++++++--- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/HandmadeMath.h b/HandmadeMath.h index 33ed44e..efef2be 100644 --- a/HandmadeMath.h +++ b/HandmadeMath.h @@ -1,5 +1,5 @@ /* - HandmadeMath.h v0.5.1 + HandmadeMath.h v0.5.2 This is a single header file with a bunch of useful functions for basic game math operations. @@ -119,7 +119,9 @@ 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 LICENSE @@ -533,9 +535,9 @@ HMM_SqrtF(float Value) #ifdef HANDMADE_MATH_NO_SSE Result = sqrtf(Value); #else - __m128 In = _mm_load_ss(&Value); + __m128 In = _mm_set_ss(Value); __m128 Out = _mm_sqrt_ss(In); - _mm_store_ss(&Result, Out); + Result = _mm_cvtss_f32(Out); #endif return(Result); @@ -549,9 +551,9 @@ HMM_RSqrtF(float Value) #ifdef HANDMADE_MATH_NO_SSE Result = 1.0f/HMM_SqrtF(Value); #else - __m128 In = _mm_load_ss(&Value); + __m128 In = _mm_set_ss(Value); __m128 Out = _mm_rsqrt_ss(In); - _mm_store_ss(&Result, Out); + Result = _mm_cvtss_f32(Out); #endif return(Result); diff --git a/README.md b/README.md index d9dd98e..a1da414 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,28 @@ # Handmade-Math ------ -Single-file public domain game math library for C/C++ + +Single-file cross-platform public domain game math library for C/C++ Version | Changes | ----------------|----------------| +**0.5.2** | Fixed SSE code in HMM_SqrtF and HMM_RSqrtF | **0.5.1** | Fixed HMM_Translate producing row-major matrices, ensured column-major order for matrices throughout | **0.5** | Added scalar operations on vectors and matrices, added += and -= for hmm_mat4, reconciled headers and implementations, tidied up in general | -**0.4** | Added SSE Optimized HMM_SqrtF, HMM_RSqrtF, Removed use of CRT | +**0.4** | Added SSE Optimized HMM_SqrtF, HMM_RSqrtF, Removed use of C Runtime | **0.3** | Added +=,-=, *=, /= for hmm_vec2, hmm_vec3, hmm_vec4 | **0.2b** | Disabled warning C4201 on MSVC, Added 64bit percision on HMM_PI | **0.2a** | Prefixed Macros | **0.2** | Updated Documentation, Fixed C Compliance, Prefixed all functions, and added better operator overloading | **0.1** | Initial Version | -_ID: In Development_ +----- +_This library is free and will stay free, but if you would like to support development, or you are a company using HandmadeMath, please consider financial support._ + +![alt text](https://cloud.githubusercontent.com/assets/8225057/5990484/70413560-a9ab-11e4-8942-1a63607c0b00.png "Logo Title Text 1") + + + ## FAQ From c9107ad5de16c8137993b78581e9f071b9efd2c1 Mon Sep 17 00:00:00 2001 From: Zak Strange Date: Sun, 28 Aug 2016 10:24:35 -0700 Subject: [PATCH 4/5] Fixed README.md links --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a1da414..b9eba81 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,8 @@ Version | Changes | ----- _This library is free and will stay free, but if you would like to support development, or you are a company using HandmadeMath, please consider financial support._ -![alt text](https://cloud.githubusercontent.com/assets/8225057/5990484/70413560-a9ab-11e4-8942-1a63607c0b00.png "Logo Title Text 1") - - +[![Patreon](https://cloud.githubusercontent.com/assets/8225057/5990484/70413560-a9ab-11e4-8942-1a63607c0b00.png)](http://www.patreon.com/strangezak) [![PayPal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.me/zakarystrange) ## FAQ @@ -33,4 +31,4 @@ This library is in the public domain. You can do whatever you want with them. **Where can I contact you to ask questions?** -You can email me at: Zak@Handmade.Network \ No newline at end of file +You can email me at: Zak@Handmade.Network From 44be431b65b10f37545467f3d5536705d8408536 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sun, 28 Aug 2016 23:27:24 -0500 Subject: [PATCH 5/5] Fix issues that came up in testing Refs #25 --- HandmadeMath.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/HandmadeMath.h b/HandmadeMath.h index 860bea6..c3783b7 100644 --- a/HandmadeMath.h +++ b/HandmadeMath.h @@ -857,7 +857,7 @@ HMM_MultiplyVec3(hmm_vec3 Left, hmm_vec3 Right) { hmm_vec3 Result = {0}; - Result.X = Left.Z * Right.X; + Result.X = Left.X * Right.X; Result.Y = Left.Y * Right.Y; Result.Z = Left.Z * Right.Z; @@ -1095,7 +1095,7 @@ HMM_MultiplyMat4ByVec4(hmm_mat4 Matrix, hmm_vec4 Vector) float Sum = 0; for(Columns = 0; Columns < 4; ++Columns) { - Sum += Matrix.Elements[Rows][Columns] * Vector.Elements[Columns]; + Sum += Matrix.Elements[Columns][Rows] * Vector.Elements[Columns]; } Result.Elements[Rows] = Sum; @@ -1160,9 +1160,9 @@ HMM_Translate(hmm_vec3 Translation) { hmm_mat4 Result = HMM_Mat4d(1.0f); - Result.Elements[0][3] = Translation.X; - Result.Elements[1][3] = Translation.Y; - Result.Elements[2][3] = Translation.Z; + Result.Elements[3][0] = Translation.X; + Result.Elements[3][1] = Translation.Y; + Result.Elements[3][2] = Translation.Z; return (Result); }