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