Fix: specular

Muy bien!!, nada mas habian 2 errores por mi parte,
'Normal' no estaba normalizado, lo que afectaba las potencias con 'pow'.
Ademas habia un error con los buffers del shader lo que afectaba toda la iluminacion.
Sin embargo lo hiciste muy bien, solo que te falto definir viewPos.
This commit is contained in:
PedroEdiaz
2024-11-14 20:11:30 -06:00
parent 52ef897137
commit 569a8d0f42
4 changed files with 16 additions and 33 deletions

View File

@@ -4,8 +4,6 @@
void set_clean_color_context( unsigned char r, unsigned char g, unsigned char b ) void set_clean_color_context( unsigned char r, unsigned char g, unsigned char b )
{ {
glEnable( GL_DEPTH_TEST ); glEnable( GL_DEPTH_TEST );
glEnable( GL_CULL_FACE );
glCullFace( GL_BACK );
glClearColor( (float)r/0xff, (float)g/0xff, (float)b/0xff, 1.0 ); glClearColor( (float)r/0xff, (float)g/0xff, (float)b/0xff, 1.0 );
} }

View File

@@ -25,7 +25,7 @@ const char * vs =
" index=idx;" " index=idx;"
" vec3 aNormal = vec3(aNormal_x,aNormal_y,aNormal_z);" " vec3 aNormal = vec3(aNormal_x,aNormal_y,aNormal_z);"
" vec3 aPos = vec3(aPos_x,aPos_y,aPos_z);" " vec3 aPos = vec3(aPos_x,aPos_y,aPos_z);"
" Normal = mat3(transpose(inverse(rot*mdl))) * aNormal;" " Normal = normalize(mat3(transpose(inverse(rot*mdl))) * aNormal);"
" gl_Position = fix * rot * mdl * vec4( aPos, 1.0 );\n" " gl_Position = fix * rot * mdl * vec4( aPos, 1.0 );\n"
" FragPos = vec3( rot * mdl * vec4(aPos, 1.0));" " FragPos = vec3( rot * mdl * vec4(aPos, 1.0));"
"}"; "}";
@@ -52,26 +52,26 @@ const char * fs =
"uniform sampler2DArray palette;" "uniform sampler2DArray palette;"
"in float index;" "in float index;"
"out vec4 FragColor;"
"in vec3 Normal;" "in vec3 Normal;"
"in vec3 FragPos;" "in vec3 FragPos;"
"uniform vec3 viewPos;\n"
"const float specularStrength = 0.5;\n" "out vec4 FragColor;"
"void main()" "void main()"
"{" "{"
" vec4 color = texture(palette, vec3(0, 0, index));\n" " vec4 color = texture(palette, vec3(0, 0, index));\n"
" vec3 lightPos = vec3(0, 0, -15);\n"
" vec3 lightDir = normalize(lightPos - FragPos);\n"
" vec3 viewPos = vec3(0, 0, -15);\n"
" vec3 viewDir = normalize(viewPos - FragPos);\n"
" vec3 lightPos = viewPos\n;"
" vec3 lightDir = normalize(lightPos - FragPos);\n"
" vec3 reflectDir = reflect(-lightDir, Normal);\n"
" float specular = pow(max(dot(viewDir, reflectDir),0.0), 16);\n"
" float diffuse = max(dot(Normal, lightDir),0.0);\n" " float diffuse = max(dot(Normal, lightDir),0.0);\n"
" vec3 viewDir = normalize(viewPos - FragPos);\n" " vec3 result = (0.5 + 0.5 * diffuse + specular) * color.rgb;\n"
" vec3 reflectDir = reflect(-lightDir, Normal);\n"
" float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);\n"
" vec3 specular = specularStrength * spec * vec3(1.0); \n"
" vec3 result = (0.5 + 0.5 * diffuse) * color.rgb + specular;\n"
" FragColor = vec4(result, color.a);\n" " FragColor = vec4(result, color.a);\n"
"}"; "}";

View File

@@ -32,8 +32,8 @@ mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned c
glEnableVertexAttribArray(3); glEnableVertexAttribArray(3);
glGenBuffers( 1, &p->n_vbo ); glGenBuffers( 1, &p->n_vbo );
glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, p->n_vbo );
glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), n+1, glBufferData( GL_ELEMENT_ARRAY_BUFFER, p->vertex*m*sizeof(float), n+1,
GL_STATIC_DRAW ); GL_STATIC_DRAW );
glVertexAttribPointer( 4,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[0]*sizeof(float)) ); glVertexAttribPointer( 4,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[0]*sizeof(float)) );
glEnableVertexAttribArray(4); glEnableVertexAttribArray(4);

View File

@@ -43,8 +43,9 @@ void klein(float *d_surface, int i, int j, int grid_size)
float * generate_surface(int grid_size) float * generate_surface(int grid_size)
{ {
unsigned char m = 4; unsigned char m = 4;
long size = grid_size*grid_size*2*9*m;
function_t f = klein; function_t f = klein;
long size = grid_size*grid_size*6*m;
float * d_surface; float * d_surface;
int k=0; int k=0;
@@ -55,14 +56,6 @@ float * generate_surface(int grid_size)
{ {
for (int j = 0; j < grid_size; j++) 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 // triangle 1, back
f(&d_surface[k + 1], i + 1, j + 1, grid_size); f(&d_surface[k + 1], i + 1, j + 1, grid_size);
k+=m; k+=m;
@@ -78,14 +71,6 @@ float * generate_surface(int grid_size)
k+=m; k+=m;
f(&d_surface[k + 1], i + 1, j + 1, grid_size); f(&d_surface[k + 1], i + 1, j + 1, grid_size);
k+=m; 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;
} }
} }