From bc909e185ffdbecc494f22f5c3926339d1d2bd09 Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Thu, 16 May 2024 16:26:45 +0200 Subject: [PATCH] add some comments --- u08-2/matrixMath.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/u08-2/matrixMath.c b/u08-2/matrixMath.c index d01b39c..f9acc51 100644 --- a/u08-2/matrixMath.c +++ b/u08-2/matrixMath.c @@ -111,7 +111,7 @@ void rotationX(mat4* out, GLfloat angle) { out->m22 = cos(angle); } -// MULTIPLY ANY TO MATRICES +// MULTIPLY ANY TWO MATRICES void multiplyAny(GLfloat* out, GLfloat* A, GLfloat* B, int wA, int hA, int wB) { int sizeOut = hA * wB; GLfloat* result = (GLfloat*) malloc(sizeOut * sizeof(GLfloat)); @@ -145,25 +145,26 @@ void scale(mat4* out, mat4* in, vec3* v) { multiply(out, &scalingMatrix, in); } -// MULTIPLY in WITH ROTATION MATRIX OF a AROUND Z AXIS +// MULTIPLY in WITH ROTATION MATRIX OF angle AROUND Z AXIS void rotateZ(mat4* out, mat4* in, GLfloat angle) { mat4 rotationMatrix; rotationZ(&rotationMatrix, angle); multiply(out, &rotationMatrix, in); } -// MULTIPLY in WITH ROTATION MATRIX OF a AROUND Y AXIS +// MULTIPLY in WITH ROTATION MATRIX OF angle AROUND Y AXIS void rotateY(mat4* out, mat4* in, GLfloat angle) { mat4 rotationMatrix; rotationY(&rotationMatrix, angle); multiply(out, &rotationMatrix, in); } -// MULTIPLY in WITH ROTATION MATRIX OF a AROUND X AXIS +// MULTIPLY in WITH ROTATION MATRIX OF angle AROUND X AXIS void rotateX(mat4* out, mat4* in, GLfloat angle) { mat4 rotationMatrix; rotationX(&rotationMatrix, angle); multiply(out, &rotationMatrix, in); } +// TRANSPOSE MATRIX OF ANY SIZE void transposeAny(GLfloat* out, GLfloat* in, int w, int h) { int size = w * h; GLfloat* result = (GLfloat*) malloc(size * sizeof(GLfloat)); @@ -178,10 +179,12 @@ void transposeAny(GLfloat* out, GLfloat* in, int w, int h) { } +// TRANSPOSE 4x4 MATRIX void transpose(mat4* out, mat4* in) { transposeAny((GLfloat*)out, (GLfloat*)in, 4, 4); } +// PRINT MATRIX OF ANY SIZE void printAny(GLfloat* M, int w, int h) { GLfloat* transposed = (GLfloat*) malloc(w * h * sizeof(GLfloat)); transposeAny(transposed, M, w, h); @@ -208,12 +211,14 @@ void mat3Print(mat3* m) { printAny((GLfloat*)m, 3, 3); } +// TURN mat4 INTO mat3 BY CUTTING LAST COLUMN AND ROW void mat3From4(mat3* out, mat4* in) { memcpy(&out->m00, &in->m00, sizeof(vec3)); memcpy(&out->m01, &in->m01, sizeof(vec3)); memcpy(&out->m02, &in->m02, sizeof(vec3)); } +// TRANSPOSE 3x3 MATRIX void mat3Transpose(mat3* out, mat3* in) { transposeAny((GLfloat*)out, (GLfloat*)in, 3, 3); } @@ -223,6 +228,7 @@ void mat3Transpose(mat3* out, mat3* in) { * d - m10 e - m11 f - m12 * g - m20 h - m21 i - m22 */ +// GET THE MINOR MATRIX OF A 3x3 MATRIX void mat3Minor(mat3* out, mat3* in) { mat3 result; @@ -241,6 +247,7 @@ void mat3Minor(mat3* out, mat3* in) { memcpy(out, &result, sizeof(mat3)); } +// GET THE COFACTOR MATRIX OF A 3x3 MATRIX void mat3Cofactor(mat3* out, mat3* in) { mat3 result; mat3Minor(out, in); @@ -251,17 +258,20 @@ void mat3Cofactor(mat3* out, mat3* in) { out->m12 *= -1; } +// GET ADJOING MATRRIX OF 3x3 MATRIX void mat3Adjoint(mat3* out, mat3* in) { mat3Cofactor(out, in); mat3Transpose(out, out); } +// MULTIPLY A 3x3 MATRIX WITH A SCALAR x (componentwise) void mat3MultiplyScalar(mat3* out, mat3* in, GLfloat x) { for (int i = 0; i < 9; i++) { ((GLfloat*)out)[i] = ((GLfloat*)in)[i] * x; } } +// CALCULATE DETERMINANT OF 3x3 MATRIX GLfloat mat3Determinant(mat3* M) { return M->m00 * M->m11 * M->m22 @@ -274,6 +284,7 @@ GLfloat mat3Determinant(mat3* M) { ; } +// GET INVERSE OF 3x3 MATRIX void mat3Inverse(mat3* out, mat3* in) { mat3 result; mat3Adjoint(&result, in); @@ -282,6 +293,7 @@ void mat3Inverse(mat3* out, mat3* in) { memcpy(out, &result, sizeof(mat3)); } +// GET THE SUM OF DIFFERENCES BETWEEON TWO MATRICES GLfloat sumDiffAny(GLfloat* A, GLfloat* B, int w, int h) { GLfloat result = 0; for (int i = 0; i < w * h; i++) { @@ -290,10 +302,12 @@ GLfloat sumDiffAny(GLfloat* A, GLfloat* B, int w, int h) { return result; } +// GET THE SUM OF DIFFERENCES BETWEEN TWO 3x3 MATRICES GLfloat mat3SumDiff(mat3* A, mat3* B) { return sumDiffAny((GLfloat*)A, (GLfloat*)B, 3, 3); } +// COMPONENTWISE SUBTRACT vec2 b FROM vec2 a void vec2Subtract(vec2* out, vec2* a, vec2* b) { out->x = a->x - b->x; out->y = a->y - b->y;