14 Commits

Author SHA1 Message Date
PedroEdiaz
177edb18b1 feat(main.c, input.c): Print important information to stdout 2025-05-11 17:30:29 -06:00
PedroEdiaz
605f4610ce Fix(parm.h): allow size to be correctly calculated 2025-05-11 17:28:30 -06:00
PedroEdiaz
8cc9587851 feat(input): more intuitive inputs 2025-05-11 15:35:58 -06:00
PedroEdiaz
fbd7821c12 feat(input): Add mouse input 2025-05-11 15:31:04 -06:00
PedroEdiaz
64a6f60b6d feat(input): Add arrow key input 2025-05-11 12:54:43 -06:00
PedroEdiaz
9ae78c3c84 fix(klein): fix klein format with stdint 2025-04-12 11:53:04 -06:00
PedroEdiaz
10133bca3e fix(windows): partialy fix windows compilation 2025-04-12 11:46:10 -06:00
PedroEdiaz
dc49810a67 feat(input,window): Add glfw debug and fix nanosleep for windows 2025-04-12 11:43:39 -06:00
PedroEdiaz
d2d6794f08 refactor(src/klein.c): Fix values on klein format 2025-04-12 11:38:55 -06:00
PedroEdiaz
533109fbcd Return color 2024-12-09 16:12:48 -06:00
PedroEdiaz
3997613d16 Fix nameing and reduce size 2024-12-09 00:40:40 -06:00
PedroEdiaz
f21121b88d Better contrast 2024-12-09 00:39:39 -06:00
PedroEdiaz
86480fe9b1 Add flow 2024-12-05 21:38:59 -06:00
PedroEdiaz
e9bd748dd8 Fix examples compilation 2024-12-05 19:19:19 -06:00
15 changed files with 199 additions and 104 deletions

View File

@@ -12,7 +12,12 @@ OBJ = \
src/main.o
EXAMPLES = \
example/basic.o
example/basic \
example/riemman \
example/n-cube \
example/n-hilbert-cube \
example/light-cone \
example/lens
CFLAGS = \
-I./ext/cglm/include \
@@ -21,17 +26,6 @@ CFLAGS = \
-I./include \
-Wall -Wno-unused-function -std=c99 -D_GNU_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
help:
@echo "Usage:"
@echo " $(MAKE) windows"
@@ -39,14 +33,16 @@ help:
@echo " $(MAKE) linux-wayland"
@echo " $(MAKE) cocoa"
@echo " $(MAKE) CC=emcc wasm"
@echo "Examples"
@echo " $(MAKE) examples"
@echo "Clean"
@echo " $(MAKE) clean"
src/main.o: src/data/shaders.h
windows: $(OBJ)
cd ext; $(MAKE) -f glfw.mk windows; cd -
$(CC) $(CFLAGS) $(OBJ) -o $(BIN) -L. -lglfw -lopengl32
#cd ext; $(MAKE) -f glfw.mk windows; cd -
$(CC) $(CFLAGS) $(OBJ) -o $(BIN) -L. -lglfw -lopengl32 -lgdi32
glfw.dll:
$(CC) -fPIC -shared -D_GLFW_WIN32 -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o $@ -lgdi32
@@ -54,11 +50,11 @@ glfw.dll:
# LINUX
linux-wayland: $(OBJ)
cd ext; $(MAKE) -f glfw.mk linux-wayland; cd -
$(CC) -o $(BIN) $(OBJ) ext/libglfw.a -lGL -lm
$(CC) -o $(BIN) $(OBJ) ext/libglfw.a -lm
linux-x11: $(OBJ)
cd ext; $(MAKE) -f glfw.mk linux-x11; cd -
$(CC) -o $(BIN) $(OBJ) ext/libglfw.a -lGL -lm
$(CC) -o $(BIN) $(OBJ) ext/libglfw.a -lm
cocoa: $(OBJ)
cd ext; $(MAKE) -f glfw.mk cocoa; cd -
@@ -68,11 +64,8 @@ wasm: $(OBJ)
$(CC) -sUSE_WEBGL2=1 -sUSE_GLFW=3 -o $(BIN).html $(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)
rm $(OBJ) $(BIN) $(EXAMPLES)
cd ext; $(MAKE) -f glfw.mk clean; cd -
@@ -82,3 +75,6 @@ examples: $(EXAMPLES)
.c.o:
$(CC) -Wno-implicit-function-declaration $(CFLAGS) -c -o $@ $<
.c:
$(CC) -I include -o $@ $< -lm

View File

@@ -35,6 +35,9 @@ make cocoa
make CC=emcc wasm
```
# Flow of Manigraph
![Flow of Manigraph](doc/flow.svg)
# Design
Manigraph is a program designed with sustainability in mind. It is built to be
portable, resource-efficient, and easy to maintain and scale. The program
@@ -56,3 +59,4 @@ they are connected through dependencies. It also highlights the scenarios where
files are not independent of one another,
![File layout](doc/file_layout.svg)

10
doc/flow.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 107 KiB

View File

@@ -34,7 +34,7 @@ void mobius(float *d_surface, int *coord, unsigned char *grid)
void torus(float *d_surface, int *coord, unsigned char *grid)
{
float u = (2 * M_PI) * ((float)coord[0] / grid[0]);
float u = (M_PI) * ((float)coord[0] / grid[0]) + M_PI;
float v = (2 * M_PI) * ((float)coord[1] / grid[1]);
d_surface[0] = (1 + 0.5 * cos(v)) * cos(u);
@@ -57,12 +57,14 @@ int main(void)
{
unsigned char i = 0;
const char *file_name[] = {"mobius.klein", "torus.klein", "klein.klein"};
struct parm parametrization[] = {{
.grid = (unsigned char[]){16, 4},
.m = 2,
.n = 3,
.f = mobius,
},
struct parm parametrization[] = {
{
.grid = (unsigned char[]){16, 4},
.m = 2,
.n = 3,
.f = mobius,
},
{
.grid = (unsigned char[]){16, 8},
.m = 2,
@@ -74,9 +76,10 @@ int main(void)
.m = 2,
.n = 4,
.f = klein,
}};
},
};
for (i = 0; i < 3; ++i)
for (i = 0; i < 4; ++i)
{
struct klein klein;
printf("writing %s\n", file_name[i]);

View File

@@ -7,7 +7,7 @@
#include <klein/parm.h>
int p = 37;
int q = 1;
int q = 7;
void lens(float *d_surface, int *coord, unsigned char * grid)
{
@@ -41,7 +41,7 @@ int main(void)
.f = lens,
};
snprintf(file_name, 0xff, "%03d-%03d-cube.klein", p, q);
snprintf(file_name, 0xff, "lens-%03d-%03d.klein", p, q);
printf("writing %s\n", file_name);
klein_parametrize(&klein, parametrization);

View File

@@ -10,7 +10,7 @@
#define M_PI 3.14159265358979323846
#endif
unsigned char dim = 4;
unsigned char dim = 17;
void cube(float *d_surface, int *coord, unsigned char *grid)
{
@@ -35,11 +35,11 @@ int main(void)
};
parametrization.grid = malloc(dim);
for (i = 0; i < dim; ++i)
parametrization.grid[i] = 1 << i;
parametrization.grid[i] = 1 + i;
snprintf(file_name, 0xff, "%03d-cube.klein", dim);
snprintf(file_name, 0xff, "cube-%03d.klein", dim);
printf("writing %s\n", file_name);
klein_parametrize(&klein, parametrization);

View File

@@ -3,6 +3,8 @@
#endif
#define KLEIN_H
#include <stdint.h>
#ifdef KLEIN_IMPLEMENT
#include <stdio.h>
#include <stdlib.h>
@@ -10,9 +12,9 @@
struct klein
{
unsigned long vertex_size;
float *vertex, *normals;
unsigned char dim;
uint64_t vertex_size;
uint8_t dim;
};
/*
@@ -39,8 +41,8 @@ int klein_export_file(struct klein klein, const char * filename)
fwrite("\0", 1, 1, file);
fwrite(&klein.dim, 1, 1, file);
fwrite(&klein.vertex_size, 8, 1, file);
fwrite(klein.vertex, 16, klein.vertex_size * klein.dim, file);
fwrite(klein.normals, 16, klein.vertex_size * klein.dim, file);
fwrite(klein.vertex, 4, klein.vertex_size * klein.dim, file);
fwrite(klein.normals, 4, klein.vertex_size * klein.dim, file);
fclose(file);
return 0;

View File

@@ -25,7 +25,7 @@ void klein_parametrize( struct klein * klein, struct parm parm );
#include <assert.h>
#endif
static inline int __factorial(int n)
static inline uint64_t __factorial(uint64_t n)
{
if (n == 1)
return 1;
@@ -33,7 +33,7 @@ static inline int __factorial(int n)
return n * __factorial(n - 1);
}
static inline int __face(int n)
static inline uint64_t __face(int n)
{
if (n == 2)
return 1;
@@ -43,8 +43,8 @@ static inline int __face(int n)
void klein_parametrize( struct klein * klein, struct parm parm)
{
unsigned int i, j, k, o, p, n;
unsigned long size, q = 0;
unsigned long i, j, o, p, n;
uint64_t k, size, q = 0;
int *face;
#ifdef TEST
@@ -56,28 +56,30 @@ void klein_parametrize( struct klein * klein, struct parm parm)
klein->dim = parm.n;
klein->vertex_size = 0;
{
unsigned char test = 0;
uint64_t test = 0;
for (o = 0; o < parm.m; o++)
{
for (p = 0; p < o; p++)
{
test += 1;
klein->vertex_size += parm.grid[p] * parm.grid[o] * 6 * __face(parm.m);
}
}
klein->vertex_size /= test;
for (o = 0; o < parm.m; o++)
for (p = 0; p < o; p++)
klein->vertex_size += (uint64_t)parm.grid[p] * parm.grid[o] * 6 * __face(parm.m)/test;
}
size = (klein->dim) * (klein->vertex_size);
klein->vertex = malloc(size * sizeof(float));
size = klein->vertex_size*klein->dim;
klein->vertex = malloc(size * sizeof(float));
face = malloc(parm.m * sizeof(int));
for (o = 0; o < parm.m; o++)
{
for (p = 0; p < o; p++)
{
for (k = 0; k < (1 << (parm.m - 2)); k++)
for (k = 0; k < ((uint64_t)1 << (parm.m - 2)); k++)
{
unsigned char skip = 0;
for (n = 0; n < parm.m; n++)
@@ -85,7 +87,7 @@ void klein_parametrize( struct klein * klein, struct parm parm)
if (n == o || n == p)
skip++;
face[n] = (k & (1 << (n - skip))) ? parm.grid[n] : 0;
face[n] = (k & ((uint64_t)1 << (n - skip))) ? parm.grid[n] : 0;
}
for (i = 0; i < parm.grid[p]; i++)

View File

@@ -61,7 +61,7 @@ const char *fs_plain =
"void main()"
"{"
" FragColor = vec4(pow(vec3(Color),vec3(1.0/2.2)),Color.a);"
" FragColor = vec4(pow(vec3(Color),vec3(1.0/1.0)),Color.a);"
"}";
const char *fs =
@@ -92,7 +92,7 @@ const char *fs =
" float specular = pow(abs(dot(normalize(Normal), halfwayDir)), 32.0);\n"
" float diffuse = abs(dot(normalize(Normal), lightDir));\n"
" vec3 result = pow((0.5 + 0.5*diffuse + 1.5*specular) * Color.rgb, "
" vec3 result = pow((0.5 + 0.5*diffuse + 1*specular) * Color.rgb, "
"vec3(1.0/2.2));\n"
" FragColor = vec4(result, Color.a);\n"
"}";

View File

@@ -1,4 +1,5 @@
#include "main.h"
#include <glad.h>
#include <GLFW/glfw3.h>
#include <cglm/quat.h>
#include <stdio.h>
@@ -9,9 +10,9 @@ unsigned char selected_axis = 0;
int window_width;
int window_height;
static versor q = GLM_QUAT_IDENTITY_INIT;
unsigned char animate_index = 0;
versor q = GLM_QUAT_IDENTITY_INIT;
vec3 axis[3] = {
{1, 0, 0},
@@ -21,6 +22,23 @@ vec3 axis[3] = {
extern struct projection projection;
static inline
void __input_update_q(versor p)
{
glm_quat_mul(p, 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_normalize(q);
}
void __error_callback_input(int x, const char * msg )
{
mlog("[GLFW] ");
mlog(msg);
mlog("\n");
}
void __key_callback_input(
GLFWwindow *window, int key, int scancode, int action, int mods)
{
@@ -42,7 +60,7 @@ void __key_callback_input(
if (selected_coord == projection.z)
return;
selected_coord = projection.w;
projection.w = selected_coord;
}
if (projection.w >= projection.m)
@@ -51,27 +69,30 @@ void __key_callback_input(
switch (key)
{
unsigned char tmp;
case GLFW_KEY_I:
case GLFW_KEY_Z:
tmp = projection.w;
projection.w = projection.x;
projection.x = tmp;
animate_index = 1;
break;
case GLFW_KEY_O:
case GLFW_KEY_X:
tmp = projection.w;
projection.w = projection.y;
projection.y = tmp;
animate_index = 2;
break;
case GLFW_KEY_P:
case GLFW_KEY_C:
tmp = projection.w;
projection.w = projection.z;
projection.z = tmp;
animate_index = 3;
break;
default:
return;
}
printf("[AXIS] (X,Y,Z,W) = (%d,%d,%d,%d)\n", projection.x, projection.y, projection.z, projection.w);
return;
}
@@ -86,26 +107,32 @@ void __window_callback_input(GLFWwindow *window, int w, int h)
glViewport((w - m) / 2, (h - m) / 2, m, m);
}
void __mouse_callback_input(
GLFWwindow *window, int button, int action, int mods)
static double xpos_s, ypos_s;
static char lbutton_down = 0;
void __mouse_callback_input(GLFWwindow* window, int button, int action, int mods)
{
unsigned char green_value;
double xpos, ypos;
if( button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
lbutton_down = 1;
if (button != GLFW_MOUSE_BUTTON_LEFT || action != GLFW_PRESS)
return;
glfwGetCursorPos(window, &xpos, &ypos);
glReadPixels((int)xpos, (int)(window_height - ypos), 1, 1, GL_GREEN,
GL_UNSIGNED_BYTE, &green_value);
switch (green_value)
if( button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE)
{
case 0xD3:
case 0xD4:
case 0xD5:
selected_axis = green_value - 0xD3;
glfwGetCursorPos(window, &xpos_s, &ypos_s);
lbutton_down = 0;
}
}
void __cursor_callback_input(
GLFWwindow *window, double xpos, double ypos)
{
if( lbutton_down )
{
versor p = GLM_QUAT_IDENTITY_INIT;
glm_quatv(p, ANGLE, (vec3){-1*(ypos-ypos_s),(xpos-xpos_s),0});
__input_update_q(p);
}
}
void __scroll_callback_input(GLFWwindow *window, double xoffset, double yoffset)
@@ -149,51 +176,69 @@ quat_t poll_input(window_t window)
{
versor p = GLM_QUAT_IDENTITY_INIT;
if (glfwGetKey((GLFWwindow *)window, 'Q') == GLFW_PRESS)
if (glfwGetKey((GLFWwindow *)window, 'W') == GLFW_PRESS)
{
glm_quatv(p, ANGLE, axis[0]);
goto end;
}
if (glfwGetKey((GLFWwindow *)window, 'W') == GLFW_PRESS)
if (glfwGetKey((GLFWwindow *)window, 'S') == GLFW_PRESS)
{
glm_quatv(p, -ANGLE, axis[0]);
goto end;
}
if (glfwGetKey((GLFWwindow *)window, 'A') == GLFW_PRESS)
{
glm_quatv(p, ANGLE, axis[1]);
goto end;
}
if (glfwGetKey((GLFWwindow *)window, 'S') == GLFW_PRESS)
{
glm_quatv(p, -ANGLE, axis[1]);
goto end;
}
if (glfwGetKey((GLFWwindow *)window, 'Z') == GLFW_PRESS)
if (glfwGetKey((GLFWwindow *)window, 'D') == GLFW_PRESS)
{
glm_quatv(p, ANGLE, axis[1]);
goto end;
}
if (glfwGetKey((GLFWwindow *)window, 'Q') == GLFW_PRESS)
{
glm_quatv(p, ANGLE, axis[2]);
goto end;
}
if (glfwGetKey((GLFWwindow *)window, 'X') == GLFW_PRESS)
if (glfwGetKey((GLFWwindow *)window, 'E') == GLFW_PRESS)
{
glm_quatv(p, -ANGLE, axis[2]);
goto end;
}
end:
glm_quat_mul(p, 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_normalize(q);
if (glfwGetKey((GLFWwindow *)window, GLFW_KEY_UP) == GLFW_PRESS)
{
glm_quatv(p, ANGLE, (vec3){1,0,0});
goto end;
}
if (glfwGetKey((GLFWwindow *)window, GLFW_KEY_DOWN) == GLFW_PRESS)
{
glm_quatv(p, -ANGLE, (vec3){1,0,0});
goto end;
}
if (glfwGetKey((GLFWwindow *)window, GLFW_KEY_RIGHT) == GLFW_PRESS)
{
glm_quatv(p, ANGLE, (vec3){0,1,0});
goto end;
}
if (glfwGetKey((GLFWwindow *)window, GLFW_KEY_LEFT) == GLFW_PRESS)
{
glm_quatv(p, -ANGLE, (vec3){0,1,0});
goto end;
}
end:
/*
// 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");
}
*/
__input_update_q(p);
return q;
}

View File

@@ -1,5 +1,6 @@
#include "main.h"
#include <stdlib.h>
#include <stdio.h>
/*
@@ -7,7 +8,7 @@
5 bytes with KLEIN
1 byte empty for scaling
1 byte with the dimention of the surface
8 bytes interprated as a long with the number of vertex
8 bytes interprated as an int with the number of vertex
n bytes with the vertex data of the surface
n bytes with the normal data of the surface
@@ -35,10 +36,10 @@ int create_surface_klein(unsigned char *path, struct surface *surface)
size = surface->dim * surface->vertex;
surface->data = malloc(16 * size);
fread(surface->data, 16, size, file);
surface->data = malloc(4 * size);
fread(surface->data, 4, size, file);
surface->norm = malloc(16 * size);
fread(surface->norm, 16, size, file);
surface->norm = malloc(4 * size);
fread(surface->norm, 4, size, file);
return 0;
}

View File

@@ -18,6 +18,24 @@
struct projection projection = {.x = 0, .y = 1, .z = 2, .w = 3, .mesh = NULL};
const char *wname = "manigraph: manifold grapher";
const char *input_map =
"\n"
"DRAG AND DROP: Graph a klein file from the file system\n"
"LEFT-CLICK: Rotate surface on DRAG direction\n"
"SCROLL: Rotate surface on SCROLL direction\n"
"\n"
"W,S: Rotate sufrace on X axis\n"
"A,D: Rotate sufrace on Y axis\n"
"Q,E: Rotate sufrace on Z axis\n"
"LEFT-RIGHT: Rotate sufrace Horizontally\n"
"UP-DOWN: Rotate sufrace Vetrically\n"
"\n"
"Z: Rotate W axis to X axis\n"
"X: Rotate W axis to Y axis\n"
"C: Rotate W axis to Z axis\n"
"1-9: Rotate n axis to W axis\n"
"\n"
;
unsigned char color[4] = {0x2F, 0x3C, 0x7E, 0xff};
@@ -129,7 +147,11 @@ int main(void)
fix_matrix_load(shader_plain, (float)WIDTH / HEIGHT);
}
mlog("[INPUTS] Imprimiendo...\n");
printf(input_map);
mlog("[MAIN LOOP] Inicializando...\n");
#ifdef EMSCRIPTEN
emscripten_set_main_loop(&main_loop, 60, 1);
return 0;

View File

@@ -4,7 +4,9 @@
error of the shaders.
*/
#include <stdint.h>
#define DEBUG
#define GLFW_INCLUDE_NONE
typedef const void *window_t;
typedef unsigned int id_t;
@@ -40,8 +42,8 @@ struct projection
struct surface
{
float *data, *norm;
unsigned long vertex;
unsigned char dim;
uint64_t vertex;
uint8_t dim;
};
/*

View File

@@ -106,6 +106,7 @@ void draw_mesh_lines(id_t shader, mesh_t p)
{
struct obj *obj = p;
glLineWidth(4);
glUseProgram(shader);
glBindVertexArray(obj->vao);
#ifndef EMSCRIPTEN

View File

@@ -4,10 +4,12 @@
#include <time.h>
void __window_callback_input(GLFWwindow *, int, int);
void __cursor_callback_input(GLFWwindow *, double, double);
void __mouse_callback_input(GLFWwindow *, int, int, int);
void __scroll_callback_input(GLFWwindow *, double, double);
void __key_callback_input(GLFWwindow *, int, int, int, int);
void __drop_callback_input(GLFWwindow *, int, const char **);
void __error_callback_input(int, const char *);
window_t init_window(unsigned int w, unsigned int h, const char *name);
@@ -33,8 +35,12 @@ static void __limit_fps_window(int max_fps)
struct timespec sleep_time;
sleep_time.tv_sec = 0;
sleep_time.tv_nsec = (long)((frame_time - elapsed_time) * 1e9);
nanosleep(&sleep_time, NULL);
#ifdef _WIN32
usleep(sleep_time.tv_nsec/1000);
#else
nanosleep(&sleep_time, NULL);
#endif
current_time = glfwGetTime();
}
@@ -44,6 +50,7 @@ static void __limit_fps_window(int max_fps)
window_t init_window(unsigned int width, unsigned int height, const char *title)
{
window_t window;
glfwSetErrorCallback(__error_callback_input);
if (!glfwInit())
return NULL;
@@ -51,6 +58,7 @@ window_t init_window(unsigned int width, unsigned int height, const char *title)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
window = (window_t)glfwCreateWindow(width, height, title, NULL, NULL);
if (!(window))
@@ -62,13 +70,12 @@ window_t init_window(unsigned int width, unsigned int height, const char *title)
glfwMakeContextCurrent((GLFWwindow *)(window));
glfwSetWindowSizeCallback((GLFWwindow *)window, __window_callback_input);
glfwSetCursorPosCallback((GLFWwindow *)window, __cursor_callback_input);
glfwSetMouseButtonCallback((GLFWwindow *)window, __mouse_callback_input);
glfwSetScrollCallback((GLFWwindow *)window, __scroll_callback_input);
glfwSetKeyCallback((GLFWwindow *)window, __key_callback_input);
glfwSetDropCallback((GLFWwindow *)window, __drop_callback_input);
__window_callback_input((GLFWwindow *)window, width, height);
return window;
}