From 7787daa2da2ecbf58728514b3ec8f7ffbf42be3a Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Tue, 22 Oct 2024 19:26:10 -0600 Subject: [PATCH] Merge: roberto -> main --- src/context.c | 3 ++ src/data/shaders.h | 7 +-- src/main.c | 108 ++++++++++++++++++++++++++++++++++----------- src/main.h | 5 ++- src/mesh.c | 12 ++--- src/surface.c | 36 ++++++++++----- 6 files changed, 122 insertions(+), 49 deletions(-) diff --git a/src/context.c b/src/context.c index f252203..99bdeb5 100644 --- a/src/context.c +++ b/src/context.c @@ -4,6 +4,9 @@ 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 bec67eb..fa08b5b 100644 --- a/src/data/shaders.h +++ b/src/data/shaders.h @@ -45,6 +45,7 @@ const char * fs_plain = " FragColor = texture( palette, vec3( 0, 0, index ) ).rgba;" "}"; + const char * fs = "#version 330 core\n" @@ -62,11 +63,7 @@ const char * fs = " vec3 lightPos = vec3(0,0,-15);" " vec3 lightDir = normalize(lightPos - FragPos);" -/* - We use the absoulute value, insted of the traditional "max(...,0.0);" - so we can ignore the orientation of the triangles in openGL. -*/ - " float diffuse = abs(dot(Normal, lightDir)); " + " float diffuse = max(dot(Normal, lightDir),0.0); " " float ambient = 0.5;" " FragColor = (ambient + diffuse)*color;" diff --git a/src/main.c b/src/main.c index 4fcadd4..df39bce 100755 --- a/src/main.c +++ b/src/main.c @@ -3,9 +3,12 @@ #include "data/shaders.h" #include -#include #include +#define CGLM_ALL_UNALIGNED +#include +#include + #define WIDTH 512 #define HEIGHT 512 @@ -19,48 +22,100 @@ unsigned char palette[] = 0x7A,0x1C,0xAC,0xff, }; -void calc_normal(float* v1, float* v2, float* v3, float* normal) +void calc_normal(float* p1, float* p2, float* p3,float* normal, unsigned char n) { - vec3 lado1, lado2; + float alpha; + vec4 v1, v2, v3; + vec4 u1, u2, u3; + + switch (n) + { + case 3: - glm_vec3_sub(v2, v1, lado1); - glm_vec3_sub(v3, v1, lado2); + glm_vec3_sub(p2, p1, v1); + glm_vec3_sub(p3, p1, v2); - glm_vec3_cross(lado1, lado2, normal); - glm_vec3_normalize(normal); + glm_vec3_cross(v1, v2, normal); + glm_vec3_normalize(normal); + return; + + case 4: + + /* + In Grant-Shmidth we need 3 linearly independian vector that forms a basis, + so we can have a ortonormal version of that basis, since, we must have + v1 = p3 - p1 + v2 = p2 - p1 + Then v3 = p1, will most certantly be linerly independiant to v1 and v2. + */ + glm_vec4_sub(p2, p1, v1); + glm_vec4_sub(p3, p1, v2); + glm_vec4_copy(p1, v3); + + /* Setup U1 */ + { + glm_vec4_copy(v1, u1); + } + + /* Setup U2 */ + { + vec4 proj; + + alpha = glm_vec4_dot(v2, u1) / glm_vec4_dot(u1, u1); + glm_vec4_scale(u1, alpha, proj); + + glm_vec4_sub(v2, proj, u2); + } + + /* Setup U3 */ + { + vec4 proj1, proj2; + + alpha = glm_vec4_dot(v3, u1) / glm_vec4_dot(u1, u1); + glm_vec4_scale(u1, alpha, proj1); + + alpha = glm_vec4_dot(v3, u2) / glm_vec4_dot(u2, u2); + glm_vec4_scale(u2, alpha, proj2); + + glm_vec4_sub(v3, proj1, u3); + glm_vec4_sub(u3, proj2, u3); + } + + glm_vec4_copy(u3, normal); + glm_vec4_normalize(normal); + return; + } } -float * fill_normal( float * d ) +float * fill_normal( float * d, unsigned char m ) { float * n; - - n = malloc((*d+1)*sizeof(float)); + 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); + 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(unsigned int, unsigned char *m); - - -void mlog(char * msg) +void mlog( char * msg ) { #ifdef DEBUG printf(msg); #endif } -int main(void) +const char * wname = "manigraph: manifold grapher"; +float * generate_surface(); + +int main( void ) { id_t shader, texture, shader_plain; mesh_t m_surface, m_axis; @@ -122,8 +177,8 @@ int main(void) unsigned char m; float * n_surface, *d_surface; d_surface = generate_surface(16,&m); - n_surface = fill_normal(d_surface); - if( !(m_surface = create_mesh(d_surface, n_surface, coordanate))) + n_surface = fill_normal(d_surface, m); + if( !(m_surface = create_mesh(d_surface, n_surface, coordanate, m))) { mlog("[MESH] Error al inicializar...\n"); goto error_mesh_surface; @@ -135,8 +190,8 @@ int main(void) mlog("[MESH] Inicializando...\n"); { float * n_axis; - n_axis = fill_normal(d_axis); - if(!(m_axis = create_mesh(d_axis, n_axis, coordanate))) + n_axis = fill_normal(d_axis, 3); + if(!(m_axis = create_mesh(d_axis, n_axis, coordanate, 3))) { mlog("[MESH] Error al inicializar...\n"); goto error_mesh_axis; @@ -154,6 +209,7 @@ int main(void) load_rot_matrix(shader_plain, q); clean_context(); + #ifndef DEBUG load_mdl_matrix(shader_plain, 0, 0); draw_mesh( m_axis); diff --git a/src/main.h b/src/main.h index d51bc7d..b6a10c1 100644 --- a/src/main.h +++ b/src/main.h @@ -4,7 +4,7 @@ error of the shaders. */ -/* #define DEBUG */ +/* #define DEBUG */ typedef const void * window_t; typedef unsigned int id_t; @@ -32,9 +32,10 @@ void close_window(window_t window); 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 ); +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 b16cab5..3f93580 100755 --- a/src/mesh.c +++ b/src/mesh.c @@ -13,38 +13,38 @@ struct obj 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 ) +mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned char m ) { unsigned char i; 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 ); for( i=0; i<4; ++i ) { - glVertexAttribPointer( i,1,GL_FLOAT, 0, 3*sizeof(float), + 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*3*sizeof(float), n+1, + 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, 3*sizeof(float), + glVertexAttribPointer( i+4,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[i]*sizeof(float)) ); glEnableVertexAttribArray(i+4); } diff --git a/src/surface.c b/src/surface.c index 8bac07b..dc25635 100644 --- a/src/surface.c +++ b/src/surface.c @@ -37,28 +37,28 @@ void klein(float *d_surface, int i, int j, int 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[2] = sin(v)*sin(u/2); + d_surface[3] = sin(v)*sin(u/2); } -float * generate_surface(int grid_size, unsigned char * m) +float * generate_surface(int grid_size, unsigned char *m ) { - float * d_surface; - function_t f; - int i,j,k=0; + unsigned int i,j,k=0; long size; + function_t f; + float * d_surface; - *m = 3; f = klein; - size = grid_size*grid_size*2*3*(*m); - + *m = 4; + size = grid_size*grid_size*12*(*m); d_surface = malloc((size+1)*sizeof(float)); d_surface[0] = size; + for( i = 0; i < grid_size; i++) { for( j = 0; j < grid_size; j++) { - // triangle 1 + // triangle 1, Front f(&d_surface[k + 1], i, j, grid_size); k+=*m; f(&d_surface[k + 1], i + 1, j, grid_size); @@ -66,13 +66,29 @@ float * generate_surface(int grid_size, unsigned char * m) f(&d_surface[k + 1], i + 1, j + 1, grid_size); k+=*m; - // triangle 2 + // 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; + 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; } }