From ee144f22866c7d0537b6a2215b3609432209588d Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Sat, 30 Nov 2024 22:33:28 -0600 Subject: [PATCH 01/18] Assert Style --- src/input.c | 20 ++--- src/main.c | 34 ++++---- src/main.o | Bin 12824 -> 12744 bytes src/mesh.c | 4 +- src/surface.c | 221 ++++++++++++++++++++++++++------------------------ 5 files changed, 140 insertions(+), 139 deletions(-) diff --git a/src/input.c b/src/input.c index 319923a..666a950 100644 --- a/src/input.c +++ b/src/input.c @@ -9,7 +9,7 @@ unsigned char selected_axis = 0; int window_width; int window_height; -unsigned char animate_index=0; +unsigned char animate_index = 0; versor q = GLM_QUAT_IDENTITY_INIT; @@ -56,23 +56,22 @@ void __key_callback_input( projection.w = projection.x; projection.x = tmp; - animate_index=1; + animate_index = 1; break; case GLFW_KEY_O: tmp = projection.w; projection.w = projection.y; projection.y = tmp; - animate_index=2; + animate_index = 2; break; case GLFW_KEY_P: tmp = projection.w; projection.w = projection.z; projection.z = tmp; - animate_index=3; + animate_index = 3; break; } - return; } @@ -114,9 +113,9 @@ void __scroll_callback_input(GLFWwindow *window, double xoffset, double yoffset) versor p = GLM_QUAT_IDENTITY_INIT; versor r = GLM_QUAT_IDENTITY_INIT; - //glm_quatv(p, yoffset * ANGLE, axis[selected_axis]); - glm_quatv(p, yoffset * ANGLE * 2, (vec3){-1,0,0}); - glm_quatv(r, xoffset * ANGLE * 2, (vec3){0,1,0}); + // glm_quatv(p, yoffset * ANGLE, axis[selected_axis]); + glm_quatv(p, yoffset * ANGLE * 2, (vec3){-1, 0, 0}); + glm_quatv(r, xoffset * ANGLE * 2, (vec3){0, 1, 0}); glm_quat_mul(p, q, q); glm_quat_mul(r, q, q); @@ -172,10 +171,11 @@ end: glm_quat_normalize(q); // LOG INFO - if(0) + if (0) { printf("QUAT: %2.5f %2.5f %2.5f %2.5f\n", q[0], q[1], q[2], q[3]); - printf("PROY: %3d %3d %3d (%3d)\n", projection.x, projection.y, projection.z, projection.w ); + printf("PROY: %3d %3d %3d (%3d)\n", projection.x, projection.y, + projection.z, projection.w); printf("\n"); } return q; diff --git a/src/main.c b/src/main.c index 508fa1e..68e27ef 100644 --- a/src/main.c +++ b/src/main.c @@ -19,7 +19,7 @@ float *generate_data_surface(unsigned int, unsigned char *); float *generate_normals_surface(float *, unsigned char); -struct projection projection = {.x = 0, .y=1, .z=2, .w=3 }; +struct projection projection = {.x = 0, .y = 1, .z = 2, .w = 3}; const char *wname = "manigraph: manifold grapher"; @@ -59,29 +59,27 @@ static inline { static float angle = 0; - if( angle > M_PI/2 ) + if (angle > M_PI / 2) { - animate_index=0; + animate_index = 0; angle = 0; - load_float_to_shader( shader, "angle", angle); - load_float_to_shader( shader_plain, "angle", angle); - set_projection_mesh( projection ); + load_float_to_shader(shader, "angle", angle); + load_float_to_shader(shader_plain, "angle", angle); + set_projection_mesh(projection); } - if( animate_index ) + if (animate_index) { - load_uint_to_shader( shader, "i", animate_index-1 ); - load_uint_to_shader( shader_plain, "i", animate_index-1 ); + load_uint_to_shader(shader, "i", animate_index - 1); + load_uint_to_shader(shader_plain, "i", animate_index - 1); - angle+=0.01; - load_float_to_shader( shader, "angle", angle); - load_float_to_shader( shader_plain, "angle", angle); + angle += 0.01; + load_float_to_shader(shader, "angle", angle); + load_float_to_shader(shader_plain, "angle", angle); } } - - clean_context(); #ifndef DEBUG @@ -93,11 +91,10 @@ static inline draw_mesh(m_axis, 1); #endif load_mdl_matrix(shader, 0, 3); - draw_mesh(m_surface,0); + draw_mesh(m_surface, 0); load_mdl_matrix(shader_plain, 0, 3); - draw_mesh(m_surface,1); - + draw_mesh(m_surface, 1); } int main(void) @@ -170,8 +167,7 @@ int main(void) projection.m = m; projection.mesh = m_surface; - set_projection_mesh( projection ); - + set_projection_mesh(projection); free(n_surface); free(d_surface); diff --git a/src/main.o b/src/main.o index 36f5d064e6c5087469b5be291aefffa4c04bd2a0..55c9e3b301dfc9b4a77fe19b3fbf4d38bbe5234e 100644 GIT binary patch liter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@#yp3n~i;2-|;5T7cBq$x!K^M#pfBv;Abt|wea~C z_#2jgzT=IbOBUbGXW8Q0`TVPe+kSpv`LXl7NgtHCUUr@zvhZKF^4V?SzhdE!w!jZr z_i;wQ!sNO$)dA7cAW7f5*Zv$NSJAGYva7QWlU?R*}#aNB>T1^#&pxBWk3;dVYq zK^k9IynjvOo@6ulG9L(N`f(oNXY?R*?lt_s)$a>eMydCdb|FYl3^(5){W08phfK$C z^Bpo5!_E2rQVcie`0vGVb1ru}D^sJ@+%d>%S;-O8EBl39ryOlM2Xob&(|N2?aXQN- zrHGZ?PV_qY>u|y894L*B`o*f#Su9okPJH<8kz<+Ghl}H#IOUA_6r9AfsK+v?mz8PomVRTrcc>PxXpY#8M_8A{9KQJ&zETEY`O>8Id*Q|akx_;+`rRvA?kr{95 zNB1|x<`|b3@aY_lXFZOMkLOt_zV`q*+wSMpah78F$5i|l(0u&qJu=)0m58qK@&5;A CvnxUX diff --git a/src/mesh.c b/src/mesh.c index 0353741..59a7278 100644 --- a/src/mesh.c +++ b/src/mesh.c @@ -91,12 +91,12 @@ void destroy_mesh(mesh_t p) free(p); } -void draw_mesh(mesh_t p, char lines ) +void draw_mesh(mesh_t p, char lines) { struct obj *obj = p; glBindVertexArray(obj->vao); - if( lines ) + if (lines) { int i; for (i = 0; i < obj->vertex; i += 3) diff --git a/src/surface.c b/src/surface.c index 3e4aaa3..0207976 100644 --- a/src/surface.c +++ b/src/surface.c @@ -12,16 +12,16 @@ #endif #ifndef CMPLX -#define CMPLX(a,b) (a+I*b) +#define CMPLX(a, b) (a + I * b) #endif -void riemman(float *d_surface, int * coords, int grid_size) +void riemman(float *d_surface, int *coords, int grid_size) { complex double eq; float u = 2 * ((float)coords[0] / grid_size) - 1; float v = 2 * ((float)coords[1] / grid_size) - 1; - eq = csqrt(CMPLX(u,v)); + eq = csqrt(CMPLX(u, v)); d_surface[0] = u; d_surface[1] = v; @@ -29,16 +29,15 @@ void riemman(float *d_surface, int * coords, int grid_size) d_surface[3] = cimag(eq); } - -void cube( float *d_surface, int * coord, int grid_size ) +void cube(float *d_surface, int *coord, int grid_size) { unsigned char i; - for(int i=0; i<4; i++ ) - d_surface[i]=(float)coord[i]/grid_size; + for (int i = 0; i < 4; i++) + d_surface[i] = (float)coord[i] / grid_size; } -void mobius(float *d_surface, int * coord, int grid_size) +void mobius(float *d_surface, int *coord, int grid_size) { const float width = 0.5; float u = (2 * M_PI) * ((float)coord[0] / grid_size); @@ -49,7 +48,7 @@ void mobius(float *d_surface, int * coord, int grid_size) d_surface[2] = v * sin(u / 2); } -void torus(float *d_surface, int * coord, int grid_size) +void torus(float *d_surface, int *coord, int grid_size) { float u = (2 * M_PI) * ((float)coord[0] / grid_size); float v = (2 * M_PI) * ((float)coord[1] / grid_size); @@ -59,10 +58,10 @@ void torus(float *d_surface, int * coord, int grid_size) d_surface[2] = 0.5 * sin(v); } -void klein(float *d_surface, int * coord, int grid_size) +void klein(float *d_surface, int *coord, int grid_size) { float u = (2 * M_PI) * ((float)coord[0] / grid_size); - float v = (2 * M_PI) * ((float)coord[1]/ grid_size); + float v = (2 * M_PI) * ((float)coord[1] / grid_size); d_surface[0] = (0.5 * cos(v) + 0.5) * cos(u); d_surface[1] = (0.5 * cos(v) + 0.5) * sin(u); @@ -75,38 +74,38 @@ typedef void (*function_t)(float *, int *, int); float *generate_data_surface(int grid_size, unsigned char *s) { unsigned int i, j, k, o, p, l, n, m; - long size, q=0; + long size, q = 0; function_t f; float *d_surface; - const int dim =2; + const int dim = 2; int cara[dim]; - char bits[dim+1]; - bits[dim]=0; + char bits[dim + 1]; + bits[dim] = 0; - f =klein ; + f = klein; *s = 4; size = grid_size * grid_size * 6 * (*s) * 24; d_surface = malloc((size + 1) * sizeof(float)); d_surface[0] = size; - - for(o = 0; o < dim; o ++) - { + + for (o = 0; o < dim; o++) + { for (p = 0; p < o; p++) { - for (k = 0; k < (1 << (dim-2)); k++) + for (k = 0; k < (1 << (dim - 2)); k++) { - unsigned char skip=0; - for(n = 0; n < dim-2; n++) + unsigned char skip = 0; + for (n = 0; n < dim - 2; n++) { - if( n==(o-1) || n==p ) + if (n == (o - 1) || n == p) skip++; - cara[n+skip] = (k & (1< Date: Sun, 1 Dec 2024 13:23:29 -0600 Subject: [PATCH 02/18] Set up struct surface, and use variable grid --- src/main.c | 45 +++++++------------------- src/mesh.c | 20 ++++++------ src/surface.c | 89 ++++++++++++++++++++++++++------------------------- 3 files changed, 67 insertions(+), 87 deletions(-) diff --git a/src/main.c b/src/main.c index 0c152f6..c47dd7f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,4 @@ #include "main.h" -#include "data/axis.h" #include "data/shaders.h" #include @@ -16,8 +15,8 @@ #define M_PI 3.14159 #endif -float *generate_data_surface(unsigned char *); -float *generate_normals_surface(float *, unsigned char); +float *generate_data_surface(unsigned char *, unsigned long *); +float *generate_normals_surface(float *, unsigned char, unsigned long); struct projection projection = {.x = 0, .y = 1, .z = 2, .w = 3}; @@ -38,9 +37,8 @@ void mlog(char *msg) } window_t window; -mesh_t m_surface, m_axis; +mesh_t m_surface; id_t shader, shader_plain; -unsigned char m; extern volatile unsigned char animate_index; @@ -83,14 +81,6 @@ static inline } clean_context(); -#ifndef DEBUG - load_mdl_matrix(shader_plain, 0, 0); - draw_mesh(m_axis); - load_mdl_matrix(shader_plain, 1, 1); - draw_mesh(m_axis); - load_mdl_matrix(shader_plain, 2, 2); - draw_mesh(m_axis); -#endif load_mdl_matrix(shader, 0, 3); draw_mesh(m_surface); @@ -155,32 +145,25 @@ int main(void) mlog("[MESH] Inicializando...\n"); { - float *n_surface, *d_surface; - d_surface = generate_data_surface(&m); - n_surface = generate_normals_surface(d_surface, m); + struct surface surface; - if (!(m_surface = create_mesh(d_surface, n_surface, m))) + surface.data = generate_data_surface(&surface.dim, &surface.vertex); + surface.norm = + generate_normals_surface(surface.data, surface.dim, surface.vertex); + + if (!(m_surface = create_mesh(surface))) { mlog("[MESH] Error al inicializar...\n"); goto error_mesh_surface; } - projection.m = m; + projection.m = surface.dim; projection.mesh = m_surface; set_projection_mesh(projection); - free(n_surface); - free(d_surface); - } - - mlog("[MESH] Inicializando...\n"); - { - if (!(m_axis = create_mesh(d_axis, NULL, 3))) - { - mlog("[MESH] Error al inicializar...\n"); - goto error_mesh_axis; - } + free(surface.norm); + free(surface.data); } mlog("[MAIN LOOP] Inicializando...\n"); @@ -193,8 +176,6 @@ int main(void) #endif mlog("[MAIN LOOP] Terminando...\n"); - mlog("[MESH] Destruyendo...\n"); - destroy_mesh(m_axis); mlog("[MESH] Destruyendo...\n"); destroy_mesh(m_surface); mlog("[SHADER] Destruyendo...\n"); @@ -207,8 +188,6 @@ int main(void) close_window(window); return 0; - mlog("[MESH] Destruyendo...\n"); - destroy_mesh(m_axis); error_mesh_axis: mlog("[MESH] Destruyendo...\n"); destroy_mesh(m_surface); diff --git a/src/mesh.c b/src/mesh.c index df044ae..8caf45f 100644 --- a/src/mesh.c +++ b/src/mesh.c @@ -47,36 +47,34 @@ void set_projection_mesh(struct projection projection) This trick can be done with glVertexAttribPointer. */ -mesh_t create_mesh(float *d, float *n, unsigned char m) +mesh_t create_mesh(struct surface surface) { unsigned char i; struct obj *p; p = malloc(sizeof(struct obj)); - p->vertex = (*d) / m; + p->vertex = surface.vertex; glGenVertexArrays(1, &p->vao); glBindVertexArray(p->vao); glGenBuffers(1, &p->d_vbo); glBindBuffer(GL_ARRAY_BUFFER, p->d_vbo); - glBufferData( - GL_ARRAY_BUFFER, p->vertex * m * sizeof(float), d + 1, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, p->vertex * surface.dim * sizeof(float), + surface.data, GL_STATIC_DRAW); - if (n) + if (surface.norm) { glGenBuffers(1, &p->n_vbo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, p->n_vbo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, p->vertex * m * sizeof(float), - n + 1, GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, + p->vertex * surface.dim * sizeof(float), surface.norm, + GL_STATIC_DRAW); } - for (i = 0; i < 4; ++i) - { + for (i = 0; i < 8; ++i) glEnableVertexAttribArray(i); - glEnableVertexAttribArray(i + 4); - } return p; } diff --git a/src/surface.c b/src/surface.c index 0a02152..fbdaf1b 100644 --- a/src/surface.c +++ b/src/surface.c @@ -22,13 +22,13 @@ #endif #include -typedef void (*function_t)(float *, int *, int); +typedef void (*function_t)(float *, int *, unsigned char *); struct parm { - function_t f; + unsigned char *grid; unsigned char m, n; - unsigned int grid; + function_t f; } parm; int factorial(int n) @@ -47,11 +47,11 @@ int numero_caras(int n) return (1 << (n - 3)) * factorial(n) / factorial(n - 2); } -void riemman(float *d_surface, int *coords, int grid) +void riemman(float *d_surface, int *coords, unsigned char *grid) { complex double eq; - float u = 2 * ((float)coords[0] / grid) - 1; - float v = 2 * ((float)coords[1] / grid) - 1; + float u = 2 * ((float)coords[0] / grid[0]) - 1; + float v = 2 * ((float)coords[1] / grid[1]) - 1; eq = csqrt(CMPLX(u, v)); @@ -61,42 +61,42 @@ void riemman(float *d_surface, int *coords, int grid) d_surface[3] = cimag(eq); } -void cube(float *d_surface, int *coord, int grid) +void cube(float *d_surface, int *coord, unsigned char *grid) { int i; for (i = 0; i < parm.m; i++) - d_surface[i] = (2 * (float)coord[i] / grid) - 1; + d_surface[i] = (2 * (float)coord[i] / grid[i]) - 1; if (parm.m == 2) d_surface[2] = 0; } -void mobius(float *d_surface, int *coord, int grid) +void mobius(float *d_surface, int *coord, unsigned char *grid) { const float width = 0.5; - float u = (2 * M_PI) * ((float)coord[0] / grid); - float v = (2 * width) * ((float)coord[1] / grid) - width; + float u = (2 * M_PI) * ((float)coord[0] / grid[0]); + float v = (2 * width) * ((float)coord[1] / grid[1]) - width; d_surface[0] = cos(u) + v * cos(u / 2) * cos(u); d_surface[1] = sin(u) + v * cos(u / 2) * sin(u); d_surface[2] = v * sin(u / 2); } -void torus(float *d_surface, int *coord, int grid) +void torus(float *d_surface, int *coord, unsigned char *grid) { - float u = (2 * M_PI) * ((float)coord[0] / grid); - float v = (2 * M_PI) * ((float)coord[1] / grid); + float u = (2 * M_PI) * ((float)coord[0] / grid[0]); + float v = (2 * M_PI) * ((float)coord[1] / grid[1]); d_surface[0] = (1 + 0.5 * cos(v)) * cos(u); d_surface[1] = (1 + 0.5 * cos(v)) * sin(u); d_surface[2] = 0.5 * sin(v); } -void klein(float *d_surface, int *coord, int grid) +void klein(float *d_surface, int *coord, unsigned char *grid) { - float u = (2 * M_PI) * ((float)coord[0] / grid); - float v = (2 * M_PI) * ((float)coord[1] / grid); + float u = (2 * M_PI) * ((float)coord[0] / grid[0]); + float v = (2 * M_PI) * ((float)coord[1] / grid[1]); d_surface[0] = (0.5 * cos(v) + 0.5) * cos(u); d_surface[1] = (0.5 * cos(v) + 0.5) * sin(u); @@ -104,17 +104,17 @@ void klein(float *d_surface, int *coord, int grid) d_surface[3] = sin(v) * sin(u / 2); } -float *generate_data_surface(unsigned char *s) +float *generate_data_surface(unsigned char *dim, unsigned long *vertex) { unsigned int i, j, k, o, p, n; long size, q = 0; float *d_surface; int *cara; - parm.f = klein; + parm.f = mobius; parm.m = 2; - parm.n = 4; - parm.grid = 16; + parm.n = 3; + parm.grid = (char[2]){16, 2}; #ifdef TEST assert(numero_caras(2) == 1); @@ -122,12 +122,16 @@ float *generate_data_surface(unsigned char *s) assert(numero_caras(4) == 24); #endif - *s = parm.n; + *dim = parm.n; + *vertex = 0; + + for (o = 0; o < parm.m; o++) + for (p = 0; p < o; p++) + *vertex += parm.grid[p] * parm.grid[o] * 6; cara = malloc(parm.m * sizeof(int)); - size = parm.grid * parm.grid * 6 * (parm.n) * numero_caras(parm.m); - d_surface = malloc((size + 1) * sizeof(float)); - d_surface[0] = size; + size = (*dim) * (*vertex); + d_surface = malloc(size * sizeof(float)); for (o = 0; o < parm.m; o++) { @@ -141,41 +145,41 @@ float *generate_data_surface(unsigned char *s) if (n == o || n == p) skip++; - cara[n] = (k & (1 << (n - skip))) ? parm.grid : 0; + cara[n] = (k & (1 << (n - skip))) ? parm.grid[n] : 0; } - for (i = 0; i < parm.grid; i++) + for (i = 0; i < parm.grid[p]; i++) { - for (j = 0; j < parm.grid; j++) + for (j = 0; j < parm.grid[o]; j++) { cara[p] = i; cara[o] = j; - parm.f(&d_surface[q + 1], cara, parm.grid); + parm.f(&d_surface[q], cara, parm.grid); q += parm.n; cara[p] = i + 1; cara[o] = j; - parm.f(&d_surface[q + 1], cara, parm.grid); + parm.f(&d_surface[q], cara, parm.grid); q += parm.n; cara[p] = i + 1; cara[o] = j + 1; - parm.f(&d_surface[q + 1], cara, parm.grid); + parm.f(&d_surface[q], cara, parm.grid); q += parm.n; cara[p] = i; cara[o] = j; - parm.f(&d_surface[q + 1], cara, parm.grid); + parm.f(&d_surface[q], cara, parm.grid); q += parm.n; cara[p] = i; cara[o] = j + 1; - parm.f(&d_surface[q + 1], cara, parm.grid); + parm.f(&d_surface[q], cara, parm.grid); q += parm.n; cara[p] = i + 1; cara[o] = j + 1; - parm.f(&d_surface[q + 1], cara, parm.grid); + parm.f(&d_surface[q], cara, parm.grid); q += parm.n; } } @@ -285,23 +289,22 @@ static void __calculate_normal( } } -float *generate_normals_surface(float *d, unsigned char m) +float *generate_normals_surface(float *d, unsigned char m, unsigned long vertex) { float *n; - n = malloc((*d + 1) * sizeof(float)); - *n = *d; - float *norm_vec; + + n = malloc((m * vertex) * sizeof(float)); + norm_vec = malloc(m * sizeof(float)); for (int i = 0; i < *d; i += 3 * m) { - __calculate_normal( - (d + 1) + i, (d + 1) + i + m, (d + 1) + i + 2 * m, norm_vec, m); - glm_vec3_copy(norm_vec, (n + 1) + i); - glm_vec3_copy(norm_vec, (n + 1) + i + m); - glm_vec3_copy(norm_vec, (n + 1) + i + 2 * m); + __calculate_normal(d + i, d + i + m, d + i + 2 * m, norm_vec, m); + glm_vec3_copy(norm_vec, n + i); + glm_vec3_copy(norm_vec, n + i + m); + glm_vec3_copy(norm_vec, n + i + 2 * m); } free(norm_vec); From a2930081375c1e4c7052e3ca762d08097d36c346 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Sun, 1 Dec 2024 13:43:27 -0600 Subject: [PATCH 03/18] Fix size with variable grid --- src/surface.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/surface.c b/src/surface.c index fbdaf1b..7aeaf4e 100644 --- a/src/surface.c +++ b/src/surface.c @@ -39,7 +39,7 @@ int factorial(int n) return n * factorial(n - 1); } -int numero_caras(int n) +int faces(int n) { if (n == 2) return 1; @@ -107,27 +107,36 @@ void klein(float *d_surface, int *coord, unsigned char *grid) float *generate_data_surface(unsigned char *dim, unsigned long *vertex) { unsigned int i, j, k, o, p, n; - long size, q = 0; + unsigned long size, q = 0; float *d_surface; int *cara; - parm.f = mobius; - parm.m = 2; - parm.n = 3; - parm.grid = (char[2]){16, 2}; + parm.f = cube; + parm.m = 5; + parm.n = 5; + parm.grid = (char[7]){16, 2, 8, 4, 3, 5, 1}; #ifdef TEST - assert(numero_caras(2) == 1); - assert(numero_caras(3) == 6); - assert(numero_caras(4) == 24); + assert(faces(2) == 1); + assert(faces(3) == 6); + assert(faces(4) == 24); #endif *dim = parm.n; *vertex = 0; - for (o = 0; o < parm.m; o++) - for (p = 0; p < o; p++) - *vertex += parm.grid[p] * parm.grid[o] * 6; + { + unsigned char test = 0; + for (o = 0; o < parm.m; o++) + { + for (p = 0; p < o; p++) + { + test += 1; + *vertex += parm.grid[p] * parm.grid[o] * 6 * faces(parm.n); + } + } + *vertex /= test; + } cara = malloc(parm.m * sizeof(int)); size = (*dim) * (*vertex); @@ -186,6 +195,7 @@ float *generate_data_surface(unsigned char *dim, unsigned long *vertex) } } } + #ifdef TEST assert(q == size); #endif From 904d686c6ef0eeb3ee01b5437b7221be4124837e Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Sun, 1 Dec 2024 16:12:17 -0600 Subject: [PATCH 04/18] Clean up codebase, rm texture, matrix -> load --- Makefile | 12 +++---- src/context.c | 9 ----- src/data/shaders.h | 80 ++++++++++++++++++----------------------- src/load.c | 34 ++++++++++++++++++ src/main.c | 42 +++++++--------------- src/main.h | 88 ++++++++++++++++++++++++++++------------------ src/matrix.c | 50 -------------------------- src/mesh.c | 20 ++++++++--- src/shader.c | 21 +++++------ src/surface.c | 6 ++-- src/texture.c | 38 -------------------- 11 files changed, 169 insertions(+), 231 deletions(-) create mode 100644 src/load.c delete mode 100644 src/matrix.c delete mode 100644 src/texture.c diff --git a/Makefile b/Makefile index 790cb3b..ffbf151 100644 --- a/Makefile +++ b/Makefile @@ -4,9 +4,8 @@ OBJ = \ ext/glad/glad.o \ src/surface.o \ src/context.o \ - src/texture.o \ src/window.o \ - src/matrix.o \ + src/load.o \ src/shader.o \ src/input.o \ src/mesh.o \ @@ -30,14 +29,13 @@ WAYLAND-LIB = \ wayland help: - @echo "Para compilar el proyecto a tu sistema operativo" - @echo "porfavor usa uno de los siguientes comandos:" + @echo "Usage:" @echo " $(MAKE) windows" @echo " $(MAKE) linux-x11" @echo " $(MAKE) linux-wayland" @echo " $(MAKE) cocoa" @echo " $(MAKE) CC=emcc wasm" - @echo "Para limpiar los archivos compilados se puede usar" + @echo "Clean" @echo " $(MAKE) clean" src/main.o: src/data/axis.h src/data/shaders.h @@ -63,7 +61,7 @@ cocoa: $(OBJ) $(CC) -framework OpenGL -o $(BIN) $(OBJ) ext/glfw.a -lGL -lglfw wasm: $(OBJ) - $(CC) -sUSE_WEBGL2=1 -sUSE_GLFW=3 -o $(BIN).js $(OBJ) + $(CC) -sUSE_WEBGL2=1 -sUSE_GLFW=3 -o $(BIN).html $(OBJ) chmod -x $(BIN).wasm libglfw.so: @@ -77,4 +75,4 @@ clean: .SUFFIXES: .c .o .c.o: - $(CC) $(CFLAGS) -c -o $@ $< + $(CC) -Wno-implicit-function-declaration $(CFLAGS) -c -o $@ $< diff --git a/src/context.c b/src/context.c index be286b5..d842310 100644 --- a/src/context.c +++ b/src/context.c @@ -3,12 +3,8 @@ #ifdef EMSCRIPTEN #include #else -#ifdef GLAD #include #include -#else -#include -#endif #endif void set_clean_color_context(unsigned char r, unsigned char g, unsigned char b) @@ -22,12 +18,7 @@ int init_context(void) #ifdef EMSCRIPTEN return 1; #else -#ifdef GLAD - return gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); -#else - return glewInit(); -#endif #endif } diff --git a/src/data/shaders.h b/src/data/shaders.h index 1c25efc..423098f 100644 --- a/src/data/shaders.h +++ b/src/data/shaders.h @@ -1,4 +1,4 @@ -const char * vs = +const char *vs = #ifdef EMSCRIPTEN "#version 300 es\n" "precision highp float;" @@ -15,94 +15,84 @@ const char * vs = "layout (location = 6) in float aNormal_z;" "layout (location = 7) in float aNormal_w;" - "uniform uint idx;" - - "uniform uint i;" "uniform float angle;" + "uniform float i;" + "uniform vec4 color;" "uniform mat4 fix;" "uniform mat4 rot;" - "uniform mat4 mdl;" - "flat out uint index;" "out vec3 Normal;" "out vec3 FragPos;" + "out vec4 Color;" "mat2 rotate2d( float angle )" "{" - "return mat2( cos(angle), sin(angle), -sin(angle), cos(angle) );" + "return mat2( cos(angle), sin(angle), -sin(angle), cos(angle) );" "}" "void main()" "{" - " index=idx;" + " Color=color;" " vec3 aNormal = vec3(aNormal_x,aNormal_y,aNormal_z);" " vec3 aPos = vec3(aPos_x,aPos_y,aPos_z);" - " aNormal[i] = (vec2(aNormal[i], aNormal_w) * rotate2d(angle))[0];" - " aPos[i] = (vec2(aPos[i], aPos_w) * rotate2d(angle))[0];" + " aNormal[int(i)] = (vec2(aNormal[int(i)], aNormal_w) * " + "rotate2d(angle))[0];" + " aPos[int(i)] = (vec2(aPos[int(i)], aPos_w) * rotate2d(angle))[0];" - " Normal = mat3(transpose(inverse(rot*mdl))) * aNormal;" - " gl_Position = fix * rot * mdl * vec4( aPos, 1.0 );\n" - " FragPos = vec3( rot * mdl * vec4(aPos, 1.0));" + " Normal = mat3(transpose(inverse(rot))) * aNormal;" + " gl_Position = fix * rot * vec4( aPos, 1.0 );\n" + " FragPos = vec3( rot * vec4(aPos, 1.0));" "}"; - -const char * fs_plain = +const char *fs_plain = #ifdef EMSCRIPTEN "#version 300 es\n" - "precision highp float;" - "precision highp sampler2DArray;" #else "#version 330 core\n" #endif - "uniform sampler2DArray palette;" - - "flat in uint index;" "out vec4 FragColor;" "in vec3 Normal;" "in vec3 FragPos;" + "in vec4 Color;" "void main()" "{" - " vec4 color = texture( palette, vec3(0,0,index)).rgba;" - " FragColor = vec4(pow(vec3(color),vec3(1.0/2.2)),color.a);" + " FragColor = vec4(pow(vec3(Color),vec3(1.0/2.2)),Color.a);" "}"; -const char * fs = +const char *fs = #ifdef EMSCRIPTEN - "#version 300 es\n" + "#version 300 es\n" "precision highp float;" - "precision highp sampler2DArray;" #else - "#version 330 core\n" + "#version 330 core\n" #endif - "uniform sampler2DArray palette;" + "in vec3 Normal;" + "in vec3 FragPos;" + "in vec4 Color;" - "flat in uint index;" - "in vec3 Normal;" - "in vec3 FragPos;" + "out vec4 FragColor;" - "out vec4 FragColor;" + "void main()" + "{" - "void main()" - "{" - " vec4 color = texture(palette, vec3(0, 0, index));\n" + " vec3 viewPos = vec3(0, 0, -15);\n" + " vec3 viewDir = normalize(viewPos - FragPos);\n" - " vec3 viewPos = vec3(0, 0, -15);\n" - " vec3 viewDir = normalize(viewPos - FragPos);\n" + " vec3 lightPos = viewPos;\n" + " vec3 lightDir = normalize(lightPos - FragPos);\n" - " vec3 lightPos = viewPos;\n" - " vec3 lightDir = normalize(lightPos - FragPos);\n" + " vec3 halfwayDir = normalize(lightDir + viewDir);\n" - " vec3 halfwayDir = normalize(lightDir + viewDir);\n" + " float specular = pow(abs(dot(normalize(Normal), halfwayDir)), 32.0);\n" + " float diffuse = abs(dot(normalize(Normal), lightDir));\n" - " float specular = pow(abs(dot(normalize(Normal), halfwayDir)), 32.0);\n" - " float diffuse = abs(dot(normalize(Normal), lightDir));\n" - - " vec3 result = pow((0.5 + 0.5*diffuse + 1.5*specular) * color.rgb, vec3(1.0/2.2));\n" - " FragColor = vec4(result, color.a);\n" - "}"; + " vec3 result = pow((0.5 + 0.5*diffuse + 1.5*specular) * Color.rgb, " + "vec3(1.0/2.2));\n" + " FragColor = vec4(result, Color.a);\n" + "}"; diff --git a/src/load.c b/src/load.c new file mode 100644 index 0000000..f676448 --- /dev/null +++ b/src/load.c @@ -0,0 +1,34 @@ +#include "main.h" +#include +#include +#include + +void fix_matrix_load(id_t shader, float ratio) +{ + mat4 m, n; + const int d = 7; + + glm_lookat((vec3){0, 0, -d}, (vec3){0, 0, 0}, (vec3){0, 1, 0}, m); + glm_perspective(CGLM_PI / 6, ratio, d - 3, d + 3, n); + glm_mat4_mul(n, m, m); + + load_mat4_to_shader(shader, "fix", (mat4_t)m); +} + +void rot_matrix_load(id_t shader, quat_t q) +{ + mat4 m; + glm_quat_mat4(q, m); + load_mat4_to_shader(shader, "rot", (mat4_t)m); +} + +void color_load(id_t shader, unsigned char color[4]) +{ + float res[4]; + + res[0] = (float)color[0] / 0xff; + res[1] = (float)color[1] / 0xff; + res[2] = (float)color[2] / 0xff; + res[3] = (float)color[3] / 0xff; + load_float4_to_shader(shader, "color", res); +} diff --git a/src/main.c b/src/main.c index c47dd7f..71bb0f0 100644 --- a/src/main.c +++ b/src/main.c @@ -22,12 +22,7 @@ struct projection projection = {.x = 0, .y = 1, .z = 2, .w = 3}; const char *wname = "manigraph: manifold grapher"; -unsigned char palette[][4] = { - {0xEB, 0xD3, 0xF8, 0xff}, - {0xEB, 0xD4, 0xF8, 0xff}, - {0xEB, 0xD5, 0xF8, 0xff}, - {0x2F, 0x3C, 0x7E, 0xff}, -}; +unsigned char color[4] = {0x2F, 0x3C, 0x7E, 0xff}; void mlog(char *msg) { @@ -53,8 +48,10 @@ static inline q = poll_input(window); - load_rot_matrix(shader, q); - load_rot_matrix(shader_plain, q); + rot_matrix_load(shader, q); + rot_matrix_load(shader_plain, q); + color_load(shader, color); + color_load(shader_plain, color); { static float angle = 0; @@ -71,8 +68,8 @@ static inline if (animate_index) { - load_uint_to_shader(shader, "i", animate_index - 1); - load_uint_to_shader(shader_plain, "i", animate_index - 1); + load_float_to_shader(shader, "i", animate_index - 1); + load_float_to_shader(shader_plain, "i", animate_index - 1); angle += 0.01; load_float_to_shader(shader, "angle", angle); @@ -81,16 +78,12 @@ static inline } clean_context(); - load_mdl_matrix(shader, 0, 3); - draw_mesh(m_surface); - - load_mdl_matrix(shader_plain, 0, 3); - draw_mesh_lines(m_surface); + draw_mesh(shader, m_surface); + draw_mesh_lines(shader_plain, m_surface); } int main(void) { - id_t texture; mlog("[VENTANA] Inicializando...\n"); { @@ -113,12 +106,6 @@ int main(void) set_clean_color_context(0xFF, 0xFF, 0xFF); } - mlog("[TEXTURE] Inicializando...\n"); - { - texture = create_palette_texture(palette, 4); - use_texture(texture); - } - mlog("[SHADER] Inicializando...\n"); { if (!(shader = create_shader())) @@ -128,7 +115,7 @@ int main(void) } load_program_to_shader(shader, vs, VERTEX); load_program_to_shader(shader, fs, FRAGMENT); - load_fix_matrix(shader, (float)WIDTH / HEIGHT); + fix_matrix_load(shader, (float)WIDTH / HEIGHT); } mlog("[SHADER] Inicializando...\n"); @@ -140,7 +127,7 @@ int main(void) } load_program_to_shader(shader_plain, vs, VERTEX); load_program_to_shader(shader_plain, fs_plain, FRAGMENT); - load_fix_matrix(shader_plain, (float)WIDTH / HEIGHT); + fix_matrix_load(shader_plain, (float)WIDTH / HEIGHT); } mlog("[MESH] Inicializando...\n"); @@ -182,13 +169,11 @@ int main(void) destroy_shader(shader_plain); mlog("[SHADER] Destruyendo...\n"); destroy_shader(shader); - mlog("[TEXTURE] Destruyendo...\n"); - destroy_texture(texture); mlog("[WINDOW] Destruyendo...\n"); close_window(window); return 0; -error_mesh_axis: +error_context: mlog("[MESH] Destruyendo...\n"); destroy_mesh(m_surface); error_mesh_surface: @@ -198,9 +183,6 @@ error_shader_plain: mlog("[SHADER] Destruyendo...\n"); destroy_shader(shader); error_shader: - mlog("[TEXTURE] Destruyendo...\n"); - destroy_texture(texture); -error_context: mlog("[WINDOW] Destruyendo...\n"); close_window(window); error_window: diff --git a/src/main.h b/src/main.h index 4798dd2..33ca763 100644 --- a/src/main.h +++ b/src/main.h @@ -4,16 +4,15 @@ error of the shaders. */ -/* #define DEBUG */ -#define GLAD +#define DEBUG -typedef const void * window_t; +typedef const void *window_t; typedef unsigned int id_t; -typedef void * mesh_t; -typedef float * quat_t; -typedef float * mat4_t; +typedef void *mesh_t; +typedef float *quat_t; +typedef float *mat4_t; -/* +/* This struct represent the proyection, where: mesh: data of surface. m: the dimention of the surface. @@ -29,14 +28,29 @@ struct projection unsigned char m, x, y, z, w; }; -/* +/* + this structure has all the information to generate + a mesh, where: + data: the buffer with the vertex coords + norm: the buffer with the norm coords + vertex: the number of vertex + dim: the dimentions of the surface +*/ +struct surface +{ + float *data, *norm; + unsigned long vertex; + unsigned char dim; +}; + +/* Init window: w: default width; h: default height; name: Name of the window. */ -window_t init_window(unsigned int w, unsigned int h, const char * name); +window_t init_window(unsigned int w, unsigned int h, const char *name); void use_window(window_t window); @@ -44,22 +58,22 @@ int is_open_window(window_t window); void close_window(window_t window); -/* +/* Create mesh: d: array of floats with the vertex data. n: array of floats with the normal data. m: Dimention of mesh */ -mesh_t create_mesh( float * d, float * n, unsigned char m ); +mesh_t create_mesh(struct surface); -void set_projection_mesh( struct projection ); +void set_projection_mesh(struct projection); void destroy_mesh(mesh_t p); -void draw_mesh(mesh_t p); +void draw_mesh(id_t, mesh_t p); -void draw_mesh_lines(mesh_t p); +void draw_mesh_lines(id_t, mesh_t p); /* Set background color: @@ -72,7 +86,7 @@ void set_clean_color_context(unsigned char r, unsigned char g, unsigned char b); void clean_context(void); -int init_context( void ); +int init_context(void); void destroy_shader(id_t shader); @@ -82,7 +96,8 @@ void use_shader(id_t shader); enum { - VERTEX, FRAGMENT + VERTEX, + FRAGMENT }; /* @@ -91,7 +106,8 @@ enum type: VERTEX or FRAGMENT */ -unsigned char load_program_to_shader(id_t shader, const char * src, unsigned int type); +unsigned char load_program_to_shader( + id_t shader, const char *src, unsigned int type); /* load float to shader: @@ -99,7 +115,7 @@ unsigned char load_program_to_shader(id_t shader, const char * src, unsigned int f: float to load */ -void load_float_to_shader(id_t shader, char * var, float f); +void load_float_to_shader(id_t shader, char *var, float f); /* load unsigned int to shader: @@ -107,7 +123,15 @@ void load_float_to_shader(id_t shader, char * var, float f); u: unsigned int to load */ -void load_uint_to_shader(id_t shader, char * var, unsigned int u); +void load_uint_to_shader(id_t shader, char *var, unsigned int u); + +/* + load float[4] to shader: + var: name of glsl variable. + f: float[4] to load +*/ + +void load_float4_to_shader(id_t shader, char *var, float f[4]); /* load matrix 4 to shader: @@ -115,32 +139,28 @@ void load_uint_to_shader(id_t shader, char * var, unsigned int u); m: Matrix to load */ -void load_mat4_to_shader(id_t shader, char * var, mat4_t m); +void load_mat4_to_shader(id_t shader, char *var, mat4_t m); /* Generate and load fix matrix, this matrix has the information of the perspective and camera information. - + ratio: default ratio of window. */ -void load_fix_matrix(id_t shader, float ratio); - -/* - Generate and load model matrix, it also sets the color - to draw. - i: From {0,1,2} select one of 3 ortogonal rotations, - One for each axis. - c: Color index of the pallete. -*/ -void load_mdl_matrix(id_t shader, unsigned char i, unsigned char c); +void fix_matrix_load(id_t shader, float ratio); /* Generate and load rotation matrix. q: quaterinon describing the rotation. */ -void load_rot_matrix(id_t shader, quat_t q); +void rot_matrix_load(id_t shader, quat_t q); + +/* + */ + +void color_load(id_t shader, unsigned char color[4]); id_t config_texture(unsigned short type); @@ -153,11 +173,11 @@ void destroy_texture(id_t texture); colors: array of color values (rgba in hex ). n: number of color on colors. */ -id_t create_palette_texture(const unsigned char colors[][4], unsigned char n ); +id_t create_palette_texture(const unsigned char colors[][4], unsigned char n); quat_t poll_input(window_t window); -#ifdef EMSCRIPTEN +#ifdef EMSCRIPTEN #ifdef GLAD #error undefine GLAD on src/main.h please #endif diff --git a/src/matrix.c b/src/matrix.c deleted file mode 100644 index 5108268..0000000 --- a/src/matrix.c +++ /dev/null @@ -1,50 +0,0 @@ -#include "main.h" -#include -#include -#include - -mat4 ortho[] = { - { - {1, 0, 0, 0}, - {0, 1, 0, 0}, - {0, 0, 1, 0}, - {0, 0, 0, 1}, - }, - { - {0, 1, 0, 0}, - {-1, 0, 0, 0}, - {0, 0, 1, 0}, - {0, 0, 0, 1}, - }, - { - {0, 0, 1, 0}, - {0, 1, 0, 0}, - {-1, 0, 0, 0}, - {0, 0, 0, 1}, - }, -}; - -void load_fix_matrix(id_t shader, float ratio) -{ - mat4 m, n; - const int d = 7; - - glm_lookat((vec3){0, 0, -d}, (vec3){0, 0, 0}, (vec3){0, 1, 0}, m); - glm_perspective(CGLM_PI / 6, ratio, d - 3, d + 3, n); - glm_mat4_mul(n, m, m); - - load_mat4_to_shader(shader, "fix", (mat4_t)m); -} - -void load_mdl_matrix(id_t shader, unsigned char i, unsigned char c) -{ - load_uint_to_shader(shader, "idx", c); - load_mat4_to_shader(shader, "mdl", (mat4_t)ortho[i]); -} - -void load_rot_matrix(id_t shader, quat_t q) -{ - mat4 m; - glm_quat_mat4(q, m); - load_mat4_to_shader(shader, "rot", (mat4_t)m); -} diff --git a/src/mesh.c b/src/mesh.c index 8caf45f..1ad14ad 100644 --- a/src/mesh.c +++ b/src/mesh.c @@ -1,9 +1,11 @@ #include "main.h" -#ifdef GLAD -#include + +#ifdef EMSCRIPTEN +#include #else -#include +#include #endif + #include #include @@ -89,19 +91,27 @@ void destroy_mesh(mesh_t p) free(p); } -void draw_mesh(mesh_t p) +void draw_mesh(id_t shader, mesh_t p) { struct obj *obj = p; + glUseProgram(shader); glBindVertexArray(obj->vao); +#ifndef EMSCRIPTEN glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); +#endif glDrawArrays(GL_TRIANGLES, 0, obj->vertex); } -void draw_mesh_lines(mesh_t p) +void draw_mesh_lines(id_t shader, mesh_t p) { struct obj *obj = p; + glUseProgram(shader); glBindVertexArray(obj->vao); +#ifndef EMSCRIPTEN glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glDrawArrays(GL_TRIANGLES, 0, obj->vertex); +#else + glDrawArrays(GL_LINES, 0, obj->vertex); +#endif } diff --git a/src/shader.c b/src/shader.c index f7d8430..212c638 100644 --- a/src/shader.c +++ b/src/shader.c @@ -1,20 +1,20 @@ #include "main.h" -#ifdef GLAD +#ifndef EMSCRIPTEN #include #else -#include +#include #endif #ifdef DEBUG #include #endif -void destroy_shader(unsigned int shader) { return glDeleteProgram(shader); } +void destroy_shader(unsigned int shader) { glDeleteProgram(shader); } unsigned int create_shader(void) { return glCreateProgram(); } -void use_shader(unsigned int program) { return glUseProgram(program); } +void use_shader(unsigned int program) { glUseProgram(program); } unsigned char load_program_to_shader( unsigned int program, const char *src, unsigned int i) @@ -54,14 +54,15 @@ void load_float_to_shader(unsigned int program, char *var, float f) glUniform1f(glGetUniformLocation(program, var), f); } -void load_uint_to_shader(unsigned int program, char *var, unsigned int u) -{ - glUseProgram(program); - glUniform1ui(glGetUniformLocation(program, var), u); -} - void load_mat4_to_shader(unsigned int program, char *var, float *mat) { glUseProgram(program); glUniformMatrix4fv(glGetUniformLocation(program, var), 1, 0, mat); } + +void load_float4_to_shader(unsigned int program, char *var, float float4[4]) +{ + glUseProgram(program); + glUniform4f(glGetUniformLocation(program, var), float4[0], float4[1], + float4[2], float4[3]); +} diff --git a/src/surface.c b/src/surface.c index 7aeaf4e..4cd2cfe 100644 --- a/src/surface.c +++ b/src/surface.c @@ -112,9 +112,9 @@ float *generate_data_surface(unsigned char *dim, unsigned long *vertex) int *cara; parm.f = cube; - parm.m = 5; - parm.n = 5; - parm.grid = (char[7]){16, 2, 8, 4, 3, 5, 1}; + parm.m = 4; + parm.n = 4; + parm.grid = (char[]){16, 8, 4, 2, 1}; #ifdef TEST assert(faces(2) == 1); diff --git a/src/texture.c b/src/texture.c deleted file mode 100644 index 6261ce0..0000000 --- a/src/texture.c +++ /dev/null @@ -1,38 +0,0 @@ -#include "main.h" -#ifdef GLAD -#include -#else -#include -#endif - -#define TYPE GL_TEXTURE_2D_ARRAY - -static id_t __config_texture(unsigned short type) -{ - id_t texture; - - glGenTextures(1, &texture); - glBindTexture(TYPE, texture); - - { - glTexParameteri(TYPE, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(TYPE, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - } - - return texture; -} - -void use_texture(id_t texture) { return glBindTexture(TYPE, texture); } - -void destroy_texture(unsigned int texture) -{ - return glDeleteTextures(1, &texture); -} - -id_t create_palette_texture(const unsigned char colors[][4], unsigned char n) -{ - id_t texture = __config_texture(TYPE); - glTexImage3D( - TYPE, 0, GL_RGBA, 1, 1, n, 0, GL_RGBA, GL_UNSIGNED_BYTE, colors); - return texture; -} From 1d254eade534b9fcbc52eb01c252a7c707fb2e4a Mon Sep 17 00:00:00 2001 From: islas Date: Sun, 1 Dec 2024 22:50:15 +0000 Subject: [PATCH 05/18] Actualizar src/surface.c --- src/surface.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/surface.c b/src/surface.c index 4cd2cfe..0d45bb4 100644 --- a/src/surface.c +++ b/src/surface.c @@ -31,6 +31,47 @@ struct parm function_t f; } parm; +// Función para escribir el archivo .klein +void write_klein_file(const char *filename, unsigned char dim, unsigned long vertex, float *vertices, float *normals) +{ + FILE *file = fopen(filename, "wb"); + if (!file) + { + perror("Error al abrir el archivo"); + exit(EXIT_FAILURE); + } + + // Escribir encabezado + fwrite("KLEIN", 1, 5, file); // Los primeros 5 bytes son "KLEIN" + fputc(0, file); // Byte vacío + fwrite(&dim, 1, 1, file); // Dimensión de la superficie + fwrite(&vertex, sizeof(unsigned long), 1, file); // Número de vértices (8 bytes) + + // Escribir dimensiones de la cuadrícula + fwrite(parm.grid, sizeof(unsigned char), dim, file); + // Debug info + for (int i = 0; i < dim; i++) { + printf("Grid[%d]: %u\n", i, parm.grid[i]); + } + + // Escribir vértices (en float) + fwrite(vertices, sizeof(float), vertex * dim, file); + // Debug info + for (unsigned long i = 0; i < vertex * dim; i++) { + printf("Vertices[%lu]: %f\n", i, vertices[i]); + } + + // Escribir normales (en float) + fwrite(normals, sizeof(float), vertex * dim, file); + // Debug info + for (unsigned long i = 0; i < vertex * dim; i++) { + printf("Normals[%lu]: %f\n", i, normals[i]); + } + + fclose(file); + printf("Archivo %s escrito correctamente.\n", filename); +} + int factorial(int n) { if (n == 1) @@ -320,3 +361,40 @@ float *generate_normals_surface(float *d, unsigned char m, unsigned long vertex) free(norm_vec); return n; } +int main() +{ + unsigned char dim; + unsigned long vertex; + float *vertices, *normals; + + // Generar datos de la superficie + vertices = generate_data_surface(&dim, &vertex); + + // Verificar datos generados + if (vertices == NULL) { + printf("Error: vertices no generados.\n"); + return 1; + } + + printf("Dim: %u, Vertex: %lu\n", dim, vertex); + + // Generar normales + normals = generate_normals_surface(vertices, dim, vertex); + + // Verificar normales generadas + if (normals == NULL) { + printf("Error: normales no generadas.\n"); + free(vertices); + return 1; + } + + // Escribir el archivo + printf("Escribiendo archivo .klein\n"); + write_klein_file("kingtin.klein", dim, vertex, vertices, normals); + + free(vertices); + free(normals); + + return 0; +} + From 987ce544290dfc70b80333dca8c9b8b301f53525 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Sun, 1 Dec 2024 21:08:35 -0600 Subject: [PATCH 06/18] Read Klein file --- src/main.c | 12 +++++------- src/main.h | 9 +++------ src/surface.c | 38 ++++++++------------------------------ 3 files changed, 16 insertions(+), 43 deletions(-) diff --git a/src/main.c b/src/main.c index 71bb0f0..15dddc2 100644 --- a/src/main.c +++ b/src/main.c @@ -15,9 +15,6 @@ #define M_PI 3.14159 #endif -float *generate_data_surface(unsigned char *, unsigned long *); -float *generate_normals_surface(float *, unsigned char, unsigned long); - struct projection projection = {.x = 0, .y = 1, .z = 2, .w = 3}; const char *wname = "manigraph: manifold grapher"; @@ -134,9 +131,11 @@ int main(void) { struct surface surface; - surface.data = generate_data_surface(&surface.dim, &surface.vertex); - surface.norm = - generate_normals_surface(surface.data, surface.dim, surface.vertex); + if( create_surface_klein("test.klein", &surface) ) + { + mlog("[MESH] Error al leer el archivo...\n"); + goto error_mesh_surface; + } if (!(m_surface = create_mesh(surface))) { @@ -146,7 +145,6 @@ int main(void) projection.m = surface.dim; projection.mesh = m_surface; - set_projection_mesh(projection); free(surface.norm); diff --git a/src/main.h b/src/main.h index 33ca763..6ae5ebd 100644 --- a/src/main.h +++ b/src/main.h @@ -36,6 +36,7 @@ struct projection vertex: the number of vertex dim: the dimentions of the surface */ + struct surface { float *data, *norm; @@ -50,6 +51,8 @@ struct surface name: Name of the window. */ +int create_surface_klein( unsigned char *, struct surface * ); + window_t init_window(unsigned int w, unsigned int h, const char *name); void use_window(window_t window); @@ -176,9 +179,3 @@ void destroy_texture(id_t texture); id_t create_palette_texture(const unsigned char colors[][4], unsigned char n); quat_t poll_input(window_t window); - -#ifdef EMSCRIPTEN -#ifdef GLAD -#error undefine GLAD on src/main.h please -#endif -#endif diff --git a/src/surface.c b/src/surface.c index 0d45bb4..1375645 100644 --- a/src/surface.c +++ b/src/surface.c @@ -32,44 +32,22 @@ struct parm } parm; // Función para escribir el archivo .klein -void write_klein_file(const char *filename, unsigned char dim, unsigned long vertex, float *vertices, float *normals) +int write_klein_file(const char *filename, unsigned char dim, unsigned long vertex, float *vertices, float *normals) { FILE *file = fopen(filename, "wb"); if (!file) - { - perror("Error al abrir el archivo"); - exit(EXIT_FAILURE); - } + return 1; // Escribir encabezado fwrite("KLEIN", 1, 5, file); // Los primeros 5 bytes son "KLEIN" - fputc(0, file); // Byte vacío + fwrite("\0", 1, 1, file); // Byte vacío fwrite(&dim, 1, 1, file); // Dimensión de la superficie - fwrite(&vertex, sizeof(unsigned long), 1, file); // Número de vértices (8 bytes) - - // Escribir dimensiones de la cuadrícula - fwrite(parm.grid, sizeof(unsigned char), dim, file); - // Debug info - for (int i = 0; i < dim; i++) { - printf("Grid[%d]: %u\n", i, parm.grid[i]); - } - - // Escribir vértices (en float) - fwrite(vertices, sizeof(float), vertex * dim, file); - // Debug info - for (unsigned long i = 0; i < vertex * dim; i++) { - printf("Vertices[%lu]: %f\n", i, vertices[i]); - } - - // Escribir normales (en float) - fwrite(normals, sizeof(float), vertex * dim, file); - // Debug info - for (unsigned long i = 0; i < vertex * dim; i++) { - printf("Normals[%lu]: %f\n", i, normals[i]); - } + fwrite(&vertex, 8, 1, file); // Número de vértices (8 bytes) + fwrite(vertices, 16, vertex * dim, file); + fwrite(normals, 16, vertex * dim, file); fclose(file); - printf("Archivo %s escrito correctamente.\n", filename); + return 0; } int factorial(int n) @@ -390,7 +368,7 @@ int main() // Escribir el archivo printf("Escribiendo archivo .klein\n"); - write_klein_file("kingtin.klein", dim, vertex, vertices, normals); + write_klein_file("test.klein", dim, vertex, vertices, normals); free(vertices); free(normals); From 733ababde001953950ab0e0bf81ac7f6a05fa617 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Sun, 1 Dec 2024 21:20:07 -0600 Subject: [PATCH 07/18] Read klein files, xd --- src/klein.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/klein.c diff --git a/src/klein.c b/src/klein.c new file mode 100644 index 0000000..e13c9c4 --- /dev/null +++ b/src/klein.c @@ -0,0 +1,30 @@ +#include "main.h" + +#include + +int create_surface_klein( unsigned char * path, struct surface * surface ) +{ + unsigned long size; + char buffer[5]; + FILE * file = fopen(path, "rb" ); + if( !file ) + return 1; + + fread(buffer, 1, 5, file ); + + if( strncmp(buffer, "KLEIN", 5 ) ) + return 1; + + fread(buffer, 1, 1, file ); + fread(&surface->dim, 1, 1, file); + fread(&surface->vertex, 8, 1, file); + + size = surface->dim * surface->vertex; + + surface->data=malloc(16*size ); + fread(surface->data, 16,size, file); + + surface->norm=malloc(16*size ); + fread(surface->norm, 16, size, file); + return 0; +} From fd598255f4b5f32b16c0a4d85471e18bbb1443c9 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Sun, 1 Dec 2024 21:33:27 -0600 Subject: [PATCH 08/18] Allow droping klein files --- src/input.c | 37 +++++++++++++++++++++++++++---------- src/main.c | 39 +++++++-------------------------------- src/window.c | 2 ++ 3 files changed, 36 insertions(+), 42 deletions(-) diff --git a/src/input.c b/src/input.c index 319923a..8a0f1a8 100644 --- a/src/input.c +++ b/src/input.c @@ -9,7 +9,7 @@ unsigned char selected_axis = 0; int window_width; int window_height; -unsigned char animate_index=0; +unsigned char animate_index = 0; versor q = GLM_QUAT_IDENTITY_INIT; @@ -56,23 +56,22 @@ void __key_callback_input( projection.w = projection.x; projection.x = tmp; - animate_index=1; + animate_index = 1; break; case GLFW_KEY_O: tmp = projection.w; projection.w = projection.y; projection.y = tmp; - animate_index=2; + animate_index = 2; break; case GLFW_KEY_P: tmp = projection.w; projection.w = projection.z; projection.z = tmp; - animate_index=3; + animate_index = 3; break; } - return; } @@ -114,9 +113,9 @@ void __scroll_callback_input(GLFWwindow *window, double xoffset, double yoffset) versor p = GLM_QUAT_IDENTITY_INIT; versor r = GLM_QUAT_IDENTITY_INIT; - //glm_quatv(p, yoffset * ANGLE, axis[selected_axis]); - glm_quatv(p, yoffset * ANGLE * 2, (vec3){-1,0,0}); - glm_quatv(r, xoffset * ANGLE * 2, (vec3){0,1,0}); + // glm_quatv(p, yoffset * ANGLE, axis[selected_axis]); + glm_quatv(p, yoffset * ANGLE * 2, (vec3){-1, 0, 0}); + glm_quatv(r, xoffset * ANGLE * 2, (vec3){0, 1, 0}); glm_quat_mul(p, q, q); glm_quat_mul(r, q, q); @@ -129,6 +128,23 @@ void __scroll_callback_input(GLFWwindow *window, double xoffset, double yoffset) glm_quat_rotatev(r, axis[2], axis[2]); } +void __drop_callback_input(GLFWwindow *window, int count, char **path) +{ + struct surface surface; + + if (create_surface_klein(*path, &surface)) + return; + + if (!(projection.mesh = create_mesh(surface))) + return; + + projection.m = surface.dim; + set_projection_mesh(projection); + + free(surface.norm); + free(surface.data); +} + quat_t poll_input(window_t window) { versor p = GLM_QUAT_IDENTITY_INIT; @@ -172,10 +188,11 @@ end: glm_quat_normalize(q); // LOG INFO - if(0) + if (0) { printf("QUAT: %2.5f %2.5f %2.5f %2.5f\n", q[0], q[1], q[2], q[3]); - printf("PROY: %3d %3d %3d (%3d)\n", projection.x, projection.y, projection.z, projection.w ); + printf("PROY: %3d %3d %3d (%3d)\n", projection.x, projection.y, + projection.z, projection.w); printf("\n"); } return q; diff --git a/src/main.c b/src/main.c index 15dddc2..224a7f0 100644 --- a/src/main.c +++ b/src/main.c @@ -15,7 +15,7 @@ #define M_PI 3.14159 #endif -struct projection projection = {.x = 0, .y = 1, .z = 2, .w = 3}; +struct projection projection = {.x = 0, .y = 1, .z = 2, .w = 3, .mesh = NULL}; const char *wname = "manigraph: manifold grapher"; @@ -29,7 +29,6 @@ void mlog(char *msg) } window_t window; -mesh_t m_surface; id_t shader, shader_plain; extern volatile unsigned char animate_index; @@ -75,8 +74,11 @@ static inline } clean_context(); - draw_mesh(shader, m_surface); - draw_mesh_lines(shader_plain, m_surface); + if (!projection.mesh) + return; + + draw_mesh(shader, projection.mesh); + draw_mesh_lines(shader_plain, projection.mesh); } int main(void) @@ -127,30 +129,6 @@ int main(void) fix_matrix_load(shader_plain, (float)WIDTH / HEIGHT); } - mlog("[MESH] Inicializando...\n"); - { - struct surface surface; - - if( create_surface_klein("test.klein", &surface) ) - { - mlog("[MESH] Error al leer el archivo...\n"); - goto error_mesh_surface; - } - - if (!(m_surface = create_mesh(surface))) - { - mlog("[MESH] Error al inicializar...\n"); - goto error_mesh_surface; - } - - projection.m = surface.dim; - projection.mesh = m_surface; - set_projection_mesh(projection); - - free(surface.norm); - free(surface.data); - } - mlog("[MAIN LOOP] Inicializando...\n"); #ifdef EMSCRIPTEN emscripten_set_main_loop(&main_loop, 60, 1); @@ -162,7 +140,7 @@ int main(void) mlog("[MAIN LOOP] Terminando...\n"); mlog("[MESH] Destruyendo...\n"); - destroy_mesh(m_surface); + destroy_mesh(projection.mesh); mlog("[SHADER] Destruyendo...\n"); destroy_shader(shader_plain); mlog("[SHADER] Destruyendo...\n"); @@ -172,9 +150,6 @@ int main(void) return 0; error_context: - mlog("[MESH] Destruyendo...\n"); - destroy_mesh(m_surface); -error_mesh_surface: mlog("[SHADER] Destruyendo...\n"); destroy_shader(shader_plain); error_shader_plain: diff --git a/src/window.c b/src/window.c index 2354aa6..521e673 100644 --- a/src/window.c +++ b/src/window.c @@ -7,6 +7,7 @@ void __window_callback_input(GLFWwindow *, int, int); void __mouse_callback_input(GLFWwindow *, int, int, int); void __scroll_callback_input(GLFWwindow *, double, double); void __key_callback_input(GLFWwindow *, int, int, int, int); +void __drop_callback_input(GLFWwindow *, int, const char **); window_t init_window(unsigned int w, unsigned int h, const char *name); @@ -64,6 +65,7 @@ window_t init_window(unsigned int width, unsigned int height, const char *title) glfwSetMouseButtonCallback((GLFWwindow *)window, __mouse_callback_input); glfwSetScrollCallback((GLFWwindow *)window, __scroll_callback_input); glfwSetKeyCallback((GLFWwindow *)window, __key_callback_input); + glfwSetDropCallback((GLFWwindow *)window, __drop_callback_input); __window_callback_input((GLFWwindow *)window, width, height); From b1897b500b219299b1a095842f796fb910471d9e Mon Sep 17 00:00:00 2001 From: islas Date: Tue, 3 Dec 2024 01:18:23 +0000 Subject: [PATCH 09/18] Actualizar src/surface.c --- src/surface.c | 77 +++++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/src/surface.c b/src/surface.c index 1375645..d5fe8a7 100644 --- a/src/surface.c +++ b/src/surface.c @@ -36,15 +36,19 @@ int write_klein_file(const char *filename, unsigned char dim, unsigned long vert { FILE *file = fopen(filename, "wb"); if (!file) - return 1; + { + perror("Error al abrir el archivo"); + exit(EXIT_FAILURE); + } // Escribir encabezado fwrite("KLEIN", 1, 5, file); // Los primeros 5 bytes son "KLEIN" - fwrite("\0", 1, 1, file); // Byte vacío + fputc(0, file); // Byte vacío fwrite(&dim, 1, 1, file); // Dimensión de la superficie - fwrite(&vertex, 8, 1, file); // Número de vértices (8 bytes) - fwrite(vertices, 16, vertex * dim, file); - fwrite(normals, 16, vertex * dim, file); + fwrite(&vertex, sizeof(unsigned long), 1, file); // Número de vértices (8 bytes) + fwrite(vertices, sizeof(float), vertex * dim, file); + fwrite(normals, sizeof(float), vertex * dim, file); + printf("Archivo %s escrito correctamente.\n", filename); fclose(file); return 0; @@ -123,17 +127,17 @@ void klein(float *d_surface, int *coord, unsigned char *grid) d_surface[3] = sin(v) * sin(u / 2); } -float *generate_data_surface(unsigned char *dim, unsigned long *vertex) +float *generate_data_surface(unsigned char *dim, unsigned long *,vertex, struct parm *parm) { unsigned int i, j, k, o, p, n; unsigned long size, q = 0; float *d_surface; int *cara; - parm.f = cube; - parm.m = 4; - parm.n = 4; - parm.grid = (char[]){16, 8, 4, 2, 1}; + parm->f = cube; + parm->m = 4; + parm->n = 4; + parm->grid = (unsigned char[]){16, 8, 4, 2, 1}; #ifdef TEST assert(faces(2) == 1); @@ -141,74 +145,74 @@ float *generate_data_surface(unsigned char *dim, unsigned long *vertex) assert(faces(4) == 24); #endif - *dim = parm.n; + *dim = parm->n; *vertex = 0; { unsigned char test = 0; - for (o = 0; o < parm.m; o++) + for (o = 0; o < parm->m; o++) { for (p = 0; p < o; p++) { test += 1; - *vertex += parm.grid[p] * parm.grid[o] * 6 * faces(parm.n); + *vertex += parm->grid[p] * parm->grid[o] * 6 * faces(parm->n); } } *vertex /= test; } - cara = malloc(parm.m * sizeof(int)); + cara = malloc(parm->m * sizeof(int)); size = (*dim) * (*vertex); d_surface = malloc(size * sizeof(float)); - for (o = 0; o < parm.m; o++) + for (o = 0; o < parm->m; o++) { for (p = 0; p < o; p++) { - for (k = 0; k < (1 << (parm.m - 2)); k++) + for (k = 0; k < (1 << (parm->m - 2)); k++) { unsigned char skip = 0; - for (n = 0; n < parm.m; n++) + for (n = 0; n < parm->m; n++) { if (n == o || n == p) skip++; - cara[n] = (k & (1 << (n - skip))) ? parm.grid[n] : 0; + cara[n] = (k & (1 << (n - skip))) ? parm->grid[n] : 0; } - for (i = 0; i < parm.grid[p]; i++) + for (i = 0; i < parm->grid[p]; i++) { - for (j = 0; j < parm.grid[o]; j++) + for (j = 0; j < parm->grid[o]; j++) { cara[p] = i; cara[o] = j; - parm.f(&d_surface[q], cara, parm.grid); - q += parm.n; + parm->f(&d_surface[q], cara, parm->grid); + q += parm->n; cara[p] = i + 1; cara[o] = j; - parm.f(&d_surface[q], cara, parm.grid); - q += parm.n; + parm->f(&d_surface[q], cara, parm->grid); + q += parm->n; cara[p] = i + 1; cara[o] = j + 1; - parm.f(&d_surface[q], cara, parm.grid); - q += parm.n; + parm->f(&d_surface[q], cara, parm->grid); + q += parm->n; cara[p] = i; cara[o] = j; - parm.f(&d_surface[q], cara, parm.grid); - q += parm.n; + parm->f(&d_surface[q], cara, parm->grid); + q += parm->n; cara[p] = i; cara[o] = j + 1; - parm.f(&d_surface[q], cara, parm.grid); - q += parm.n; + parm->f(&d_surface[q], cara, parm->grid); + q += parm->n; cara[p] = i + 1; cara[o] = j + 1; - parm.f(&d_surface[q], cara, parm.grid); - q += parm.n; + parm->f(&d_surface[q], cara, parm->grid); + q += parm->n; } } } @@ -341,12 +345,19 @@ float *generate_normals_surface(float *d, unsigned char m, unsigned long vertex) } int main() { + + struct parm parm_instance; + parm_instance.grid = (unsigned char[]){16, 8, 4, 2, 1}; + parm_instance.m = 4; + parm_instance.n = 4; + parm_instance.f = cube; + unsigned char dim; unsigned char dim; unsigned long vertex; float *vertices, *normals; // Generar datos de la superficie - vertices = generate_data_surface(&dim, &vertex); + vertices = generate_data_surface(&dim, &vertex, &parm_instance); // Verificar datos generados if (vertices == NULL) { From 33940b7bb689d74433c45d30f3dcef0ae22f21e1 Mon Sep 17 00:00:00 2001 From: islas Date: Tue, 3 Dec 2024 01:28:33 +0000 Subject: [PATCH 10/18] Actualizar src/surface.c --- src/surface.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/surface.c b/src/surface.c index d5fe8a7..a434037 100644 --- a/src/surface.c +++ b/src/surface.c @@ -29,7 +29,7 @@ struct parm unsigned char *grid; unsigned char m, n; function_t f; -} parm; +}; // Función para escribir el archivo .klein int write_klein_file(const char *filename, unsigned char dim, unsigned long vertex, float *vertices, float *normals) @@ -127,7 +127,7 @@ void klein(float *d_surface, int *coord, unsigned char *grid) d_surface[3] = sin(v) * sin(u / 2); } -float *generate_data_surface(unsigned char *dim, unsigned long *,vertex, struct parm *parm) +float *generate_data_surface(unsigned char *dim, unsigned long *vertex, struct parm *parm) { unsigned int i, j, k, o, p, n; unsigned long size, q = 0; @@ -352,7 +352,6 @@ int main() parm_instance.n = 4; parm_instance.f = cube; unsigned char dim; - unsigned char dim; unsigned long vertex; float *vertices, *normals; From f8eb20c0cfe185df3fac50de2fc572ef22bec04c Mon Sep 17 00:00:00 2001 From: islas Date: Tue, 3 Dec 2024 01:31:47 +0000 Subject: [PATCH 11/18] Actualizar src/surface.c --- src/surface.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/surface.c b/src/surface.c index a434037..4bf66a7 100644 --- a/src/surface.c +++ b/src/surface.c @@ -88,10 +88,10 @@ void cube(float *d_surface, int *coord, unsigned char *grid) { int i; - for (i = 0; i < parm.m; i++) + for (i = 0; i < 4; i++) d_surface[i] = (2 * (float)coord[i] / grid[i]) - 1; - if (parm.m == 2) + if (4 == 2) d_surface[2] = 0; } From f514fc7ffe2716675b294a28ad53e991266b8bf7 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Tue, 3 Dec 2024 22:18:48 -0600 Subject: [PATCH 12/18] setup examples and clean codebase --- example/basic.c | 92 ++++++++++ example/n-cube.c | 50 ++++++ example/riemman.c | 50 ++++++ include/klein/klein.h | 48 ++++++ include/klein/norm.h | 118 +++++++++++++ include/klein/parm.h | 134 +++++++++++++++ src/data/axis.h | 40 ----- src/klein.c | 34 ++-- src/surface.c | 388 ------------------------------------------ 9 files changed, 516 insertions(+), 438 deletions(-) create mode 100644 example/basic.c create mode 100644 example/n-cube.c create mode 100644 example/riemman.c create mode 100644 include/klein/klein.h create mode 100644 include/klein/norm.h create mode 100644 include/klein/parm.h delete mode 100644 src/data/axis.h delete mode 100644 src/surface.c diff --git a/example/basic.c b/example/basic.c new file mode 100644 index 0000000..0488340 --- /dev/null +++ b/example/basic.c @@ -0,0 +1,92 @@ +#include +#include + +#define KLEIN_IMPLEMENT +#include +#include +#include + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +void cube(float *d_surface, int *coord, unsigned char *grid) +{ + int i; + + for (i = 0; i < 4; i++) + d_surface[i] = (2 * (float)coord[i] / grid[i]) - 1; + + if (4 == 2) + d_surface[2] = 0; +} + +void mobius(float *d_surface, int *coord, unsigned char *grid) +{ + const float width = 0.5; + float u = (2 * M_PI) * ((float)coord[0] / grid[0]); + float v = (2 * width) * ((float)coord[1] / grid[1]) - width; + + d_surface[0] = cos(u) + v * cos(u / 2) * cos(u); + d_surface[1] = sin(u) + v * cos(u / 2) * sin(u); + d_surface[2] = v * sin(u / 2); +} + +void torus(float *d_surface, int *coord, unsigned char *grid) +{ + float u = (2 * M_PI) * ((float)coord[0] / grid[0]); + float v = (2 * M_PI) * ((float)coord[1] / grid[1]); + + d_surface[0] = (1 + 0.5 * cos(v)) * cos(u); + d_surface[1] = (1 + 0.5 * cos(v)) * sin(u); + d_surface[2] = 0.5 * sin(v); +} + +void klein(float *d_surface, int *coord, unsigned char *grid) +{ + float u = (2 * M_PI) * ((float)coord[0] / grid[0]); + float v = (2 * M_PI) * ((float)coord[1] / grid[1]); + + d_surface[0] = (0.5 * cos(v) + 0.5) * cos(u); + d_surface[1] = (0.5 * cos(v) + 0.5) * sin(u); + d_surface[2] = sin(v) * cos(u / 2); + d_surface[3] = sin(v) * sin(u / 2); +} + +int main(void) +{ + unsigned char i = 0; + const char *file_name[] = {"mobius.klein", "torus.klein", "klein.klein"}; + struct parm parametrization[] = {{ + .grid = (unsigned char[]){16, 4}, + .m = 2, + .n = 3, + .f = mobius, + }, + { + .grid = (unsigned char[]){16, 8}, + .m = 2, + .n = 3, + .f = torus, + }, + { + .grid = (unsigned char[]){16, 16}, + .m = 2, + .n = 4, + .f = klein, + }}; + + for (i = 0; i < 3; ++i) + { + struct klein klein; + printf("writing %s\n", file_name[i]); + + klein_parametrize(&klein, parametrization[i]); + klein_normalize(&klein); + klein_export_file(klein, file_name[i]); + + free(klein.vertex); + free(klein.normals); + } + return 0; +} diff --git a/example/n-cube.c b/example/n-cube.c new file mode 100644 index 0000000..74ec02a --- /dev/null +++ b/example/n-cube.c @@ -0,0 +1,50 @@ +#include +#include + +#define KLEIN_IMPLEMENT +#include +#include +#include + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +unsigned char dim = 4; + +void cube(float *d_surface, int *coord, unsigned char *grid) +{ + int i; + + for (i = 0; i < dim; i++) + d_surface[i] = (2 * (float)coord[i] / grid[i]) - 1; + + if (dim == 2) + d_surface[2] = 0; +} + +int main(void) +{ + unsigned char i; + char file_name[0xff]; + struct klein klein; + struct parm parametrization = { + .m = dim, + .n = dim, + .f = cube, + }; + + parametrization.grid = malloc(dim); + + for (i = 0; i < dim; ++i) + parametrization.grid[i] = 1 << i; + + snprintf(file_name, 0xff, "%03d-cube.klein", dim); + printf("writing %s\n", file_name); + + klein_parametrize(&klein, parametrization); + klein_normalize(&klein); + klein_export_file(klein, file_name); + + return 0; +} diff --git a/example/riemman.c b/example/riemman.c new file mode 100644 index 0000000..8af7920 --- /dev/null +++ b/example/riemman.c @@ -0,0 +1,50 @@ +#include +#include +#include + +#define KLEIN_IMPLEMENT +#include +#include +#include + +#ifndef CMPLX +#define CMPLX(a, b) (a + I * b) +#endif + +complex float f(complex float z) { return csqrt(z); } + +void riemman(float *d_surface, int *coords, unsigned char *grid) +{ + complex float eq; + float u = 2 * ((float)coords[0] / grid[0]) - 1; + float v = 2 * ((float)coords[1] / grid[1]) - 1; + + eq = f(CMPLX(u, v)); + + d_surface[0] = u; + d_surface[1] = v; + d_surface[2] = creal(eq); + d_surface[3] = cimag(eq); +} + +int main(void) +{ + const char *file_name = "riemman.klein"; + struct klein klein; + struct parm parametrization = { + .grid = (unsigned char[]){16, 4}, + .m = 2, + .n = 4, + .f = riemman, + }; + + printf("writing %s\n", file_name); + + klein_parametrize(&klein, parametrization); + klein_normalize(&klein); + klein_export_file(klein, file_name); + + free(klein.vertex); + free(klein.normals); + return 0; +} diff --git a/include/klein/klein.h b/include/klein/klein.h new file mode 100644 index 0000000..6265adf --- /dev/null +++ b/include/klein/klein.h @@ -0,0 +1,48 @@ +#ifdef KLEIN_H +#error file included twice +#endif +#define KLEIN_H + +#ifdef KLEIN_IMPLEMENT +#include +#include +#endif + +struct klein +{ + unsigned long vertex_size; + float *vertex, *normals; + unsigned char dim; +}; + +/* + The klein format must have: + 5 bytes with klein. + 1 byte empty for expantions + 1 byte with the dimention of the surface + +*/ + +static inline +int klein_export_file(struct klein klein, const char * filename); + + +#ifdef KLEIN_IMPLEMENT +static inline +int klein_export_file(struct klein klein, const char * filename) +{ + FILE *file = fopen(filename, "wb"); + if (!file) + return 1; + + fwrite("KLEIN", 1, 5, file); + fwrite("\0", 1, 1, file); + fwrite(&klein.dim, 1, 1, file); + fwrite(&klein.vertex_size, 8, 1, file); + fwrite(klein.vertex, 16, klein.vertex_size * klein.dim, file); + fwrite(klein.normals, 16, klein.vertex_size * klein.dim, file); + + fclose(file); + return 0; +} +#endif diff --git a/include/klein/norm.h b/include/klein/norm.h new file mode 100644 index 0000000..bca38f3 --- /dev/null +++ b/include/klein/norm.h @@ -0,0 +1,118 @@ +static inline +void __calculate_normal( float *p1, float *p2, float *p3, float *normal, unsigned char n) +{ unsigned char i; + float alpha; + float *v1, *v2, *v3; + float *u1, *u2, *u3; + + v1 = malloc(n * sizeof(float)); + v2 = malloc(n * sizeof(float)); + v3 = malloc(n * sizeof(float)); + u1 = malloc(n * sizeof(float)); + u2 = malloc(n * sizeof(float)); + u3 = malloc(n * sizeof(float)); + + /* + Calculate a normal vector of a plain using Gram-Schmidt process + */ + { + for (i = 0; i < n; ++i) + { + v1[i] = p2[i] - p1[i]; + v2[i] = p3[i] - p1[i]; + v3[i] = p1[i]; + } + + for (i = 0; i < n; ++i) + { + u1[i] = v1[i]; + } + + { + float proj[n]; + float dot_v2_u1 = 0.0f, dot_u1_u1 = 0.0f; + for (i = 0; i < n; ++i) + { + dot_v2_u1 += v2[i] * u1[i]; + dot_u1_u1 += u1[i] * u1[i]; + } + alpha = dot_v2_u1 / dot_u1_u1; + + for (i = 0; i < n; ++i) + { + proj[i] = u1[i] * alpha; + u2[i] = v2[i] - proj[i]; + } + } + + { + float proj1[n], proj2[n]; + float dot_v3_u1 = 0.0f, dot_u1_u1 = 0.0f; + float dot_v3_u2 = 0.0f, dot_u2_u2 = 0.0f; + + for (i = 0; i < n; ++i) + { + dot_v3_u1 += v3[i] * u1[i]; + dot_u1_u1 += u1[i] * u1[i]; + } + for (i = 0; i < n; ++i) + { + proj1[i] = u1[i] * (dot_v3_u1 / dot_u1_u1); + } + + for (i = 0; i < n; ++i) + { + dot_v3_u2 += v3[i] * u2[i]; + dot_u2_u2 += u2[i] * u2[i]; + } + for (i = 0; i < n; ++i) + { + proj2[i] = u2[i] * (dot_v3_u2 / dot_u2_u2); + u3[i] = v3[i] - proj1[i] - proj2[i]; + } + } + + float magnitude = 0.0f; + for (i = 0; i < n; ++i) + { + magnitude += u3[i] * u3[i]; + } + magnitude = sqrtf(magnitude); + + for (i = 0; i < n; ++i) + { + normal[i] = u3[i] / magnitude; + } + + free(v1); + free(v2); + free(v3); + free(u1); + free(u2); + free(u3); + return; + } +} + +void klein_normalize(struct klein * klein) +{ + unsigned long i; + unsigned char j; + float *norm_vec; + + klein->normals = malloc((klein->dim * klein->vertex_size) * sizeof(float)); + norm_vec = malloc(klein->dim * sizeof(float)); + + for (i = 0; i < klein->vertex_size; i += 3 * klein->dim) + { + __calculate_normal(klein->vertex + i, klein->vertex + i + klein->dim, klein->vertex + i + 2 * klein->dim, norm_vec, klein->dim); + for (j = 0; j < klein->dim; ++j ) + { + (klein->normals + i)[j]=norm_vec[j]; + (klein->normals + i + klein->dim)[j]=norm_vec[j]; + (klein->normals + i + 2*klein->dim)[j]=norm_vec[j]; + } + } + + free(norm_vec); +} diff --git a/include/klein/parm.h b/include/klein/parm.h new file mode 100644 index 0000000..de888b4 --- /dev/null +++ b/include/klein/parm.h @@ -0,0 +1,134 @@ +#ifndef KLEIN_H +#warning Please include klein/klein.h before klein/parm.h +#endif + +#ifdef KLEIN_PARM_H +#error file included twice +#endif +#define KLEIN_PARM_H + + +typedef void (*function_t)(float *, int *, unsigned char *); + +struct parm +{ + unsigned char *grid; + unsigned char m, n; + function_t f; +}; + + +void klein_parametrize( struct klein * klein, struct parm parm ); + +#ifdef KLEIN_IMPLEMENT +#ifdef TEST +#include +#endif + +static inline int __factorial(int n) +{ + if (n == 1) + return 1; + + return n * __factorial(n - 1); +} + +static inline int __face(int n) +{ + if (n == 2) + return 1; + + return (1 << (n - 3)) * __factorial(n) / __factorial(n - 2); +} + +void klein_parametrize( struct klein * klein, struct parm parm) +{ + unsigned int i, j, k, o, p, n; + unsigned long size, q = 0; + int *face; + +#ifdef TEST + assert(__face(2) == 1); + assert(__face(3) == 6); + assert(__face(4) == 24); +#endif + + klein->dim = parm.n; + klein->vertex_size = 0; + { + unsigned char test = 0; + for (o = 0; o < parm.m; o++) + { + for (p = 0; p < o; p++) + { + test += 1; + klein->vertex_size += parm.grid[p] * parm.grid[o] * 6 * __face(parm.m); + } + } + klein->vertex_size /= test; + } + + size = (klein->dim) * (klein->vertex_size); + klein->vertex = malloc(size * sizeof(float)); + + face = malloc(parm.m * sizeof(int)); + + for (o = 0; o < parm.m; o++) + { + for (p = 0; p < o; p++) + { + for (k = 0; k < (1 << (parm.m - 2)); k++) + { + unsigned char skip = 0; + for (n = 0; n < parm.m; n++) + { + if (n == o || n == p) + skip++; + + face[n] = (k & (1 << (n - skip))) ? parm.grid[n] : 0; + } + + for (i = 0; i < parm.grid[p]; i++) + { + for (j = 0; j < parm.grid[o]; j++) + { + face[p] = i; + face[o] = j; + parm.f(&klein->vertex[q], face, parm.grid); + q += parm.n; + + face[p] = i + 1; + face[o] = j; + parm.f(&klein->vertex[q], face, parm.grid); + q += parm.n; + + face[p] = i + 1; + face[o] = j + 1; + parm.f(&klein->vertex[q], face, parm.grid); + q += parm.n; + + face[p] = i; + face[o] = j; + parm.f(&klein->vertex[q], face, parm.grid); + q += parm.n; + + face[p] = i; + face[o] = j + 1; + parm.f(&klein->vertex[q], face, parm.grid); + q += parm.n; + + face[p] = i + 1; + face[o] = j + 1; + parm.f(&klein->vertex[q], face, parm.grid); + q += parm.n; + } + } + } + } + } + +#ifdef TEST + assert(q == size); +#endif +} +#endif diff --git a/src/data/axis.h b/src/data/axis.h deleted file mode 100644 index 3ccddc8..0000000 --- a/src/data/axis.h +++ /dev/null @@ -1,40 +0,0 @@ -#undef A -#undef B -#undef C -#undef D -#undef E -#undef F -#undef G -#undef H - -#define A -2.0,-0.05,-0.05, -#define B -2.0,-0.05, 0.05, -#define C -2.0, 0.05,-0.05, -#define D -2.0, 0.05, 0.05, -#define E 2.0,-0.05,-0.05, -#define F 2.0,-0.05, 0.05, -#define G 2.0, 0.05,-0.05, -#define H 2.0, 0.05, 0.05, - -float d_axis[] = -{ - 3*3*2*6, - - A C E - G E C - - E G F - H F G - - F H B - D B H - - B D A - C A D - - C D G - H G D - - E B A - B E F -}; diff --git a/src/klein.c b/src/klein.c index e13c9c4..8add64b 100644 --- a/src/klein.c +++ b/src/klein.c @@ -2,29 +2,43 @@ #include -int create_surface_klein( unsigned char * path, struct surface * surface ) +/* + KLEIN Format: + 5 bytes with KLEIN + 1 byte empty for scaling + 1 byte with the dimention of the surface + 8 bytes interprated as a long with the number of vertex + n bytes with the vertex data of the surface + n bytes with the normal data of the surface + + where n is the size of the vertex and normal data that could be + calculated as the dimention of the surface time the number of vertes + time the size of a 16 bytes float. +*/ + +int create_surface_klein(unsigned char *path, struct surface *surface) { unsigned long size; char buffer[5]; - FILE * file = fopen(path, "rb" ); - if( !file ) + FILE *file = fopen(path, "rb"); + if (!file) return 1; - fread(buffer, 1, 5, file ); + fread(buffer, 1, 5, file); - if( strncmp(buffer, "KLEIN", 5 ) ) + if (strncmp(buffer, "KLEIN", 5)) return 1; - fread(buffer, 1, 1, file ); + fread(buffer, 1, 1, file); fread(&surface->dim, 1, 1, file); fread(&surface->vertex, 8, 1, file); - + size = surface->dim * surface->vertex; - surface->data=malloc(16*size ); - fread(surface->data, 16,size, file); + surface->data = malloc(16 * size); + fread(surface->data, 16, size, file); - surface->norm=malloc(16*size ); + surface->norm = malloc(16 * size); fread(surface->norm, 16, size, file); return 0; } diff --git a/src/surface.c b/src/surface.c deleted file mode 100644 index 4bf66a7..0000000 --- a/src/surface.c +++ /dev/null @@ -1,388 +0,0 @@ -#include -#include -#include -#include - -#define TEST - -#define CGLM_ALL_UNALIGNED -#include -#include - -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - -#ifndef CMPLX -#define CMPLX(a, b) (a + I * b) -#endif - -#ifdef TEST -#include -#endif -#include - -typedef void (*function_t)(float *, int *, unsigned char *); - -struct parm -{ - unsigned char *grid; - unsigned char m, n; - function_t f; -}; - -// Función para escribir el archivo .klein -int write_klein_file(const char *filename, unsigned char dim, unsigned long vertex, float *vertices, float *normals) -{ - FILE *file = fopen(filename, "wb"); - if (!file) - { - perror("Error al abrir el archivo"); - exit(EXIT_FAILURE); - } - - // Escribir encabezado - fwrite("KLEIN", 1, 5, file); // Los primeros 5 bytes son "KLEIN" - fputc(0, file); // Byte vacío - fwrite(&dim, 1, 1, file); // Dimensión de la superficie - fwrite(&vertex, sizeof(unsigned long), 1, file); // Número de vértices (8 bytes) - fwrite(vertices, sizeof(float), vertex * dim, file); - fwrite(normals, sizeof(float), vertex * dim, file); - printf("Archivo %s escrito correctamente.\n", filename); - - fclose(file); - return 0; -} - -int factorial(int n) -{ - if (n == 1) - return 1; - - return n * factorial(n - 1); -} - -int faces(int n) -{ - if (n == 2) - return 1; - - return (1 << (n - 3)) * factorial(n) / factorial(n - 2); -} - -void riemman(float *d_surface, int *coords, unsigned char *grid) -{ - complex double eq; - float u = 2 * ((float)coords[0] / grid[0]) - 1; - float v = 2 * ((float)coords[1] / grid[1]) - 1; - - eq = csqrt(CMPLX(u, v)); - - d_surface[0] = u; - d_surface[1] = v; - d_surface[2] = creal(eq); - d_surface[3] = cimag(eq); -} - -void cube(float *d_surface, int *coord, unsigned char *grid) -{ - int i; - - for (i = 0; i < 4; i++) - d_surface[i] = (2 * (float)coord[i] / grid[i]) - 1; - - if (4 == 2) - d_surface[2] = 0; -} - -void mobius(float *d_surface, int *coord, unsigned char *grid) -{ - const float width = 0.5; - float u = (2 * M_PI) * ((float)coord[0] / grid[0]); - float v = (2 * width) * ((float)coord[1] / grid[1]) - width; - - d_surface[0] = cos(u) + v * cos(u / 2) * cos(u); - d_surface[1] = sin(u) + v * cos(u / 2) * sin(u); - d_surface[2] = v * sin(u / 2); -} - -void torus(float *d_surface, int *coord, unsigned char *grid) -{ - float u = (2 * M_PI) * ((float)coord[0] / grid[0]); - float v = (2 * M_PI) * ((float)coord[1] / grid[1]); - - d_surface[0] = (1 + 0.5 * cos(v)) * cos(u); - d_surface[1] = (1 + 0.5 * cos(v)) * sin(u); - d_surface[2] = 0.5 * sin(v); -} - -void klein(float *d_surface, int *coord, unsigned char *grid) -{ - float u = (2 * M_PI) * ((float)coord[0] / grid[0]); - float v = (2 * M_PI) * ((float)coord[1] / grid[1]); - - d_surface[0] = (0.5 * cos(v) + 0.5) * cos(u); - d_surface[1] = (0.5 * cos(v) + 0.5) * sin(u); - d_surface[2] = sin(v) * cos(u / 2); - d_surface[3] = sin(v) * sin(u / 2); -} - -float *generate_data_surface(unsigned char *dim, unsigned long *vertex, struct parm *parm) -{ - unsigned int i, j, k, o, p, n; - unsigned long size, q = 0; - float *d_surface; - int *cara; - - parm->f = cube; - parm->m = 4; - parm->n = 4; - parm->grid = (unsigned char[]){16, 8, 4, 2, 1}; - -#ifdef TEST - assert(faces(2) == 1); - assert(faces(3) == 6); - assert(faces(4) == 24); -#endif - - *dim = parm->n; - *vertex = 0; - - { - unsigned char test = 0; - for (o = 0; o < parm->m; o++) - { - for (p = 0; p < o; p++) - { - test += 1; - *vertex += parm->grid[p] * parm->grid[o] * 6 * faces(parm->n); - } - } - *vertex /= test; - } - - cara = malloc(parm->m * sizeof(int)); - size = (*dim) * (*vertex); - d_surface = malloc(size * sizeof(float)); - - for (o = 0; o < parm->m; o++) - { - for (p = 0; p < o; p++) - { - for (k = 0; k < (1 << (parm->m - 2)); k++) - { - unsigned char skip = 0; - for (n = 0; n < parm->m; n++) - { - if (n == o || n == p) - skip++; - - cara[n] = (k & (1 << (n - skip))) ? parm->grid[n] : 0; - } - - for (i = 0; i < parm->grid[p]; i++) - { - for (j = 0; j < parm->grid[o]; j++) - { - cara[p] = i; - cara[o] = j; - parm->f(&d_surface[q], cara, parm->grid); - q += parm->n; - - cara[p] = i + 1; - cara[o] = j; - parm->f(&d_surface[q], cara, parm->grid); - q += parm->n; - - cara[p] = i + 1; - cara[o] = j + 1; - parm->f(&d_surface[q], cara, parm->grid); - q += parm->n; - - cara[p] = i; - cara[o] = j; - parm->f(&d_surface[q], cara, parm->grid); - q += parm->n; - - cara[p] = i; - cara[o] = j + 1; - parm->f(&d_surface[q], cara, parm->grid); - q += parm->n; - - cara[p] = i + 1; - cara[o] = j + 1; - parm->f(&d_surface[q], cara, parm->grid); - q += parm->n; - } - } - } - } - } - -#ifdef TEST - assert(q == size); -#endif - return d_surface; -} - -static void __calculate_normal( - float *p1, float *p2, float *p3, float *normal, unsigned char n) -{ - unsigned char i; - float alpha; - float *v1, *v2, *v3; - float *u1, *u2, *u3; - - v1 = malloc(n * sizeof(float)); - v2 = malloc(n * sizeof(float)); - v3 = malloc(n * sizeof(float)); - u1 = malloc(n * sizeof(float)); - u2 = malloc(n * sizeof(float)); - u3 = malloc(n * sizeof(float)); - - /* - Calculate a normal vector of a plain using Gram-Schmidt process - */ - { - for (i = 0; i < n; ++i) - { - v1[i] = p2[i] - p1[i]; - v2[i] = p3[i] - p1[i]; - v3[i] = p1[i]; - } - - for (i = 0; i < n; ++i) - { - u1[i] = v1[i]; - } - - { - float proj[n]; - float dot_v2_u1 = 0.0f, dot_u1_u1 = 0.0f; - for (i = 0; i < n; ++i) - { - dot_v2_u1 += v2[i] * u1[i]; - dot_u1_u1 += u1[i] * u1[i]; - } - alpha = dot_v2_u1 / dot_u1_u1; - - for (i = 0; i < n; ++i) - { - proj[i] = u1[i] * alpha; - u2[i] = v2[i] - proj[i]; - } - } - - { - float proj1[n], proj2[n]; - float dot_v3_u1 = 0.0f, dot_u1_u1 = 0.0f; - float dot_v3_u2 = 0.0f, dot_u2_u2 = 0.0f; - - for (i = 0; i < n; ++i) - { - dot_v3_u1 += v3[i] * u1[i]; - dot_u1_u1 += u1[i] * u1[i]; - } - for (i = 0; i < n; ++i) - { - proj1[i] = u1[i] * (dot_v3_u1 / dot_u1_u1); - } - - for (i = 0; i < n; ++i) - { - dot_v3_u2 += v3[i] * u2[i]; - dot_u2_u2 += u2[i] * u2[i]; - } - for (i = 0; i < n; ++i) - { - proj2[i] = u2[i] * (dot_v3_u2 / dot_u2_u2); - u3[i] = v3[i] - proj1[i] - proj2[i]; - } - } - - float magnitude = 0.0f; - for (i = 0; i < n; ++i) - { - magnitude += u3[i] * u3[i]; - } - magnitude = sqrtf(magnitude); - - for (i = 0; i < n; ++i) - { - normal[i] = u3[i] / magnitude; - } - - free(v1); - free(v2); - free(v3); - free(u1); - free(u2); - free(u3); - return; - } -} - -float *generate_normals_surface(float *d, unsigned char m, unsigned long vertex) -{ - float *n; - float *norm_vec; - - n = malloc((m * vertex) * sizeof(float)); - - norm_vec = malloc(m * sizeof(float)); - - for (int i = 0; i < *d; i += 3 * m) - { - - __calculate_normal(d + i, d + i + m, d + i + 2 * m, norm_vec, m); - glm_vec3_copy(norm_vec, n + i); - glm_vec3_copy(norm_vec, n + i + m); - glm_vec3_copy(norm_vec, n + i + 2 * m); - } - - free(norm_vec); - return n; -} -int main() -{ - - struct parm parm_instance; - parm_instance.grid = (unsigned char[]){16, 8, 4, 2, 1}; - parm_instance.m = 4; - parm_instance.n = 4; - parm_instance.f = cube; - unsigned char dim; - unsigned long vertex; - float *vertices, *normals; - - // Generar datos de la superficie - vertices = generate_data_surface(&dim, &vertex, &parm_instance); - - // Verificar datos generados - if (vertices == NULL) { - printf("Error: vertices no generados.\n"); - return 1; - } - - printf("Dim: %u, Vertex: %lu\n", dim, vertex); - - // Generar normales - normals = generate_normals_surface(vertices, dim, vertex); - - // Verificar normales generadas - if (normals == NULL) { - printf("Error: normales no generadas.\n"); - free(vertices); - return 1; - } - - // Escribir el archivo - printf("Escribiendo archivo .klein\n"); - write_klein_file("test.klein", dim, vertex, vertices, normals); - - free(vertices); - free(normals); - - return 0; -} - From 65017c859c5ac18d7743dece403340e0357a82dc Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Tue, 3 Dec 2024 22:21:22 -0600 Subject: [PATCH 13/18] Fix compilation --- Makefile | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index ffbf151..5966624 100644 --- a/Makefile +++ b/Makefile @@ -2,19 +2,23 @@ BIN = manigraph OBJ = \ ext/glad/glad.o \ - src/surface.o \ src/context.o \ src/window.o \ - src/load.o \ src/shader.o \ + src/klein.o \ src/input.o \ + src/load.o \ src/mesh.o \ src/main.o +EXAMPLES = \ + example/basic.o + CFLAGS = \ -I./ext/cglm/include \ -I./ext/glfw/include \ -I./ext/glad \ + -I./include \ -Wall -Wno-unused-function -std=c99 -D_GNU_SOURCE \ WAYLAND-LIB = \ @@ -38,7 +42,7 @@ help: @echo "Clean" @echo " $(MAKE) clean" -src/main.o: src/data/axis.h src/data/shaders.h +src/main.o: src/data/shaders.h windows: $(OBJ) cd ext; $(MAKE) -f glfw.mk windows; cd - @@ -72,6 +76,8 @@ clean: cd ext; $(MAKE) -f glfw.mk clean; cd - +examples: $(EXAMPLES) + .SUFFIXES: .c .o .c.o: From b928dcd82d3a4aecd753705a3b8c3c40e9c44bd4 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Tue, 3 Dec 2024 22:42:53 -0600 Subject: [PATCH 14/18] Update Readme --- README.md | 96 +++++++++++-------------------------------------------- 1 file changed, 18 insertions(+), 78 deletions(-) diff --git a/README.md b/README.md index 805c89e..7869d68 100644 --- a/README.md +++ b/README.md @@ -1,92 +1,32 @@ -# Manigraph: Graficadora de variedades -Diaz Camacho Pedro Emilio +# Manigraph +Manigraph is a cutting-edge tool for multidimentional surface visualization, +capable of handling over a hundred dimensions. It allows users to interactively +explore each dimension in an intuitive and dynamic way. +Import your .klein files, which represents multidimentional surface. -# Resumen -manigraph es un graficador interactiva de variedades que lee archivos binarios, con la información de -una variedad multidimensional y grafica esta variedad en una proyección tridimensional. - -# Dependencias -Para poder compilar el proyecto hace falta lo siguiente. -## Programas -- `cc`: Cualquier compilador de C. -- `git`: Gestor de dependencias. (Opcional) -- `make`: Herramienta para compilar automáticamente. (Opcional) - -## Librerías -- `openGL`: A cross-platform API for rendering 2D and 3D graphics. (no incluida) -- `glew`: The OpenGL Extension Wrangler Library (no incluida) -- `glfw`: A multi-platform library for OpenGL, OpenGL ES, Vulkan, window and input. -- `cglm`: Highly Optimized 2D / 3D Graphics Math (glm) for C. -### Linux -Para compilar `glfw` en linux hacen falta las siguientes librerías. (no incluidas) -- `x11`: libXcursor-devel libXi-devel libXinerama-devel libXrandr-devel -- `wayland`: libwayland-dev libxkbcommon-dev wayland-protocols - -# Descargas -- [git](https://git-scm.com/downloads/win) -- [MinGW](https://github.com/niXman/mingw-builds-binaries/releases) -- [glew](https://glew.sourceforge.net/) - -# Clonar el código fuente +# Source code ``` -git clone https://gitea.adles.top/software/manigraph.git +git clone https://gitea.axiolutions.top/software/manigraph.git cd manigraph git submodule update --init --recursive ``` -# Compilación -Para compilar manualmente el proyecto se puede usar `make` o ejecutar los comandos manualmente -en el código fuente de `manigraph`. +# Dependecies +You may need system specific dependecies to build *Manigraph*. +[https://www.glfw.org/docs/3.3/compile.html#compile_deps](Reference) -## Make -Los siguientes comandos sirven para compilar `manigraph` deacuerdo al sistema operativo. +## Linux +- `x11`: libXcursor-devel libXi-devel libXinerama-devel libXrandr-devel +- `wayland`: libwayland-dev libxkbcommon-dev wayland-protocols + +# Building +A C compiler is needed for building *Manigraph*. and any of those commands ``` make windows +./compile.bat make linux-x11 make linux-wayland make cocoa -``` - -## Manualmente -se pueden compilar el proyecto manualmente sin necesidad del programa `make`. - -### GLFW -Si tienes problemas compilando `GLFW` puedes checar su página web oficial -explicando el tema: [Compiling GLFW](https://www.glfw.org/docs/3.3/compile.html). - -#### Windows -``` -cc -fPIC -shared -D_GLFW_WIN32 -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o glfw.dll -lgdi32 -``` - -#### Linux -``` -cc -fPIC -shared -D_GLFW_X11 -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o libglfw.so -cc -fPIC -shared -D_GLFW_WAYLAND -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o libglfw.so -``` - -#### Mac -``` -cc -fPIC -shared -D_GLFW_COCOA -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o libglfw.so -``` - -### Manigraph -#### Windows -``` -cc -I ext/cglm/include/ -I ext/glfw/include/ src/*.c -o manigraph -L. -lglfw -lopengl32 -lglew32 -``` -#### Linux -``` -cc -I ext/cglm/include/ -I ext/glfw/include/ src/*.c -o manigraph -L. -lglfw -lGL -lGLEW -``` -#### Mac -``` -cc -framework GL -I ext/cglm/include/ -I ext/glfw/include/ src/*.c -o manigraph -L. -lglfw -lGLEW -``` - -# Ejecutar -En linux se puede ejecutar el programa sin instalar la libreria de glfw usando: -``` -LD_LIBRARY_PATH=. ./manigraph +make CC=emcc wasm ``` From 7344c6bfcb1723c435ca0df24f0d0ebdd2c23c37 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Tue, 3 Dec 2024 22:44:39 -0600 Subject: [PATCH 15/18] Fix --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7869d68..bddc67b 100644 --- a/README.md +++ b/README.md @@ -4,22 +4,24 @@ capable of handling over a hundred dimensions. It allows users to interactively explore each dimension in an intuitive and dynamic way. Import your .klein files, which represents multidimentional surface. -# Source code +# Building + +## Source code ``` git clone https://gitea.axiolutions.top/software/manigraph.git cd manigraph git submodule update --init --recursive ``` -# Dependecies +## Dependecies You may need system specific dependecies to build *Manigraph*. -[https://www.glfw.org/docs/3.3/compile.html#compile_deps](Reference) +[Reference](https://www.glfw.org/docs/3.3/compile.html#compile_deps) -## Linux +### Linux - `x11`: libXcursor-devel libXi-devel libXinerama-devel libXrandr-devel - `wayland`: libwayland-dev libxkbcommon-dev wayland-protocols -# Building +## Commands A C compiler is needed for building *Manigraph*. and any of those commands ``` From b2f6707efc2ce5e639671551060da62f878b799a Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Tue, 3 Dec 2024 22:51:50 -0600 Subject: [PATCH 16/18] Update norm.h to be safe --- include/klein/norm.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/klein/norm.h b/include/klein/norm.h index bca38f3..72b1729 100644 --- a/include/klein/norm.h +++ b/include/klein/norm.h @@ -1,3 +1,16 @@ +#ifdef KLEIN_NORM_H +#error file included twice +#endif +#define KLEIN_NORM_H + + +#ifndef KLEIN_H +#warning Please include klein/klein.h +#endif + +void klein_normalize(struct klein * klein); + +#ifdef KLEIN_IMPLEMENT static inline void __calculate_normal( float *p1, float *p2, float *p3, float *normal, unsigned char n) { unsigned char i; @@ -116,3 +129,4 @@ void klein_normalize(struct klein * klein) free(norm_vec); } +#endif From d97a35b89840ab59c0a194b6c4413e34a2ac0db0 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Wed, 4 Dec 2024 10:32:38 -0600 Subject: [PATCH 17/18] Documentation --- README.md | 24 ++++++++++ doc/es/dev/estructura-archivos.md | 80 ------------------------------- doc/es/dev/tipos.md | 43 ----------------- doc/es/tech/diseno.md | 59 ----------------------- doc/es/tech/problemas.md | 22 --------- doc/es/user.md | 19 -------- doc/file_layout.svg | 10 ++++ 7 files changed, 34 insertions(+), 223 deletions(-) delete mode 100644 doc/es/dev/estructura-archivos.md delete mode 100644 doc/es/dev/tipos.md delete mode 100644 doc/es/tech/diseno.md delete mode 100644 doc/es/tech/problemas.md delete mode 100644 doc/es/user.md create mode 100644 doc/file_layout.svg diff --git a/README.md b/README.md index bddc67b..c6cc299 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ explore each dimension in an intuitive and dynamic way. Import your .klein files, which represents multidimentional surface. # Building +Manigraph is written in C99, and uses OpenGL 2.0 for the rendering. So you just +need a C compiler to build this project. ## Source code ``` @@ -32,3 +34,25 @@ make linux-wayland make cocoa make CC=emcc wasm ``` + +# Design +Manigraph is a program designed with sustainability in mind. It is built to be +portable, resource-efficient, and easy to maintain and scale. The program +follows a Data-Oriented Programming (DOP) paradigm, where each file is +responsible for a specific data type and manages its own dependencies. Each +function is named after the file it resides in, ensuring a clear organization +and structure. + +The independence of data types allows for efficient scaling and maintenance. +The main file is a special case in this design: it is responsible for combining +all the data types in a comprehensive manner, actng as the workflow of the program. + +# Contributing +Before commiting use `clang-format`, for coding style consistency. Your +contribution must be whitin the design principles and the concept of Manigraph. + +The following diagram illustrates the relationships between files, showing how +they are connected through dependencies. It also highlights the scenarios where +files are not independent of one another, + +![File layout](doc/file_layout.svg) diff --git a/doc/es/dev/estructura-archivos.md b/doc/es/dev/estructura-archivos.md deleted file mode 100644 index 5ff3837..0000000 --- a/doc/es/dev/estructura-archivos.md +++ /dev/null @@ -1,80 +0,0 @@ -# Estructura de archivos -Díaz Camacho Pedro Emilio - -# Headers -## main.h -Este archivo incluye los prototipos internos, que va a usar el programa -funge como api interna para los desarrolladores y debe estar bien documentada. - -# codigo fuente - -## main.c -Este archivo debe configurar el programa antes de iniciarse, avisar de los problemas -que eviten que el programa se ejecute correctamente, iniciar y terminar los objetos que use el -programa, así como ejecutar la función principal -### Depende de -- - -## context.c -En este archivo se escoge un color para limpiar la pantalla y tiene una función para limpiar -tanto lo dibujado en pantalla, como distintos buffers gráficos. -### Depende de -- - -## input.c -Incluye la función `poll_input`, que se ejecuta en cada ciclo del programa y devuelve un -cuaternion de tipo `float *` que representa la rotación total del cubo, después de interactuar -con el programa. -### Depende de -- -- - -## matrix.c -Incluye las funciónes para configurar y cargar en la GPU las 3 matrices principales. -- `fix_matrix` esta matriz no cambia y se usa para configurar la perspectiva y la vista que -va a usar el programa en todo momento. -- `mdl_matrix` esta matriz depende de cada mesh y describe la rotación única de cada objeto, -se usa para rotar los ejes ortogonamente con un indice. -- `rot_matrix` esta matriz describe la rotación total de todos los objetos y se configura con -un cuaternio -### Depende de -- -- -- - -## mesh.c -Este archivo se usa para crear, destruir y dibujar objetos de tipo `mesh`, un `mesh` es una -colección de triángulos que describen un objeto multidimensional. -### Depende de -- -- - -## shader.c -Debe crear, destruir y usar los shaders, estos shaders son programas para la gpu escritos en glsl para -además aquí se carga memoria en gpu con las funciónes `gload_` -### Depende de -- - -## texture.c -Debe cargar, usar y destruir las texturas, que va a usar la gpu, también crea una textura -para una paleta de colores definida, esto para manipular fácilmente los colores que usará el programa. -### Depende de -- - -## window.c -Crea, usa, limpie y cierra una ventana, una ventana tiene un contexto de OpenGL asociado para poder -dibujar objetos en ella, tiene una función especial para avisar si la ventana está abierta para -dibujar. -### Depende de -- - -# Datos -## data/cube.h -Aquí esta la información del mesh de un cubo, este se va a borrar cuando podamos -importar meshes para graficarlos. - -## data/axis.h -Aquí está el mesh de un solo eje, este se va a rotar para hacer los otros 2 ejes. - -## data/shaders.h -Aquí estan los shaders en glsl para dibujar en pantalla diff --git a/doc/es/dev/tipos.md b/doc/es/dev/tipos.md deleted file mode 100644 index 5f7e184..0000000 --- a/doc/es/dev/tipos.md +++ /dev/null @@ -1,43 +0,0 @@ -# Tipos de datos del proyecto -Díaz Camacho Pedro Emilio - -# Introducción -En este archivo vamos a detallar los tipos de datos únicos del programa, estos -tipos de datos son estructuras que representan objetos computacionales y son -necesarios para integrar las distintas partes del programa. - -## window_t -En `window.c` tenemos una estructura que es la ventana y esta se representa por `window_t`, -este tipo de dato es un pointer que es manejado por `glfw`. - -## id_t -`id_t` es un tipo de dato que representa objetos de `OpenGL`, estos datos -normalmente vienen enumerados, y en `manigraph` se usan para representar `shaders`, -`programs` y `texture`. - -## mesh_t -Este es un tipo de dato usado para interactuar con `mesh.c`, este representa la triangulación de un -objeto gráfico y funciona ocultando las variables que `OpenGL` necesita en un `void *`. - -## mat4_t -`mat4_t` se usará para representar las matrices, se usará este tipo de dato exclusivamente -en `matrix.c` y funcionará con el tipo de dato `mat4` de `cglm`. - -## quat_t -Este tipo de dato sirve para representar un quaternio, este quaternio representa la rotación -del objeto gráficado en la proyección tridimensional, y se usará para crear una matriz que -pueda entender `glsl`. - -## narray -`narray` es un tipo de dato que funciona como un `array` enumerado, esto significa que el primer -elemento del `array` dice cuantos elementos tiene este, sin contar el primer elemento, y los demas elementos -funcionan como un `array` normal. - -esta estructura nos ayuda a trabajar con arrays de tamaños arbitrario de forma óptima y sin tener que marcar el -último elemento de forma especial. - -### narray_u8_t -es un `narray` para `char` y `unsigned char`. -### narray_float_t -es un `narray` para `float`. - diff --git a/doc/es/tech/diseno.md b/doc/es/tech/diseno.md deleted file mode 100644 index 3b23f49..0000000 --- a/doc/es/tech/diseno.md +++ /dev/null @@ -1,59 +0,0 @@ -# Manual de Diseño -Diaz Camacho Pedro Emilio - -# Objetivos -## Prioridad alta -- [ ] El programa debe actualizarse cuando interactuan con el. -- [ ] El usuario quiere: - - [X] Ver 3 ejes en todo momento. - - [ ] Ver que ejes esta viendo. - - [ ] Ver cuantos ejes hay. -- [X] El usuario quiere: - - [X] Rotar estos ejes, - - [X] Ver donde quedan los ejes despues de la rotados. -- [ ] El usuario experimentado quiere ejecutar el programa y pasar la variedad: - - [ ] Como argumento del programa. - - [ ] Desde la entrada estandar del programa. - -## Prioridad media - -- [ ] El usuario quiere: - - [ ] Cambiar los ejes que se ven por pares. - - [ ] Cambiar los ejes donde esta la informacion de los ejes. -- [ ] El usuario quiere interactuar con el programa: - - [ ] Usando solo el mouse. - - [ ] Usando solo el teclado. - -## Prioridad baja -- [ ] El usuario quiere guardar las transformaciones como un archivo GIF. - -# Camino - -0. Graficadora de un cubo. - 1. Se hace un programa que muestre un cubo. - 1. Se muestran los 3 ejes del cubo - 1. Se rota el cubo en sus 3 ejes usando el teclado. - 1. Se rota el cubo en sus 3 ejes usando el mouse. - 1. Se muestran la etiqueta de los ejes. - 1. Se hace el formato de objetos multidimensionales. -0. Programa ejemplo - 1. Se hace un programa que divida un cuadrado en triangulos. - 1. Se mapea cada vertice del triangulo en un punto de la cinta de - mobius usando una parametrizacion - 1. Se mapea cada vertice del triangulo en un punto de la botella de - klein usando una parametrizacion - 1. Se divide cada lado de un cubo de n dimensiones, en triangulos. - -0. Graficadora de variedades - 1. Se generaliza el programa para usar este formato. - 1. Se muestran la informacion de los ejes mostrados y cuantos hay. - 1. Se pueden cambiar los ejes por pares - 1. Usando el mouse. - 1. Usando el teclado. -0. Detalles. - 1. Se anima el cambio de eje. - 1. Se hace el generador de GIF. - 1. Se puede cambiar el shader - 1. Documentacion - - diff --git a/doc/es/tech/problemas.md b/doc/es/tech/problemas.md deleted file mode 100644 index e449956..0000000 --- a/doc/es/tech/problemas.md +++ /dev/null @@ -1,22 +0,0 @@ -# Problemas esperados al desarrollar -Diaz Camacho Pedro Emilio - -## OpenGL esperan objetos tridimensionales, no de n dimensiones. - -Usando glVertexAttribPointer podemos pasar las coordenadas que -queramos de los puntos de las variedades, y con ello usar objetos 3D en -el shader. - -## Rotar ejes por pares en una animacion continua. -- Con glVertexAttribPointer podemos pasar las coordenadas del eje -seleccionado, al shader. -- Con la funcion mix del shader y bloqueando el input, podemos hacer -una animacion pasando una variable con la cpu. -- Cuando la animacion termine intercambiar los indices del layout, -usando glVertexAttribPointer otra vez. - -## Volumen de la variedad. -Dado que la funcion parametrizadora es continua, mandamos la frontera -de un n-cubo a la frontera de una variedad, por lo que solo basta -graficar la frontera de la variedad. - diff --git a/doc/es/user.md b/doc/es/user.md deleted file mode 100644 index a62cd2f..0000000 --- a/doc/es/user.md +++ /dev/null @@ -1,19 +0,0 @@ -# Manual de usario para Manigraph -Manigraph es un graficador interactivo de variedades que lee archivos -binarios de una variedad multidimensional y los proyecta a 3 dimensiones. - -# Glosario -Llamaremos a los 3 ejes de la proyección tridimensional `X`, `Y` y `Z`, -este sistema de coordenadas es derecho con el eje `Z` apuntando afuera de la pantalla - -# Controles -## Teclado -- `Q` Rota el eje `X` de forma horaria. -- `W` Rota el eje `X` de forma antihoraria. -- `A` Rota el eje `Y` de forma horaria. -- `S` Rota el eje `Y` de forma antihoraria. -- `Z` Rota el eje `Z` de forma horaria. -- `X` Rota el eje `Z` de forma antihoraria. -## Mouse -- Hacer `click` en un eje lo selecciona. -- El `scroll` del mouse hace girar al eje seleccionado. diff --git a/doc/file_layout.svg b/doc/file_layout.svg new file mode 100644 index 0000000..d4de8a9 --- /dev/null +++ b/doc/file_layout.svg @@ -0,0 +1,10 @@ + + + + + + + + examplemain.cfigure.kleinGLFWCGLMNUKLEAROpenGLgui.ccontext.cmesh.cshader.ctexture.cGLADdata/shader.hload.cinput.cwindow.ccallbacks \ No newline at end of file From a08b17e975bded7c9684924a1c4aa2e91ce33693 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Wed, 4 Dec 2024 10:37:10 -0600 Subject: [PATCH 18/18] Update colors --- doc/file_layout.svg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/file_layout.svg b/doc/file_layout.svg index d4de8a9..7debb5c 100644 --- a/doc/file_layout.svg +++ b/doc/file_layout.svg @@ -1,4 +1,4 @@ - + @@ -7,4 +7,4 @@ - examplemain.cfigure.kleinGLFWCGLMNUKLEAROpenGLgui.ccontext.cmesh.cshader.ctexture.cGLADdata/shader.hload.cinput.cwindow.ccallbacks \ No newline at end of file + examplemain.cfigure.kleinGLFWCGLMNUKLEAROpenGLgui.ccontext.cmesh.cshader.ctexture.cGLADdata/shader.hload.cinput.cwindow.ccallbacks \ No newline at end of file