Read Klein file

This commit is contained in:
PedroEdiaz
2024-12-01 21:08:35 -06:00
parent 1d254eade5
commit 987ce54429
3 changed files with 16 additions and 43 deletions

View File

@@ -15,9 +15,6 @@
#define M_PI 3.14159 #define M_PI 3.14159
#endif #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}; struct projection projection = {.x = 0, .y = 1, .z = 2, .w = 3};
const char *wname = "manigraph: manifold grapher"; const char *wname = "manigraph: manifold grapher";
@@ -134,9 +131,11 @@ int main(void)
{ {
struct surface surface; struct surface surface;
surface.data = generate_data_surface(&surface.dim, &surface.vertex); if( create_surface_klein("test.klein", &surface) )
surface.norm = {
generate_normals_surface(surface.data, surface.dim, surface.vertex); mlog("[MESH] Error al leer el archivo...\n");
goto error_mesh_surface;
}
if (!(m_surface = create_mesh(surface))) if (!(m_surface = create_mesh(surface)))
{ {
@@ -146,7 +145,6 @@ int main(void)
projection.m = surface.dim; projection.m = surface.dim;
projection.mesh = m_surface; projection.mesh = m_surface;
set_projection_mesh(projection); set_projection_mesh(projection);
free(surface.norm); free(surface.norm);

View File

@@ -36,6 +36,7 @@ struct projection
vertex: the number of vertex vertex: the number of vertex
dim: the dimentions of the surface dim: the dimentions of the surface
*/ */
struct surface struct surface
{ {
float *data, *norm; float *data, *norm;
@@ -50,6 +51,8 @@ struct surface
name: Name of the window. 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); window_t init_window(unsigned int w, unsigned int h, const char *name);
void use_window(window_t window); 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); id_t create_palette_texture(const unsigned char colors[][4], unsigned char n);
quat_t poll_input(window_t window); quat_t poll_input(window_t window);
#ifdef EMSCRIPTEN
#ifdef GLAD
#error undefine GLAD on src/main.h please
#endif
#endif

View File

@@ -32,44 +32,22 @@ struct parm
} parm; } parm;
// Función para escribir el archivo .klein // 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"); FILE *file = fopen(filename, "wb");
if (!file) if (!file)
{ return 1;
perror("Error al abrir el archivo");
exit(EXIT_FAILURE);
}
// Escribir encabezado // Escribir encabezado
fwrite("KLEIN", 1, 5, file); // Los primeros 5 bytes son "KLEIN" 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(&dim, 1, 1, file); // Dimensión de la superficie
fwrite(&vertex, sizeof(unsigned long), 1, file); // Número de vértices (8 bytes) fwrite(&vertex, 8, 1, file); // Número de vértices (8 bytes)
fwrite(vertices, 16, vertex * dim, file);
// Escribir dimensiones de la cuadrícula fwrite(normals, 16, vertex * dim, file);
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); fclose(file);
printf("Archivo %s escrito correctamente.\n", filename); return 0;
} }
int factorial(int n) int factorial(int n)
@@ -390,7 +368,7 @@ int main()
// Escribir el archivo // Escribir el archivo
printf("Escribiendo archivo .klein\n"); 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(vertices);
free(normals); free(normals);