#include #include #include #include #include #include "matrixMath.h" // MATRICES IN COLUMN MAJOR void vec3Zero(vec3* out) { for (int i = 0; i < 3; i++) { ((GLfloat*)out)[i] = 0; } } void vec3Add(vec3* out, vec3* a, vec3* b) { for (int i = 0; i < 3; i++) { ((GLfloat*)out)[i] = ((GLfloat*)a)[i] + ((GLfloat*)b)[i]; } } void vec3Multiply(vec3* out, vec3* a, GLfloat x) { for (int i = 0; i < 3; i++) { ((GLfloat*)out)[i] = ((GLfloat*)a)[i] * x; } } void vec3Subtract(vec3* out, vec3* a, vec3* b) { vec3 minusB; vec3Multiply(&minusB, b, -1); vec3Add(out, a, &minusB); } void vec3Cross(vec3* out, vec3* a, vec3* b) { vec3 result; result.x = a->y * b->z - a->z * b->y; result.y = a->z * b->x - a->x * b->z; result.z = a->x * b->y - a->y * b->x; memcpy(out, &result, sizeof(vec3)); } GLfloat vec3Length(vec3* a) { return (GLfloat)sqrt(a->x * a->x + a->y * a->y + a->z * a->z); } GLfloat vec3Dot(vec3* a, vec3* b) { return a->x * b->x + a->y * b->y + a->z * b->z; } void vec3Normalise(vec3* out, vec3* a) { vec3Multiply(out, a, 1 / vec3Length(a)); } // CREATE 4x4 IDENTITY MATRIX void identity(mat4* out) { for (int i = 0; i < 16; i++) { ((GLfloat*)out)[i] = (i % 4 == i / 4); } } // CREATE 4x4 TRANSLATION MATRIX void translation(mat4* out, vec3* v) { identity(out); out->m03 = v->x; out->m13 = v->y; out->m23 = v->z; } // CREATE 4x4 SCALING MATRIX void scaling(mat4* out, vec3* v) { identity(out); out->m00 = v->x; out->m11 = v->y; out->m22 = v->z; } // CREATE 4x4 ROTATION MATRIX AROUND Z AXIS /* cos a -sin a 0 0 * sin a cos a 0 0 * 0 0 1 0 * 0 0 0 1 */ void rotationZ(mat4* out, GLfloat angle) { identity(out); out->m00 = cos(angle); out->m10 = sin(angle); out->m01 = -sin(angle); out->m11 = cos(angle); } // CREATE 4x4 ROTATION MATRIX AROUND Y AXIS void rotationY(mat4* out, GLfloat angle) { identity(out); out->m00 = cos(angle); out->m20 = -sin(angle); out->m02 = sin(angle); out->m22 = cos(angle); } // CREATE 4x4 ROTATION MATRIX AROUND Y AXIS void rotationX(mat4* out, GLfloat angle) { identity(out); out->m11 = cos(angle); out->m21 = sin(angle); out->m12 = -sin(angle); out->m22 = cos(angle); } // MULTIPLY ANY TO 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)); for (int i = 0; i < sizeOut; i++) { result[i] = 0; for (int j = 0; j < wA; j++) { result[i] += A[j * hA + i % hA] * B[j + i / hA * wB]; } } memcpy(out, result, sizeOut * sizeof(GLfloat)); free(result); result = NULL; } // MULTIPLY TWO 4x4 MATRICES void multiply(mat4* out, mat4* A, mat4* B) { 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) { mat4 translationMatrix; translation(&translationMatrix, v); multiply(out, &translationMatrix, in); } // MULTIPLY in WITH SCALING MATRIX OF v void scale(mat4* out, mat4* in, vec3* v) { mat4 scalingMatrix; scaling(&scalingMatrix, v); multiply(out, &scalingMatrix, in); } // MULTIPLY in WITH ROTATION MATRIX OF a 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 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 void rotateX(mat4* out, mat4* in, GLfloat angle) { mat4 rotationMatrix; rotationX(&rotationMatrix, angle); multiply(out, &rotationMatrix, in); } void transposeAny(GLfloat* out, GLfloat* in, int w, int h) { int size = w * h; GLfloat* result = (GLfloat*) malloc(size * sizeof(GLfloat)); for (int i = 0; i < size; i++) { result[i] = in[(i % w) * h + i / w]; } memcpy(out, result, size * sizeof(GLfloat)); free(result); result = NULL; } void transpose(mat4* out, mat4* in) { transposeAny((GLfloat*)out, (GLfloat*)in, 4, 4); } void printAny(GLfloat* M, int w, int h) { GLfloat* transposed = (GLfloat*) malloc(w * h * sizeof(GLfloat)); transposeAny(transposed, M, w, h); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { printf("%.4f ", transposed[i * w + j]); } printf("\n"); } free(transposed); transposed = NULL; } void vec3Print(vec3* a) { printAny((GLfloat*)a, 1, 3); } void mat4Print(mat4* m) { printAny((GLfloat*)m, 4, 4); }