From 2ea471b3cef9fc1a101d62cf4dc31a5b09fd1e4f Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 30 Aug 2016 18:53:53 -0500 Subject: [PATCH 1/2] Add HMM_Transpose --- HandmadeMath.h | 20 ++++++++++++++++++++ test/hmm_test.cpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/HandmadeMath.h b/HandmadeMath.h index 7981a46..687e0ed 100644 --- a/HandmadeMath.h +++ b/HandmadeMath.h @@ -393,6 +393,8 @@ HMMDEF hmm_mat4 HMM_MultiplyMat4f(hmm_mat4 Matrix, float Scalar); HMMDEF hmm_vec4 HMM_MultiplyMat4ByVec4(hmm_mat4 Matrix, hmm_vec4 Vector); HMMDEF hmm_mat4 HMM_DivideMat4f(hmm_mat4 Matrix, float Scalar); +HMMDEF hmm_mat4 HMM_Transpose(hmm_mat4 Matrix); + HMMDEF hmm_mat4 HMM_Orthographic(float Left, float Right, float Bottom, float Top, float Near, float Far); HMMDEF hmm_mat4 HMM_Perspective(float FOV, float AspectRatio, float Near, float Far); HMMDEF hmm_mat4 HMM_Translate(hmm_vec3 Translation); @@ -1135,6 +1137,24 @@ HMM_DivideMat4f(hmm_mat4 Matrix, float Scalar) return (Result); } +hmm_mat4 +HMM_Transpose(hmm_mat4 Matrix) +{ + hmm_mat4 Result = HMM_Mat4(); + + int Columns; + for(Columns = 0; Columns < 4; ++Columns) + { + int Rows; + for(Rows = 0; Rows < 4; ++Rows) + { + Result.Elements[Rows][Columns] = Matrix.Elements[Columns][Rows]; + } + } + + return (Result); +} + hmm_mat4 HMM_Orthographic(float Left, float Right, float Bottom, float Top, float Near, float Far) { diff --git a/test/hmm_test.cpp b/test/hmm_test.cpp index 8ee6e14..94506de 100644 --- a/test/hmm_test.cpp +++ b/test/hmm_test.cpp @@ -206,6 +206,41 @@ TEST(VectorOps, DotVec4) EXPECT_FLOAT_EQ(HMM_Dot(v1, v2), 70.0f); } +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(Addition, Vec2) { hmm_vec2 v2_1 = HMM_Vec2(1.0f, 2.0f); From 5ddb0ed10d355e5d90098cf5e379579f7f065538 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 30 Aug 2016 19:14:31 -0500 Subject: [PATCH 2/2] Tweak whitespace --- HandmadeMath.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/HandmadeMath.h b/HandmadeMath.h index 687e0ed..a7ee64a 100644 --- a/HandmadeMath.h +++ b/HandmadeMath.h @@ -409,7 +409,6 @@ HMMDEF hmm_mat4 HMM_LookAt(hmm_vec3 Eye, hmm_vec3 Center, hmm_vec3 Up); #ifdef HANDMADE_MATH_CPP_MODE - HMMDEF float HMM_Dot(hmm_vec2 VecOne, hmm_vec2 VecTwo); HMMDEF float HMM_Dot(hmm_vec3 VecOne, hmm_vec3 VecTwo); HMMDEF float HMM_Dot(hmm_vec4 VecOne, hmm_vec4 VecTwo); @@ -580,7 +579,6 @@ HMM_ToRadians(float Degrees) return (Result); } - HINLINE float HMM_DotVec2(hmm_vec2 VecOne, hmm_vec2 VecTwo) { @@ -599,7 +597,6 @@ HMM_DotVec3(hmm_vec3 VecOne, hmm_vec3 VecTwo) return (Result); } - HINLINE float HMM_DotVec4(hmm_vec4 VecOne, hmm_vec4 VecTwo) { @@ -609,7 +606,6 @@ HMM_DotVec4(hmm_vec4 VecOne, hmm_vec4 VecTwo) return (Result); } - HINLINE float HMM_LengthSquared(hmm_vec3 A) { @@ -646,7 +642,6 @@ HMM_Power(float Base, int Exponent) return (Result); } - HINLINE float HMM_Lerp(float A, float Time, float B) { @@ -673,7 +668,6 @@ HMM_Clamp(float Min, float Value, float Max) return (Result); } - HINLINE hmm_vec3 HMM_Normalize(hmm_vec3 A) {