diff --git a/Makefile b/Makefile index 1c0ed9c..2c23c49 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ BIN = manigraph OBJ = \ + src/surface.o \ src/context.o \ src/texture.o \ src/window.o \ @@ -47,10 +48,12 @@ windows: $(OBJ) glfw.dll glfw.dll: $(CC) -fPIC -shared -D_GLFW_WIN32 -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o $@ -lgdi32 +src/main.o: src/data/shaders.h src/data/cube.h src/data/axis.h + # LINUX linux-x11: $(OBJ) $(MAKE) BKN=_GLFW_X11 libglfw.so - $(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw -lm + $(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw -lm linux-wayland: $(OBJ) $(MAKE) BKN=_GLFW_WAYLAND libglfw.so diff --git a/src/data/axis.h b/src/data/axis.h index d957c16..3ccddc8 100644 --- a/src/data/axis.h +++ b/src/data/axis.h @@ -6,6 +6,7 @@ #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, diff --git a/src/main.c b/src/main.c index 93599f8..47e7f0c 100755 --- a/src/main.c +++ b/src/main.c @@ -1,9 +1,10 @@ #include "main.h" -#include "data/cube.h" #include "data/axis.h" #include "data/shaders.h" +#include #include #include + #include #define WIDTH 512 @@ -49,6 +50,7 @@ float * fill_normal( float * d ) } const char * wname = "manigraph: manifold grapher"; +float * generate_surface(); int main( void ) { @@ -82,11 +84,13 @@ int main( void ) /* Fill m_cube */ { - float * n_cube; + float * n_cube, *d_cube; + d_cube = generate_surface(16); n_cube = fill_normal( d_cube ); if( !( m_cube = create_mesh( d_cube, n_cube, coordanate ) ) ) goto error_mesh_cube; free( n_cube ); + free( d_cube); } /* Fill m_axis */ @@ -116,8 +120,10 @@ int main( void ) load_mdl_matrix( shader_plain, 2, 2 ); draw_mesh( m_axis ); #endif + load_mdl_matrix( shader, 0, 3 ); draw_mesh( m_cube ); + } destroy_mesh( m_axis ); diff --git a/src/surface.c b/src/surface.c new file mode 100644 index 0000000..89cec8c --- /dev/null +++ b/src/surface.c @@ -0,0 +1,92 @@ +#include +#include +#include + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +typedef void(*function_t)(float*,int, int, int); + +void mobius(float *d_surface, int i, int j, int grid_size) +{ + const float width = 0.5; + float u = (2*M_PI) * ((float)i/grid_size ); + float v = (2*width) * ((float)j/grid_size ) - 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 toro(float *d_surface, int i, int j, int grid_size) +{ + float u = (2*M_PI) * ((float)i/grid_size ); + float v = (2*M_PI) * ((float)j/grid_size ); + + 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 i, int j, int grid_size) +{ + float u = (2*M_PI) * ((float)i/grid_size ); + float v = (2*M_PI) * ((float)j/grid_size ); + + 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[2] = sin(v)*sin(u/2); +} + +float * generate_surface(int grid_size) +{ + long size = grid_size*grid_size*2*3*3*2; + function_t f = klein; + float * d_surface; + int k=0; + + d_surface = malloc((size+1)*sizeof(float)); + d_surface[0] = size; + + for (int i = 0; i < grid_size; i++) + { + for (int j = 0; j < grid_size; j++) + { + // triangle 1, front + f(&d_surface[k + 1], i, j, grid_size); + k+=3; + f(&d_surface[k + 1], i + 1, j, grid_size); + k+=3; + f(&d_surface[k + 1], i + 1, j + 1, grid_size); + k+=3; + + // triangle 1, back + f(&d_surface[k + 1], i + 1, j + 1, grid_size); + k+=3; + f(&d_surface[k + 1], i + 1, j, grid_size); + k+=3; + f(&d_surface[k + 1], i, j, grid_size); + k+=3; + + // triangle 2, font + f(&d_surface[k + 1], i, j, grid_size); + k+=3; + f(&d_surface[k + 1], i, j + 1, grid_size); + k+=3; + f(&d_surface[k + 1], i + 1, j + 1, grid_size); + k+=3; + + // triangle 2, back + f(&d_surface[k + 1], i + 1, j + 1, grid_size); + k+=3; + f(&d_surface[k + 1], i, j + 1, grid_size); + k+=3; + f(&d_surface[k + 1], i, j, grid_size); + k+=3; + } + } + + return d_surface; +}