add some comments

This commit is contained in:
Luca Conte 2024-05-16 16:26:45 +02:00
parent 9eafb739e6
commit bc909e185f
1 changed files with 18 additions and 4 deletions

View File

@ -111,7 +111,7 @@ void rotationX(mat4* out, GLfloat angle) {
out->m22 = cos(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) { void multiplyAny(GLfloat* out, GLfloat* A, GLfloat* B, int wA, int hA, int wB) {
int sizeOut = hA * wB; int sizeOut = hA * wB;
GLfloat* result = (GLfloat*) malloc(sizeOut * sizeof(GLfloat)); GLfloat* result = (GLfloat*) malloc(sizeOut * sizeof(GLfloat));
@ -145,25 +145,26 @@ void scale(mat4* out, mat4* in, vec3* v) {
multiply(out, &scalingMatrix, in); 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) { void rotateZ(mat4* out, mat4* in, GLfloat angle) {
mat4 rotationMatrix; mat4 rotationMatrix;
rotationZ(&rotationMatrix, angle); rotationZ(&rotationMatrix, angle);
multiply(out, &rotationMatrix, in); 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) { void rotateY(mat4* out, mat4* in, GLfloat angle) {
mat4 rotationMatrix; mat4 rotationMatrix;
rotationY(&rotationMatrix, angle); rotationY(&rotationMatrix, angle);
multiply(out, &rotationMatrix, in); 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) { void rotateX(mat4* out, mat4* in, GLfloat angle) {
mat4 rotationMatrix; mat4 rotationMatrix;
rotationX(&rotationMatrix, angle); rotationX(&rotationMatrix, angle);
multiply(out, &rotationMatrix, in); multiply(out, &rotationMatrix, in);
} }
// TRANSPOSE MATRIX OF ANY SIZE
void transposeAny(GLfloat* out, GLfloat* in, int w, int h) { void transposeAny(GLfloat* out, GLfloat* in, int w, int h) {
int size = w * h; int size = w * h;
GLfloat* result = (GLfloat*) malloc(size * sizeof(GLfloat)); 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) { void transpose(mat4* out, mat4* in) {
transposeAny((GLfloat*)out, (GLfloat*)in, 4, 4); transposeAny((GLfloat*)out, (GLfloat*)in, 4, 4);
} }
// PRINT MATRIX OF ANY SIZE
void printAny(GLfloat* M, int w, int h) { void printAny(GLfloat* M, int w, int h) {
GLfloat* transposed = (GLfloat*) malloc(w * h * sizeof(GLfloat)); GLfloat* transposed = (GLfloat*) malloc(w * h * sizeof(GLfloat));
transposeAny(transposed, M, w, h); transposeAny(transposed, M, w, h);
@ -208,12 +211,14 @@ void mat3Print(mat3* m) {
printAny((GLfloat*)m, 3, 3); printAny((GLfloat*)m, 3, 3);
} }
// TURN mat4 INTO mat3 BY CUTTING LAST COLUMN AND ROW
void mat3From4(mat3* out, mat4* in) { void mat3From4(mat3* out, mat4* in) {
memcpy(&out->m00, &in->m00, sizeof(vec3)); memcpy(&out->m00, &in->m00, sizeof(vec3));
memcpy(&out->m01, &in->m01, sizeof(vec3)); memcpy(&out->m01, &in->m01, sizeof(vec3));
memcpy(&out->m02, &in->m02, sizeof(vec3)); memcpy(&out->m02, &in->m02, sizeof(vec3));
} }
// TRANSPOSE 3x3 MATRIX
void mat3Transpose(mat3* out, mat3* in) { void mat3Transpose(mat3* out, mat3* in) {
transposeAny((GLfloat*)out, (GLfloat*)in, 3, 3); transposeAny((GLfloat*)out, (GLfloat*)in, 3, 3);
} }
@ -223,6 +228,7 @@ void mat3Transpose(mat3* out, mat3* in) {
* d - m10 e - m11 f - m12 * d - m10 e - m11 f - m12
* g - m20 h - m21 i - m22 * g - m20 h - m21 i - m22
*/ */
// GET THE MINOR MATRIX OF A 3x3 MATRIX
void mat3Minor(mat3* out, mat3* in) { void mat3Minor(mat3* out, mat3* in) {
mat3 result; mat3 result;
@ -241,6 +247,7 @@ void mat3Minor(mat3* out, mat3* in) {
memcpy(out, &result, sizeof(mat3)); memcpy(out, &result, sizeof(mat3));
} }
// GET THE COFACTOR MATRIX OF A 3x3 MATRIX
void mat3Cofactor(mat3* out, mat3* in) { void mat3Cofactor(mat3* out, mat3* in) {
mat3 result; mat3 result;
mat3Minor(out, in); mat3Minor(out, in);
@ -251,17 +258,20 @@ void mat3Cofactor(mat3* out, mat3* in) {
out->m12 *= -1; out->m12 *= -1;
} }
// GET ADJOING MATRRIX OF 3x3 MATRIX
void mat3Adjoint(mat3* out, mat3* in) { void mat3Adjoint(mat3* out, mat3* in) {
mat3Cofactor(out, in); mat3Cofactor(out, in);
mat3Transpose(out, out); mat3Transpose(out, out);
} }
// MULTIPLY A 3x3 MATRIX WITH A SCALAR x (componentwise)
void mat3MultiplyScalar(mat3* out, mat3* in, GLfloat x) { void mat3MultiplyScalar(mat3* out, mat3* in, GLfloat x) {
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
((GLfloat*)out)[i] = ((GLfloat*)in)[i] * x; ((GLfloat*)out)[i] = ((GLfloat*)in)[i] * x;
} }
} }
// CALCULATE DETERMINANT OF 3x3 MATRIX
GLfloat mat3Determinant(mat3* M) { GLfloat mat3Determinant(mat3* M) {
return return
M->m00 * M->m11 * M->m22 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) { void mat3Inverse(mat3* out, mat3* in) {
mat3 result; mat3 result;
mat3Adjoint(&result, in); mat3Adjoint(&result, in);
@ -282,6 +293,7 @@ void mat3Inverse(mat3* out, mat3* in) {
memcpy(out, &result, sizeof(mat3)); memcpy(out, &result, sizeof(mat3));
} }
// GET THE SUM OF DIFFERENCES BETWEEON TWO MATRICES
GLfloat sumDiffAny(GLfloat* A, GLfloat* B, int w, int h) { GLfloat sumDiffAny(GLfloat* A, GLfloat* B, int w, int h) {
GLfloat result = 0; GLfloat result = 0;
for (int i = 0; i < w * h; i++) { for (int i = 0; i < w * h; i++) {
@ -290,10 +302,12 @@ GLfloat sumDiffAny(GLfloat* A, GLfloat* B, int w, int h) {
return result; return result;
} }
// GET THE SUM OF DIFFERENCES BETWEEN TWO 3x3 MATRICES
GLfloat mat3SumDiff(mat3* A, mat3* B) { GLfloat mat3SumDiff(mat3* A, mat3* B) {
return sumDiffAny((GLfloat*)A, (GLfloat*)B, 3, 3); return sumDiffAny((GLfloat*)A, (GLfloat*)B, 3, 3);
} }
// COMPONENTWISE SUBTRACT vec2 b FROM vec2 a
void vec2Subtract(vec2* out, vec2* a, vec2* b) { void vec2Subtract(vec2* out, vec2* a, vec2* b) {
out->x = a->x - b->x; out->x = a->x - b->x;
out->y = a->y - b->y; out->y = a->y - b->y;