From e0f3c7d2ff0ea0c1c5cf77c83dd1d462f9090b1a Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Wed, 2 Apr 2025 17:26:16 +0200 Subject: [PATCH] matrix math beginnings --- meson.build | 3 ++- src/main.c | 11 ++++++++ src/matrix-math.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++ src/matrix-math.h | 23 ++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/matrix-math.c create mode 100644 src/matrix-math.h diff --git a/meson.build b/meson.build index f4c14b5..89c9769 100644 --- a/meson.build +++ b/meson.build @@ -6,7 +6,8 @@ m_dep = cc.find_library('m', required : false) src_files = [ './src/main.c', './src/shader.c', - './src/log.c' + './src/log.c', + './src/matrix-math.c' ] executable('cg1', diff --git a/src/main.c b/src/main.c index f20b9e9..29b47e6 100644 --- a/src/main.c +++ b/src/main.c @@ -10,6 +10,7 @@ #include "shader.h" #include "log.h" #include "shader.h" +#include "matrix-math.h" #define STATUS_INTERVAL 0.5 @@ -78,6 +79,16 @@ void updateStatusDisplay() { } void init(void) { + + mat4 t = { + 1, 2, 3, 4, + 5, 6, 7, 8, + 9, 10, 11, 12, + 13, 14, 15, 16 + }; + + mat4Print(t); + INFO("Compiling Shaders..."); // create and compile vertex shader diff --git a/src/matrix-math.c b/src/matrix-math.c new file mode 100644 index 0000000..f561bb0 --- /dev/null +++ b/src/matrix-math.c @@ -0,0 +1,67 @@ +#include "matrix-math.h" +#include +#include + +/** + * overwrites a 4x4 matrix with the identity matrix + */ +void mat4Identity(mat4 mat) { + for (int i = 0; i < 16; i++) { + mat[i] = (i % 4 == i / 4) ? 1 : 0; + } +} + +/** + * copies a mat4 from src to dst + */ +void mat4Copy(mat4 src, mat4 dst) { + for (int i = 0; i < 16; i++) { + dst[i] = src[i]; + } +} + +/** + * sets all the values in a mat4 to zero + */ +void mat4Empty(mat4 mat) { + for (int i = 0; i < 16; i++) { + mat[i] = 0; + } +} + +/** + * mutliplies A with B and stores the result in results + */ +void mat4Multiply(mat4 result, mat4 A, mat4 B) { + // if result is one of the arguments, modify copy instead of result directly + if (result == A || result == B) { + mat4 tempResult; + mat4Multiply(tempResult, A, B); + mat4Copy(tempResult, result); + return; + } + + for (int i = 0; i < 16; i++) { + int col = i / 4; + int row = i % 4; + GLfloat* curr = &(result[i]); + + *curr = 0; + + for (int j = 0; j < 4; j++) { + *curr += A[row + j * 4] * B[j + col * 4]; + } + } +} + +/** + * prints a mat4 to the screen + */ +void mat4Print(mat4 m) { + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + printf("%.2f ", m[j * 4 + i]); + } + printf("\n"); + } +} \ No newline at end of file diff --git a/src/matrix-math.h b/src/matrix-math.h new file mode 100644 index 0000000..d1a098b --- /dev/null +++ b/src/matrix-math.h @@ -0,0 +1,23 @@ +#ifndef MATRIX_MATH_H +#define MATRIX_MATH_H + +#include + +// TODO: figure out better type definition to avoid constant casting to GLfloat* +// possibly typedef float mat4[16]; + +/** + * !!! ALL matrices are in column major + */ + +typedef GLfloat vec4[4]; + +typedef GLfloat mat4[16]; + +extern void mat4Identity(mat4 mat); +extern void mat4Copy(mat4 src, mat4 dst); +extern void mat4Empty(mat4 mat); +extern void mat4Multiply(mat4 result, mat4 A, mat4 B); +extern void mat4Print(mat4 m); + +#endif \ No newline at end of file