From 994bd3987c10009406ca4c824c312819ad5aa1a1 Mon Sep 17 00:00:00 2001 From: alan Date: Fri, 18 Oct 2024 09:30:11 -0600 Subject: [PATCH] Toro agregado --- Makefile | 2 +- src/banda_mob.c | 59 ------------------------------------------- src/main.c | 10 ++++---- src/surface.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 65 deletions(-) delete mode 100644 src/banda_mob.c create mode 100644 src/surface.c diff --git a/Makefile b/Makefile index 537717a..eb21dbf 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ BIN = manigraph OBJ = \ - src/banda_mob.o \ + src/surface.o \ src/context.o \ src/texture.o \ src/window.o \ diff --git a/src/banda_mob.c b/src/banda_mob.c deleted file mode 100644 index 76bf8e9..0000000 --- a/src/banda_mob.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include "main.h" -#include -#include - -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - -#define GRID_SIZE 16 -#define WIDTH 0.5 - -typedef void(*function_t)(float*,int,int); - -void mobius(float *p, int i, int j) -{ - float u = (2*M_PI) * ((float)i/(GRID_SIZE) ); - float v = (2*WIDTH) * ((float)j/(GRID_SIZE) ) - WIDTH; - - p[0] = cos(u) + v * cos(u / 2) * cos(u); - p[1] = sin(u) + v * cos(u / 2) * sin(u); - p[2] = v * sin(u / 2); -} - -float * acabron(){ - long size = GRID_SIZE*GRID_SIZE*2*3*3; - function_t f = mobius; - float * d_cube; - int k=0; - - d_cube = malloc((size+1)*sizeof(float)); - d_cube[0] = size; - - for (int i = 0; i < GRID_SIZE; i++) - { - for (int j = 0; j < GRID_SIZE; j++) - { - // Triángulo 1 - f(&d_cube[k + 1], i, j); - k+=3; - f(&d_cube[k + 1], i + 1, j); - k+=3; - f(&d_cube[k + 1], i + 1, j + 1); - k+=3; - - // Triángulo 2 - f(&d_cube[k + 1], i, j); - k+=3; - f(&d_cube[k + 1], i, j + 1); - k+=3; - f(&d_cube[k + 1], i + 1, j + 1); - k+=3; - } - } - - - - return d_cube; -} diff --git a/src/main.c b/src/main.c index bfd7331..443459b 100644 --- a/src/main.c +++ b/src/main.c @@ -18,11 +18,11 @@ unsigned char palette[] = }; const char * wname = "manigraph: manifold grapher"; -float * acabron(void); +float * gen_surface(); int main( void ) { - float * d_cube; + float * points; id_t shader, texture; mesh_t m_cube, m_axis; window_t window; @@ -35,7 +35,7 @@ int main( void ) glewInit(); - d_cube = acabron(); + points = gen_surface(256); if( !( shader = create_shader() ) ) goto error_shader; @@ -46,10 +46,10 @@ int main( void ) load_fix_matrix( shader, (float)WIDTH/HEIGHT ); - if( !( m_cube = create_mesh( d_cube ) ) ) + if( !( m_cube = create_mesh( points ) ) ) goto error_mesh_cube; - free(d_cube); + free(points); if( !( m_axis = create_mesh( d_axis ) ) ) goto error_mesh_axis; diff --git a/src/surface.c b/src/surface.c new file mode 100644 index 0000000..7fd34bf --- /dev/null +++ b/src/surface.c @@ -0,0 +1,66 @@ +#include +#include +#include + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +#define WIDTH 0.5 + +typedef void(*function_t)(float*,int, int, int); + +void mobius(float *p, int i, int j, int GRID_SIZE) +{ + float u = (2*M_PI) * ((float)i/(GRID_SIZE) ); + float v = (2*WIDTH) * ((float)j/(GRID_SIZE) ) - WIDTH; + + p[0] = cos(u) + v * cos(u / 2) * cos(u); + p[1] = sin(u) + v * cos(u / 2) * sin(u); + p[2] = v * sin(u / 2); +} + +void toro(float *p, int i, int j, int GRID_SIZE) +{ + float u = (2*M_PI) * ((float)i/(GRID_SIZE) ); + float v = (2*WIDTH) * ((float)j ); + + p[0] = (1 + 0.5*cos(v))*cos(u); + p[1] = (1 + 0.5*cos(v)) * sin(u); + p[2] = 0.5*sin(v); +} + +float * gen_surface(int GRID_SIZE) +{ + long size = GRID_SIZE*GRID_SIZE*2*3*3; + function_t f = mobius; + float * points; + int k=0; + + points = malloc((size+1)*sizeof(float)); + points[0] = size; + + for (int i = 0; i < GRID_SIZE; i++) + { + for (int j = 0; j < GRID_SIZE; j++) + { + // Triángulo 1 + f(&points[k + 1], i, j, GRID_SIZE); + k+=3; + f(&points[k + 1], i + 1, j, GRID_SIZE); + k+=3; + f(&points[k + 1], i + 1, j + 1, GRID_SIZE); + k+=3; + + // Triángulo 2 + f(&points[k + 1], i, j, GRID_SIZE); + k+=3; + f(&points[k + 1], i, j + 1, GRID_SIZE); + k+=3; + f(&points[k + 1], i + 1, j + 1, GRID_SIZE); + k+=3; + } + } + + return points; +}