This commit is contained in:
Your Name
2024-10-17 17:58:55 -06:00
parent 3137c23479
commit fda999bfb1
4 changed files with 51 additions and 26 deletions

View File

@@ -31,12 +31,6 @@ narray_float_t d_cube =
A B E A B E
B E F B E F
};
narray_float_t n_cube =
{
3 * 3 * 2 * 6,
A C E A C E
C E G C E G

View File

@@ -2,6 +2,7 @@ const char * vs =
"#version 330 core\n" "#version 330 core\n"
"layout (location = 0) in vec3 aPos;" "layout (location = 0) in vec3 aPos;"
"layout (location = 1) in vec3 aNormal;"
"uniform float idx;" "uniform float idx;"
"uniform mat4 fix;" "uniform mat4 fix;"
@@ -9,13 +10,19 @@ const char * vs =
"uniform mat4 mdl;" "uniform mat4 mdl;"
"out float index;" "out float index;"
"out vec3 Normal;"
"out vec3 FragPos;"
"void main()" "void main()"
"{" "{"
" index=idx;" " index=idx;"
" gl_Position = fix * rot * mdl * vec4( aPos, 1.0 );\n" " gl_Position = fix * rot * mdl * vec4( aPos, 1.0 );\n"
"Normal = aNormal;"
"FragPos = vec3( rot * vec4(aPos, 1.0));"
"}"; "}";
const char * fs = const char * fs =
"#version 330 core\n" "#version 330 core\n"
@@ -23,8 +30,21 @@ const char * fs =
"in float index;" "in float index;"
"out vec4 FragColor;" "out vec4 FragColor;"
"in vec3 Normal;"
"in vec3 FragPos;"
"void main()" "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;"
"}"; "}";

8
src/main.c Normal file → Executable file
View File

@@ -29,7 +29,7 @@ void calc_normal(float* v1, float* v2, float* v3, float* normal)
void fill_normal( float * d, float * n ) void fill_normal( float * d, float * n )
{ {
*n = *d; //*n = *d;
for (int i = 0; i < *d; i += 9) for (int i = 0; i < *d; i += 9)
{ {
@@ -38,6 +38,8 @@ void fill_normal( float * d, float * n )
glm_vec3_copy( norm_vec, (n+1)+i ); 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+3 );
glm_vec3_copy( norm_vec, (n+1)+i+6 ); glm_vec3_copy( norm_vec, (n+1)+i+6 );
} }
} }
@@ -57,8 +59,7 @@ int main( void )
glewInit(); glewInit();
fill_normal( d_cube, d_cube+(int)*d_cube);
fill_normal( d_cube, n_cube );
if( !( shader = create_shader() ) ) if( !( shader = create_shader() ) )
goto error_shader; goto error_shader;
@@ -67,6 +68,7 @@ int main( void )
gload_program( shader, fs, FRAGMENT ); gload_program( shader, fs, FRAGMENT );
use_shader( shader ); use_shader( shader );
load_fix_matrix( shader, (float)WIDTH/HEIGHT ); load_fix_matrix( shader, (float)WIDTH/HEIGHT );
if( !( m_cube = create_mesh( d_cube ) ) ) if( !( m_cube = create_mesh( d_cube ) ) )

View File

@@ -4,28 +4,35 @@
struct obj 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; struct obj * p;
p=malloc(sizeof(struct obj)); p=malloc(sizeof(struct obj));
p->vertex=(*mesh)/3; p->vertex=(*d)/3;
glGenVertexArrays( 1, &p->vao ); glGenVertexArrays( 1, &p->n_vao );
glGenBuffers( 1, &p->vbo ); glGenVertexArrays( 1, &p->d_vao );
glGenBuffers( 1, &p->d_vbo );
glGenBuffers( 1, &p->n_vbo );
glBindVertexArray( p->vao ); glBindVertexArray( p->d_vao );
glBindBuffer( GL_ARRAY_BUFFER, p->vbo ); glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo );
glBufferData( GL_ARRAY_BUFFER, (p->vertex*3)*sizeof(int), mesh+1, glBufferData( GL_ARRAY_BUFFER, 2*(p->vertex*3)*sizeof(float), d+1,
GL_STATIC_DRAW ); 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); glEnableVertexAttribArray(0);
glVertexAttribPointer( 1,3,GL_FLOAT, *d, 3*sizeof(float), NULL );
glEnableVertexAttribArray(1);
return p; return p;
} }
@@ -33,8 +40,10 @@ void destroy_mesh( mesh_t p )
{ {
struct obj * obj ; struct obj * obj ;
obj = p; obj = p;
glDeleteVertexArrays( 1, &obj->vao ); glDeleteVertexArrays( 1, &obj->d_vao );
glDeleteBuffers( 1, &obj->vbo ); glDeleteVertexArrays( 1, &obj->n_vao );
glDeleteBuffers( 1, &obj->d_vbo );
glDeleteBuffers( 1, &obj->n_vbo );
free( p ); free( p );
} }
@@ -42,7 +51,7 @@ void draw_mesh( mesh_t p )
{ {
struct obj * obj=p; struct obj * obj=p;
glBindVertexArray( obj->vao ); glBindVertexArray( obj->d_vao );
#ifdef DEBUG #ifdef DEBUG
{ {
int i; int i;