From 2a5a101080991e2b5c42e425575176bf563c9b82 Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Tue, 22 Oct 2024 23:07:30 -0600 Subject: [PATCH] Change projection w/keyboard --- src/context.c | 3 -- src/data/shaders.h | 3 +- src/input.c | 49 +++++++++++++++++++++++++++++++-- src/main.c | 26 +++++++++++++----- src/main.h | 21 ++++++++++++-- src/mesh.c | 68 +++++++++++++++++++++++++++++++++++----------- src/surface.c | 18 +----------- 7 files changed, 140 insertions(+), 48 deletions(-) diff --git a/src/context.c b/src/context.c index d65a220..d391ba4 100644 --- a/src/context.c +++ b/src/context.c @@ -4,9 +4,6 @@ void set_clean_color_context( unsigned char r, unsigned char g, unsigned char b ) { glEnable( GL_DEPTH_TEST ); - glEnable( GL_CULL_FACE ); - - glCullFace( GL_BACK ); glClearColor( (float)r/0xff, (float)g/0xff, (float)b/0xff, 1.0 ); } diff --git a/src/data/shaders.h b/src/data/shaders.h index 8900068..03cdc2c 100644 --- a/src/data/shaders.h +++ b/src/data/shaders.h @@ -5,6 +5,7 @@ const char * vs = "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;" @@ -63,7 +64,7 @@ const char * fs = " vec3 lightPos = vec3(0,0,-15);" " vec3 lightDir = normalize(lightPos - FragPos);" - " float diffuse = max(dot(Normal, lightDir),0.0); " + " float diffuse = abs(dot(Normal, lightDir)); " " FragColor = (0.5 + 0.5*diffuse)*color;" "}"; diff --git a/src/input.c b/src/input.c index 1956640..a2cd342 100644 --- a/src/input.c +++ b/src/input.c @@ -18,15 +18,60 @@ vec3 axis[3] = {0, 0, 1}, }; +extern struct projection projection; + void __key_callback_input(GLFWwindow * window, int key, int scancode, int action, int mods ) { if( action != GLFW_PRESS) return; - if( GLFW_KEY_0 <= key && key <= GLFW_KEY_9 ) + + if( GLFW_KEY_0= projection.m ) + return; + if( selected_coord == projection.x ) + return; + if( selected_coord == projection.y ) + return; + if( selected_coord == projection.z ) + return; + + selected_coord = projection.w; } + + if( projection.w >= projection.m ) + return; + + switch( key ) + { + unsigned char tmp; + + case GLFW_KEY_P: + tmp=projection.w; + projection.w=projection.x; + projection.x=tmp; + break; + case GLFW_KEY_O: + tmp=projection.w; + projection.w=projection.y; + projection.y=tmp; + break; + case GLFW_KEY_I: + tmp=projection.w; + projection.w=projection.z; + projection.z=tmp; + break; + } + + set_projection_mesh( projection ); + return; + + } void __window_callback_input(GLFWwindow * window, int w, int h) diff --git a/src/main.c b/src/main.c index b02523d..aa3db23 100755 --- a/src/main.c +++ b/src/main.c @@ -5,10 +5,12 @@ #include #include - #define WIDTH 512 #define HEIGHT 512 +float * generate_data_surface(unsigned int, unsigned char *); +float * generate_normals_surface(float *, unsigned char); + unsigned char palette[] = { 0xEB,0xD3,0xF8,0xff, @@ -17,12 +19,15 @@ unsigned char palette[] = 0x7A,0x1C,0xAC,0xff, }; -unsigned char coordanate[4] = {0,1,2,3}; const char * wname = "manigraph: manifold grapher"; -float * generate_data_surface(unsigned int, unsigned char *); -float * generate_normals_surface(float *, unsigned char); - +struct projection projection = +{ + .x=0, + .y=1, + .z=2, + .w=3, +}; void mlog( char * msg ) { @@ -94,18 +99,25 @@ int main( void ) float * n_surface, *d_surface; d_surface = generate_data_surface(16,&m); n_surface = generate_normals_surface(d_surface, m); - if( !(m_surface = create_mesh(d_surface, n_surface, coordanate, 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.mesh=m_surface; + free(n_surface); free(d_surface); + } mlog("[MESH] Inicializando...\n"); { - if(!(m_axis = create_mesh(d_axis, NULL, coordanate, 3))) + if(!(m_axis = create_mesh(d_axis, NULL, 3))) { mlog("[MESH] Error al inicializar...\n"); goto error_mesh_axis; diff --git a/src/main.h b/src/main.h index b6a10c1..fdcb284 100644 --- a/src/main.h +++ b/src/main.h @@ -12,6 +12,22 @@ 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. + x: the coordanate of the x axis. + y: the coordanate of the y axis. + z: the coordanate of the z axis. + w: the coordanate of the w axis. +*/ + +struct projection +{ + mesh_t mesh; + unsigned char m, x, y, z, w; +}; + /* Init window: w: default width; @@ -31,11 +47,12 @@ void close_window(window_t window); Create mesh: d: array of floats with the vertex data. n: array of floats with the normal data. - coordanate: the number of the 3 axis that we are displaying. m: Dimention of mesh */ -mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned char m ); +mesh_t create_mesh( float * d, float * n, unsigned char m ); + +void set_projection_mesh( struct projection ); void destroy_mesh(mesh_t p); diff --git a/src/mesh.c b/src/mesh.c index 0179f26..1d1d60b 100755 --- a/src/mesh.c +++ b/src/mesh.c @@ -1,19 +1,49 @@ #include "main.h" #include #include +#include struct obj { unsigned int vertex, vao, n_vbo, d_vbo; }; +void set_projection_mesh( struct projection projection ) +{ + struct obj * p; + p=projection.mesh; + + glBindVertexArray( p->vao ); + + glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo ); + glVertexAttribPointer( 0,1,GL_FLOAT, 0, projection.m*sizeof(float), + (float*)(projection.x*sizeof(float)) ); + glVertexAttribPointer( 1,1,GL_FLOAT, 0, projection.m*sizeof(float), + (float*)(projection.y*sizeof(float)) ); + glVertexAttribPointer( 2,1,GL_FLOAT, 0, projection.m*sizeof(float), + (float*)(projection.z*sizeof(float)) ); + glVertexAttribPointer( 3,1,GL_FLOAT, 0, projection.m*sizeof(float), + (float*)(projection.w*sizeof(float)) ); + + glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo ); + glVertexAttribPointer( 4,1,GL_FLOAT, 0, projection.m*sizeof(float), + (float*)(projection.x*sizeof(float)) ); + glVertexAttribPointer( 5,1,GL_FLOAT, 0, projection.m*sizeof(float), + (float*)(projection.y*sizeof(float)) ); + glVertexAttribPointer( 6,1,GL_FLOAT, 0, projection.m*sizeof(float), + (float*)(projection.z*sizeof(float)) ); + glVertexAttribPointer( 7,1,GL_FLOAT, 0, projection.m*sizeof(float), + (float*)(projection.w*sizeof(float)) ); +} + /* In this function we load all the vertex and normal datas on two diferents buffers, so we can access the coordanates that we want to display using the layout location in GLSL. This trick can be done with glVertexAttribPointer. */ -mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned char m ) + +mesh_t create_mesh( float * d, float * n, unsigned char m ) { unsigned char i; struct obj * p; @@ -23,35 +53,41 @@ mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned c p->vertex=(*d)/m; glGenVertexArrays( 1, &p->vao ); + glBindVertexArray( p->vao ); glGenBuffers( 1, &p->d_vbo ); - glBindVertexArray( p->vao ); glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo ); glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), d+1, GL_STATIC_DRAW ); - for( i=0; i<4; ++i ) + if( n ) { - glVertexAttribPointer( i,1,GL_FLOAT, 0, m*sizeof(float), - (float*)(coordanate[i]*sizeof(float)) ); - glEnableVertexAttribArray(i); + glGenBuffers( 1, &p->n_vbo ); + glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo ); + glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), n+1, + GL_STATIC_DRAW ); } - if( !n ) - return p; - - glGenBuffers( 1, &p->n_vbo ); - glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo ); - glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), n+1, - GL_STATIC_DRAW ); - for( i=0; i<4; ++i ) { - glVertexAttribPointer( i+4,1,GL_FLOAT, 0, m*sizeof(float), - (float*)(coordanate[i]*sizeof(float)) ); + glEnableVertexAttribArray(i); 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; } diff --git a/src/surface.c b/src/surface.c index b856f1f..89d425c 100644 --- a/src/surface.c +++ b/src/surface.c @@ -53,7 +53,7 @@ float * generate_data_surface(int grid_size, unsigned char *m ) f = klein; *m = 4; - size = grid_size*grid_size*12*(*m); + size = grid_size*grid_size*6*(*m); d_surface = malloc((size+1)*sizeof(float)); d_surface[0] = size; @@ -70,22 +70,6 @@ float * generate_data_surface(int grid_size, unsigned char *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, Frontt - 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; - // triangle 2, Back f(&d_surface[k + 1], i, j, grid_size); k+=*m;