From fda999bfb1ca00803db91f2b16379c59c6f74f18 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 17 Oct 2024 17:58:55 -0600 Subject: [PATCH] PPP --- src/data/cube.h | 6 ------ src/data/shaders.h | 22 +++++++++++++++++++++- src/main.c | 16 +++++++++------- src/mesh.c | 33 +++++++++++++++++++++------------ 4 files changed, 51 insertions(+), 26 deletions(-) mode change 100644 => 100755 src/main.c diff --git a/src/data/cube.h b/src/data/cube.h index 3769b79..a6cbb1f 100644 --- a/src/data/cube.h +++ b/src/data/cube.h @@ -31,12 +31,6 @@ narray_float_t d_cube = A B E B E F -}; - -narray_float_t n_cube = -{ - 3 * 3 * 2 * 6, - A C E C E G diff --git a/src/data/shaders.h b/src/data/shaders.h index 1628553..9c3039c 100644 --- a/src/data/shaders.h +++ b/src/data/shaders.h @@ -2,6 +2,7 @@ const char * vs = "#version 330 core\n" "layout (location = 0) in vec3 aPos;" + "layout (location = 1) in vec3 aNormal;" "uniform float idx;" "uniform mat4 fix;" @@ -9,13 +10,19 @@ const char * vs = "uniform mat4 mdl;" "out float index;" + "out vec3 Normal;" + "out vec3 FragPos;" "void main()" "{" " index=idx;" " gl_Position = fix * rot * mdl * vec4( aPos, 1.0 );\n" + "Normal = aNormal;" + "FragPos = vec3( rot * vec4(aPos, 1.0));" "}"; + + const char * fs = "#version 330 core\n" @@ -23,8 +30,21 @@ const char * fs = "in float index;" "out vec4 FragColor;" + "in vec3 Normal;" + "in vec3 FragPos;" "void main()" "{" - " FragColor = texture( palette, vec3( 0, 0, index ) ).rgba;" + " vec3 lightColor = vec3(1,1,1);" + " vec3 lightPos = vec3(10,10,10);" + " vec3 norm = normalize(Normal);" + + " vec3 lightDir = normalize(lightPos - FragPos);" + + " float diffuse = max(dot(norm, lightDir), 0.0);" + " float ambient = 0.1;" + + " FragColor = vec4((ambient + diffuse)*lightColor,1.0)* texture( palette, vec3( 0, 0, index ) ).rgba;" "}"; + + diff --git a/src/main.c b/src/main.c old mode 100644 new mode 100755 index 0872456..ddf395e --- a/src/main.c +++ b/src/main.c @@ -29,15 +29,17 @@ void calc_normal(float* v1, float* v2, float* v3, float* normal) void fill_normal( float * d, float * n ) { - *n = *d; + //*n = *d; for (int i = 0; i < *d; i += 9) { vec3 norm_vec; calc_normal((d+1)+i, (d+1)+i+3, (d+1)+i+6, norm_vec); - 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 ); + glm_vec3_copy( norm_vec, (n+1)+i+3 ); + glm_vec3_copy( norm_vec, (n+1)+i+6 ); + + } } @@ -57,15 +59,15 @@ int main( void ) glewInit(); - - fill_normal( d_cube, n_cube ); + fill_normal( d_cube, d_cube+(int)*d_cube); if( !( shader = create_shader() ) ) goto error_shader; gload_program( shader, vs, VERTEX ); gload_program( shader, fs, FRAGMENT ); - use_shader( shader ); + use_shader( shader ); + load_fix_matrix( shader, (float)WIDTH/HEIGHT ); diff --git a/src/mesh.c b/src/mesh.c index 4bfb8d1..d477d27 100755 --- a/src/mesh.c +++ b/src/mesh.c @@ -4,28 +4,35 @@ struct obj { - unsigned int vertex, vao, vbo; + unsigned int vertex, n_vao, d_vao, n_vbo, d_vbo; }; -mesh_t create_mesh( narray_float_t mesh ) +mesh_t create_mesh( narray_float_t d ) { struct obj * p; p=malloc(sizeof(struct obj)); - p->vertex=(*mesh)/3; + p->vertex=(*d)/3; - glGenVertexArrays( 1, &p->vao ); - glGenBuffers( 1, &p->vbo ); + glGenVertexArrays( 1, &p->n_vao ); + glGenVertexArrays( 1, &p->d_vao ); + glGenBuffers( 1, &p->d_vbo ); + glGenBuffers( 1, &p->n_vbo ); - glBindVertexArray( p->vao ); - glBindBuffer( GL_ARRAY_BUFFER, p->vbo ); - glBufferData( GL_ARRAY_BUFFER, (p->vertex*3)*sizeof(int), mesh+1, + glBindVertexArray( p->d_vao ); + glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo ); + glBufferData( GL_ARRAY_BUFFER, 2*(p->vertex*3)*sizeof(float), d+1, GL_STATIC_DRAW ); - glVertexAttribPointer( 0,3,GL_FLOAT, 0, 3*sizeof(int), NULL ); + glVertexAttribPointer( 0,3,GL_FLOAT, 0, 3*sizeof(float), NULL ); glEnableVertexAttribArray(0); + + glVertexAttribPointer( 1,3,GL_FLOAT, *d, 3*sizeof(float), NULL ); + glEnableVertexAttribArray(1); + + return p; } @@ -33,8 +40,10 @@ void destroy_mesh( mesh_t p ) { struct obj * obj ; obj = p; - glDeleteVertexArrays( 1, &obj->vao ); - glDeleteBuffers( 1, &obj->vbo ); + glDeleteVertexArrays( 1, &obj->d_vao ); + glDeleteVertexArrays( 1, &obj->n_vao ); + glDeleteBuffers( 1, &obj->d_vbo ); + glDeleteBuffers( 1, &obj->n_vbo ); free( p ); } @@ -42,7 +51,7 @@ void draw_mesh( mesh_t p ) { struct obj * obj=p; - glBindVertexArray( obj->vao ); + glBindVertexArray( obj->d_vao ); #ifdef DEBUG { int i;