Merge branch 'main' into experimental

This commit is contained in:
PedroEdiaz
2024-11-27 10:58:58 -06:00
10 changed files with 261 additions and 96 deletions

View File

@@ -18,6 +18,17 @@ CFLAGS = \
-I./ext/glad \
-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 "Para compilar el proyecto a tu sistema operativo"
@echo "porfavor usa uno de los siguientes comandos:"
@@ -25,6 +36,7 @@ help:
@echo " $(MAKE) linux-x11"
@echo " $(MAKE) linux-wayland"
@echo " $(MAKE) cocoa"
@echo " $(MAKE) CC=emcc wasm"
@echo "Para limpiar los archivos compilados se puede usar"
@echo " $(MAKE) clean"
@@ -34,6 +46,10 @@ 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)
cd ext; $(MAKE) -f glfw.mk linux-wayland; cd -
$(CC) -o $(BIN) $(OBJ) ext/libglfw.a -lGL -lm
@@ -46,6 +62,12 @@ cocoa: $(OBJ)
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)
libglfw.so:
$(CC) -fPIC -shared $(DFLAGS) -D_GLFW_BUILD_DLL -Iext/glfw/deps/wayland ./ext/glfw/src/*.c -o $@
clean:
rm $(OBJ) $(BIN)
cd ext; $(MAKE) -f glfw.mk clean; cd -

View File

@@ -1,27 +1,35 @@
#include "main.h"
#ifdef EMSCRIPTEN
#include <GL/gl.h>
#else
#ifdef GLAD
#include <glad.h>
#include <GLFW/glfw3.h>
#include <glad.h>
#else
#include <GL/glew.h>
#endif
#endif
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);
}
int init_context(void)
{
#ifdef EMSCRIPTEN
return 1;
#else
#ifdef GLAD
return gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
#else
return glewInit() == GLEW_OK;
return glewInit();
#endif
#endif
}
void clean_context(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); }

View File

@@ -1,6 +1,10 @@
const char * vs =
#ifdef EMSCRIPTEN
"#version 300 es\n"
"precision highp float;"
#else
"#version 330 core\n"
#endif
"layout (location = 0) in float aPos_x;"
"layout (location = 1) in float aPos_y;"
"layout (location = 2) in float aPos_z;"
@@ -11,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));"
@@ -32,11 +50,17 @@ const char * vs =
const char * fs_plain =
"#version 330 core\n"
#ifdef EMSCRIPTEN
"#version 300 es\n"
"precision highp float;"
"precision highp sampler2DArray;"
#else
"#version 330 core\n"
#endif
"uniform sampler2DArray palette;"
"in float index;"
"flat in uint index;"
"out vec4 FragColor;"
"in vec3 Normal;"
"in vec3 FragPos;"
@@ -46,27 +70,38 @@ const char * fs_plain =
" FragColor = texture( palette, vec3( 0, 0, index ) ).rgba;"
"}";
const char * fs =
"#version 330 core\n"
#ifdef EMSCRIPTEN
"#version 300 es\n"
"precision highp float;"
"precision highp sampler2DArray;"
#else
"#version 330 core\n"
#endif
"uniform sampler2DArray palette;"
"uniform sampler2DArray palette;"
"in float index;"
"out vec4 FragColor;"
"in vec3 Normal;"
"in vec3 FragPos;"
"flat in uint index;"
"in vec3 Normal;"
"in vec3 FragPos;"
"void main()"
"{"
"out vec4 FragColor;"
" vec4 color = texture( palette, vec3( 0, 0, index ) ).rgba;"
" vec3 lightPos = vec3(0,0,-15);"
" vec3 lightDir = normalize(lightPos - FragPos);"
"void main()"
"{"
" vec4 color = texture(palette, vec3(0, 0, index));\n"
" float diffuse = abs(dot(Normal, lightDir)); "
" vec3 viewPos = vec3(0, 0, -15);\n"
" vec3 viewDir = normalize(viewPos - FragPos);\n"
" FragColor = (0.5 + 0.5*diffuse)*color;"
"}";
" vec3 lightPos = viewPos;\n"
" vec3 lightDir = normalize(lightPos - FragPos);\n"
" vec3 halfwayDir = normalize(lightDir + viewDir);\n"
" 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"
"}";

View File

@@ -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;
}
@@ -165,5 +171,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;
}

View File

@@ -8,18 +8,26 @@
#define WIDTH 512
#define HEIGHT 512
#ifdef EMSCRIPTEN
#include <emscripten.h>
#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)
@@ -29,11 +37,67 @@ void mlog(char *msg)
#endif
}
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
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, 1);
load_mdl_matrix(shader_plain, 1, 1);
draw_mesh(m_axis, 1);
load_mdl_matrix(shader_plain, 2, 2);
draw_mesh(m_axis, 1);
#endif
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)
{
id_t shader, texture, shader_plain;
mesh_t m_surface, m_axis;
window_t window;
id_t texture;
mlog("[VENTANA] Inicializando...\n");
{
@@ -53,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");
@@ -93,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);
}
@@ -117,29 +183,12 @@ int main(void)
}
mlog("[MAIN LOOP] Inicializando...\n");
while (is_open_window(window))
{
quat_t q;
q = poll_input(window);
load_rot_matrix(shader, q);
load_rot_matrix(shader_plain, q);
clean_context();
#ifndef DEBUG
load_mdl_matrix(shader_plain, 0, 0);
draw_mesh(m_axis);
load_mdl_matrix(shader_plain, 1, 1);
draw_mesh(m_axis);
load_mdl_matrix(shader_plain, 2, 2);
draw_mesh(m_axis);
load_mdl_matrix(shader, 0, 3);
#ifdef EMSCRIPTEN
emscripten_set_main_loop(&main_loop, 0, 1);
#else
load_mdl_matrix(shader_plain, 0, 3);
while (is_open_window(window))
main_loop();
#endif
draw_mesh(m_surface);
}
mlog("[MAIN LOOP] Terminando...\n");
mlog("[MESH] Destruyendo...\n");

View File

@@ -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.

View File

@@ -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]);
}

View File

@@ -68,8 +68,8 @@ mesh_t create_mesh(float *d, float *n, unsigned char m)
{
glGenBuffers(1, &p->n_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, p->n_vbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, p->vertex * m * sizeof(float), n + 1,
GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, p->vertex * m * sizeof(float),
n + 1, GL_STATIC_DRAW);
}
for (i = 0; i < 4; ++i)
@@ -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);
}
}

View File

@@ -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);

View File

@@ -1,3 +1,4 @@
#include <complex.h>
#include <math.h>
#include <stdlib.h>
@@ -9,31 +10,64 @@
#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 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 +75,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 +86,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 +96,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;
}
}