From 904d686c6ef0eeb3ee01b5437b7221be4124837e Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Sun, 1 Dec 2024 16:12:17 -0600 Subject: [PATCH] Clean up codebase, rm texture, matrix -> load --- Makefile | 12 +++---- src/context.c | 9 ----- src/data/shaders.h | 80 ++++++++++++++++++----------------------- src/load.c | 34 ++++++++++++++++++ src/main.c | 42 +++++++--------------- src/main.h | 88 ++++++++++++++++++++++++++++------------------ src/matrix.c | 50 -------------------------- src/mesh.c | 20 ++++++++--- src/shader.c | 21 +++++------ src/surface.c | 6 ++-- src/texture.c | 38 -------------------- 11 files changed, 169 insertions(+), 231 deletions(-) create mode 100644 src/load.c delete mode 100644 src/matrix.c delete mode 100644 src/texture.c diff --git a/Makefile b/Makefile index 790cb3b..ffbf151 100644 --- a/Makefile +++ b/Makefile @@ -4,9 +4,8 @@ OBJ = \ ext/glad/glad.o \ src/surface.o \ src/context.o \ - src/texture.o \ src/window.o \ - src/matrix.o \ + src/load.o \ src/shader.o \ src/input.o \ src/mesh.o \ @@ -30,14 +29,13 @@ WAYLAND-LIB = \ wayland help: - @echo "Para compilar el proyecto a tu sistema operativo" - @echo "porfavor usa uno de los siguientes comandos:" + @echo "Usage:" @echo " $(MAKE) windows" @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 "Clean" @echo " $(MAKE) clean" src/main.o: src/data/axis.h src/data/shaders.h @@ -63,7 +61,7 @@ cocoa: $(OBJ) $(CC) -framework OpenGL -o $(BIN) $(OBJ) ext/glfw.a -lGL -lglfw wasm: $(OBJ) - $(CC) -sUSE_WEBGL2=1 -sUSE_GLFW=3 -o $(BIN).js $(OBJ) + $(CC) -sUSE_WEBGL2=1 -sUSE_GLFW=3 -o $(BIN).html $(OBJ) chmod -x $(BIN).wasm libglfw.so: @@ -77,4 +75,4 @@ clean: .SUFFIXES: .c .o .c.o: - $(CC) $(CFLAGS) -c -o $@ $< + $(CC) -Wno-implicit-function-declaration $(CFLAGS) -c -o $@ $< diff --git a/src/context.c b/src/context.c index be286b5..d842310 100644 --- a/src/context.c +++ b/src/context.c @@ -3,12 +3,8 @@ #ifdef EMSCRIPTEN #include #else -#ifdef GLAD #include #include -#else -#include -#endif #endif void set_clean_color_context(unsigned char r, unsigned char g, unsigned char b) @@ -22,12 +18,7 @@ int init_context(void) #ifdef EMSCRIPTEN return 1; #else -#ifdef GLAD - return gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); -#else - return glewInit(); -#endif #endif } diff --git a/src/data/shaders.h b/src/data/shaders.h index 1c25efc..423098f 100644 --- a/src/data/shaders.h +++ b/src/data/shaders.h @@ -1,4 +1,4 @@ -const char * vs = +const char *vs = #ifdef EMSCRIPTEN "#version 300 es\n" "precision highp float;" @@ -15,94 +15,84 @@ const char * vs = "layout (location = 6) in float aNormal_z;" "layout (location = 7) in float aNormal_w;" - "uniform uint idx;" - - "uniform uint i;" "uniform float angle;" + "uniform float i;" + "uniform vec4 color;" "uniform mat4 fix;" "uniform mat4 rot;" - "uniform mat4 mdl;" - "flat out uint index;" "out vec3 Normal;" "out vec3 FragPos;" + "out vec4 Color;" "mat2 rotate2d( float angle )" "{" - "return mat2( cos(angle), sin(angle), -sin(angle), cos(angle) );" + "return mat2( cos(angle), sin(angle), -sin(angle), cos(angle) );" "}" "void main()" "{" - " index=idx;" + " Color=color;" " vec3 aNormal = vec3(aNormal_x,aNormal_y,aNormal_z);" " vec3 aPos = vec3(aPos_x,aPos_y,aPos_z);" - " aNormal[i] = (vec2(aNormal[i], aNormal_w) * rotate2d(angle))[0];" - " aPos[i] = (vec2(aPos[i], aPos_w) * rotate2d(angle))[0];" + " aNormal[int(i)] = (vec2(aNormal[int(i)], aNormal_w) * " + "rotate2d(angle))[0];" + " aPos[int(i)] = (vec2(aPos[int(i)], aPos_w) * rotate2d(angle))[0];" - " Normal = mat3(transpose(inverse(rot*mdl))) * aNormal;" - " gl_Position = fix * rot * mdl * vec4( aPos, 1.0 );\n" - " FragPos = vec3( rot * mdl * vec4(aPos, 1.0));" + " Normal = mat3(transpose(inverse(rot))) * aNormal;" + " gl_Position = fix * rot * vec4( aPos, 1.0 );\n" + " FragPos = vec3( rot * vec4(aPos, 1.0));" "}"; - -const char * fs_plain = +const char *fs_plain = #ifdef EMSCRIPTEN "#version 300 es\n" - "precision highp float;" - "precision highp sampler2DArray;" #else "#version 330 core\n" #endif - "uniform sampler2DArray palette;" - - "flat in uint index;" "out vec4 FragColor;" "in vec3 Normal;" "in vec3 FragPos;" + "in vec4 Color;" "void main()" "{" - " vec4 color = texture( palette, vec3(0,0,index)).rgba;" - " FragColor = vec4(pow(vec3(color),vec3(1.0/2.2)),color.a);" + " FragColor = vec4(pow(vec3(Color),vec3(1.0/2.2)),Color.a);" "}"; -const char * fs = +const char *fs = #ifdef EMSCRIPTEN - "#version 300 es\n" + "#version 300 es\n" "precision highp float;" - "precision highp sampler2DArray;" #else - "#version 330 core\n" + "#version 330 core\n" #endif - "uniform sampler2DArray palette;" + "in vec3 Normal;" + "in vec3 FragPos;" + "in vec4 Color;" - "flat in uint index;" - "in vec3 Normal;" - "in vec3 FragPos;" + "out vec4 FragColor;" - "out vec4 FragColor;" + "void main()" + "{" - "void main()" - "{" - " vec4 color = texture(palette, vec3(0, 0, index));\n" + " vec3 viewPos = vec3(0, 0, -15);\n" + " vec3 viewDir = normalize(viewPos - FragPos);\n" - " vec3 viewPos = vec3(0, 0, -15);\n" - " vec3 viewDir = normalize(viewPos - FragPos);\n" + " vec3 lightPos = viewPos;\n" + " vec3 lightDir = normalize(lightPos - FragPos);\n" - " vec3 lightPos = viewPos;\n" - " vec3 lightDir = normalize(lightPos - FragPos);\n" + " vec3 halfwayDir = normalize(lightDir + viewDir);\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" - " 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(1.0/2.2));\n" - " FragColor = vec4(result, color.a);\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/load.c b/src/load.c new file mode 100644 index 0000000..f676448 --- /dev/null +++ b/src/load.c @@ -0,0 +1,34 @@ +#include "main.h" +#include +#include +#include + +void fix_matrix_load(id_t shader, float ratio) +{ + mat4 m, n; + const int d = 7; + + glm_lookat((vec3){0, 0, -d}, (vec3){0, 0, 0}, (vec3){0, 1, 0}, m); + 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); +} + +void rot_matrix_load(id_t shader, quat_t q) +{ + mat4 m; + glm_quat_mat4(q, m); + load_mat4_to_shader(shader, "rot", (mat4_t)m); +} + +void color_load(id_t shader, unsigned char color[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); +} diff --git a/src/main.c b/src/main.c index c47dd7f..71bb0f0 100644 --- a/src/main.c +++ b/src/main.c @@ -22,12 +22,7 @@ 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}, - {0x2F, 0x3C, 0x7E, 0xff}, -}; +unsigned char color[4] = {0x2F, 0x3C, 0x7E, 0xff}; void mlog(char *msg) { @@ -53,8 +48,10 @@ static inline q = poll_input(window); - load_rot_matrix(shader, q); - load_rot_matrix(shader_plain, q); + rot_matrix_load(shader, q); + rot_matrix_load(shader_plain, q); + color_load(shader, color); + color_load(shader_plain, color); { static float angle = 0; @@ -71,8 +68,8 @@ static inline if (animate_index) { - load_uint_to_shader(shader, "i", animate_index - 1); - load_uint_to_shader(shader_plain, "i", animate_index - 1); + load_float_to_shader(shader, "i", animate_index - 1); + load_float_to_shader(shader_plain, "i", animate_index - 1); angle += 0.01; load_float_to_shader(shader, "angle", angle); @@ -81,16 +78,12 @@ static inline } clean_context(); - load_mdl_matrix(shader, 0, 3); - draw_mesh(m_surface); - - load_mdl_matrix(shader_plain, 0, 3); - draw_mesh_lines(m_surface); + draw_mesh(shader, m_surface); + draw_mesh_lines(shader_plain, m_surface); } int main(void) { - id_t texture; mlog("[VENTANA] Inicializando...\n"); { @@ -113,12 +106,6 @@ int main(void) set_clean_color_context(0xFF, 0xFF, 0xFF); } - mlog("[TEXTURE] Inicializando...\n"); - { - texture = create_palette_texture(palette, 4); - use_texture(texture); - } - mlog("[SHADER] Inicializando...\n"); { if (!(shader = create_shader())) @@ -128,7 +115,7 @@ int main(void) } load_program_to_shader(shader, vs, VERTEX); load_program_to_shader(shader, fs, FRAGMENT); - load_fix_matrix(shader, (float)WIDTH / HEIGHT); + fix_matrix_load(shader, (float)WIDTH / HEIGHT); } mlog("[SHADER] Inicializando...\n"); @@ -140,7 +127,7 @@ int main(void) } load_program_to_shader(shader_plain, vs, VERTEX); load_program_to_shader(shader_plain, fs_plain, FRAGMENT); - load_fix_matrix(shader_plain, (float)WIDTH / HEIGHT); + fix_matrix_load(shader_plain, (float)WIDTH / HEIGHT); } mlog("[MESH] Inicializando...\n"); @@ -182,13 +169,11 @@ int main(void) destroy_shader(shader_plain); mlog("[SHADER] Destruyendo...\n"); destroy_shader(shader); - mlog("[TEXTURE] Destruyendo...\n"); - destroy_texture(texture); mlog("[WINDOW] Destruyendo...\n"); close_window(window); return 0; -error_mesh_axis: +error_context: mlog("[MESH] Destruyendo...\n"); destroy_mesh(m_surface); error_mesh_surface: @@ -198,9 +183,6 @@ error_shader_plain: mlog("[SHADER] Destruyendo...\n"); destroy_shader(shader); error_shader: - mlog("[TEXTURE] Destruyendo...\n"); - destroy_texture(texture); -error_context: mlog("[WINDOW] Destruyendo...\n"); close_window(window); error_window: diff --git a/src/main.h b/src/main.h index 4798dd2..33ca763 100644 --- a/src/main.h +++ b/src/main.h @@ -4,16 +4,15 @@ error of the shaders. */ -/* #define DEBUG */ -#define GLAD +#define DEBUG -typedef const void * window_t; +typedef const void *window_t; typedef unsigned int id_t; -typedef void * mesh_t; -typedef float * quat_t; -typedef float * mat4_t; +typedef void *mesh_t; +typedef float *quat_t; +typedef float *mat4_t; -/* +/* This struct represent the proyection, where: mesh: data of surface. m: the dimention of the surface. @@ -29,14 +28,29 @@ struct projection unsigned char m, x, y, z, w; }; -/* +/* + this structure has all the information to generate + a mesh, where: + data: the buffer with the vertex coords + norm: the buffer with the norm coords + vertex: the number of vertex + dim: the dimentions of the surface +*/ +struct surface +{ + float *data, *norm; + unsigned long vertex; + unsigned char dim; +}; + +/* Init window: w: default width; h: default height; name: Name of the window. */ -window_t init_window(unsigned int w, unsigned int h, const char * name); +window_t init_window(unsigned int w, unsigned int h, const char *name); void use_window(window_t window); @@ -44,22 +58,22 @@ int is_open_window(window_t window); void close_window(window_t window); -/* +/* Create mesh: d: array of floats with the vertex data. n: array of floats with the normal data. m: Dimention of mesh */ -mesh_t create_mesh( float * d, float * n, unsigned char m ); +mesh_t create_mesh(struct surface); -void set_projection_mesh( struct projection ); +void set_projection_mesh(struct projection); void destroy_mesh(mesh_t p); -void draw_mesh(mesh_t p); +void draw_mesh(id_t, mesh_t p); -void draw_mesh_lines(mesh_t p); +void draw_mesh_lines(id_t, mesh_t p); /* Set background color: @@ -72,7 +86,7 @@ void set_clean_color_context(unsigned char r, unsigned char g, unsigned char b); void clean_context(void); -int init_context( void ); +int init_context(void); void destroy_shader(id_t shader); @@ -82,7 +96,8 @@ void use_shader(id_t shader); enum { - VERTEX, FRAGMENT + VERTEX, + FRAGMENT }; /* @@ -91,7 +106,8 @@ enum type: VERTEX or FRAGMENT */ -unsigned char load_program_to_shader(id_t shader, const char * src, unsigned int type); +unsigned char load_program_to_shader( + id_t shader, const char *src, unsigned int type); /* load float to shader: @@ -99,7 +115,7 @@ unsigned char load_program_to_shader(id_t shader, const char * src, unsigned int f: float to load */ -void load_float_to_shader(id_t shader, char * var, float f); +void load_float_to_shader(id_t shader, char *var, float f); /* load unsigned int to shader: @@ -107,7 +123,15 @@ void load_float_to_shader(id_t shader, char * var, float f); u: unsigned int to load */ -void load_uint_to_shader(id_t shader, char * var, unsigned int u); +void load_uint_to_shader(id_t shader, char *var, unsigned int u); + +/* + load float[4] to shader: + var: name of glsl variable. + f: float[4] to load +*/ + +void load_float4_to_shader(id_t shader, char *var, float f[4]); /* load matrix 4 to shader: @@ -115,32 +139,28 @@ void load_uint_to_shader(id_t shader, char * var, unsigned int u); m: Matrix to load */ -void load_mat4_to_shader(id_t shader, char * var, mat4_t m); +void load_mat4_to_shader(id_t shader, char *var, mat4_t m); /* Generate and load fix matrix, this matrix has the information of the perspective and camera information. - + ratio: default ratio of window. */ -void load_fix_matrix(id_t shader, float ratio); - -/* - Generate and load model matrix, it also sets the color - to draw. - i: From {0,1,2} select one of 3 ortogonal rotations, - One for each axis. - c: Color index of the pallete. -*/ -void load_mdl_matrix(id_t shader, unsigned char i, unsigned char c); +void fix_matrix_load(id_t shader, float ratio); /* Generate and load rotation matrix. q: quaterinon describing the rotation. */ -void load_rot_matrix(id_t shader, quat_t q); +void rot_matrix_load(id_t shader, quat_t q); + +/* + */ + +void color_load(id_t shader, unsigned char color[4]); id_t config_texture(unsigned short type); @@ -153,11 +173,11 @@ void destroy_texture(id_t texture); colors: array of color values (rgba in hex ). n: number of color on colors. */ -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); -#ifdef EMSCRIPTEN +#ifdef EMSCRIPTEN #ifdef GLAD #error undefine GLAD on src/main.h please #endif diff --git a/src/matrix.c b/src/matrix.c deleted file mode 100644 index 5108268..0000000 --- a/src/matrix.c +++ /dev/null @@ -1,50 +0,0 @@ -#include "main.h" -#include -#include -#include - -mat4 ortho[] = { - { - {1, 0, 0, 0}, - {0, 1, 0, 0}, - {0, 0, 1, 0}, - {0, 0, 0, 1}, - }, - { - {0, 1, 0, 0}, - {-1, 0, 0, 0}, - {0, 0, 1, 0}, - {0, 0, 0, 1}, - }, - { - {0, 0, 1, 0}, - {0, 1, 0, 0}, - {-1, 0, 0, 0}, - {0, 0, 0, 1}, - }, -}; - -void load_fix_matrix(id_t shader, float ratio) -{ - mat4 m, n; - const int d = 7; - - glm_lookat((vec3){0, 0, -d}, (vec3){0, 0, 0}, (vec3){0, 1, 0}, m); - 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); -} - -void load_mdl_matrix(id_t shader, unsigned char i, unsigned char c) -{ - load_uint_to_shader(shader, "idx", c); - load_mat4_to_shader(shader, "mdl", (mat4_t)ortho[i]); -} - -void load_rot_matrix(id_t shader, quat_t q) -{ - mat4 m; - glm_quat_mat4(q, m); - load_mat4_to_shader(shader, "rot", (mat4_t)m); -} diff --git a/src/mesh.c b/src/mesh.c index 8caf45f..1ad14ad 100644 --- a/src/mesh.c +++ b/src/mesh.c @@ -1,9 +1,11 @@ #include "main.h" -#ifdef GLAD -#include + +#ifdef EMSCRIPTEN +#include #else -#include +#include #endif + #include #include @@ -89,19 +91,27 @@ void destroy_mesh(mesh_t p) free(p); } -void draw_mesh(mesh_t p) +void draw_mesh(id_t shader, mesh_t p) { struct obj *obj = p; + glUseProgram(shader); glBindVertexArray(obj->vao); +#ifndef EMSCRIPTEN glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); +#endif glDrawArrays(GL_TRIANGLES, 0, obj->vertex); } -void draw_mesh_lines(mesh_t p) +void draw_mesh_lines(id_t shader, mesh_t p) { struct obj *obj = p; + glUseProgram(shader); glBindVertexArray(obj->vao); +#ifndef EMSCRIPTEN glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glDrawArrays(GL_TRIANGLES, 0, obj->vertex); +#else + glDrawArrays(GL_LINES, 0, obj->vertex); +#endif } diff --git a/src/shader.c b/src/shader.c index f7d8430..212c638 100644 --- a/src/shader.c +++ b/src/shader.c @@ -1,20 +1,20 @@ #include "main.h" -#ifdef GLAD +#ifndef EMSCRIPTEN #include #else -#include +#include #endif #ifdef DEBUG #include #endif -void destroy_shader(unsigned int shader) { return glDeleteProgram(shader); } +void destroy_shader(unsigned int shader) { glDeleteProgram(shader); } unsigned int create_shader(void) { return glCreateProgram(); } -void use_shader(unsigned int program) { return glUseProgram(program); } +void use_shader(unsigned int program) { glUseProgram(program); } unsigned char load_program_to_shader( unsigned int program, const char *src, unsigned int i) @@ -54,14 +54,15 @@ 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); glUniformMatrix4fv(glGetUniformLocation(program, var), 1, 0, mat); } + +void load_float4_to_shader(unsigned int program, char *var, float float4[4]) +{ + glUseProgram(program); + glUniform4f(glGetUniformLocation(program, var), float4[0], float4[1], + float4[2], float4[3]); +} diff --git a/src/surface.c b/src/surface.c index 7aeaf4e..4cd2cfe 100644 --- a/src/surface.c +++ b/src/surface.c @@ -112,9 +112,9 @@ float *generate_data_surface(unsigned char *dim, unsigned long *vertex) int *cara; parm.f = cube; - parm.m = 5; - parm.n = 5; - parm.grid = (char[7]){16, 2, 8, 4, 3, 5, 1}; + parm.m = 4; + parm.n = 4; + parm.grid = (char[]){16, 8, 4, 2, 1}; #ifdef TEST assert(faces(2) == 1); diff --git a/src/texture.c b/src/texture.c deleted file mode 100644 index 6261ce0..0000000 --- a/src/texture.c +++ /dev/null @@ -1,38 +0,0 @@ -#include "main.h" -#ifdef GLAD -#include -#else -#include -#endif - -#define TYPE GL_TEXTURE_2D_ARRAY - -static id_t __config_texture(unsigned short type) -{ - id_t texture; - - glGenTextures(1, &texture); - glBindTexture(TYPE, texture); - - { - glTexParameteri(TYPE, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(TYPE, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - } - - return texture; -} - -void use_texture(id_t texture) { return glBindTexture(TYPE, texture); } - -void destroy_texture(unsigned int texture) -{ - return glDeleteTextures(1, &texture); -} - -id_t create_palette_texture(const unsigned char colors[][4], unsigned char n) -{ - id_t texture = __config_texture(TYPE); - glTexImage3D( - TYPE, 0, GL_RGBA, 1, 1, n, 0, GL_RGBA, GL_UNSIGNED_BYTE, colors); - return texture; -}