diff --git a/include/client/okuu/okuu.c b/include/client/okuu/okuu.c index a67ef63..561e234 100644 --- a/include/client/okuu/okuu.c +++ b/include/client/okuu/okuu.c @@ -1,2 +1,126 @@ #include "okuu.h" +#define OKUU_MFALL(F, P, D) \ + F(P##D##i_t, D##i) \ + F(P##D##ui_t, D##ui) \ + F(P##D##f_t, D##f) +#define OKUU_MFGALL(F, P) \ + F(P##gi_t, GLint, i) \ + F(P##gui_t, GLuint, ui) \ + F(P##gf_t, GLfloat, f) + +/** MATRIX GEN FUNCS **/ + +#define OKUU_IDENT4(T, S) \ + inline T okuu_ident##S(void) { \ + return (T) {{ \ + {1, 0, 0, 0}, \ + {0, 1, 0, 0}, \ + {0, 0, 1, 0}, \ + {0, 0, 0, 1}, \ + }}; \ + } +OKUU_MFALL(OKUU_IDENT4, mat, 4) + +#define OKUU_IDENT3(T, S) \ + inline T okuu_ident##S(void) { \ + return (T) {{ \ + {1, 0, 0}, \ + {0, 1, 0}, \ + {0, 0, 1}, \ + }}; \ + } +OKUU_MFALL(OKUU_IDENT3, mat, 3) + +#define OKUU_IDENT2(T, S) \ + inline T okuu_ident##S(void) { \ + return (T) {{ \ + {1, 0}, \ + {0, 1}, \ + }}; \ + } +OKUU_MFALL(OKUU_IDENT2, mat, 2) + +mat4f_t okuu_ortho_ex + (GLfloat left, GLfloat right, + GLfloat top, GLfloat bottom, + GLfloat near, GLfloat far) +{ + // TODO this + return (mat4f_t) {{{}}}; +} + +inline mat4f_t okuu_ortho + (GLfloat left, GLfloat right, + GLfloat top, GLfloat bottom) +{ + return okuu_ortho_ex(left, right,top, bottom, -1, 1); +} + +/** END MATRIX GEN FUNCS **/ + +/** MATRIX OPERATIONS **/ + +#define OKUU_TRANSW(T, D, P, S) \ + T* okuu_transw##S(T* in) { \ + int i, j; P swap; \ + for(i = 0; i < D; ++i) { \ + for(j = 0; j < D; ++j) { \ + swap = in->mat[i][j]; \ + in->mat[i][j] = in->mat[D - i - 1][D - j - 1]; \ + in->mat[D - i - 1][D - j - 1] = swap; \ + } \ + } \ + return in; \ + } +#define OKUU_TRANSW_MALL(T, D) \ + OKUU_TRANSW(T##D##i_t, D, GLint, D##i) \ + OKUU_TRANSW(T##D##ui_t, D, GLuint, D##ui) \ + OKUU_TRANSW(T##D##f_t, D, GLfloat, D##f) +OKUU_TRANSW_MALL(mat, 4) +OKUU_TRANSW_MALL(mat, 3) +OKUU_TRANSW_MALL(mat, 2) + +#define OKUU_TRANS_SQ(T, S) \ + inline T okuu_trans##S(T in) { \ + return *okuu_transw##S(&in); \ + } +OKUU_MFALL(OKUU_TRANS_SQ, mat, 4) +OKUU_MFALL(OKUU_TRANS_SQ, mat, 3) +OKUU_MFALL(OKUU_TRANS_SQ, mat, 2) + +#define OKUU_TRANS(R, C, P, TS) \ + mat##R##x##C##TS##_t \ + okuu_trans##C##x##R##TS(mat##C##x##R##TS##_t in) \ + { \ + int i, j; mat##R##x##C##TS##_t out; \ + for(i = 0; i < R; ++i) \ + for(j = 0; j < C; ++j) \ + out.mat[j][i] = in.mat[i][j]; \ + return out; \ + } +#define OKUU_TRANS_MALL(R, C) \ + OKUU_TRANS(R, C, GLint, i) \ + OKUU_TRANS(R, C, GLuint, ui) \ + OKUU_TRANS(R, C, GLfloat, f) +OKUU_TRANS_MALL(4, 3) +OKUU_TRANS_MALL(3, 4) +OKUU_TRANS_MALL(4, 2) +OKUU_TRANS_MALL(2, 4) +OKUU_TRANS_MALL(3, 2) +OKUU_TRANS_MALL(2, 3) + +/** END MATRIX OPERATIONS **/ + +/** DEBUG FUNCTIONS **/ + +#define OKUU_PRINTM(T, P, S) \ + void okuu_printm##S(void* mat, int rows, int cols) { \ + int i, j; \ + for(i = 0; i < rows; ++i) \ + for(j = 0; j < cols; ++j) \ + printf("%f ", ((T*)mat)->gl[i*cols + j]); \ + } +OKUU_MFGALL(OKUU_PRINTM, mat) + +/** END DEBUG FUNCTIONS **/ \ No newline at end of file diff --git a/include/client/okuu/okuu.h b/include/client/okuu/okuu.h index 770cab7..060ca1b 100644 --- a/include/client/okuu/okuu.h +++ b/include/client/okuu/okuu.h @@ -1,10 +1,20 @@ #ifndef CSC_OKUU_H #define CSC_OKUU_H +#include #include /** PRIMITIVES **/ +#define OKUU_MGTYPE(T) \ + typedef union { \ + T** mat; \ + T* gl; \ + } +OKUU_MGTYPE(GLint) matgi_t; +OKUU_MGTYPE(GLuint) matgui_t; +OKUU_MGTYPE(GLfloat) matgf_t; + #define OKUU_MTYPE(T, R, C) \ typedef union { \ T mat[R][C]; \ @@ -20,6 +30,13 @@ OKUU_MTALL(4, 4, mat4); OKUU_MTALL(3, 3, mat3); OKUU_MTALL(2, 2, mat2); +OKUU_MTALL(3, 4, mat4x3); +OKUU_MTALL(4, 3, mat3x4); +OKUU_MTALL(2, 4, mat4x2); +OKUU_MTALL(4, 2, mat2x4); +OKUU_MTALL(2, 3, mat3x2); +OKUU_MTALL(3, 2, mat2x3); + OKUU_MTALL(4, 1, vec4); OKUU_MTALL(3, 1, vec3); OKUU_MTALL(2, 1, vec2); @@ -28,40 +45,68 @@ OKUU_MTALL(2, 1, vec2); /** MATRIX GEN FUNCS **/ -#define OKUU_MGALL(M) \ - M(mat4i_t, i) \ - M(mat4ui_t, ui) \ - M(mat4f_t, f) - -#define OKUU_IDENT(T, S) \ - inline T okuu_ident##S(void) { \ - return (T) {{ \ - {1, 0, 0, 0}, \ - {0, 1, 0, 0}, \ - {0, 0, 1, 0}, \ - {0, 0, 0, 1}, \ - }}; \ - } - -OKUU_MGALL(OKUU_IDENT) +#define OKUU_IDENT_PROTO(S) \ + mat##S##_t okuu_ident##S(void); +#define OKUU_IDENT_ALL(D) \ + OKUU_IDENT_PROTO(D##i) \ + OKUU_IDENT_PROTO(D##ui) \ + OKUU_IDENT_PROTO(D##f) +OKUU_IDENT_ALL(4) +OKUU_IDENT_ALL(3) +OKUU_IDENT_ALL(2) mat4f_t okuu_ortho_ex (GLfloat left, GLfloat right, GLfloat top, GLfloat bottom, GLfloat near, GLfloat far); -inline mat4f_t okuu_ortho +mat4f_t okuu_ortho (GLfloat left, GLfloat right, - GLfloat top, GLfloat bottom) -{ - return okuu_ortho_ex(left, right,top, bottom, -1, 1); -} - + GLfloat top, GLfloat bottom); /** END MATRIX GEN FUNCS **/ /** MATRIX OPERATIONS **/ +#define OKUU_SQTRANSW_ALL(F, D) \ + F(D##i) \ + F(D##ui) \ + F(D##f) + +#define OKUU_TRANSW_PROTO(S) \ + mat##S##_t* okuu_transw##S(mat##S##_t* in); +OKUU_SQTRANSW_ALL(OKUU_TRANSW_PROTO, 4) +OKUU_SQTRANSW_ALL(OKUU_TRANSW_PROTO, 3) +OKUU_SQTRANSW_ALL(OKUU_TRANSW_PROTO, 2) + +#define OKUU_SQTRANS_PROTO(S) \ + mat##S##_t okuu_trans##S(mat##S##_t in); +OKUU_SQTRANSW_ALL(OKUU_SQTRANS_PROTO, 4) +OKUU_SQTRANSW_ALL(OKUU_SQTRANS_PROTO, 3) +OKUU_SQTRANSW_ALL(OKUU_SQTRANS_PROTO, 2) + +#define OKUU_TRANS_PROTO(S, R, C) \ + mat##R##x##C##S##_t \ + okuu_trans##C##x##R##S(mat##C##x##R##S##_t in); +#define OKUU_TRANS_ALL(R, C) \ + OKUU_TRANS_PROTO(i, R, C) \ + OKUU_TRANS_PROTO(ui, R, C) \ + OKUU_TRANS_PROTO(f, R, C) +OKUU_TRANS_ALL(4, 3) +OKUU_TRANS_ALL(3, 4) +OKUU_TRANS_ALL(4, 2) +OKUU_TRANS_ALL(2, 4) +OKUU_TRANS_ALL(3, 2) +OKUU_TRANS_ALL(2, 3) + /** END MATRIX OPERATIONS **/ +/** DEBUG FUNCTIONS **/ + +void okuu_printmi(void* mat, int rows, int cols); +void okuu_printmui(void* mat, int rows, int cols); +void okuu_printmf(void* mat, int rows, int cols); + +/** END DEBUG FUNCTIONS **/ + #endif diff --git a/src/client/main.c b/src/client/main.c index 34006a5..f8721cc 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -5,6 +5,8 @@ #include #include +#include "okuu/okuu.h" + static struct { SDL_Window* hwnd; SDL_GLContext gl; @@ -13,6 +15,16 @@ static struct { void draw(); int main(int argc, char** argv) { + mat4f_t test = {{ + {1, 2, 1, 2}, + {3, 4, 3, 4}, + {5, 6, 5, 6}, + {7, 8, 7, 8}, + }}; + + okuu_transw4f(&test); + okuu_printmf(&test, 4, 4); + if(argc != 3) return -1;