Added comments on math stuff

This commit is contained in:
JonasJan2 2024-06-19 12:50:11 +02:00
parent 3c0b9f42c1
commit 94a01bec41
1 changed files with 115 additions and 1 deletions

View File

@ -8,31 +8,53 @@
// MATRICES IN COLUMN MAJOR // MATRICES IN COLUMN MAJOR
// Sets a vector to be (0,0,0)T.
void vec3Zero(vec3* out) { void vec3Zero(vec3* out) {
assert(out != NULL);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
((GLfloat*)out)[i] = 0; ((GLfloat*)out)[i] = 0;
} }
} }
// Adds two vectors and writes the result into out.
void vec3Add(vec3* out, vec3* a, vec3* b) { void vec3Add(vec3* out, vec3* a, vec3* b) {
assert(a != NULL);
assert(b != NULL);
assert(out != NULL);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
((GLfloat*)out)[i] = ((GLfloat*)a)[i] + ((GLfloat*)b)[i]; ((GLfloat*)out)[i] = ((GLfloat*)a)[i] + ((GLfloat*)b)[i];
} }
} }
// Multiply a vector by a factor.
void vec3Multiply(vec3* out, vec3* a, GLfloat x) { void vec3Multiply(vec3* out, vec3* a, GLfloat x) {
assert(out != NULL);
assert(a != NULL);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
((GLfloat*)out)[i] = ((GLfloat*)a)[i] * x; ((GLfloat*)out)[i] = ((GLfloat*)a)[i] * x;
} }
} }
// Divide a vector by another vector.
void vec3Subtract(vec3* out, vec3* a, vec3* b) { void vec3Subtract(vec3* out, vec3* a, vec3* b) {
assert(out != NULL);
assert(a != NULL);
assert(b != NULL);
vec3 minusB; vec3 minusB;
vec3Multiply(&minusB, b, -1); vec3Multiply(&minusB, b, -1);
vec3Add(out, a, &minusB); vec3Add(out, a, &minusB);
} }
// Calculates the cross product of two vectors.
void vec3Cross(vec3* out, vec3* a, vec3* b) { void vec3Cross(vec3* out, vec3* a, vec3* b) {
assert(out != NULL);
assert(a != NULL);
assert(b != NULL);
vec3 result; vec3 result;
result.x = a->y * b->z - a->z * b->y; result.x = a->y * b->z - a->z * b->y;
result.y = a->z * b->x - a->x * b->z; result.y = a->z * b->x - a->x * b->z;
@ -40,19 +62,31 @@ void vec3Cross(vec3* out, vec3* a, vec3* b) {
memcpy(out, &result, sizeof(vec3)); memcpy(out, &result, sizeof(vec3));
} }
// Return the length of a vector.
GLfloat vec3Length(vec3* a) { GLfloat vec3Length(vec3* a) {
assert(a != NULL);
return (GLfloat)sqrt(a->x * a->x + a->y * a->y + a->z * a->z); return (GLfloat)sqrt(a->x * a->x + a->y * a->y + a->z * a->z);
} }
// Return the dot product of a vector.
GLfloat vec3Dot(vec3* a, vec3* b) { GLfloat vec3Dot(vec3* a, vec3* b) {
assert(a != NULL);
assert(b != NULL);
return a->x * b->x + a->y * b->y + a->z * b->z; return a->x * b->x + a->y * b->y + a->z * b->z;
} }
// Normalise a vector.
void vec3Normalise(vec3* out, vec3* a) { void vec3Normalise(vec3* out, vec3* a) {
assert(out != NULL);
assert(a != NULL);
vec3Multiply(out, a, 1 / vec3Length(a)); vec3Multiply(out, a, 1 / vec3Length(a));
} }
// CREATE 4x4 IDENTITY MATRIX // CREATE 4x4 IDENTITY MATRIX
void identity(mat4* out) { void identity(mat4* out) {
assert(out != NULL);
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
((GLfloat*)out)[i] = (i % 4 == i / 4); ((GLfloat*)out)[i] = (i % 4 == i / 4);
} }
@ -60,6 +94,8 @@ void identity(mat4* out) {
// CREATE 4x4 TRANSLATION MATRIX // CREATE 4x4 TRANSLATION MATRIX
void translation(mat4* out, vec3* v) { void translation(mat4* out, vec3* v) {
assert(out != NULL);
identity(out); identity(out);
out->m03 = v->x; out->m03 = v->x;
out->m13 = v->y; out->m13 = v->y;
@ -68,6 +104,8 @@ void translation(mat4* out, vec3* v) {
// CREATE 4x4 SCALING MATRIX // CREATE 4x4 SCALING MATRIX
void scaling(mat4* out, vec3* v) { void scaling(mat4* out, vec3* v) {
assert(out != NULL);
identity(out); identity(out);
out->m00 = v->x; out->m00 = v->x;
out->m11 = v->y; out->m11 = v->y;
@ -81,6 +119,7 @@ void scaling(mat4* out, vec3* v) {
* 0 0 0 1 * 0 0 0 1
*/ */
void rotationZ(mat4* out, GLfloat angle) { void rotationZ(mat4* out, GLfloat angle) {
assert(out != NULL);
identity(out); identity(out);
out->m00 = cos(angle); out->m00 = cos(angle);
out->m10 = sin(angle); out->m10 = sin(angle);
@ -95,6 +134,8 @@ void rotationZ(mat4* out, GLfloat angle) {
* 0 0 0 1 * 0 0 0 1
*/ */
void rotationY(mat4* out, GLfloat angle) { void rotationY(mat4* out, GLfloat angle) {
assert(out != NULL);
identity(out); identity(out);
out->m00 = cos(angle); out->m00 = cos(angle);
out->m20 = -sin(angle); out->m20 = -sin(angle);
@ -104,6 +145,8 @@ void rotationY(mat4* out, GLfloat angle) {
// CREATE 4x4 ROTATION MATRIX AROUND Y AXIS // CREATE 4x4 ROTATION MATRIX AROUND Y AXIS
void rotationX(mat4* out, GLfloat angle) { void rotationX(mat4* out, GLfloat angle) {
assert(out != NULL);
identity(out); identity(out);
out->m11 = cos(angle); out->m11 = cos(angle);
out->m21 = sin(angle); out->m21 = sin(angle);
@ -113,6 +156,10 @@ void rotationX(mat4* out, GLfloat angle) {
// MULTIPLY ANY TWO 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) {
assert(out != NULL);
assert(A != NULL);
assert(B != NULL);
int sizeOut = hA * wB; int sizeOut = hA * wB;
GLfloat* result = (GLfloat*) malloc(sizeOut * sizeof(GLfloat)); GLfloat* result = (GLfloat*) malloc(sizeOut * sizeof(GLfloat));
for (int i = 0; i < sizeOut; i++) { for (int i = 0; i < sizeOut; i++) {
@ -128,11 +175,19 @@ void multiplyAny(GLfloat* out, GLfloat* A, GLfloat* B, int wA, int hA, int wB) {
// MULTIPLY TWO 4x4 MATRICES // MULTIPLY TWO 4x4 MATRICES
void multiply(mat4* out, mat4* A, mat4* B) { void multiply(mat4* out, mat4* A, mat4* B) {
assert(out != NULL);
assert(A != NULL);
assert(B != NULL);
multiplyAny((GLfloat*)out, (GLfloat*)A, (GLfloat*)B, 4, 4, 4); multiplyAny((GLfloat*)out, (GLfloat*)A, (GLfloat*)B, 4, 4, 4);
} }
// MULTIPLY in WITH TRANSLATION MATRIX OF v // MULTIPLY in WITH TRANSLATION MATRIX OF v
void translate(mat4* out, mat4* in, vec3* v) { void translate(mat4* out, mat4* in, vec3* v) {
assert(out != NULL);
assert(in != NULL);
assert(v != NULL);
mat4 translationMatrix; mat4 translationMatrix;
translation(&translationMatrix, v); translation(&translationMatrix, v);
multiply(out, &translationMatrix, in); multiply(out, &translationMatrix, in);
@ -140,6 +195,10 @@ void translate(mat4* out, mat4* in, vec3* v) {
// MULTIPLY in WITH SCALING MATRIX OF v // MULTIPLY in WITH SCALING MATRIX OF v
void scale(mat4* out, mat4* in, vec3* v) { void scale(mat4* out, mat4* in, vec3* v) {
assert(out != NULL);
assert(in != NULL);
assert(v != NULL);
mat4 scalingMatrix; mat4 scalingMatrix;
scaling(&scalingMatrix, v); scaling(&scalingMatrix, v);
multiply(out, &scalingMatrix, in); multiply(out, &scalingMatrix, in);
@ -147,18 +206,27 @@ void scale(mat4* out, mat4* in, vec3* v) {
// MULTIPLY in WITH ROTATION MATRIX OF angle 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) {
assert(out != NULL);
assert(in != NULL);
mat4 rotationMatrix; mat4 rotationMatrix;
rotationZ(&rotationMatrix, angle); rotationZ(&rotationMatrix, angle);
multiply(out, &rotationMatrix, in); multiply(out, &rotationMatrix, in);
} }
// MULTIPLY in WITH ROTATION MATRIX OF angle 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) {
assert(out != NULL);
assert(in != NULL);
mat4 rotationMatrix; mat4 rotationMatrix;
rotationY(&rotationMatrix, angle); rotationY(&rotationMatrix, angle);
multiply(out, &rotationMatrix, in); multiply(out, &rotationMatrix, in);
} }
// MULTIPLY in WITH ROTATION MATRIX OF angle 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) {
assert(out != NULL);
assert(in != NULL);
mat4 rotationMatrix; mat4 rotationMatrix;
rotationX(&rotationMatrix, angle); rotationX(&rotationMatrix, angle);
multiply(out, &rotationMatrix, in); multiply(out, &rotationMatrix, in);
@ -166,6 +234,9 @@ void rotateX(mat4* out, mat4* in, GLfloat angle) {
// TRANSPOSE MATRIX OF ANY SIZE // 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) {
assert(out != NULL);
assert(in != NULL);
int size = w * h; int size = w * h;
GLfloat* result = (GLfloat*) malloc(size * sizeof(GLfloat)); GLfloat* result = (GLfloat*) malloc(size * sizeof(GLfloat));
@ -181,11 +252,15 @@ void transposeAny(GLfloat* out, GLfloat* in, int w, int h) {
// TRANSPOSE 4x4 MATRIX // TRANSPOSE 4x4 MATRIX
void transpose(mat4* out, mat4* in) { void transpose(mat4* out, mat4* in) {
assert(out != NULL);
assert(in != NULL);
transposeAny((GLfloat*)out, (GLfloat*)in, 4, 4); transposeAny((GLfloat*)out, (GLfloat*)in, 4, 4);
} }
// PRINT MATRIX OF ANY SIZE // PRINT MATRIX OF ANY SIZE
void printAny(GLfloat* M, int w, int h) { void printAny(GLfloat* M, int w, int h) {
assert(M != NULL);
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);
@ -200,19 +275,28 @@ void printAny(GLfloat* M, int w, int h) {
} }
void vec3Print(vec3* a) { void vec3Print(vec3* a) {
assert(a != NULL);
printAny((GLfloat*)a, 1, 3); printAny((GLfloat*)a, 1, 3);
} }
void mat4Print(mat4* m) { void mat4Print(mat4* m) {
assert(m != NULL);
printAny((GLfloat*)m, 4, 4); printAny((GLfloat*)m, 4, 4);
} }
void mat3Print(mat3* m) { void mat3Print(mat3* m) {
assert(m != NULL);
printAny((GLfloat*)m, 3, 3); printAny((GLfloat*)m, 3, 3);
} }
// TURN mat4 INTO mat3 BY CUTTING LAST COLUMN AND ROW // TURN mat4 INTO mat3 BY CUTTING LAST COLUMN AND ROW
void mat3From4(mat3* out, mat4* in) { void mat3From4(mat3* out, mat4* in) {
assert(out != NULL);
assert(in != NULL);
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));
@ -220,6 +304,9 @@ void mat3From4(mat3* out, mat4* in) {
// TRANSPOSE 3x3 MATRIX // TRANSPOSE 3x3 MATRIX
void mat3Transpose(mat3* out, mat3* in) { void mat3Transpose(mat3* out, mat3* in) {
assert(out != NULL);
assert(in != NULL);
transposeAny((GLfloat*)out, (GLfloat*)in, 3, 3); transposeAny((GLfloat*)out, (GLfloat*)in, 3, 3);
} }
@ -230,6 +317,9 @@ void mat3Transpose(mat3* out, mat3* in) {
*/ */
// GET THE MINOR MATRIX OF A 3x3 MATRIX // GET THE MINOR MATRIX OF A 3x3 MATRIX
void mat3Minor(mat3* out, mat3* in) { void mat3Minor(mat3* out, mat3* in) {
assert(out != NULL);
assert(in != NULL);
mat3 result; mat3 result;
result.m00 = in->m11 * in->m22 - in->m21 * in->m12; result.m00 = in->m11 * in->m22 - in->m21 * in->m12;
@ -249,6 +339,9 @@ void mat3Minor(mat3* out, mat3* in) {
// GET THE COFACTOR MATRIX OF A 3x3 MATRIX // GET THE COFACTOR MATRIX OF A 3x3 MATRIX
void mat3Cofactor(mat3* out, mat3* in) { void mat3Cofactor(mat3* out, mat3* in) {
assert(out != NULL);
assert(in != NULL);
mat3 result; mat3 result;
mat3Minor(out, in); mat3Minor(out, in);
@ -260,12 +353,18 @@ void mat3Cofactor(mat3* out, mat3* in) {
// GET ADJOING MATRRIX OF 3x3 MATRIX // GET ADJOING MATRRIX OF 3x3 MATRIX
void mat3Adjoint(mat3* out, mat3* in) { void mat3Adjoint(mat3* out, mat3* in) {
assert(out != NULL);
assert(in != NULL);
mat3Cofactor(out, in); mat3Cofactor(out, in);
mat3Transpose(out, out); mat3Transpose(out, out);
} }
// MULTIPLY A 3x3 MATRIX WITH A SCALAR x (componentwise) // 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) {
assert(out != NULL);
assert(in != NULL);
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;
} }
@ -273,6 +372,8 @@ void mat3MultiplyScalar(mat3* out, mat3* in, GLfloat x) {
// CALCULATE DETERMINANT OF 3x3 MATRIX // CALCULATE DETERMINANT OF 3x3 MATRIX
GLfloat mat3Determinant(mat3* M) { GLfloat mat3Determinant(mat3* M) {
assert(M != NULL);
return return
M->m00 * M->m11 * M->m22 M->m00 * M->m11 * M->m22
+ M->m01 * M->m12 * M->m20 + M->m01 * M->m12 * M->m20
@ -286,6 +387,9 @@ GLfloat mat3Determinant(mat3* M) {
// GET INVERSE OF 3x3 MATRIX // GET INVERSE OF 3x3 MATRIX
void mat3Inverse(mat3* out, mat3* in) { void mat3Inverse(mat3* out, mat3* in) {
assert(out != NULL);
assert(in != NULL);
mat3 result; mat3 result;
mat3Adjoint(&result, in); mat3Adjoint(&result, in);
mat3MultiplyScalar(&result, &result, 1 / mat3Determinant(in)); mat3MultiplyScalar(&result, &result, 1 / mat3Determinant(in));
@ -293,8 +397,11 @@ void mat3Inverse(mat3* out, mat3* in) {
memcpy(out, &result, sizeof(mat3)); memcpy(out, &result, sizeof(mat3));
} }
// GET THE SUM OF DIFFERENCES BETWEEON TWO MATRICES // GET THE SUM OF DIFFERENCES BETWEEN TWO MATRICES
GLfloat sumDiffAny(GLfloat* A, GLfloat* B, int w, int h) { GLfloat sumDiffAny(GLfloat* A, GLfloat* B, int w, int h) {
assert(A != NULL);
assert(B != NULL);
GLfloat result = 0; GLfloat result = 0;
for (int i = 0; i < w * h; i++) { for (int i = 0; i < w * h; i++) {
result += fabs(A[i] - B[i]); result += fabs(A[i] - B[i]);
@ -304,11 +411,18 @@ GLfloat sumDiffAny(GLfloat* A, GLfloat* B, int w, int h) {
// GET THE SUM OF DIFFERENCES BETWEEN TWO 3x3 MATRICES // GET THE SUM OF DIFFERENCES BETWEEN TWO 3x3 MATRICES
GLfloat mat3SumDiff(mat3* A, mat3* B) { GLfloat mat3SumDiff(mat3* A, mat3* B) {
assert(A != NULL);
assert(B != NULL);
return sumDiffAny((GLfloat*)A, (GLfloat*)B, 3, 3); return sumDiffAny((GLfloat*)A, (GLfloat*)B, 3, 3);
} }
// COMPONENTWISE SUBTRACT vec2 b FROM vec2 a // COMPONENTWISE SUBTRACT vec2 b FROM vec2 a
void vec2Subtract(vec2* out, vec2* a, vec2* b) { void vec2Subtract(vec2* out, vec2* a, vec2* b) {
assert(out != NULL);
assert(a != NULL);
assert(b != NULL);
out->x = a->x - b->x; out->x = a->x - b->x;
out->y = a->y - b->y; out->y = a->y - b->y;
} }