#version 420 in vec2 Fragment_UV; in vec4 Fragment_Color; in vec3 toLightVector[4]; in vec3 toEyeVector; layout(location = 0) out vec4 out_color; uniform vec4 lightColor[4]; uniform vec3 attenuation[4]; uniform sampler2D Texture; uniform sampler2D normalMap; void main() { vec4 totalDiffuse = vec4(0.0, 0.0, 0.0, 1.0); vec4 totalSpecular = vec4(0.0, 0.0, 0.0, 1.0); /* We get the normal from the normal map and we transform it into world space */ vec3 normal = normalize( (255.0 / 128.0 * texture(normalMap, Fragment_UV) - 1.0).xyz ); for(int i = 0; i < 4; i++) { /*Light Attenuation*/ float dist = length(toLightVector[i]); float attFactor = attenuation[i].x + (attenuation[i].y * dist) + (attenuation[i].z * dist * dist); vec3 unitToLightVector = normalize(toLightVector[i]); /*Diffuse lighting*/ /*La intensidad es el cos entre la normal y el vector hacia la luz*/ float brightness = dot(unitToLightVector, normalize(normal)); /*No queremos luz negativa ni mayor a 1*/ brightness = max(brightness, 0.0); totalDiffuse = totalDiffuse + (brightness * lightColor[i]) / attFactor; /* Specular lighting */ /* Reflejamos el vector hacia la luz (su inverso) en la normal */ vec3 Vector_ReflectedLight = reflect(-unitToLightVector, normal); /* La luz especular es el cos del angulo entre el vector hacia el ojo y la luz reflectada en la normal */ float specularity = clamp( dot(Vector_ReflectedLight, toEyeVector), 0, 1 ); float reflectivity = 0.5; /* Que tan grande es el specular highlight */ specularity = pow(specularity, 10); totalSpecular = totalSpecular + clamp( reflectivity * specularity * lightColor[i], 0, 1) / attFactor; } /* Ambient Light */ totalDiffuse = max(totalDiffuse, 0.2); /* Juntamos todo para el color final*/ out_color = totalDiffuse * texture(Texture, Fragment_UV) + totalSpecular; }