From 987ce544290dfc70b80333dca8c9b8b301f53525 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Sun, 1 Dec 2024 21:08:35 -0600 Subject: [PATCH] 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);