diff --git a/Makefile b/Makefile index b2e71e1..790cb3b 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ BIN = manigraph OBJ = \ + ext/glad/glad.o \ src/surface.o \ src/context.o \ src/texture.o \ @@ -14,6 +15,7 @@ OBJ = \ CFLAGS = \ -I./ext/cglm/include \ -I./ext/glfw/include \ + -I./ext/glad \ -Wall -Wno-unused-function -std=c99 -D_GNU_SOURCE \ WAYLAND-LIB = \ @@ -37,52 +39,42 @@ help: @echo " $(MAKE) CC=emcc wasm" @echo "Para limpiar los archivos compilados se puede usar" @echo " $(MAKE) clean" - @echo "Para ejecturar el programa sin instalarlos se puede usar:" - @echo " $(MAKE) run-linux" src/main.o: src/data/axis.h src/data/shaders.h -# WINDOWS -windows: $(OBJ) glfw.dll - $(CC) $(CFLAGS) $(OBJ) -o $(BIN) -L. -lglfw -lopengl32 -lglew32 +windows: $(OBJ) + cd ext; $(MAKE) -f glfw.mk windows; cd - + $(CC) $(CFLAGS) $(OBJ) -o $(BIN) -L. -lglfw -lopengl32 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/axis.h - # LINUX linux-wayland: $(OBJ) - for i in $(WAYLAND-LIB); \ - do \ - wayland-scanner client-header ext/glfw/deps/wayland/$$i.xml ext/glfw/deps/wayland/$$i-client-protocol.h; \ - wayland-scanner private-code ext/glfw/deps/wayland/$$i.xml ext/glfw/deps/wayland/$$i-client-protocol-code.h; \ - done - $(MAKE) DFLAGS="-D_GLFW_WAYLAND" libglfw.so - $(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw -lm + cd ext; $(MAKE) -f glfw.mk linux-wayland; cd - + $(CC) -o $(BIN) $(OBJ) ext/libglfw.a -lGL -lm linux-x11: $(OBJ) - $(MAKE) DFLAGS="-D_GLFW_X11" libglfw.so - $(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw -lm + cd ext; $(MAKE) -f glfw.mk linux-x11; cd - + $(CC) -o $(BIN) $(OBJ) ext/libglfw.a -lGL -lm -run-linux: - LD_LIBRARY_PATH=. ./$(BIN) - -# COCOA cocoa: $(OBJ) - $(MAKE) DFLAGS="-D_GLFW_COCOA" libglfw.so - $(CC) -framework OpenGL -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw + cd ext; $(MAKE) -f glfw.mk cocoa; cd - + $(CC) -framework OpenGL -o $(BIN) $(OBJ) ext/glfw.a -lGL -lglfw wasm: $(OBJ) - $(CC) -sUSE_WEBGL2=1 -sUSE_GLFW=3 -o mani.html $(OBJ) + $(CC) -sUSE_WEBGL2=1 -sUSE_GLFW=3 -o $(BIN).js $(OBJ) + chmod -x $(BIN).wasm libglfw.so: $(CC) -fPIC -shared $(DFLAGS) -D_GLFW_BUILD_DLL -Iext/glfw/deps/wayland ./ext/glfw/src/*.c -o $@ clean: - rm $(OBJ) $(BIN) ext/glfw/deps/wayland/*.h + rm $(OBJ) $(BIN) + cd ext; $(MAKE) -f glfw.mk clean; cd - + .SUFFIXES: .c .o -.c.o: src/main.h +.c.o: $(CC) $(CFLAGS) -c -o $@ $< diff --git a/ext/glfw.mk b/ext/glfw.mk new file mode 100644 index 0000000..3d03183 --- /dev/null +++ b/ext/glfw.mk @@ -0,0 +1,71 @@ +BIN = libglfw.a +OBJ = \ + glfw/src/context.o \ + glfw/src/egl_context.o \ + glfw/src/glx_context.o \ + glfw/src/init.o \ + glfw/src/input.o \ + glfw/src/linux_joystick.o \ + glfw/src/monitor.o \ + glfw/src/null_init.o \ + glfw/src/null_joystick.o \ + glfw/src/null_monitor.o \ + glfw/src/null_window.o \ + glfw/src/osmesa_context.o \ + glfw/src/platform.o \ + glfw/src/posix_module.o \ + glfw/src/posix_poll.o \ + glfw/src/posix_thread.o \ + glfw/src/posix_time.o \ + glfw/src/vulkan.o \ + glfw/src/wgl_context.o \ + glfw/src/win32_init.o \ + glfw/src/win32_joystick.o \ + glfw/src/win32_module.o \ + glfw/src/win32_monitor.o \ + glfw/src/win32_thread.o \ + glfw/src/win32_time.o \ + glfw/src/win32_window.o \ + glfw/src/window.o \ + glfw/src/wl_init.o \ + glfw/src/wl_monitor.o \ + glfw/src/wl_window.o \ + glfw/src/x11_init.o \ + glfw/src/x11_monitor.o \ + glfw/src/x11_window.o \ + glfw/src/xkb_unicode.o + +WAYLAND-LIB = \ + xdg-shell \ + relative-pointer-unstable-v1 \ + xdg-decoration-unstable-v1 \ + pointer-constraints-unstable-v1 \ + viewporter \ + idle-inhibit-unstable-v1 \ + fractional-scale-v1 \ + xdg-activation-v1 \ + wayland + +windows: + $(MAKE) $(OBJ) CFLAGS="-D_GLFW_WIN32 -Iglfw/deps/wayland" + $(AR) rsc $(BIN) $(OBJ) + +linux-x11: + $(MAKE) $(OBJ) CFLAGS="-D_GLFW_X11 -Iglfw/deps/wayland" + $(AR) rsc $(BIN) $(OBJ) + + +linux-wayland: + for i in $(WAYLAND-LIB); \ + do \ + wayland-scanner client-header glfw/deps/wayland/$$i.xml glfw/deps/wayland/$$i-client-protocol.h; \ + wayland-scanner private-code glfw/deps/wayland/$$i.xml glfw/deps/wayland/$$i-client-protocol-code.h; \ + done + $(MAKE) $(OBJ) CFLAGS="-D_GLFW_WAYLAND -fPIC -Iglfw/deps/wayland" + $(AR) rsc $(BIN) $(OBJ) + + + +clean: + rm $(OBJ) $(BIN) + rm glfw/deps/wayland/*.h diff --git a/src/context.c b/src/context.c index c9f00b8..be286b5 100644 --- a/src/context.c +++ b/src/context.c @@ -4,8 +4,8 @@ #include #else #ifdef GLAD -#include #include +#include #else #include #endif @@ -14,7 +14,6 @@ void set_clean_color_context(unsigned char r, unsigned char g, unsigned char b) { glEnable(GL_DEPTH_TEST); - glClearColor((float)r / 0xff, (float)g / 0xff, (float)b / 0xff, 1.0); } diff --git a/src/data/shaders.h b/src/data/shaders.h index e166136..1c25efc 100644 --- a/src/data/shaders.h +++ b/src/data/shaders.h @@ -15,20 +15,34 @@ const char * vs = "layout (location = 6) in float aNormal_z;" "layout (location = 7) in float aNormal_w;" - "uniform float idx;" + "uniform uint idx;" + + "uniform uint i;" + "uniform float angle;" + "uniform mat4 fix;" "uniform mat4 rot;" "uniform mat4 mdl;" - "out float index;" + "flat out uint index;" "out vec3 Normal;" "out vec3 FragPos;" + "mat2 rotate2d( float angle )" + "{" + "return mat2( cos(angle), sin(angle), -sin(angle), cos(angle) );" + "}" + "void main()" "{" " index=idx;" + " 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];" + " 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));" @@ -46,14 +60,15 @@ const char * fs_plain = #endif "uniform sampler2DArray palette;" - "in float index;" + "flat in uint index;" "out vec4 FragColor;" "in vec3 Normal;" "in vec3 FragPos;" "void main()" "{" - " FragColor = texture( palette, vec3( 0, 0, index ) ).rgba;" + " vec4 color = texture( palette, vec3(0,0,index)).rgba;" + " FragColor = vec4(pow(vec3(color),vec3(1.0/2.2)),color.a);" "}"; const char * fs = @@ -67,7 +82,7 @@ const char * fs = "uniform sampler2DArray palette;" - "in float index;" + "flat in uint index;" "in vec3 Normal;" "in vec3 FragPos;" @@ -85,9 +100,9 @@ const char * fs = " vec3 halfwayDir = normalize(lightDir + viewDir);\n" - " float specular = pow(max(dot(normalize(Normal), halfwayDir), 0.0), 16.0);\n" - " float diffuse = max(dot(normalize(Normal), lightDir), 0.0);\n" + " float specular = pow(abs(dot(normalize(Normal), halfwayDir)), 32.0);\n" + " float diffuse = abs(dot(normalize(Normal), lightDir));\n" - " vec3 result = (0.5 + 0.5 * diffuse + specular) * color.rgb;\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/input.c b/src/input.c index a639003..319923a 100644 --- a/src/input.c +++ b/src/input.c @@ -9,6 +9,8 @@ unsigned char selected_axis = 0; int window_width; int window_height; +unsigned char animate_index=0; + versor q = GLM_QUAT_IDENTITY_INIT; vec3 axis[3] = { @@ -49,25 +51,28 @@ void __key_callback_input( switch (key) { unsigned char tmp; - - case GLFW_KEY_P: + case GLFW_KEY_I: tmp = projection.w; projection.w = projection.x; projection.x = tmp; + + animate_index=1; break; case GLFW_KEY_O: tmp = projection.w; projection.w = projection.y; projection.y = tmp; + animate_index=2; break; - case GLFW_KEY_I: + case GLFW_KEY_P: tmp = projection.w; projection.w = projection.z; projection.z = tmp; + animate_index=3; break; } - set_projection_mesh(projection); + return; } @@ -107,13 +112,21 @@ void __mouse_callback_input( 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, 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); + glm_quat_rotatev(p, axis[0], axis[0]); glm_quat_rotatev(p, axis[1], axis[1]); glm_quat_rotatev(p, axis[2], axis[2]); + + glm_quat_rotatev(r, axis[0], axis[0]); + glm_quat_rotatev(r, axis[1], axis[1]); + glm_quat_rotatev(r, axis[2], axis[2]); } quat_t poll_input(window_t window) @@ -157,5 +170,13 @@ end: glm_quat_rotatev(p, axis[1], axis[1]); glm_quat_rotatev(p, axis[2], axis[2]); glm_quat_normalize(q); + + // LOG INFO + 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("\n"); + } return q; } diff --git a/src/main.c b/src/main.c index 6e90b29..508fa1e 100644 --- a/src/main.c +++ b/src/main.c @@ -12,18 +12,22 @@ #include #endif +#ifndef M_PI +#define M_PI 3.14159 +#endif + float *generate_data_surface(unsigned int, unsigned char *); float *generate_normals_surface(float *, unsigned char); -const char *wname = "manigraph: manifold grapher"; +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"; unsigned char palette[][4] = { {0xEB, 0xD3, 0xF8, 0xff}, {0xEB, 0xD4, 0xF8, 0xff}, {0xEB, 0xD5, 0xF8, 0xff}, - {0x7A, 0x1C, 0xAC, 0xff}, + {0x2F, 0x3C, 0x7E, 0xff}, }; void mlog(char *msg) @@ -37,6 +41,8 @@ window_t window; mesh_t m_surface, m_axis; id_t shader, shader_plain; +extern volatile unsigned char animate_index; + #ifndef EMSCRIPTEN static inline #endif @@ -46,23 +52,52 @@ static inline quat_t q; q = poll_input(window); + load_rot_matrix(shader, q); load_rot_matrix(shader_plain, q); + { + static float angle = 0; + + if( angle > M_PI/2 ) + { + animate_index=0; + angle = 0; + + load_float_to_shader( shader, "angle", angle); + load_float_to_shader( shader_plain, "angle", angle); + set_projection_mesh( projection ); + } + + if( animate_index ) + { + 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); + } + } + + + clean_context(); #ifndef DEBUG load_mdl_matrix(shader_plain, 0, 0); - draw_mesh(m_axis); + draw_mesh(m_axis, 1); load_mdl_matrix(shader_plain, 1, 1); - draw_mesh(m_axis); + draw_mesh(m_axis, 1); load_mdl_matrix(shader_plain, 2, 2); - draw_mesh(m_axis); - load_mdl_matrix(shader, 0, 3); -#else - load_mdl_matrix(shader_plain, 0, 3); + draw_mesh(m_axis, 1); #endif - draw_mesh(m_surface); + load_mdl_matrix(shader, 0, 3); + draw_mesh(m_surface,0); + + load_mdl_matrix(shader_plain, 0, 3); + draw_mesh(m_surface,1); + } int main(void) @@ -87,7 +122,7 @@ int main(void) mlog("[CONTEXT] Error al inicializar...\n"); goto error_context; } - set_clean_color_context(0x2E, 0x07, 0x3F); + set_clean_color_context(0xFF, 0xFF, 0xFF); } mlog("[TEXTURE] Inicializando...\n"); @@ -127,15 +162,16 @@ int main(void) d_surface = generate_data_surface(16, &m); n_surface = generate_normals_surface(d_surface, m); - projection.m = m; - if (!(m_surface = create_mesh(d_surface, n_surface, m))) { mlog("[MESH] Error al inicializar...\n"); goto error_mesh_surface; } + projection.m = m; projection.mesh = m_surface; + set_projection_mesh( projection ); + free(n_surface); free(d_surface); @@ -152,7 +188,8 @@ int main(void) mlog("[MAIN LOOP] Inicializando...\n"); #ifdef EMSCRIPTEN - emscripten_set_main_loop(&main_loop, 0, 1); + emscripten_set_main_loop(&main_loop, 60, 1); + return 0; #else while (is_open_window(window)) main_loop(); diff --git a/src/main.h b/src/main.h index 130a91a..a14d487 100644 --- a/src/main.h +++ b/src/main.h @@ -5,7 +5,7 @@ */ /* #define DEBUG */ -/* #define GLAD */ +#define GLAD typedef const void * window_t; typedef unsigned int id_t; @@ -57,7 +57,7 @@ void set_projection_mesh( struct projection ); void destroy_mesh(mesh_t p); -void draw_mesh(mesh_t p); +void draw_mesh(mesh_t p, char lines); /* Set background color: @@ -99,6 +99,14 @@ unsigned char load_program_to_shader(id_t shader, const char * src, unsigned int void load_float_to_shader(id_t shader, char * var, float f); +/* + load unsigned int to shader: + var: name of glsl variable. + u: unsigned int to load +*/ + +void load_uint_to_shader(id_t shader, char * var, unsigned int u); + /* load matrix 4 to shader: var: name of glsl variable. @@ -146,3 +154,9 @@ 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/matrix.c b/src/matrix.c index 2ddb925..5108268 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -30,7 +30,7 @@ void load_fix_matrix(id_t shader, float ratio) const int d = 7; glm_lookat((vec3){0, 0, -d}, (vec3){0, 0, 0}, (vec3){0, 1, 0}, m); - glm_perspective(CGLM_PI / 4, ratio, d - 3, d + 3, n); + 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); @@ -38,7 +38,7 @@ void load_fix_matrix(id_t shader, float ratio) void load_mdl_matrix(id_t shader, unsigned char i, unsigned char c) { - load_float_to_shader(shader, "idx", c); + load_uint_to_shader(shader, "idx", c); load_mat4_to_shader(shader, "mdl", (mat4_t)ortho[i]); } diff --git a/src/mesh.c b/src/mesh.c index 100be67..0353741 100644 --- a/src/mesh.c +++ b/src/mesh.c @@ -78,19 +78,6 @@ mesh_t create_mesh(float *d, float *n, unsigned char m) glEnableVertexAttribArray(i + 4); } - { - struct projection projection = { - .x = 0, - .y = 1, - .z = 2, - .w = 3, - }; - - projection.m = m; - projection.mesh = p; - set_projection_mesh(projection); - } - return p; } @@ -104,18 +91,19 @@ void destroy_mesh(mesh_t p) free(p); } -void draw_mesh(mesh_t p) +void draw_mesh(mesh_t p, char lines ) { struct obj *obj = p; glBindVertexArray(obj->vao); -#ifdef DEBUG + if( lines ) { int i; for (i = 0; i < obj->vertex; i += 3) glDrawArrays(GL_LINE_LOOP, i, 3); } -#else - glDrawArrays(GL_TRIANGLES, 0, obj->vertex); -#endif + else + { + glDrawArrays(GL_TRIANGLES, 0, obj->vertex); + } } diff --git a/src/shader.c b/src/shader.c index eec91c2..f7d8430 100644 --- a/src/shader.c +++ b/src/shader.c @@ -54,6 +54,12 @@ 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); diff --git a/src/surface.c b/src/surface.c index e505f2f..c87daa5 100644 --- a/src/surface.c +++ b/src/surface.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -10,31 +11,58 @@ #define M_PI 3.14159265358979323846 #endif -void mobius(float *d_surface, int i, int j, int grid_size) +#ifndef CMPLX +#define CMPLX(a,b) (a+I*b) +#endif + +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)); + + 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, int grid_size ) +{ + unsigned char i; + + 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) { const float width = 0.5; - float u = (2 * M_PI) * ((float)i / grid_size); - float v = (2 * width) * ((float)j / grid_size) - width; + float u = (2 * M_PI) * ((float)coord[0] / grid_size); + float v = (2 * width) * ((float)coord[1] / 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 torus(float *d_surface, int i, int j, int grid_size) +void torus(float *d_surface, int * coord, int grid_size) { - float u = (2 * M_PI) * ((float)i / grid_size); - float v = (2 * M_PI) * ((float)j / grid_size); + float u = (2 * M_PI) * ((float)coord[0] / grid_size); + float v = (2 * M_PI) * ((float)coord[1] / 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) +void klein(float *d_surface, int * coord, int grid_size) { - float u = (2 * M_PI) * ((float)i / grid_size); - float v = (2 * M_PI) * ((float)j / grid_size); + float u = (2 * M_PI) * ((float)coord[0] / 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); @@ -42,46 +70,86 @@ void klein(float *d_surface, int i, int j, int grid_size) d_surface[3] = sin(v) * sin(u / 2); } -typedef void (*function_t)(float *, int, int, int); +typedef void (*function_t)(float *, int *, int); -float *generate_data_surface(int grid_size, unsigned char *m) +float *generate_data_surface(int grid_size, unsigned char *s) { - unsigned int i, j, k = 0; - long size; + unsigned int i, j, k, o, p, l, n, m; + long size, q=0; function_t f; float *d_surface; - f = klein; - *m = 4; - size = grid_size * grid_size * 6 * (*m); + const int dim =2; + int cara[dim]; + char bits[dim+1]; + bits[dim]=0; + + f =klein ; + *s = 4; + + size = grid_size * grid_size * 6 * (*s) * 24; d_surface = malloc((size + 1) * sizeof(float)); d_surface[0] = size; - - for (i = 0; i < grid_size; i++) - { - for (j = 0; j < grid_size; j++) + + for(o = 0; o < dim; o ++) + { + for (p = 0; p < o; p++) { - // triangle 1, Front - f(&d_surface[k + 1], i, j, grid_size); - k += *m; - f(&d_surface[k + 1], i + 1, j, grid_size); - k += *m; - f(&d_surface[k + 1], i + 1, j + 1, grid_size); - k += *m; + for (k = 0; k < (1 << (dim-2)); k++) + { + unsigned char skip=0; + for(n = 0; n < dim-2; n++) + { + if( n==(o-1) || n==p ) + skip++; - // triangle 2, Back - f(&d_surface[k + 1], i, j, grid_size); - k += *m; - f(&d_surface[k + 1], i, j + 1, grid_size); - k += *m; - f(&d_surface[k + 1], i + 1, j + 1, grid_size); - k += *m; + cara[n+skip] = (k & (1<