From 3f26a6824c2b4f7836ca3e17f519a20ebee638fb Mon Sep 17 00:00:00 2001 From: Allan Feng Date: Thu, 9 Feb 2023 19:03:22 -0500 Subject: [PATCH 01/13] Framework for Matrix --- examples/vk_window.cpp | 2 +- include/glibby/math/general_math.h | 42 +++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/examples/vk_window.cpp b/examples/vk_window.cpp index f85de5f..0c2c45b 100644 --- a/examples/vk_window.cpp +++ b/examples/vk_window.cpp @@ -11,4 +11,4 @@ int main() glibby::Window window(800, 600, "Demo Window"); window.Run(); glibby::Window::Terminate(); -} \ No newline at end of file +} \ No newline at end of file diff --git a/include/glibby/math/general_math.h b/include/glibby/math/general_math.h index ac3b80a..f0c274a 100644 --- a/include/glibby/math/general_math.h +++ b/include/glibby/math/general_math.h @@ -80,15 +80,6 @@ namespace glibby Vec(const Point3D& other) : x(other.x), y(other.y), z(other.z), w(0.0f) {} }; - typedef Vec Vec2; - typedef Vec Vec3; - typedef Vec Vec4; - typedef Vec Vec2d; - typedef Vec Vec3d; - typedef Vec Vec4d; - typedef Vec Vec2i; - typedef Vec Vec3i; - typedef Vec Vec4i; template Vec operator-(Vec vec) @@ -291,9 +282,36 @@ namespace glibby return Vec(x, -y, z); } - template - struct Mat + + template + struct MAT + { + T data[n][n]; + + }; + + template + struct MAT { - float data[rows][cols]; + + }; + template + struct MAT + { + }; + template + struct MAT + { + + }; + + template + using MAT2x2 = MAT; + template + using MAT3x3 = MAT; + template + using MAT4x4 = MAT; + + } From 736c55839959cbad7f104620abb359e33b4b1daa Mon Sep 17 00:00:00 2001 From: Allan Feng Date: Thu, 9 Feb 2023 19:13:21 -0500 Subject: [PATCH 02/13] Framework for Matrix(with Vec added back) --- include/glibby/math/general_math.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/glibby/math/general_math.h b/include/glibby/math/general_math.h index f0c274a..24dd99b 100644 --- a/include/glibby/math/general_math.h +++ b/include/glibby/math/general_math.h @@ -80,6 +80,15 @@ namespace glibby Vec(const Point3D& other) : x(other.x), y(other.y), z(other.z), w(0.0f) {} }; + typedef Vec Vec2; + typedef Vec Vec3; + typedef Vec Vec4; + typedef Vec Vec2d; + typedef Vec Vec3d; + typedef Vec Vec4d; + typedef Vec Vec2i; + typedef Vec Vec3i; + typedef Vec Vec4i; template Vec operator-(Vec vec) From 32eb4e0b8cd93e431d182cc141d97def3240f6d1 Mon Sep 17 00:00:00 2001 From: Allan Feng Date: Fri, 10 Feb 2023 17:32:25 -0500 Subject: [PATCH 03/13] Matrix Structures construction and basic functions for matrix manipulation --- include/glibby/math/general_math.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/include/glibby/math/general_math.h b/include/glibby/math/general_math.h index 24dd99b..b0142af 100644 --- a/include/glibby/math/general_math.h +++ b/include/glibby/math/general_math.h @@ -296,7 +296,27 @@ namespace glibby struct MAT { T data[n][n]; - + MAT(T all) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + data[i][i] = all; + } + } + } + MAT(T mat[n][n]) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + data[i][i] = mat[i][i]; + } + } + } + const T& getValue(int row, int col) }; template From 7479f595a383274d63cad9a02d7491e30899e100 Mon Sep 17 00:00:00 2001 From: Allan Feng Date: Fri, 10 Feb 2023 17:33:28 -0500 Subject: [PATCH 04/13] Matrix Structures construction and basic functions for matrix manipulation updated --- include/glibby/math/general_math.h | 161 +++++++++++++++++++++++++---- 1 file changed, 140 insertions(+), 21 deletions(-) diff --git a/include/glibby/math/general_math.h b/include/glibby/math/general_math.h index b0142af..98a343e 100644 --- a/include/glibby/math/general_math.h +++ b/include/glibby/math/general_math.h @@ -292,17 +292,28 @@ namespace glibby } - template + template struct MAT { T data[n][n]; + size_t size = n; + MAT() + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + data[i][j] = 0; + } + } + } MAT(T all) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { - data[i][i] = all; + data[i][j] = all; } } } @@ -312,29 +323,32 @@ namespace glibby { for (int j = 0; j < n; j++) { - data[i][i] = mat[i][i]; + data[i][j] = mat[i][j]; } } } - const T& getValue(int row, int col) - }; - - template - struct MAT - { - - }; - template - struct MAT - { - - }; - template - struct MAT - { - + MAT(const MAT& copy) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + data[i][j] = copy.data[i][j]; + } + } + } + MAT& operator =(const MAT& rhs) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + data[i][j] = rhs.data[i][j]; + } + } + return *this; + } }; - template using MAT2x2 = MAT; template @@ -342,5 +356,110 @@ namespace glibby template using MAT4x4 = MAT; + template + void print_matrix(const MAT& mat) + { + + for (int i = 0; i < n; i++) + { + if (i == 0)std::cout << "["; + else std::cout << " "; + for (int j = 0; j < n; j++) + { + std::cout << mat.data[i][j]; + if (j != n - 1)std::cout << " "; + } + if (i == n - 1)std::cout << "]"; + else std::cout << " "; + std::cout << std::endl; + } + + } + template + MAT scalar_multiplication(float coefficient, const MAT& mat) + { + MAT result(mat); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + result.data[i][j] *= coefficient; + } + } + return result; + } + template + MAT scalar_division(float divisor, const MAT& mat) + { + MAT result(mat); + if (divisor == 0) + { + std::cout << "Cannot divide by 0." << std::endl; + exit(1); + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + result.data[i][j] /= divisor; + } + } + return result; + } + template + MAT matrix_addition(const MAT& mat1, const MAT& mat2) + { + MAT result; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + result.data[i][j] = mat1.data[i][j] + mat2.data[i][j]; + } + } + return result; + } + template + MAT matrix_subtraction(const MAT& mat1, const MAT& mat2) + { + MAT result; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + result.data[i][j] = mat1.data[i][j] - mat2.data[i][j]; + } + } + return result; + } + template + MAT matrix_multiplication(const MAT& mat1, const MAT& mat2) + { + MAT result; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + for (int k = 0; k < n; k++) + { + result.data[i][j] += mat1.data[i][k] * mat2.data[k][j]; + } + } + } + return result; + } + template + MAT matrix_transposition(const MAT& mat1) + { + MAT result; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + result.data[i][j] = mat1.data[j][i]; + } + } + return result; + } } From d65aea79675e678055af83971b58e42e0f48d68a Mon Sep 17 00:00:00 2001 From: Allan Feng Date: Tue, 14 Feb 2023 17:19:00 -0500 Subject: [PATCH 05/13] Matrix determinant --- include/glibby/math/general_math.h | 90 +++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/include/glibby/math/general_math.h b/include/glibby/math/general_math.h index 837a822..81de7ba 100644 --- a/include/glibby/math/general_math.h +++ b/include/glibby/math/general_math.h @@ -461,5 +461,91 @@ namespace glibby } return result; } - -} + template + float matrix_determinant(const MAT& mat) + { + float result; + if (n == 2) + { + result = (mat.data[0][0] * mat.data[1][1] - mat.data[0][1] * mat.data[1][0]); + } + if (n == 3) + { + MAT temp2x2_1; + MAT temp2x2_2; + MAT temp2x2_3; + int count1 = 0, count2 = 0, count3 = 0; + for (int j = 0; j < 3; j++) + { + for (int i = 0; i < 2; i++) + { + if (j == 0) + { + temp2x2_2.data[i][count2] = mat.data[i + 1][j]; + temp2x2_3.data[i][count3] = mat.data[i + 1][j]; + } + if (j == 1) + { + temp2x2_1.data[i][count1] = mat.data[i + 1][j]; + temp2x2_3.data[i][count3] = mat.data[i + 1][j]; + } + if (j == 2) + { + temp2x2_1.data[i][count1] = mat.data[i + 1][j]; + temp2x2_2.data[i][count2] = mat.data[i + 1][j]; + } + } + if (j == 0)count2++; count3++; + if (j == 1)count1++; + } + result = mat.data[0][0] * matrix_determinant(temp2x2_1) + - mat.data[0][1] * matrix_determinant(temp2x2_2) + + mat.data[0][2] * matrix_determinant(temp2x2_3); + } + if (n == 4) + { + MAT temp3x3_1; + MAT temp3x3_2; + MAT temp3x3_3; + MAT temp3x3_4; + int count1 = 0, count2 = 0, count3 = 0, count4 = 0; + for (int j = 0; j < 4; j++) + { + for (int i = 0; i < 3; i++) + { + if (j == 0) + { + temp3x3_2.data[i][count2] = mat.data[i + 1][j]; + temp3x3_3.data[i][count3] = mat.data[i + 1][j]; + temp3x3_4.data[i][count4] = mat.data[i + 1][j]; + } + if (j == 1) + { + temp3x3_1.data[i][count1] = mat.data[i + 1][j]; + temp3x3_3.data[i][count3] = mat.data[i + 1][j]; + temp3x3_4.data[i][count4] = mat.data[i + 1][j]; + } + if (j == 2) + { + temp3x3_1.data[i][count1] = mat.data[i + 1][j]; + temp3x3_2.data[i][count2] = mat.data[i + 1][j]; + temp3x3_4.data[i][count4] = mat.data[i + 1][j]; + } + if (j == 3) + { + temp3x3_1.data[i][count1] = mat.data[i + 1][j]; + temp3x3_2.data[i][count2] = mat.data[i + 1][j]; + temp3x3_3.data[i][count3] = mat.data[i + 1][j]; + } + } + if (j == 0)count2++; count3++; count4++; + if (j == 1)count1++; count3++; count4++; + if (j == 2)count1++; count2++; count4++; + } + result = mat.data[0][0] * matrix_determinant(temp3x3_1) + - mat.data[0][1] * matrix_determinant(temp3x3_2) + + mat.data[0][2] * matrix_determinant(temp3x3_3) + - mat.data[0][3] * matrix_determinant(temp3x3_4); + } + return result; + } \ No newline at end of file From caf74fd52c9c1d1e3c653a0cc99f05dce5df321a Mon Sep 17 00:00:00 2001 From: Allan Feng Date: Tue, 14 Feb 2023 17:33:16 -0500 Subject: [PATCH 06/13] Matrix Overloaded Stream Operators >> and << --- include/glibby/math/general_math.h | 37 +++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/include/glibby/math/general_math.h b/include/glibby/math/general_math.h index 81de7ba..cc4fb59 100644 --- a/include/glibby/math/general_math.h +++ b/include/glibby/math/general_math.h @@ -548,4 +548,39 @@ namespace glibby - mat.data[0][3] * matrix_determinant(temp3x3_4); } return result; - } \ No newline at end of file + } + template + std::ostream& operator <<(std::ostream& out, const MAT& mat) + { + for (int i = 0; i < n; i++) + { + if (i == 0) + out << "["; + else + out << " "; + for (int j = 0; j < n; j++) + { + out << mat.data[i][j]; + if (j != n - 1) + out << " "; + } + if (i == n - 1) + out << "]"; + else + out << " "; + out << std::endl; + } + return out; + } + template + std::istream& operator >>(std::istream& in, MAT& mat) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + in >> mat.data[i][j]; + } + } + return in; + } From 1754668d11b38b329b83e135fa7459983eddd572 Mon Sep 17 00:00:00 2001 From: Allan Feng Date: Fri, 24 Feb 2023 17:45:32 -0500 Subject: [PATCH 07/13] Partial Specialization for MAT2x2,3x3, and 4x4, and LookAt function for MAT4x4 --- include/glibby/math/general_math.h | 230 +++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+) diff --git a/include/glibby/math/general_math.h b/include/glibby/math/general_math.h index cc4fb59..28f90eb 100644 --- a/include/glibby/math/general_math.h +++ b/include/glibby/math/general_math.h @@ -349,6 +349,7 @@ namespace glibby return *this; } }; + template using MAT2x2 = MAT; template @@ -356,6 +357,219 @@ namespace glibby template using MAT4x4 = MAT; + template + struct MAT + { + T data[4][4]; + size_t size = 4; + MAT(Vec v1, Vec v2, Vec v3, Vec v4) + { + Vec vall[4]; + vall[0] = v1; + vall[1] = v2; + vall[2] = v3; + vall[3] = v4; + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + data[i][j] = vall[i].data[j]; + } + } + } + MAT() + { + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + data[i][j] = 0; + } + } + } + MAT(T all) + { + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + data[i][j] = all; + } + } + } + MAT(T mat[4][4]) + { + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + data[i][j] = mat[i][j]; + } + } + } + MAT(const MAT4x4& copy) + { + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + data[i][j] = copy.data[i][j]; + } + } + } + MAT4x4& operator =(const MAT4x4& rhs) + { + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + data[i][j] = rhs.data[i][j]; + } + } + return *this; + } + }; + template + struct MAT + { + T data[3][3]; + size_t size = 3; + MAT(Vec v1, Vec v2, Vec v3) + { + Vec vall[3]; + vall[0] = v1; + vall[1] = v2; + vall[2] = v3; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + data[i][j] = vall[i][j]; + } + } + } + MAT() + { + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + data[i][j] = 0; + } + } + } + MAT(T all) + { + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + data[i][j] = all; + } + } + } + MAT(T mat[3][3]) + { + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + data[i][j] = mat[i][j]; + } + } + } + MAT(const MAT3x3& copy) + { + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + data[i][j] = copy.data[i][j]; + } + } + } + MAT3x3& operator =(const MAT3x3& rhs) + { + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + data[i][j] = rhs.data[i][j]; + } + } + return *this; + } + }; + template + struct MAT + { + T data[2][2]; + size_t size = 2; + MAT(Vec v1, Vec v2) + { + Vec vall[2]; + vall[0] = v1; + vall[1] = v2; + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < 2; j++) + { + data[i][j] = vall[i][j]; + } + } + } + MAT() + { + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < 2; j++) + { + data[i][j] = 0; + } + } + } + MAT(T all) + { + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < 2; j++) + { + data[i][j] = all; + } + } + } + MAT(T mat[2][2]) + { + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < 2; j++) + { + data[i][j] = mat[i][j]; + } + } + } + MAT(const MAT2x2& copy) + { + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < 2; j++) + { + data[i][j] = copy.data[i][j]; + } + } + } + MAT2x2& operator =(const MAT2x2& rhs) + { + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < 2; j++) + { + data[i][j] = rhs.data[i][j]; + } + } + return *this; + } + }; template void print_matrix(const MAT& mat) { @@ -584,3 +798,19 @@ namespace glibby } return in; } + template + MAT4x4 LookAt(const Vec& eye, const Vec& at, const Vec& up) + { + Vec zaxis = Normalize(at - eye); + Vec xaxis = Normalize(Cross(zaxis, up)); + Vec yaxis = Cross(zaxis, xaxis); + zaxis = -zaxis; + MAT4x4 m(Vec4(xaxis.x, xaxis.y, xaxis.z, -Dot(xaxis, eye)), + Vec4(yaxis.x, yaxis.y, yaxis.z, -Dot(yaxis, eye)), + Vec4(zaxis.x, zaxis.y, zaxis.z, -Dot(zaxis, eye)), + Vec4(0, 0, 0, 1)); + return m; + + } + + From 1d7eabbfe0931f833e8aefd1d7429336ff4e71af Mon Sep 17 00:00:00 2001 From: Allan Feng Date: Fri, 24 Feb 2023 18:05:15 -0500 Subject: [PATCH 08/13] minor fixes for mat2x2 and 3x3 on vector operator[] use --- include/glibby/math/general_math.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/glibby/math/general_math.h b/include/glibby/math/general_math.h index 28f90eb..6073735 100644 --- a/include/glibby/math/general_math.h +++ b/include/glibby/math/general_math.h @@ -444,7 +444,7 @@ namespace glibby { for (int j = 0; j < 3; j++) { - data[i][j] = vall[i][j]; + data[i][j] = vall[i].data[j]; } } } @@ -514,7 +514,7 @@ namespace glibby { for (int j = 0; j < 2; j++) { - data[i][j] = vall[i][j]; + data[i][j] = vall[i].data[j]; } } } From def6bdd900009d669ac1e37e80cad98ac6f078ae Mon Sep 17 00:00:00 2001 From: Allan Feng Date: Fri, 17 Mar 2023 17:42:53 -0400 Subject: [PATCH 09/13] Orthogonal and Perspective Projection implementation --- include/glibby/math/general_math.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/include/glibby/math/general_math.h b/include/glibby/math/general_math.h index 6073735..db6c42a 100644 --- a/include/glibby/math/general_math.h +++ b/include/glibby/math/general_math.h @@ -812,5 +812,34 @@ namespace glibby return m; } + // Orthogonal Projection Matrix + MAT4x4 orthogonalProjection(float left, float right, float bottom, float top, float near, float far) + { + MAT4x4 projection; + + projection.data[0][0] = 2.0f / (right - left); + projection.data[1][1] = 2.0f / (top - bottom); + projection.data[2][2] = -2.0f / (far - near); + projection.data[0][3] = -(right + left) / (right - left); + projection.data[1][3] = -(top + bottom) / (top - bottom); + projection.data[2][3] = -(far + near) / (far - near); + + return projection; + } + + // Perspective Projection Matrix + MAT4x4 perspectiveProjection(float fovY, float aspectRatio, float near, float far) + { + MAT4x4 projection; + + float f = 1.0f / tan(fovY * 0.5f); + projection.data[0][0] = f / aspectRatio; + projection.data[1][1] = f; + projection.data[2][2] = (near + far) / (near - far); + projection.data[2][3] = (2.0f * near * far) / (near - far); + projection.data[3][2] = -1.0f; + + return projection; + } From 10679a55bfc78052bbdabf8c61b438cd4f39ebee Mon Sep 17 00:00:00 2001 From: Allan Feng Date: Fri, 24 Mar 2023 17:24:08 -0400 Subject: [PATCH 10/13] Creating test cases for MAT2x2 and MAT3x3, and creating operator== for checking --- include/glibby/math/general_math.h | 18 +++++++++-- tests/math_tests.cpp | 49 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/include/glibby/math/general_math.h b/include/glibby/math/general_math.h index db6c42a..eb99c6b 100644 --- a/include/glibby/math/general_math.h +++ b/include/glibby/math/general_math.h @@ -8,7 +8,7 @@ namespace glibby { - #define FLT_NEAR_ZERO 0.0001f +#define FLT_NEAR_ZERO 0.0001f template struct Vec @@ -260,7 +260,7 @@ namespace glibby os << ")"; return os; } - + template std::istream& operator>>(std::istream& is, Vec& vec) { @@ -570,6 +570,19 @@ namespace glibby return *this; } }; + + template + bool operator==(const MAT& mat1, const MAT& mat2) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (mat1.data[i][j] != mat2.data[i][j]) return false; + } + } + return true; + } template void print_matrix(const MAT& mat) { @@ -841,5 +854,6 @@ namespace glibby return projection; } +} diff --git a/tests/math_tests.cpp b/tests/math_tests.cpp index 63bc739..8c4b064 100644 --- a/tests/math_tests.cpp +++ b/tests/math_tests.cpp @@ -149,3 +149,52 @@ TEST_CASE("VecN Operations", "[math][vecN]") copyConstructedVec /= 5.0f; CHECK(copyConstructedVec == simpleVec); } +TEST_CASE("MAT2x2 Operations", "[math][mat2x2]") +{ + const MAT2x2 zeroMat; + const MAT2x2 oneMat(1); + const MAT2x2 simpleMat({ {1,2},{3,4} }); + const MAT2x2 negativeMat({ {-1,-2},{-3,-4} }); + const MAT2x2 doubleMat({ 2,4 }, { 6,8 }); + const MAT2x2 productMat({ 14,20 }, { 30,44 }); + const MAT2x2 transposedMat({ 1,3 }, { 2,4 }); + CHECK(scalar_multiplication(2, simpleMat) == doubleMat); + CHECK(scalar_division(2, doubleMat) == simpleMat); + + CHECK(matrix_addition(simpleMat, negativeMat) == zeroMat); + CHECK(matrix_subtraction(simpleMat, simpleMat) == zeroMat); + CHECK(matrix_subtraction(simpleMat, negativeMat) == doubleMat); + CHECK(matrix_multiplication(simpleMat, doubleMat) == productMat); + + CHECK(matrix_transposition(simpleMat) == transposedMat); + CHECK(matrix_determinant(simpleMat) == -2); +} +TEST_CASE("MAT3x3 Operations", "[math][mat3x3]") +{ + const MAT3x3 zeroMat; + const MAT3x3 oneMat(1); + const MAT3x3 simpleMat({ {1,2,3},{4,5,6},{7,8,9} }); + const MAT3x3 negativeMat({ {-1,-2,-3},{-4,-5,-6},{-7,-8,-9 } }); + const MAT3x3 doubleMat({ {2,4,6},{8,10,12},{14,16,18} }); + const MAT3x3 productMat({ {60,72,84},{132,162,192},{204,252,300} }); + const MAT3x3 transposedMat({ 1,4,7 }, { 2,5,8 }, { 3,6,9 }); + CHECK(scalar_multiplication(2, simpleMat) == doubleMat); + CHECK(scalar_division(2, doubleMat) == simpleMat); + + CHECK(matrix_addition(simpleMat, negativeMat) == zeroMat); + CHECK(matrix_subtraction(simpleMat, simpleMat) == zeroMat); + CHECK(matrix_subtraction(simpleMat, negativeMat) == doubleMat); + CHECK(matrix_multiplication(simpleMat, doubleMat) == productMat); + + CHECK(matrix_transposition(simpleMat) == transposedMat); + CHECK(matrix_determinant(simpleMat) == -2); + +} +TEST_CASE("MAT4x4 Operations", "[math][mat4x4]") +{ + +} +TEST_CASE("MATnxn Operations", "[math][matnxn]") +{ + +} From 3967046353e9c6a67072ace2b03ecdbf46c07300 Mon Sep 17 00:00:00 2001 From: Allan Feng Date: Tue, 4 Apr 2023 17:23:35 -0400 Subject: [PATCH 11/13] Completion of MAT4x4 test cases --- tests/math_tests.cpp | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/tests/math_tests.cpp b/tests/math_tests.cpp index 8c4b064..18c5d70 100644 --- a/tests/math_tests.cpp +++ b/tests/math_tests.cpp @@ -1,5 +1,7 @@ #include "glibby/math/general_math.h" +#define _USE_MATH_DEFINES +#include #include #include @@ -192,8 +194,38 @@ TEST_CASE("MAT3x3 Operations", "[math][mat3x3]") } TEST_CASE("MAT4x4 Operations", "[math][mat4x4]") { + const MAT4x4 zeroMat; + const MAT4x4 oneMat(1); + const MAT4x4 simpleMat({ {1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16} }); + const MAT4x4 negativeMat({ {-1,-2,-3,-4},{-5,-6,-7,-8},{-9,-10,-11,-12},{-13,-14,-15,-16} }); + const MAT4x4 doubleMat({ {2,4,6,8},{10,12,14,16},{18,20,22,24},{26,28,30,32} }); + const MAT4x4 productMat({ {180,200,220,240},{404,456,508,560},{628,712,796,880},852,968,1084,1200}); + const MAT4x4 transposedMat({ 1,5,9,13}, { 2,6,10,14 }, { 3,7,11,15 }, {4,8,12,16}); + CHECK(scalar_multiplication(2, simpleMat) == doubleMat); + CHECK(scalar_division(2, doubleMat) == simpleMat); + + CHECK(matrix_addition(simpleMat, negativeMat) == zeroMat); + CHECK(matrix_subtraction(simpleMat, simpleMat) == zeroMat); + CHECK(matrix_subtraction(simpleMat, negativeMat) == doubleMat); + CHECK(matrix_multiplication(simpleMat, doubleMat) == productMat); + + CHECK(matrix_transposition(simpleMat) == transposedMat); + CHECK(matrix_determinant(simpleMat) == -2); + float left = -10.0f, right = 10.0f, bottom = -10.0f, top = 10.0f, near = 1.0f, far = 100.0f; + MAT4x4 orthoProj = orthogonalProjection(left, right, bottom, top, near, far); + MAT4x4 orthoProj_result({ {0.1,0,0,0},{0,0.1,0,0},{0,0,-0.020202,-1.0202},{0,0,0,0} }); + CHECK(orthoProj == orthoProj_result); + float fovY = M_PI / 3.0f, aspectRatio = 16.0f / 9.0f; + MAT4x4 perspProj = perspectiveProjection(fovY, aspectRatio, near, far); + MAT4x4 perspProj_result({ {0.974279,0,0,0},{0,1.73205,0,0},{0,0,-1,0202,-2.0202},{0,0,-1,0} }); + CHECK(perspProj == perspProj_result); + Vec v1(1, 0, 1); + Vec v2(0, 1, 1); + Vec v3(0, 0, 1); + MAT4x4 Look_At = LookAt(v1, v2, v3); + MAT4x4 Look_At_result({ {0.707107,0.707107,0,-0.707107},{0,0,-1,1},{0.707107,-0.707107,0,-0.707107},{0,0,0,1} }); +} -} TEST_CASE("MATnxn Operations", "[math][matnxn]") { From baa437db8b5ce7a20ee02f0bec56b6cf6d1412da Mon Sep 17 00:00:00 2001 From: Allan Feng Date: Fri, 21 Apr 2023 23:27:45 -0400 Subject: [PATCH 12/13] Fixing minor errors and adding inline to orthographic and perspective projection functions --- include/glibby/math/general_math.h | 4 ++-- tests/math_tests.cpp | 9 ++------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/include/glibby/math/general_math.h b/include/glibby/math/general_math.h index eb99c6b..c04a309 100644 --- a/include/glibby/math/general_math.h +++ b/include/glibby/math/general_math.h @@ -826,7 +826,7 @@ namespace glibby } // Orthogonal Projection Matrix - MAT4x4 orthogonalProjection(float left, float right, float bottom, float top, float near, float far) + inline MAT4x4 orthogonalProjection(float left, float right, float bottom, float top, float near, float far) { MAT4x4 projection; @@ -841,7 +841,7 @@ namespace glibby } // Perspective Projection Matrix - MAT4x4 perspectiveProjection(float fovY, float aspectRatio, float near, float far) + inline MAT4x4 perspectiveProjection(float fovY, float aspectRatio, float near, float far) { MAT4x4 projection; diff --git a/tests/math_tests.cpp b/tests/math_tests.cpp index 18c5d70..b05d700 100644 --- a/tests/math_tests.cpp +++ b/tests/math_tests.cpp @@ -199,7 +199,7 @@ TEST_CASE("MAT4x4 Operations", "[math][mat4x4]") const MAT4x4 simpleMat({ {1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16} }); const MAT4x4 negativeMat({ {-1,-2,-3,-4},{-5,-6,-7,-8},{-9,-10,-11,-12},{-13,-14,-15,-16} }); const MAT4x4 doubleMat({ {2,4,6,8},{10,12,14,16},{18,20,22,24},{26,28,30,32} }); - const MAT4x4 productMat({ {180,200,220,240},{404,456,508,560},{628,712,796,880},852,968,1084,1200}); + const MAT4x4 productMat({ {180,200,220,240},{404,456,508,560},{628,712,796,880},{852,968,1084,1200} }); const MAT4x4 transposedMat({ 1,5,9,13}, { 2,6,10,14 }, { 3,7,11,15 }, {4,8,12,16}); CHECK(scalar_multiplication(2, simpleMat) == doubleMat); CHECK(scalar_division(2, doubleMat) == simpleMat); @@ -217,7 +217,7 @@ TEST_CASE("MAT4x4 Operations", "[math][mat4x4]") CHECK(orthoProj == orthoProj_result); float fovY = M_PI / 3.0f, aspectRatio = 16.0f / 9.0f; MAT4x4 perspProj = perspectiveProjection(fovY, aspectRatio, near, far); - MAT4x4 perspProj_result({ {0.974279,0,0,0},{0,1.73205,0,0},{0,0,-1,0202,-2.0202},{0,0,-1,0} }); + MAT4x4 perspProj_result({ {0.974279,0,0,0},{0,1.73205,0,0},{0,0,-1.0202,-2.0202},{0,0,-1,0} }); CHECK(perspProj == perspProj_result); Vec v1(1, 0, 1); Vec v2(0, 1, 1); @@ -225,8 +225,3 @@ TEST_CASE("MAT4x4 Operations", "[math][mat4x4]") MAT4x4 Look_At = LookAt(v1, v2, v3); MAT4x4 Look_At_result({ {0.707107,0.707107,0,-0.707107},{0,0,-1,1},{0.707107,-0.707107,0,-0.707107},{0,0,0,1} }); } - -TEST_CASE("MATnxn Operations", "[math][matnxn]") -{ - -} From f34a3db015855441686c5779cd3d9873448be366 Mon Sep 17 00:00:00 2001 From: Allan Feng Date: Wed, 26 Apr 2023 00:24:12 -0400 Subject: [PATCH 13/13] Minor changes to formatting --- include/glibby/math/general_math.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/glibby/math/general_math.h b/include/glibby/math/general_math.h index 02cc9f1..8b34243 100644 --- a/include/glibby/math/general_math.h +++ b/include/glibby/math/general_math.h @@ -722,8 +722,8 @@ namespace glibby temp2x2_2.data[i][count2] = mat.data[i + 1][j]; } } - if (j == 0)count2++; count3++; - if (j == 1)count1++; + if (j == 0){count2++; count3++;} + if (j == 1) { count1++; } } result = mat.data[0][0] * matrix_determinant(temp2x2_1) - mat.data[0][1] * matrix_determinant(temp2x2_2) @@ -765,9 +765,9 @@ namespace glibby temp3x3_3.data[i][count3] = mat.data[i + 1][j]; } } - if (j == 0)count2++; count3++; count4++; - if (j == 1)count1++; count3++; count4++; - if (j == 2)count1++; count2++; count4++; + if (j == 0) { count2++; count3++; count4++; } + if (j == 1) { count1++; count3++; count4++; } + if (j == 2) { count1++; count2++; count4++; } } result = mat.data[0][0] * matrix_determinant(temp3x3_1) - mat.data[0][1] * matrix_determinant(temp3x3_2)