From 93369795d30b823f9453c643844f690eb7e17211 Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Wed, 15 May 2024 13:55:31 +0200 Subject: [PATCH] fix matrix stuff (finally) --- u07/matrixMath.c | 37 ++++++++++++++++---- u07/matrixMath.h | 4 ++- u07/test.c | 80 ++++++++++++++++++++++++++++++++++--------- u07/vertexShader.glsl | 7 ++-- 4 files changed, 103 insertions(+), 25 deletions(-) diff --git a/u07/matrixMath.c b/u07/matrixMath.c index d47fdfd..95d59c5 100644 --- a/u07/matrixMath.c +++ b/u07/matrixMath.c @@ -89,6 +89,11 @@ void rotationZ(mat4* out, GLfloat angle) { } // CREATE 4x4 ROTATION MATRIX AROUND Y AXIS +/* cos a 0 sin a 0 + * 0 1 0 0 + * -sin a 0 cos a 0 + * 0 0 0 1 +*/ void rotationY(mat4* out, GLfloat angle) { identity(out); out->m00 = cos(angle); @@ -209,7 +214,16 @@ void mat3From4(mat3* out, mat4* in) { memcpy(&out->m02, &in->m02, sizeof(vec3)); } -void mat3Adjoint(mat3* out, mat3* in) { +void mat3Transpose(mat3* out, mat3* in) { + transposeAny((GLfloat*)out, (GLfloat*)in, 3, 3); +} + +/** + * a - m00 b - m01 c - m02 + * d - m10 e - m11 f - m12 + * g - m20 h - m21 i - m22 + */ +void mat3Minor(mat3* out, mat3* in) { mat3 result; result.m00 = in->m11 * in->m22 - in->m21 * in->m12; @@ -227,6 +241,21 @@ void mat3Adjoint(mat3* out, mat3* in) { memcpy(out, &result, sizeof(mat3)); } +void mat3Cofactor(mat3* out, mat3* in) { + mat3 result; + mat3Minor(out, in); + + out->m01 *= -1; + out->m10 *= -1; + out->m21 *= -1; + out->m12 *= -1; +} + +void mat3Adjoint(mat3* out, mat3* in) { + mat3Cofactor(out, in); + mat3Transpose(out, out); +} + void mat3MultiplyScalar(mat3* out, mat3* in, GLfloat x) { for (int i = 0; i < 9; i++) { ((GLfloat*)out)[i] = ((GLfloat*)in)[i] * x; @@ -245,10 +274,6 @@ GLfloat mat3Determinant(mat3* M) { ; } -void mat3Transpose(mat3* out, mat3* in) { - transposeAny((GLfloat*)out, (GLfloat*)in, 3, 3); -} - void mat3Inverse(mat3* out, mat3* in) { mat3 result; mat3Adjoint(&result, in); @@ -260,7 +285,7 @@ void mat3Inverse(mat3* out, mat3* in) { GLfloat sumDiffAny(GLfloat* A, GLfloat* B, int w, int h) { GLfloat result = 0; for (int i = 0; i < w * h; i++) { - result += abs(A[i] - B[i]); + result += fabs(A[i] - B[i]); } return result; } diff --git a/u07/matrixMath.h b/u07/matrixMath.h index 78bc07b..a34b6fb 100644 --- a/u07/matrixMath.h +++ b/u07/matrixMath.h @@ -86,10 +86,12 @@ extern void mat4Print(mat4* m); extern void mat3Print(mat3* m); extern void mat3From4(mat3* out, mat4* in); -extern void mat3Adjoint(mat3* out, mat3* in); extern void mat3MultiplyScalar(mat3* out, mat3* in, GLfloat x); extern GLfloat mat3Determinant(mat3* m); extern void mat3Transpose(mat3* out, mat3* in); +extern void mat3Minor(mat3* out, mat3* in); +extern void mat3Cofactor(mat3* out, mat3* in); +extern void mat3Adjoint(mat3* out, mat3* in); extern void mat3Inverse(mat3* out, mat3* in); extern GLfloat sumDiffAny(GLfloat* A, GLfloat* B, int w, int h); diff --git a/u07/test.c b/u07/test.c index eb61b70..b317647 100644 --- a/u07/test.c +++ b/u07/test.c @@ -5,22 +5,28 @@ #include "matrixMath.h" + +#define KRED "\x1B[31m" +#define KGRN "\x1B[32m" +#define KNRM "\x1B[0m" + #define EPSILON 0.001f +int exitCode = 0; + void printTest(char* name, bool result) { if (result) { - printf(" PASSED - "); + printf("%s PASSED%s - %s\n", KGRN, KNRM, name); } else { - printf("!!! FAILED - "); + printf("%s!!! FAILED%s - %s\n", KRED, KNRM, name); + exitCode = 1; } - printf("%s", name); - printf("\n"); } void testSumDiff() { - mat3 A = {1.0f, 0.0f, 0.0f, -1.0f, 0.0f, -1.0f, 1.0f, -1.0f, 9.0f}; - mat3 B = {0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f, 8.0f}; - GLfloat target = 7.0f; + mat3 A = {1, 0, 0, -1, 0, -1, 1, -1, 9}; + mat3 B = {0, 1, -1, 0, 0, 1, 1, -1, 8}; + GLfloat target = 7; GLfloat value = mat3SumDiff(&A, &B); @@ -38,9 +44,28 @@ void testSumDiff() { printTest("mat3SumDiff", result); } +void testMat3Minor() { + mat3 M = {2, 0, 1, -1, 5, -1, 3, 2, -2}; + mat3 target = {-8, 5, -17, -2, -7, 4, -5, -1, 10}; + + mat3Minor(&M, &M); + + printTest("mat3Minor", mat3SumDiff(&M, &target) < EPSILON); +} + +void testMat3Cofactor() { + mat3 M = {2, 0, 1, -1, 5, -1, 3, 2, -2}; + mat3 target = {-8, -5, -17, 2, -7, -4, -5, 1, 10}; + + mat3Cofactor(&M, &M); + + printTest("mat3Cofactor", mat3SumDiff(&M, &target) < EPSILON); +} + void testMat3Adjoint() { - mat3 M = {2.0f, 0.0f, 1.0f, -1.0f, 5.0f, -1.0f, 3.0f, 2.0f, -2.0f}; - mat3 target = {-8.0f, 5.0f, -17.0f, -2.0f, -7.0f, 4.0f, -5.0f, -1.0f, 10.0f}; + mat3 M = {1, 2, -1, 2, 1, 2, -1, 2, 1}; + mat3 target = {-3, -4, 5, -4, 0, -4, 5, -4, -3}; + mat3Adjoint(&M, &M); @@ -48,9 +73,9 @@ void testMat3Adjoint() { } void testMat3MultiplyScalar() { - mat3 M = {1.0f, 2.0f, 3.0f, 4.0f, 0.0f, -1.5f, -2.5f, -3.5f, -4.5f}; + mat3 M = {1, 2, 3, 4, 0, -1.5f, -2.5f, -3.5f, -4.5f}; GLfloat x = 0.9f; - mat3 target = {0.9f, 1.8f, 2.7f, 3.6f, 0.0f, -1.35f, -2.25f, -3.15f, -4.05f}; + mat3 target = {0.9f, 1.8f, 2.7f, 3.6f, 0, -1.35f, -2.25f, -3.15f, -4.05f}; mat3MultiplyScalar(&M, &M, x); @@ -58,25 +83,48 @@ void testMat3MultiplyScalar() { } void testMat3Determinant() { - mat3 M = {1.0f, -3.0f, 2.0f, 3.0f, -1.0f, 3.0f, 2.0f, -3.0f, 1.0f}; - GLfloat target = -15.0f; + mat3 M = {1, -3, 2, 3, -1, 3, 2, -3, 1}; + GLfloat target = -15; printTest("mat3Determinant", fabs(mat3Determinant(&M) - target) < EPSILON); } void testMat3Inverse() { - mat3 M = {1.0f, 2.0f, -1.0f, 2.0f, 1.0f, 2.0f, -1.0f, 2.0f, 1.0f}; - mat3 target = {0.1875f, 0.25f, -0.3125f, 0.2f, 0.0f, 0.25f, -0.3125f, 0.25f, 0.1875f}; + mat3 M = {1, 2, -1, 2, 1, 2, -1, 2, 1}; + mat3 target = {0.1875f, 0.25f, -0.3125f, 0.25f, 0, 0.25f, -0.3125f, 0.25f, 0.1875f}; mat3Inverse(&M, &M); - printTest("mat3Inverse", mat3SumDiff(&M, &target) < EPSILON); + bool result = mat3SumDiff(&M, &target) < EPSILON; + + if (!result) { + printf("\nM:\n"); + mat3Print(&M); + printf("\ntarget:\n"); + mat3Print(&target); + } + + printTest("mat3Inverse", result); +} + +void testMat3Transpose() { + mat3 M = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + mat3 target = {1, 4, 7, 2, 5, 8, 3, 6, 9}; + + mat3Transpose(&M, &M); + + printTest("mat3Transpose", mat3SumDiff(&M, &target) < EPSILON); } int main(void) { testSumDiff(); + testMat3Minor(); + testMat3Cofactor(); testMat3Adjoint(); testMat3MultiplyScalar(); testMat3Determinant(); testMat3Inverse(); + testMat3Transpose(); + + return exitCode; } \ No newline at end of file diff --git a/u07/vertexShader.glsl b/u07/vertexShader.glsl index fe5716a..dff30aa 100644 --- a/u07/vertexShader.glsl +++ b/u07/vertexShader.glsl @@ -12,6 +12,9 @@ out vec3 fragmentPosition; void main() { normal = normalModelView * aNormal; - gl_Position = projection * modelView * vec4(aPosition, 1.0); - fragmentPosition = vec3(modelView * vec4(aPosition, 1.0)); + vec4 modelViewPos = modelView * vec4(aPosition, 1.0); + + gl_Position = projection * modelViewPos; + + fragmentPosition = vec3(modelViewPos); } \ No newline at end of file