diff --git a/shaders/manif.fs b/shaders/manif.fs index 855c144..03829a0 100644 --- a/shaders/manif.fs +++ b/shaders/manif.fs @@ -7,9 +7,15 @@ out vec4 fragColor; uniform vec4 couleur; uniform vec4 lumpos; +uniform mat4 view; void main() { + const vec3 vue = vec3(0, 0, -1); + const vec4 soleil_color = vec4(0.81f, 0.78f, 0.66f, 1.0f); vec3 torche = normalize(pos_model.xyz - lumpos.xyz); - float intensite_eclairage = clamp(dot(vec_normal, -torche), 0, 1); - fragColor = intensite_eclairage * couleur; + float intensite_lumineuse = clamp(dot(vec_normal, -torche), 0, 1); + vec3 reflet = (transpose(inverse(view)) * vec4(reflect(torche, vec_normal), 0)).xyz; + float intensite_specularite = pow(clamp(dot(reflet, -vue), 0, 1), 10); + + fragColor = intensite_specularite * vec4(1.0f, 0.0f, 0.0f, 1.0f) + 0.1 * soleil_color + 0.9 * intensite_lumineuse * couleur; } diff --git a/src/manif.c b/src/manif.c index 1426020..2e23b23 100644 --- a/src/manif.c +++ b/src/manif.c @@ -49,38 +49,36 @@ static void draw(void) { const GLfloat couleur_plan[] = {0.3f, 0.3f, 0.3f, 1}, couleur_heros[] = {1, 1, 0, 1}; - GLint couleur_loc = glGetUniformLocation(_pId, "couleur"); + GLint couleur_gpu = glGetUniformLocation(_pId, "couleur"); static double deplacement = 0; - const GLfloat lumpos[] = {1, 2, 3, 1}; - GLint lum_loc = glGetUniformLocation(_pId, "lumpos"); - glUniform4fv(lum_loc, 1, lumpos); + const GLfloat lumpos[] = {1, (GLfloat)(2 + 2 * sin(deplacement)), 1, 1}; + glUniform4fv(glGetUniformLocation(_pId, "lumpos"), 1, lumpos); - gl4duBindMatrix(matrix_proj); - gl4duLoadIdentityf(); + bindAndLoadf(matrix_proj); GLfloat ratio = (GLfloat)_dims[0] / (GLfloat)_dims[1]; gl4duFrustumf(-1, 1, -ratio, ratio, 1, 1000); - gl4duBindMatrix(matrix_view); - gl4duLoadIdentityf(); + bindAndLoadf(matrix_view); const GLfloat distance = 2; - gl4duLookAtf(0, distance, distance, 0, 0, 0, 0, 1, 0); + const GLfloat eye[3] = {0, distance, distance}; + gl4duLookAtf(eye[0], eye[1], eye[2], 0, 0, 0, 0, 1, 0); + glUniform4fv(glGetUniformLocation(_pId, "camerapos"), 1, eye); - gl4duBindMatrix(matrix_model); - gl4duLoadIdentityf(); + bindAndLoadf(matrix_model); gl4duRotatef(-90, 1, 0, 0); gl4duScalef(4 * distance, 2 * distance, 1); gl4duSendMatrices(); - glUniform4fv(couleur_loc, 1, couleur_plan); + glUniform4fv(couleur_gpu, 1, couleur_plan); gl4dgDraw(_planId); - gl4duBindMatrix(matrix_model); - gl4duLoadIdentityf(); + bindAndLoadf(matrix_model); gl4duScalef(1, 1.5, 1); + gl4duRotatef((GLfloat)(180 * deplacement / M_PI), 0, 1, 0); gl4duSendMatrices(); - glUniform4fv(couleur_loc, 1, couleur_heros); + glUniform4fv(couleur_gpu, 1, couleur_heros); gl4dgDraw(_herosId); deplacement += 0.4 * M_PI * dt; glUseProgram(0);