aboutsummaryrefslogtreecommitdiff
path: root/07-july/resources/shaders/shader.frag
blob: 406d004f590f8d6dbec2c85b955974f6f9ad9eb1 (plain)
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
#version 420

in vec3 World_Normal;
in vec2 Fragment_UV;
in vec4 Fragment_Color;
in vec3 toLightVector[4];
in vec3 toEyeVector;

out vec4 out_color;

uniform vec4 lightColor[4];
uniform vec3 attenuation[4];
uniform sampler2D Texture;

void main()
{
    vec4 totalDiffuse = vec4(0.0, 0.0, 0.0, 1.0);
    vec4 totalSpecular = vec4(0.0, 0.0, 0.0, 1.0);

    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(World_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, World_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 = 1;
        /* 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;
}