From fe3b99d480a864e409a3b15569846270eee2fb89 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Fri, 18 Oct 2024 20:26:53 -0600 Subject: [PATCH] Setup for test on R4 --- Makefile | 7 ++- src/data/axis.h | 1 + src/data/shaders.h | 28 +++++++++++- src/input.c | 16 ++++++- src/main.c | 105 +++++++++++++++++++++++++++------------------ src/main.h | 2 +- src/mesh.c | 27 +++++++++--- src/shader.c | 2 +- src/surface.c | 93 +++++++++++++++++++++++++++++++++++++++ src/window.c | 4 +- 10 files changed, 227 insertions(+), 58 deletions(-) create mode 100644 src/surface.c diff --git a/Makefile b/Makefile index 6dae268..2c23c49 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ BIN = manigraph OBJ = \ + src/surface.o \ src/context.o \ src/texture.o \ src/window.o \ @@ -13,7 +14,7 @@ OBJ = \ CFLAGS = \ -I./ext/cglm/include \ -I./ext/glfw/include \ - -Wall -Wno-unused-function -std=c99 \ + -Wall -Wno-unused-function -std=c99 \ WAYLAND-LIB = \ xdg-shell \ @@ -47,10 +48,12 @@ windows: $(OBJ) glfw.dll glfw.dll: $(CC) -fPIC -shared -D_GLFW_WIN32 -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o $@ -lgdi32 +src/main.o: src/data/shaders.h src/data/cube.h src/data/axis.h + # LINUX linux-x11: $(OBJ) $(MAKE) BKN=_GLFW_X11 libglfw.so - $(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw -lm + $(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw -lm linux-wayland: $(OBJ) $(MAKE) BKN=_GLFW_WAYLAND libglfw.so diff --git a/src/data/axis.h b/src/data/axis.h index d957c16..3ccddc8 100644 --- a/src/data/axis.h +++ b/src/data/axis.h @@ -6,6 +6,7 @@ #undef F #undef G #undef H + #define A -2.0,-0.05,-0.05, #define B -2.0,-0.05, 0.05, #define C -2.0, 0.05,-0.05, diff --git a/src/data/shaders.h b/src/data/shaders.h index 8e22431..98d2e24 100644 --- a/src/data/shaders.h +++ b/src/data/shaders.h @@ -1,8 +1,14 @@ const char * vs = "#version 330 core\n" - "layout (location = 0) in vec3 aPos;" - "layout (location = 1) in vec3 aNormal;" + "layout (location = 0) in float aPos_x;" + "layout (location = 1) in float aPos_y;" + "layout (location = 2) in float aPos_z;" + "layout (location = 3) in float aPos_w;" + "layout (location = 4) in float aNormal_x;" + "layout (location = 5) in float aNormal_y;" + "layout (location = 6) in float aNormal_z;" + "layout (location = 7) in float aNormal_w;" "uniform float idx;" "uniform mat4 fix;" @@ -16,12 +22,30 @@ const char * vs = "void main()" "{" " index=idx;" + " vec3 aNormal = vec3(aNormal_x,aNormal_y,aNormal_z);" + " vec3 aPos = vec3(aPos_x,aPos_y,aPos_z);" " 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));" "}"; +const char * fs_plain = + "#version 330 core\n" + + "uniform sampler2DArray palette;" + + "in float index;" + "out vec4 FragColor;" + "in vec3 Normal;" + "in vec3 FragPos;" + + "void main()" + "{" + " FragColor = texture( palette, vec3( 0, 0, index ) ).rgba;" + "}"; + + const char * fs = "#version 330 core\n" diff --git a/src/input.c b/src/input.c index b761aa2..5b80e03 100644 --- a/src/input.c +++ b/src/input.c @@ -1,6 +1,7 @@ #include "main.h" #include #include +#include #define ANGLE ((float)0x01/0xff*2*GLM_PI) @@ -17,6 +18,17 @@ vec3 axis[3] = {0, 0, 1}, }; +void __key_callback(GLFWwindow * window, int key, int scancode, int action, int mods ) +{ + if( action != GLFW_PRESS) + return; + + if( GLFW_KEY_0 <= key && key <= GLFW_KEY_9 ) + { + printf("%d\n", key-GLFW_KEY_0 ); + } +} + void __window_callback(GLFWwindow * window, int w, int h) { int m; @@ -37,8 +49,8 @@ void __mouse_callback(GLFWwindow* window, int button, int action, int mods) return; glfwGetCursorPos(window, &xpos, &ypos); - glReadPixels((int)xpos, (int)(window_height - ypos), 1, 1, GL_GREEN, GL_UNSIGNED_BYTE, &green_value); - + glReadPixels((int)xpos, (int)(window_height - ypos), 1, 1, GL_GREEN, + GL_UNSIGNED_BYTE, &green_value); switch(green_value) { diff --git a/src/main.c b/src/main.c index baebeb3..922ec41 100755 --- a/src/main.c +++ b/src/main.c @@ -1,14 +1,16 @@ #include "main.h" -#include "data/cube.h" #include "data/axis.h" #include "data/shaders.h" -#include #include +#include #include +#include #define WIDTH 512 #define HEIGHT 512 +unsigned char coordanate[4] = {0,1,2,3}; + unsigned char palette[] = { 16, @@ -20,8 +22,9 @@ unsigned char palette[] = void calc_normal(float* v1, float* v2, float* v3,float* normal, unsigned char n) { + float alpha; vec4 lado1, lado2; - vec4 u1, u2; + vec4 u1, u2, proy; switch (n) { @@ -35,21 +38,20 @@ void calc_normal(float* v1, float* v2, float* v3,float* normal, unsigned char n) return; case 4: +#if 0 glm_vec4_sub(v2, v1, lado1); glm_vec4_sub(v3, v1, lado2); glm_vec4_copy(lado1, u1); - float alfa = glm_vec4_dot(lado2, u1) / glm_vec4_dot(u1, u1); - vec4 proy; - glm_vec4_scale(u1, alfa, proy); + alpha = glm_vec4_dot(lado2, u1) / glm_vec4_dot(u1, u1); + glm_vec4_scale(u1, alpha, proy); glm_vec4_sub(lado2, proy, u2); glm_vec4_normalize(u2); glm_vec4_copy(u2, normal); - - default: - break; +#endif + return; } } @@ -58,24 +60,25 @@ float * fill_normal( float * d, unsigned char m ) float * n; n = malloc( (*d+1)*sizeof(float)); *n = *d; - for (int i = 0; i < *d; i += 9) + for (int i = 0; i < *d; i += 3*m) { - vec3 norm_vec; - calc_normal((d+1)+i, (d+1)+i+3, (d+1)+i+6, norm_vec, m); + vec4 norm_vec; + + calc_normal((d+1)+i, (d+1)+i+m, (d+1)+i+2*m, norm_vec, m); glm_vec3_copy( norm_vec, (n+1)+i ); - glm_vec3_copy( norm_vec, (n+1)+i+3 ); - glm_vec3_copy( norm_vec, (n+1)+i+6 ); + glm_vec3_copy( norm_vec, (n+1)+i+m ); + glm_vec3_copy( norm_vec, (n+1)+i+2*m ); } return n; } const char * wname = "manigraph: manifold grapher"; +float * generate_surface(); int main( void ) { - float * n_cube, *n_axis; - id_t shader, texture; + id_t shader, texture, shader_plain; mesh_t m_cube, m_axis; window_t window; @@ -87,63 +90,83 @@ int main( void ) glewInit(); - if( !( shader = create_shader() ) ) - goto error_shader; - - gload_program( shader, vs, VERTEX ); - gload_program( shader, fs, FRAGMENT ); - use_shader( shader ); - - load_fix_matrix( shader, (float)WIDTH/HEIGHT ); - - n_cube = fill_normal( d_cube, 3 ); - n_axis = fill_normal( d_axis, 3 ); - - if( !( m_cube = create_mesh( d_cube, n_cube ) ) ) - goto error_mesh_cube; - - if( !( m_axis = create_mesh( d_axis, n_axis ) ) ) - goto error_mesh_axis; - - free( n_cube ); - free( n_axis ); - texture=create_palette_texture( palette ); use_texture( texture ); + if( !( shader = create_shader() ) ) + goto error_shader; + gload_program( shader, vs, VERTEX ); + gload_program( shader, fs, FRAGMENT ); + + if( !( shader_plain = create_shader() ) ) + goto error_shader_plain; + gload_program( shader_plain, vs, VERTEX ); + gload_program( shader_plain, fs_plain, FRAGMENT ); + + load_fix_matrix( shader, (float)WIDTH/HEIGHT ); + load_fix_matrix( shader_plain, (float)WIDTH/HEIGHT ); + + /* Fill m_cube */ + { + float * n_cube, *d_cube; + d_cube = generate_surface(16); + n_cube = fill_normal( d_cube, 4 ); + if( !( m_cube = create_mesh( d_cube, n_cube, coordanate, 4 ) ) ) + goto error_mesh_cube; + free( n_cube ); + free( d_cube); + } + + /* Fill m_axis */ + { + float * n_axis; + n_axis = fill_normal( d_axis, 3 ); + if( !( m_axis = create_mesh( d_axis, n_axis, coordanate, 3 ) ) ) + goto error_mesh_axis; + free( n_axis ); + } + 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, 0, 0 ); + load_mdl_matrix( shader_plain, 0, 0 ); draw_mesh( m_axis ); - load_mdl_matrix( shader, 1, 1 ); + load_mdl_matrix( shader_plain, 1, 1 ); draw_mesh( m_axis ); - load_mdl_matrix( shader, 2, 2 ); + load_mdl_matrix( shader_plain, 2, 2 ); draw_mesh( m_axis ); #endif + load_mdl_matrix( shader, 0, 3 ); draw_mesh( m_cube ); + } - destroy_texture( texture ); destroy_mesh( m_axis ); destroy_mesh( m_cube ); + destroy_shader( shader_plain ); destroy_shader( shader ); + destroy_texture( texture ); close_window( window ); return 0; error_mesh_axis: destroy_mesh( m_cube ); error_mesh_cube: + destroy_shader( shader_plain ); +error_shader_plain: destroy_shader( shader ); error_shader: close_window( window ); + destroy_texture( texture ); error_window: return 1; } diff --git a/src/main.h b/src/main.h index 71057cf..b0fd681 100644 --- a/src/main.h +++ b/src/main.h @@ -22,7 +22,7 @@ int is_open_window(window_t window); void close_window(window_t window); -mesh_t create_mesh( float * d, float * n); +mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned char m ); void destroy_mesh(mesh_t p); diff --git a/src/mesh.c b/src/mesh.c index 7ba7f28..21861c1 100755 --- a/src/mesh.c +++ b/src/mesh.c @@ -7,30 +7,43 @@ struct obj unsigned int vertex, vao, n_vbo, d_vbo; }; -mesh_t create_mesh( float * d, float * n ) +mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned char m ) { struct obj * p; p=malloc(sizeof(struct obj)); - p->vertex=(*d)/3; + p->vertex=(*d)/m; glGenVertexArrays( 1, &p->vao ); glGenBuffers( 1, &p->d_vbo ); glBindVertexArray( p->vao ); glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo ); - glBufferData( GL_ARRAY_BUFFER, p->vertex*3*sizeof(float), d+1, + glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), d+1, GL_STATIC_DRAW ); - glVertexAttribPointer( 0,3,GL_FLOAT, 0, 3*sizeof(float), NULL ); + glVertexAttribPointer( 0,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[0]*sizeof(float)) ); glEnableVertexAttribArray(0); + glVertexAttribPointer( 1,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[1]*sizeof(float)) ); + glEnableVertexAttribArray(1); + glVertexAttribPointer( 2,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[2]*sizeof(float)) ); + glEnableVertexAttribArray(2); + glVertexAttribPointer( 3,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[3]*sizeof(float)) ); + glEnableVertexAttribArray(3); glGenBuffers( 1, &p->n_vbo ); glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo ); - glBufferData( GL_ARRAY_BUFFER, p->vertex*3*sizeof(float), n+1, + glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), n+1, GL_STATIC_DRAW ); - glVertexAttribPointer( 1,3,GL_FLOAT, 0, 3*sizeof(float), NULL ); - glEnableVertexAttribArray(1); + glVertexAttribPointer( 4,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[0]*sizeof(float)) ); + glEnableVertexAttribArray(4); + glVertexAttribPointer( 5,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[1]*sizeof(float)) ); + glEnableVertexAttribArray(5); + glVertexAttribPointer( 6,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[2]*sizeof(float)) ); + glEnableVertexAttribArray(6); + glVertexAttribPointer( 7,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[3]*sizeof(float)) ); + glEnableVertexAttribArray(7); + return p; } diff --git a/src/shader.c b/src/shader.c index d4dbc71..f7b8b41 100644 --- a/src/shader.c +++ b/src/shader.c @@ -37,8 +37,8 @@ unsigned char gload_program( unsigned int program, const char * src, shader = glCreateShader(type[i]); glShaderSource( shader, 1, (const GLchar **)&src, ((void*)0 )); glCompileShader(shader); - glGetShaderiv( shader, GL_COMPILE_STATUS, &status ); + glGetShaderiv( shader, GL_COMPILE_STATUS, &status ); if( !status ) { diff --git a/src/surface.c b/src/surface.c new file mode 100644 index 0000000..40f67df --- /dev/null +++ b/src/surface.c @@ -0,0 +1,93 @@ +#include +#include +#include + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +typedef void(*function_t)(float*,int, int, int); + +void mobius(float *d_surface, int i, int j, 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; + + 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 toro(float *d_surface, int i, int j, int grid_size) +{ + float u = (2*M_PI) * ((float)i/grid_size ); + float v = (2*M_PI) * ((float)j/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) +{ + float u = (2*M_PI) * ((float)i/grid_size ); + float v = (2*M_PI) * ((float)j/grid_size ); + + d_surface[0] = (0.5*cos(v) + 0.5) * cos(u); + d_surface[1] = (0.5*cos(v) + 0.5) * sin(u); + d_surface[2] = sin(v) * cos(u/2); + d_surface[3] = sin(v)*sin(u/2); +} + +float * generate_surface(int grid_size) +{ + unsigned char m = 4; + long size = grid_size*grid_size*2*9*m; + function_t f = klein; + float * d_surface; + int k=0; + + d_surface = malloc((size+1)*sizeof(float)); + d_surface[0] = size; + + for (int i = 0; i < grid_size; i++) + { + for (int j = 0; j < grid_size; j++) + { + // triangle 1, front + f(&d_surface[k + 1], i, j, grid_size); + k+=m; + f(&d_surface[k + 1], i + 1, j, grid_size); + k+=m; + f(&d_surface[k + 1], i + 1, j + 1, grid_size); + k+=m; + + // triangle 1, back + f(&d_surface[k + 1], i + 1, j + 1, grid_size); + k+=m; + f(&d_surface[k + 1], i + 1, j, grid_size); + k+=m; + f(&d_surface[k + 1], i, j, grid_size); + k+=m; + + // triangle 2, font + f(&d_surface[k + 1], i, j, grid_size); + k+=m; + f(&d_surface[k + 1], i, j + 1, grid_size); + k+=m; + f(&d_surface[k + 1], i + 1, j + 1, grid_size); + k+=m; + + // triangle 2, back + f(&d_surface[k + 1], i + 1, j + 1, grid_size); + k+=m; + f(&d_surface[k + 1], i, j + 1, grid_size); + k+=m; + f(&d_surface[k + 1], i, j, grid_size); + k+=m; + } + } + + return d_surface; +} diff --git a/src/window.c b/src/window.c index a02fb78..1ac027a 100644 --- a/src/window.c +++ b/src/window.c @@ -4,12 +4,11 @@ void __window_callback(GLFWwindow *, int, int); void __mouse_callback(GLFWwindow *, int, int, int); void __scroll_callback(GLFWwindow *, double, double); +void __key_callback(GLFWwindow *, int, int, int, int); window_t init_window(unsigned int w, unsigned int h, const char * name) { void * window; - void __mouse_callback(GLFWwindow *window, int button, int action, int mods); - void __scroll_callback(GLFWwindow *window, double xoffset, double yoffset); if( !glfwInit() ) return NULL; @@ -29,6 +28,7 @@ window_t init_window(unsigned int w, unsigned int h, const char * name) glfwSetWindowSizeCallback(window, __window_callback); glfwSetMouseButtonCallback((GLFWwindow*)window, __mouse_callback); glfwSetScrollCallback((GLFWwindow*)window, __scroll_callback); + glfwSetKeyCallback((GLFWwindow*)window, __key_callback); __window_callback( window, w, h );