1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#version 420
in vec2 Fragment_UV;
in vec4 Fragment_Color;
in vec3 toLightVector[4];
in vec3 toEyeVector;
in vec3 tangent0;
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;
out_color = vec4(tangent0, 1.0);
}
|