10 Commits

Author SHA1 Message Date
PedroEdiaz
b83f0f07e3 Merge main, change compile.bat, add struct gui 2025-02-04 10:16:09 -06:00
PedroEdiaz
52aa9d4ac2 Fix size of klein files, debug of glfw 2024-12-15 19:58:58 -06:00
Crisel
bc537cf9f2 ui better 2024-12-10 00:06:17 -06:00
Crisel
81eb79662e 3rd ui 2024-12-02 14:59:47 -06:00
Crisel
d2e2c6eef7 2nd ui colors 2024-12-02 14:39:32 -06:00
Crisel
57091d3408 1rst ui 2024-12-02 14:12:01 -06:00
Crisel
56339e9e10 changes 2024-11-29 15:49:17 -06:00
PedroEdiaz
03796fdaff Fix: OpenGL changes of Nuklear 2024-11-07 14:14:20 -06:00
PedroEdiaz
46869cbcde WIP: Working window that breaks OpenGL settings 2024-11-06 19:27:02 -06:00
Crisel
25df66b5f8 wip2 2024-11-06 15:57:45 -06:00
16 changed files with 273 additions and 205 deletions

View File

@@ -9,6 +9,7 @@ OBJ = \
src/input.o \ src/input.o \
src/load.o \ src/load.o \
src/mesh.o \ src/mesh.o \
src/gui.o \
src/main.o src/main.o
EXAMPLES = \ EXAMPLES = \
@@ -16,13 +17,13 @@ EXAMPLES = \
example/riemman \ example/riemman \
example/n-cube \ example/n-cube \
example/n-hilbert-cube \ example/n-hilbert-cube \
example/light-cone \
example/lens example/lens
CFLAGS = \ CFLAGS = \
-I./ext/cglm/include \ -I./ext/cglm/include \
-I./ext/glfw/include \ -I./ext/glfw/include \
-I./ext/glad \ -I./ext/glad \
-I./ext/nuklear \
-I./include \ -I./include \
-Wall -Wno-unused-function -std=c99 -D_GNU_SOURCE \ -Wall -Wno-unused-function -std=c99 -D_GNU_SOURCE \
@@ -41,8 +42,8 @@ help:
src/main.o: src/data/shaders.h src/main.o: src/data/shaders.h
windows: $(OBJ) windows: $(OBJ)
#cd ext; $(MAKE) -f glfw.mk windows; cd - cd ext; $(MAKE) -f glfw.mk windows; cd -
$(CC) $(CFLAGS) $(OBJ) -o $(BIN) -L. -lglfw -lopengl32 -lgdi32 $(CC) $(CFLAGS) $(OBJ) -o $(BIN) -L. -lglfw -lopengl32
glfw.dll: glfw.dll:
$(CC) -fPIC -shared -D_GLFW_WIN32 -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o $@ -lgdi32 $(CC) -fPIC -shared -D_GLFW_WIN32 -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o $@ -lgdi32

View File

@@ -1,3 +1,3 @@
:: git submodule update --init --recursive :: git submodule update --init --recursive
:: gcc -fPIC -shared -Iext/glfw/deps/MinGW -D_GLFW_WIN32 -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o glfw.dll -lgdi32 :: gcc -fPIC -shared -Iext/glfw/deps/MinGW -D_GLFW_WIN32 -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o glfw.dll -lgdi32
gcc -I ext/cglm/include -I ext/glfw/include src/*.c -o manigraph -L . -lglfw -lopengl32 -lglew32 gcc -I ext/glad -I ext/cglm/include -I ext/nuklear -I ext/glfw/include src/*.c ext/glad/glad.c -o manigraph -L . -lglfw -lopengl32

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

View File

@@ -10,7 +10,7 @@
#define M_PI 3.14159265358979323846 #define M_PI 3.14159265358979323846
#endif #endif
unsigned char dim = 17; unsigned char dim = 10;
void cube(float *d_surface, int *coord, unsigned char *grid) void cube(float *d_surface, int *coord, unsigned char *grid)
{ {

View File

@@ -3,8 +3,6 @@
#endif #endif
#define KLEIN_H #define KLEIN_H
#include <stdint.h>
#ifdef KLEIN_IMPLEMENT #ifdef KLEIN_IMPLEMENT
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -12,9 +10,9 @@
struct klein struct klein
{ {
unsigned long vertex_size;
float *vertex, *normals; float *vertex, *normals;
uint64_t vertex_size; unsigned char dim;
uint8_t dim;
}; };
/* /*

View File

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

View File

@@ -9,7 +9,6 @@
void set_clean_color_context(unsigned char r, unsigned char g, unsigned char b) 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); glClearColor((float)r / 0xff, (float)g / 0xff, (float)b / 0xff, 1.0);
} }
@@ -22,4 +21,11 @@ int init_context(void)
#endif #endif
} }
void clean_context(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } void clean_context(void)
{
/* This is important cos, Nuklear Disable DEPTH TEST */
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT );
}

32
src/data/cube.h Normal file
View File

@@ -0,0 +1,32 @@
#define A -1,-1,-1,
#define B -1,-1, 1,
#define C -1, 1,-1,
#define D -1, 1, 1,
#define E 1,-1,-1,
#define F 1,-1, 1,
#define G 1, 1,-1,
#define H 1, 1, 1,
narray_float_t d_cube =
{
3*3*2*6,
A C E
C E G
E G F
G F H
F H B
H B D
B D A
D A C
C D G
D G H
A B E
B E F
};

83
src/gui.c Normal file
View File

@@ -0,0 +1,83 @@
#include <glad.h>
#include "main.h"
#define MAX_VERTEX_BUFFER 512 * 1024
#define MAX_ELEMENT_BUFFER 128 * 1024
#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_IMPLEMENTATION
#define NK_GLFW_GL3_IMPLEMENTATION
#include <nuklear.h>
#include <demo/glfw_opengl3/nuklear_glfw_gl3.h>
struct gui gui =
{
.file_name = {0},
.projection = (struct projection){ .mesh=NULL, .x =0, .y=1, .z=2 },
};
float surface_color[4] = {(float)0x2F/0xFF, (float)0x3C/0xFF, (float)0x7E/0xFF, (float)0xff/0xFF};
static inline
void restore_surface_color( void )
{
gui.picked_color[0] = surface_color[0];
gui.picked_color[1] = surface_color[1];
gui.picked_color[2] = surface_color[2];
gui.picked_color[3] = surface_color[3];
}
struct nk_glfw glfw ={0};
struct nk_context * context;
void draw_gui( void )
{
nk_glfw3_new_frame(&glfw);
if (nk_begin(context, "MANIGRAPH", nk_rect(10, 10, 256, 256),
NK_WINDOW_BORDER |NK_WINDOW_SCALABLE | NK_WINDOW_MOVABLE | NK_WINDOW_MINIMIZABLE) )
{
/* Color picker para personalización */
{
struct nk_colorf * p = (void*)&gui.picked_color;
nk_layout_row_dynamic(context, 120, 1);
*p = nk_color_picker(context, *p, NK_RGBA);
}
nk_layout_row_dynamic(context, 20, 1);
if (nk_button_label(context, "Restablecer"))
restore_surface_color();
nk_layout_row_dynamic(context, 12, 1);
nk_label(context, (char*)gui.file_name, NK_TEXT_ALIGN_LEFT);
nk_label_colored(context, "Atajos de teclado:", NK_TEXT_ALIGN_LEFT, nk_rgb(255, 255, 255)); // Encabezado
nk_label(context, " - W/S: Rotar eje X", NK_TEXT_ALIGN_LEFT);
nk_label(context, " - A/D: Rotar eje Y", NK_TEXT_ALIGN_LEFT);
nk_label(context, " - Z/X: Rotar eje Z", NK_TEXT_ALIGN_LEFT);
}
nk_end(context);
nk_glfw3_render(&glfw, NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
}
void init_gui( window_t window )
{
struct nk_font_atlas * atlas;
restore_surface_color();
{
context = nk_glfw3_init(&glfw,(GLFWwindow*) window, NK_GLFW3_DEFAULT );
nk_glfw3_font_stash_begin(&glfw, &atlas);
nk_glfw3_font_stash_end(&glfw);
}
}

View File

@@ -3,16 +3,18 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <cglm/quat.h> #include <cglm/quat.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#define ANGLE ((float)0x01 / 0xff * 2 * GLM_PI) #define ANGLE ((float)0x01 / 0xff * 2 * GLM_PI)
unsigned char selected_axis = 0; unsigned char selected_axis = 0;
int window_width; int window_width = 512;
int window_height; int window_height = 512;
static versor q = GLM_QUAT_IDENTITY_INIT;
unsigned char animate_index = 0; unsigned char animate_index = 0;
extern struct gui gui;
versor q = GLM_QUAT_IDENTITY_INIT;
vec3 axis[3] = { vec3 axis[3] = {
{1, 0, 0}, {1, 0, 0},
@@ -20,17 +22,6 @@ vec3 axis[3] = {
{0, 0, 1}, {0, 0, 1},
}; };
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 ) void __error_callback_input(int x, const char * msg )
{ {
@@ -51,48 +42,45 @@ void __key_callback_input(
selected_coord = key - GLFW_KEY_0 - 1; selected_coord = key - GLFW_KEY_0 - 1;
if (selected_coord >= projection.m) if (selected_coord >= gui.projection.m)
return; return;
if (selected_coord == projection.x) if (selected_coord == gui.projection.x)
return; return;
if (selected_coord == projection.y) if (selected_coord == gui.projection.y)
return; return;
if (selected_coord == projection.z) if (selected_coord == gui.projection.z)
return; return;
projection.w = selected_coord; selected_coord = gui.projection.w;
} }
if (projection.w >= projection.m) if (gui.projection.w >= gui.projection.m)
return; return;
switch (key) switch (key)
{ {
unsigned char tmp; unsigned char tmp;
case GLFW_KEY_Z: case GLFW_KEY_I:
tmp = projection.w; tmp = gui.projection.w;
projection.w = projection.x; gui.projection.w = gui.projection.x;
projection.x = tmp; gui.projection.x = tmp;
animate_index = 1; animate_index = 1;
break; break;
case GLFW_KEY_X: case GLFW_KEY_O:
tmp = projection.w; tmp = gui.projection.w;
projection.w = projection.y; gui.projection.w = gui.projection.y;
projection.y = tmp; gui.projection.y = tmp;
animate_index = 2; animate_index = 2;
break; break;
case GLFW_KEY_C: case GLFW_KEY_P:
tmp = projection.w; tmp = gui.projection.w;
projection.w = projection.z; gui.projection.w = gui.projection.z;
projection.z = tmp; gui.projection.z = tmp;
animate_index = 3; animate_index = 3;
break; break;
default:
return;
} }
printf("[AXIS] (X,Y,Z,W) = (%d,%d,%d,%d)\n", projection.x, projection.y, projection.z, projection.w);
return; return;
} }
@@ -107,32 +95,26 @@ void __window_callback_input(GLFWwindow *window, int w, int h)
glViewport((w - m) / 2, (h - m) / 2, m, m); glViewport((w - m) / 2, (h - m) / 2, m, m);
} }
static double xpos_s, ypos_s; void __mouse_callback_input(
static char lbutton_down = 0; GLFWwindow *window, int button, int action, int mods)
void __mouse_callback_input(GLFWwindow* window, int button, int action, int mods)
{ {
if( button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) unsigned char green_value;
lbutton_down = 1; double xpos, ypos;
if( button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) 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)
{ {
glfwGetCursorPos(window, &xpos_s, &ypos_s); case 0xD3:
lbutton_down = 0; case 0xD4:
case 0xD5:
selected_axis = green_value - 0xD3;
} }
}
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) void __scroll_callback_input(GLFWwindow *window, double xoffset, double yoffset)
@@ -155,18 +137,20 @@ void __scroll_callback_input(GLFWwindow *window, double xoffset, double yoffset)
glm_quat_rotatev(r, axis[2], axis[2]); glm_quat_rotatev(r, axis[2], axis[2]);
} }
void __drop_callback_input(GLFWwindow *window, int count, char **path) void __drop_callback_input(GLFWwindow *window, int count, unsigned char **path)
{ {
struct surface surface; struct surface surface;
if (create_surface_klein(*path, &surface)) if (create_surface_klein(*path, &surface))
return; return;
if (!(projection.mesh = create_mesh(surface))) if (!(gui.projection.mesh = create_mesh(surface)))
return; return;
projection.m = surface.dim; strcpy(gui.file_name, *path);
set_projection_mesh(projection);
gui.projection.m = surface.dim;
set_projection_mesh(gui.projection);
free(surface.norm); free(surface.norm);
free(surface.data); free(surface.data);
@@ -176,69 +160,43 @@ quat_t poll_input(window_t window)
{ {
versor p = GLM_QUAT_IDENTITY_INIT; versor p = GLM_QUAT_IDENTITY_INIT;
if (glfwGetKey((GLFWwindow *)window, 'W') == GLFW_PRESS) if (glfwGetKey((GLFWwindow *)window, 'Q') == GLFW_PRESS)
{ {
glm_quatv(p, ANGLE, axis[0]); glm_quatv(p, ANGLE, axis[0]);
goto end; goto end;
} }
if (glfwGetKey((GLFWwindow *)window, 'S') == GLFW_PRESS) if (glfwGetKey((GLFWwindow *)window, 'W') == GLFW_PRESS)
{ {
glm_quatv(p, -ANGLE, axis[0]); glm_quatv(p, -ANGLE, axis[0]);
goto end; goto end;
} }
if (glfwGetKey((GLFWwindow *)window, 'A') == GLFW_PRESS) if (glfwGetKey((GLFWwindow *)window, 'A') == GLFW_PRESS)
{
glm_quatv(p, -ANGLE, axis[1]);
goto end;
}
if (glfwGetKey((GLFWwindow *)window, 'D') == GLFW_PRESS)
{ {
glm_quatv(p, ANGLE, axis[1]); glm_quatv(p, ANGLE, axis[1]);
goto end; goto end;
} }
if (glfwGetKey((GLFWwindow *)window, 'Q') == GLFW_PRESS) if (glfwGetKey((GLFWwindow *)window, 'S') == GLFW_PRESS)
{
glm_quatv(p, -ANGLE, axis[1]);
goto end;
}
if (glfwGetKey((GLFWwindow *)window, 'Z') == GLFW_PRESS)
{ {
glm_quatv(p, ANGLE, axis[2]); glm_quatv(p, ANGLE, axis[2]);
goto end; goto end;
} }
if (glfwGetKey((GLFWwindow *)window, 'E') == GLFW_PRESS) if (glfwGetKey((GLFWwindow *)window, 'X') == GLFW_PRESS)
{ {
glm_quatv(p, -ANGLE, axis[2]); glm_quatv(p, -ANGLE, axis[2]);
goto end; goto end;
} }
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: end:
/* glm_quat_mul(p, q, q);
// LOG INFO glm_quat_rotatev(p, axis[0], axis[0]);
if (0) glm_quat_rotatev(p, axis[1], axis[1]);
{ glm_quat_rotatev(p, axis[2], axis[2]);
printf("QUAT: %2.5f %2.5f %2.5f %2.5f\n", q[0], q[1], q[2], q[3]); glm_quat_normalize(q);
printf("\n");
}
*/
__input_update_q(p);
return q; return q;
} }

View File

@@ -1,6 +1,5 @@
#include "main.h" #include "main.h"
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
/* /*
@@ -8,7 +7,7 @@
5 bytes with KLEIN 5 bytes with KLEIN
1 byte empty for scaling 1 byte empty for scaling
1 byte with the dimention of the surface 1 byte with the dimention of the surface
8 bytes interprated as an int with the number of vertex 8 bytes interprated as a long with the number of vertex
n bytes with the vertex data of the surface n bytes with the vertex data of the surface
n bytes with the normal data of the surface n bytes with the normal data of the surface

View File

@@ -22,13 +22,7 @@ void rot_matrix_load(id_t shader, quat_t q)
load_mat4_to_shader(shader, "rot", (mat4_t)m); load_mat4_to_shader(shader, "rot", (mat4_t)m);
} }
void color_load(id_t shader, unsigned char color[4]) void color_load(id_t shader, float res[4])
{ {
float res[4];
res[0] = (float)color[0] / 0xff;
res[1] = (float)color[1] / 0xff;
res[2] = (float)color[2] / 0xff;
res[3] = (float)color[3] / 0xff;
load_float4_to_shader(shader, "color", res); load_float4_to_shader(shader, "color", res);
} }

View File

@@ -7,6 +7,11 @@
#define WIDTH 512 #define WIDTH 512
#define HEIGHT 512 #define HEIGHT 512
#include <glad.h>
float *generate_data_surface(unsigned int, unsigned char *);
float *generate_normals_surface(float *, unsigned char);
#ifdef EMSCRIPTEN #ifdef EMSCRIPTEN
#include <emscripten.h> #include <emscripten.h>
#endif #endif
@@ -15,29 +20,13 @@
#define M_PI 3.14159 #define M_PI 3.14159
#endif #endif
struct projection projection = {.x = 0, .y = 1, .z = 2, .w = 3, .mesh = NULL}; window_t window;
id_t shader, shader_plain;
const char *wname = "manigraph: manifold grapher"; 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}; extern volatile unsigned char animate_index;
extern struct gui gui;
void mlog(char *msg) void mlog(char *msg)
{ {
@@ -46,11 +35,6 @@ void mlog(char *msg)
#endif #endif
} }
window_t window;
id_t shader, shader_plain;
extern volatile unsigned char animate_index;
#ifndef EMSCRIPTEN #ifndef EMSCRIPTEN
static inline static inline
#endif #endif
@@ -62,10 +46,16 @@ static inline
q = poll_input(window); q = poll_input(window);
gui.quat[0] = q[0];
gui.quat[1] = q[1];
gui.quat[2] = q[2];
gui.quat[3] = q[3];
rot_matrix_load(shader, q); rot_matrix_load(shader, q);
rot_matrix_load(shader_plain, q); rot_matrix_load(shader_plain, q);
color_load(shader, color); color_load(shader, gui.picked_color);
color_load(shader_plain, color); color_load(shader_plain, gui.picked_color);
{ {
static float angle = 0; static float angle = 0;
@@ -77,7 +67,7 @@ static inline
load_float_to_shader(shader, "angle", angle); load_float_to_shader(shader, "angle", angle);
load_float_to_shader(shader_plain, "angle", angle); load_float_to_shader(shader_plain, "angle", angle);
set_projection_mesh(projection); set_projection_mesh(gui.projection);
} }
if (animate_index) if (animate_index)
@@ -92,16 +82,19 @@ static inline
} }
clean_context(); clean_context();
if (!projection.mesh)
return;
draw_mesh(shader, projection.mesh); if (gui.projection.mesh)
draw_mesh_lines(shader_plain, projection.mesh); {
draw_mesh(shader, gui.projection.mesh);
draw_mesh_lines(shader_plain, gui.projection.mesh);
}
draw_gui();
} }
int main(void) int main(void)
{ {
mlog("[VENTANA] Inicializando...\n"); mlog("[VENTANA] Inicializando...\n");
{ {
if (!(window = init_window(WIDTH, HEIGHT, wname))) if (!(window = init_window(WIDTH, HEIGHT, wname)))
@@ -147,11 +140,12 @@ int main(void)
fix_matrix_load(shader_plain, (float)WIDTH / HEIGHT); fix_matrix_load(shader_plain, (float)WIDTH / HEIGHT);
} }
mlog("[INPUTS] Imprimiendo...\n"); mlog("[GUI] Inicializando...\n");
printf(input_map); {
init_gui(window);
}
mlog("[MAIN LOOP] Inicializando...\n"); mlog("[MAIN LOOP] Inicializando...\n");
#ifdef EMSCRIPTEN #ifdef EMSCRIPTEN
emscripten_set_main_loop(&main_loop, 60, 1); emscripten_set_main_loop(&main_loop, 60, 1);
return 0; return 0;
@@ -162,7 +156,7 @@ int main(void)
mlog("[MAIN LOOP] Terminando...\n"); mlog("[MAIN LOOP] Terminando...\n");
mlog("[MESH] Destruyendo...\n"); mlog("[MESH] Destruyendo...\n");
destroy_mesh(projection.mesh); destroy_mesh(gui.projection.mesh);
mlog("[SHADER] Destruyendo...\n"); mlog("[SHADER] Destruyendo...\n");
destroy_shader(shader_plain); destroy_shader(shader_plain);
mlog("[SHADER] Destruyendo...\n"); mlog("[SHADER] Destruyendo...\n");
@@ -172,12 +166,12 @@ int main(void)
return 0; return 0;
error_context: error_context:
mlog("[SHADER] Destruyendo...\n");
destroy_shader(shader_plain);
error_shader_plain:
mlog("[SHADER] Destruyendo...\n"); mlog("[SHADER] Destruyendo...\n");
destroy_shader(shader); destroy_shader(shader);
error_shader: error_shader:
mlog("[SHADER] Destruyendo...\n");
destroy_shader(shader_plain);
error_shader_plain:
mlog("[WINDOW] Destruyendo...\n"); mlog("[WINDOW] Destruyendo...\n");
close_window(window); close_window(window);
error_window: error_window:

View File

@@ -4,7 +4,6 @@
error of the shaders. error of the shaders.
*/ */
#include <stdint.h>
#define DEBUG #define DEBUG
#define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_NONE
@@ -24,12 +23,19 @@ typedef float *mat4_t;
w: the coordanate of the w axis. w: the coordanate of the w axis.
*/ */
struct projection struct gui
{ {
mesh_t mesh; float quat[4];
unsigned char m, x, y, z, w; float picked_color[4];
char file_name[0xff];
struct projection
{
mesh_t mesh;
unsigned char m, x, y, z, w;
} projection;
}; };
/* /*
this structure has all the information to generate this structure has all the information to generate
a mesh, where: a mesh, where:
@@ -42,8 +48,8 @@ struct projection
struct surface struct surface
{ {
float *data, *norm; float *data, *norm;
uint64_t vertex; unsigned long vertex;
uint8_t dim; unsigned char dim;
}; };
/* /*
@@ -165,7 +171,7 @@ void rot_matrix_load(id_t shader, quat_t q);
/* /*
*/ */
void color_load(id_t shader, unsigned char color[4]); void color_load(id_t shader, float* color);
id_t config_texture(unsigned short type); id_t config_texture(unsigned short type);
@@ -181,3 +187,6 @@ void destroy_texture(id_t texture);
id_t create_palette_texture(const unsigned char colors[][4], unsigned char n); id_t create_palette_texture(const unsigned char colors[][4], unsigned char n);
quat_t poll_input(window_t window); quat_t poll_input(window_t window);
void draw_gui( void );
void init_gui( window_t window );

View File

@@ -106,12 +106,12 @@ void draw_mesh_lines(id_t shader, mesh_t p)
{ {
struct obj *obj = p; struct obj *obj = p;
glLineWidth(4);
glUseProgram(shader); glUseProgram(shader);
glBindVertexArray(obj->vao); glBindVertexArray(obj->vao);
#ifndef EMSCRIPTEN #ifndef EMSCRIPTEN
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawArrays(GL_TRIANGLES, 0, obj->vertex); glDrawArrays(GL_TRIANGLES, 0, obj->vertex);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
#else #else
glDrawArrays(GL_LINES, 0, obj->vertex); glDrawArrays(GL_LINES, 0, obj->vertex);
#endif #endif

View File

@@ -4,7 +4,6 @@
#include <time.h> #include <time.h>
void __window_callback_input(GLFWwindow *, int, int); void __window_callback_input(GLFWwindow *, int, int);
void __cursor_callback_input(GLFWwindow *, double, double);
void __mouse_callback_input(GLFWwindow *, int, int, int); void __mouse_callback_input(GLFWwindow *, int, int, int);
void __scroll_callback_input(GLFWwindow *, double, double); void __scroll_callback_input(GLFWwindow *, double, double);
void __key_callback_input(GLFWwindow *, int, int, int, int); void __key_callback_input(GLFWwindow *, int, int, int, int);
@@ -35,12 +34,8 @@ static void __limit_fps_window(int max_fps)
struct timespec sleep_time; struct timespec sleep_time;
sleep_time.tv_sec = 0; sleep_time.tv_sec = 0;
sleep_time.tv_nsec = (long)((frame_time - elapsed_time) * 1e9); sleep_time.tv_nsec = (long)((frame_time - elapsed_time) * 1e9);
#ifdef _WIN32
usleep(sleep_time.tv_nsec/1000);
#else
nanosleep(&sleep_time, NULL); nanosleep(&sleep_time, NULL);
#endif
current_time = glfwGetTime(); current_time = glfwGetTime();
} }
@@ -70,7 +65,6 @@ window_t init_window(unsigned int width, unsigned int height, const char *title)
glfwMakeContextCurrent((GLFWwindow *)(window)); glfwMakeContextCurrent((GLFWwindow *)(window));
glfwSetWindowSizeCallback((GLFWwindow *)window, __window_callback_input); glfwSetWindowSizeCallback((GLFWwindow *)window, __window_callback_input);
glfwSetCursorPosCallback((GLFWwindow *)window, __cursor_callback_input);
glfwSetMouseButtonCallback((GLFWwindow *)window, __mouse_callback_input); glfwSetMouseButtonCallback((GLFWwindow *)window, __mouse_callback_input);
glfwSetScrollCallback((GLFWwindow *)window, __scroll_callback_input); glfwSetScrollCallback((GLFWwindow *)window, __scroll_callback_input);
glfwSetKeyCallback((GLFWwindow *)window, __key_callback_input); glfwSetKeyCallback((GLFWwindow *)window, __key_callback_input);
@@ -81,8 +75,13 @@ window_t init_window(unsigned int width, unsigned int height, const char *title)
void use_window(window_t window) { glfwMakeContextCurrent((void *)window); } void use_window(window_t window) { glfwMakeContextCurrent((void *)window); }
extern int window_width, window_height;
int is_open_window(window_t window) int is_open_window(window_t window)
{ {
/* Restore window proportions if Nuklear breaks it */
__window_callback_input((GLFWwindow *)window, window_width, window_height);
glfwSwapBuffers((void *)window); glfwSwapBuffers((void *)window);
glfwPollEvents(); glfwPollEvents();