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);