From fb2cbe20db6b60ace6b50492652ad4a04a8a4231 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Wed, 30 Oct 2024 22:38:23 -0600 Subject: [PATCH 01/14] glew -> glad & libglfw.so -> libglfw.a --- Makefile | 57 ++++++++++++------------------------------ ext/glfw.mk | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 41 deletions(-) create mode 100644 ext/glfw.mk diff --git a/Makefile b/Makefile index 86ced9c..672fbd1 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,18 +15,8 @@ OBJ = \ CFLAGS = \ -I./ext/cglm/include \ -I./ext/glfw/include \ - -Wall -Wno-unused-function -std=c99 -D_POSIX_C_SOURCE \ - -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 + -I./ext/glad \ + -Wall -Wno-unused-function -std=c99 -D_GNU_SOURCE \ help: @echo "Para compilar el proyecto a tu sistema operativo" @@ -36,47 +27,31 @@ help: @echo " $(MAKE) cocoa" @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 - -# 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 - -libglfw.so: - $(CC) -fPIC -shared $(DFLAGS) -D_GLFW_BUILD_DLL -Iext/glfw/deps/wayland ./ext/glfw/src/*.c -o $@ + cd ext; $(MAKE) -f glfw.mk cocoa; cd - + $(CC) -framework OpenGL -o $(BIN) $(OBJ) ext/glfw.a -lGL -lglfw 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 From c37614ae5236c7352ea9d1d73b675106c5ced40d Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Wed, 30 Oct 2024 22:44:58 -0600 Subject: [PATCH 02/14] default: GLAD --- src/main.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.h b/src/main.h index 130a91a..37f7130 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; From 583c0cb92ffb5d83e2afeb1b49bea0c57ebaec25 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Mon, 4 Nov 2024 20:56:50 -0600 Subject: [PATCH 03/14] Use modern scrolling for better UX --- src/input.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/input.c b/src/input.c index a639003..fdb9188 100644 --- a/src/input.c +++ b/src/input.c @@ -107,13 +107,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) From 8811c4e0b7333c4c2ec36a1e5877212bc8a53434 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Wed, 20 Nov 2024 11:38:23 -0600 Subject: [PATCH 04/14] Prepare for Rn -> Rm surfaces --- src/surface.c | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/surface.c b/src/surface.c index 342efcf..aa76d09 100644 --- a/src/surface.c +++ b/src/surface.c @@ -9,31 +9,45 @@ #define M_PI 3.14159265358979323846 #endif -void mobius(float *d_surface, int i, int j, int 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 torus_5d(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); + float r1 = (float)coord[2] / grid_size; + float r2 = (float)coord[3] / grid_size; + + d_surface[0] = (r1 + r2 * cos(v)) * cos(u); + d_surface[1] = (r1 + r2 * cos(v)) * sin(u); + d_surface[2] = r2 * sin(v); + d_surface[3] = r1; + d_surface[4] = r2; +} + +void klein(float *d_surface, int * coord, int 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); @@ -41,7 +55,7 @@ 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) { @@ -52,6 +66,7 @@ float *generate_data_surface(int grid_size, unsigned char *m) f = klein; *m = 4; + size = grid_size * grid_size * 6 * (*m); d_surface = malloc((size + 1) * sizeof(float)); d_surface[0] = size; @@ -61,19 +76,19 @@ float *generate_data_surface(int grid_size, unsigned char *m) for (j = 0; j < grid_size; j++) { // triangle 1, Front - f(&d_surface[k + 1], i, j, grid_size); + f(&d_surface[k + 1], (int[2]){i, j}, grid_size); k += *m; - f(&d_surface[k + 1], i + 1, j, grid_size); + f(&d_surface[k + 1], (int[2]){i + 1, j}, grid_size); k += *m; - f(&d_surface[k + 1], i + 1, j + 1, grid_size); + f(&d_surface[k + 1], (int[2]){i + 1, j + 1}, grid_size); k += *m; // triangle 2, Back - f(&d_surface[k + 1], i, j, grid_size); + f(&d_surface[k + 1], (int[2]){i, j}, grid_size); k += *m; - f(&d_surface[k + 1], i, j + 1, grid_size); + f(&d_surface[k + 1], (int[2]){i, j + 1}, grid_size); k += *m; - f(&d_surface[k + 1], i + 1, j + 1, grid_size); + f(&d_surface[k + 1], (int[2]){i + 1, j + 1}, grid_size); k += *m; } } From e4a4a8c60362883de86e8668305cb862fdb48afc Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Thu, 21 Nov 2024 18:56:05 -0600 Subject: [PATCH 05/14] Add riemman surfaces family --- src/surface.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/surface.c b/src/surface.c index aa76d09..2acc0e8 100644 --- a/src/surface.c +++ b/src/surface.c @@ -1,3 +1,4 @@ +#include #include #include @@ -9,6 +10,25 @@ #define M_PI 3.14159265358979323846 #endif +#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 mobius(float *d_surface, int * coord, int grid_size) { const float width = 0.5; From 805ff18e21c787bbcd176031ce64f98bd95957bc Mon Sep 17 00:00:00 2001 From: alan Date: Tue, 26 Nov 2024 22:21:56 -0600 Subject: [PATCH 06/14] expandiendo a Rn avance --- src/surface.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 91 insertions(+), 7 deletions(-) diff --git a/src/surface.c b/src/surface.c index 2acc0e8..b568302 100644 --- a/src/surface.c +++ b/src/surface.c @@ -77,7 +77,7 @@ void klein(float *d_surface, int * coord, int grid_size) 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 *m) { unsigned int i, j, k = 0; long size; @@ -96,26 +96,110 @@ float *generate_data_surface(int grid_size, unsigned char *m) for (j = 0; j < grid_size; j++) { // triangle 1, Front - f(&d_surface[k + 1], (int[2]){i, j}, grid_size); + f(&d_surface[k + 1], &cara, grid_size); k += *m; - f(&d_surface[k + 1], (int[2]){i + 1, j}, grid_size); + f(&d_surface[k + 1], &cara, j}, grid_size); k += *m; - f(&d_surface[k + 1], (int[2]){i + 1, j + 1}, grid_size); + f(&d_surface[k + 1], &cara, j + 1}, grid_size); k += *m; // triangle 2, Back - f(&d_surface[k + 1], (int[2]){i, j}, grid_size); + f(&d_surface[k + 1], &cara, grid_size); k += *m; - f(&d_surface[k + 1], (int[2]){i, j + 1}, grid_size); + f(&d_surface[k + 1], &cara+ 1}, grid_size); k += *m; - f(&d_surface[k + 1], (int[2]){i + 1, j + 1}, grid_size); + f(&d_surface[k + 1], &cara, j + 1}, grid_size); k += *m; } } + return d_surface; +} */ + +float *generate_data_surface(int grid_size, unsigned char *s) +{ + unsigned int i, j, k, o, p, l, n, m, q = 0; + long size; + function_t f; + float *d_surface; + int dim = 2; + + f = klein; + *s = 4; + + int cara[2]; + + size = grid_size * grid_size * 2 * dim * (*s); + d_surface = malloc((size + 1) * sizeof(float)); + d_surface[0] = size; + + for(o = 0; o < dim; o ++) + { + for (p = o + 1; p < dim; p++) + { + for (k = 0; k < (1 << o); k++) + { + for(l = 0; l < (1 << p); l += (1 << (o + 1))) + { + for(m = 0; m < (1 << dim); m += (1 << (p + 1))) + { + for(n = 0; n < dim; n++) + { + if(((k + l + m) & (1 << n)) == 0) + { + if(n != o && n != p) + cara[n] = 0; + } + else + { + if(n != o && n != p) + cara[n] = 1; + } + + for(i = 0; i < grid_size; i++) + { + cara[o] = i; + for (j = 0; j < grid_size; j++) + { + cara[p] = j; + + f(&d_surface[q + 1], cara, grid_size); + q += *s; + cara[o] = i + 1; + f(&d_surface[q + 1], cara, grid_size); + q += *s; + cara[o] = i + 1; + cara [p] = j + 1; + f(&d_surface[q + 1], cara, grid_size); + q += *s; + + cara[o] = i; + cara [p] = j; + f(&d_surface[q + 1], cara, grid_size); + q += *s; + cara [p] = j + 1; + f(&d_surface[q + 1], cara, grid_size); + q += *s; + cara[o] = i + 1; + cara [p] = j + 1; + f(&d_surface[q + 1], cara, grid_size); + q += *s; + } + } + + + } + } + } + } + } + } + return d_surface; } + + static void __calculate_normal( float *p1, float *p2, float *p3, float *normal, unsigned char n) { From a8ae6aaef67133f060866c6b0eb09bc954dcfe19 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Tue, 26 Nov 2024 23:49:02 -0600 Subject: [PATCH 07/14] Add gamma correction, change illumination values --- src/context.c | 2 +- src/data/shaders.h | 30 ++++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/context.c b/src/context.c index c9f00b8..4eb36bb 100644 --- a/src/context.c +++ b/src/context.c @@ -14,7 +14,7 @@ void set_clean_color_context(unsigned char r, unsigned char g, unsigned char b) { glEnable(GL_DEPTH_TEST); - + glEnable(GL_FRAMEBUFFER_SRGB); 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..48294f5 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_w, aNormal[i]) * rotate2d(angle))[0];" + " aPos[i] = (vec2(aPos_w, aPos[i]) * 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,7 +60,7 @@ const char * fs_plain = #endif "uniform sampler2DArray palette;" - "in float index;" + "flat in uint index;" "out vec4 FragColor;" "in vec3 Normal;" "in vec3 FragPos;" @@ -67,7 +81,7 @@ const char * fs = "uniform sampler2DArray palette;" - "in float index;" + "flat in uint index;" "in vec3 Normal;" "in vec3 FragPos;" @@ -85,9 +99,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" - " FragColor = vec4(result, color.a);\n" + " vec3 result = (0.5 + 1.5*diffuse + 4*specular) * color.rgb;\n" + " FragColor = vec4(result, color.a)*1/2.2;\n" "}"; From 589721a953d98c2278ec77765fdaa1a71d335419 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Tue, 26 Nov 2024 23:51:30 -0600 Subject: [PATCH 08/14] WIP: Rotations in R4, and change colors --- src/input.c | 20 +++++++++++++++--- src/main.c | 59 ++++++++++++++++++++++++++++++++++++++++------------ src/main.h | 12 +++++++++-- src/matrix.c | 2 +- src/mesh.c | 24 ++++++--------------- src/shader.c | 6 ++++++ 6 files changed, 86 insertions(+), 37 deletions(-) diff --git a/src/input.c b/src/input.c index a639003..4e4280a 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,24 +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; } @@ -157,5 +163,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 4e6790f..fd7ae8d 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 = 3, .y=1, .z=2, .w=0 }; -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 @@ -45,23 +51,48 @@ void main_loop(void) quat_t q; q = poll_input(window); + load_rot_matrix(shader, q); load_rot_matrix(shader_plain, q); + { + static float angle = 0; + + + if( animate_index ) + { + angle+=0.01; + + load_uint_to_shader( shader, "i", animate_index-1 ); + load_uint_to_shader( shader_plain, "i", animate_index-1 ); + load_float_to_shader( shader, "angle", angle); + load_float_to_shader( shader_plain, "angle", angle); + } + + if( angle > M_PI/2 && angle ) + { + animate_index=0; + angle = 0; + } + } + + 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) @@ -86,7 +117,7 @@ int main(void) mlog("[CONTEXT] Error al inicializar...\n"); goto error_context; } - set_clean_color_context(0x2E, 0x07, 0x3F); + set_clean_color_context(0xFB, 0xEA, 0xEB); } mlog("[TEXTURE] Inicializando...\n"); @@ -126,16 +157,18 @@ 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); } diff --git a/src/main.h b/src/main.h index 130a91a..c003e36 100644 --- a/src/main.h +++ b/src/main.h @@ -4,7 +4,7 @@ error of the shaders. */ -/* #define DEBUG */ +/* #define DEBUG */ /* #define GLAD */ typedef const void * window_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. diff --git a/src/matrix.c b/src/matrix.c index 2ddb925..b5c48f9 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -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); From 8d1f54604ebda500d6524cfad222f01b1faa7b62 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Wed, 27 Nov 2024 00:20:51 -0600 Subject: [PATCH 09/14] Optimize and work with dim=2 --- src/surface.c | 97 +++++++++++++++++++++++---------------------------- 1 file changed, 44 insertions(+), 53 deletions(-) diff --git a/src/surface.c b/src/surface.c index b568302..bc14bc7 100644 --- a/src/surface.c +++ b/src/surface.c @@ -90,6 +90,7 @@ typedef void (*function_t)(float *, int *, int); size = grid_size * grid_size * 6 * (*m); d_surface = malloc((size + 1) * sizeof(float)); d_surface[0] = size; + ++d_surface; for (i = 0; i < grid_size; i++) { @@ -122,76 +123,66 @@ float *generate_data_surface(int grid_size, unsigned char *s) long size; function_t f; float *d_surface; - int dim = 2; + const int dim =2; + int cara[dim]; f = klein; *s = 4; - int cara[2]; - - size = grid_size * grid_size * 2 * dim * (*s); + size = grid_size * grid_size * 6 * (*s); d_surface = malloc((size + 1) * sizeof(float)); d_surface[0] = size; - + for(o = 0; o < dim; o ++) - { - for (p = o + 1; p < dim; p++) + { + for (p = 0; p < o; p++) { - for (k = 0; k < (1 << o); k++) + for (k = 0; k < (1 << (dim-2)); k++) { - for(l = 0; l < (1 << p); l += (1 << (o + 1))) + for(n = 0; n < dim; n ++) { - for(m = 0; m < (1 << dim); m += (1 << (p + 1))) + if( n==o || n== p ) + continue; + + cara[n] = (k & (1< Date: Wed, 27 Nov 2024 17:21:59 -0600 Subject: [PATCH 10/14] Fix: encoding for n-cube faces --- src/surface.c | 82 +++++++++++++-------------------------------------- 1 file changed, 20 insertions(+), 62 deletions(-) diff --git a/src/surface.c b/src/surface.c index bc14bc7..79f75af 100644 --- a/src/surface.c +++ b/src/surface.c @@ -29,6 +29,14 @@ void riemman(float *d_surface, int * coords, int grid_size) } +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; @@ -50,20 +58,6 @@ void torus(float *d_surface, int * coord, int grid_size) d_surface[2] = 0.5 * sin(v); } -void torus_5d(float *d_surface, int * coord, int grid_size) -{ - float u = (2 * M_PI) * ((float)coord[0] / grid_size); - float v = (2 * M_PI) * ((float)coord[1] / grid_size); - float r1 = (float)coord[2] / grid_size; - float r2 = (float)coord[3] / grid_size; - - d_surface[0] = (r1 + r2 * cos(v)) * cos(u); - d_surface[1] = (r1 + r2 * cos(v)) * sin(u); - d_surface[2] = r2 * sin(v); - d_surface[3] = r1; - d_surface[4] = r2; -} - void klein(float *d_surface, int * coord, int grid_size) { float u = (2 * M_PI) * ((float)coord[0] / grid_size); @@ -77,59 +71,22 @@ void klein(float *d_surface, int * coord, int grid_size) typedef void (*function_t)(float *, int *, int); -/* float *generate_data_surface(int grid_size, unsigned char *m) -{ - unsigned int i, j, k = 0; - long size; - function_t f; - float *d_surface; - - f = klein; - *m = 4; - - size = grid_size * grid_size * 6 * (*m); - d_surface = malloc((size + 1) * sizeof(float)); - d_surface[0] = size; - ++d_surface; - - for (i = 0; i < grid_size; i++) - { - for (j = 0; j < grid_size; j++) - { - // triangle 1, Front - f(&d_surface[k + 1], &cara, grid_size); - k += *m; - f(&d_surface[k + 1], &cara, j}, grid_size); - k += *m; - f(&d_surface[k + 1], &cara, j + 1}, grid_size); - k += *m; - - // triangle 2, Back - f(&d_surface[k + 1], &cara, grid_size); - k += *m; - f(&d_surface[k + 1], &cara+ 1}, grid_size); - k += *m; - f(&d_surface[k + 1], &cara, j + 1}, grid_size); - k += *m; - } - } - - return d_surface; -} */ - float *generate_data_surface(int grid_size, unsigned char *s) { - unsigned int i, j, k, o, p, l, n, m, q = 0; - long size; + unsigned int i, j, k, o, p, l, n, m; + long size, q=0; function_t f; float *d_surface; const int dim =2; int cara[dim]; - f = klein; + char bits[dim+1]; + bits[dim]=0; + + f =klein ; *s = 4; - size = grid_size * grid_size * 6 * (*s); + size = grid_size * grid_size * 6 * (*s) * 24; d_surface = malloc((size + 1) * sizeof(float)); d_surface[0] = size; @@ -139,12 +96,13 @@ float *generate_data_surface(int grid_size, unsigned char *s) { for (k = 0; k < (1 << (dim-2)); k++) { - for(n = 0; n < dim; n ++) + unsigned char skip=0; + for(n = 0; n < dim-2; n++) { - if( n==o || n== p ) - continue; + if( n==(o-1) || n==p ) + skip++; - cara[n] = (k & (1< Date: Wed, 27 Nov 2024 17:31:47 -0600 Subject: [PATCH 11/14] Fix: compilation error --- src/context.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/context.c b/src/context.c index 4eb36bb..449e703 100644 --- a/src/context.c +++ b/src/context.c @@ -4,8 +4,8 @@ #include #else #ifdef GLAD -#include #include +#include #else #include #endif From 1e009d5bdfe6ac2f11ee4358739d030bfa9a308c Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Thu, 28 Nov 2024 09:55:48 -0600 Subject: [PATCH 12/14] Fix gamma, add Glad, error if glad is compiled with emscripten --- Makefile | 2 ++ src/context.c | 3 +-- src/data/shaders.h | 7 ++++--- src/main.c | 8 +++++--- src/main.h | 8 +++++++- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index b2e71e1..28b3e45 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 = \ diff --git a/src/context.c b/src/context.c index 4eb36bb..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); - glEnable(GL_FRAMEBUFFER_SRGB); 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 48294f5..0e17dc0 100644 --- a/src/data/shaders.h +++ b/src/data/shaders.h @@ -67,7 +67,8 @@ const char * fs_plain = "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 = @@ -102,6 +103,6 @@ const char * fs = " float specular = pow(abs(dot(normalize(Normal), halfwayDir)), 32.0);\n" " float diffuse = abs(dot(normalize(Normal), lightDir));\n" - " vec3 result = (0.5 + 1.5*diffuse + 4*specular) * color.rgb;\n" - " FragColor = vec4(result, color.a)*1/2.2;\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/main.c b/src/main.c index fd7ae8d..7a84d89 100644 --- a/src/main.c +++ b/src/main.c @@ -19,7 +19,7 @@ float *generate_data_surface(unsigned int, unsigned char *); float *generate_normals_surface(float *, unsigned char); -struct projection projection = {.x = 3, .y=1, .z=2, .w=0 }; +struct projection projection = {.x = 0, .y=1, .z=2, .w=3 }; const char *wname = "manigraph: manifold grapher"; @@ -65,6 +65,7 @@ void main_loop(void) load_uint_to_shader( shader, "i", animate_index-1 ); load_uint_to_shader( shader_plain, "i", animate_index-1 ); + load_float_to_shader( shader, "angle", angle); load_float_to_shader( shader_plain, "angle", angle); } @@ -117,7 +118,7 @@ int main(void) mlog("[CONTEXT] Error al inicializar...\n"); goto error_context; } - set_clean_color_context(0xFB, 0xEA, 0xEB); + set_clean_color_context(0xFF, 0xFF, 0xFF); } mlog("[TEXTURE] Inicializando...\n"); @@ -184,7 +185,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 c003e36..9fb0637 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; @@ -154,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 From 6c7c22ce509614c74bd9d5025aec8b8bcd18798d Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Thu, 28 Nov 2024 16:23:39 -0600 Subject: [PATCH 13/14] Fix: rotations on R4 --- Makefile | 3 ++- src/data/shaders.h | 4 ++-- src/input.c | 1 - src/main.c | 30 ++++++++++++++++-------------- src/main.h | 2 +- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 28b3e45..c4435ff 100644 --- a/Makefile +++ b/Makefile @@ -76,7 +76,8 @@ cocoa: $(OBJ) $(CC) -framework OpenGL -o $(BIN) $(OBJ) -L. -lGLEW -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 $@ diff --git a/src/data/shaders.h b/src/data/shaders.h index 0e17dc0..1c25efc 100644 --- a/src/data/shaders.h +++ b/src/data/shaders.h @@ -40,8 +40,8 @@ const char * vs = " vec3 aNormal = vec3(aNormal_x,aNormal_y,aNormal_z);" " vec3 aPos = vec3(aPos_x,aPos_y,aPos_z);" - " aNormal[i] = (vec2(aNormal_w, aNormal[i]) * rotate2d(angle))[0];" - " aPos[i] = (vec2(aPos_w, aPos[i]) * rotate2d(angle))[0];" + " 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" diff --git a/src/input.c b/src/input.c index 4e4280a..76d85fe 100644 --- a/src/input.c +++ b/src/input.c @@ -73,7 +73,6 @@ void __key_callback_input( } - set_projection_mesh(projection); return; } diff --git a/src/main.c b/src/main.c index 7a84d89..20da610 100644 --- a/src/main.c +++ b/src/main.c @@ -58,26 +58,29 @@ void main_loop(void) { static float angle = 0; - - if( animate_index ) - { - angle+=0.01; - - load_uint_to_shader( shader, "i", animate_index-1 ); - load_uint_to_shader( shader_plain, "i", animate_index-1 ); - - load_float_to_shader( shader, "angle", angle); - load_float_to_shader( shader_plain, "angle", angle); - } - - if( angle > M_PI/2 && angle ) + 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 @@ -166,7 +169,6 @@ int main(void) projection.m = m; projection.mesh = m_surface; - set_projection_mesh( projection ); diff --git a/src/main.h b/src/main.h index 9fb0637..b939d5b 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; From 590fa226580557f0de0e626a31d360745d037169 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Thu, 28 Nov 2024 18:52:53 -0600 Subject: [PATCH 14/14] Better use of viewport --- src/matrix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/matrix.c b/src/matrix.c index b5c48f9..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);