summaryrefslogtreecommitdiff
path: root/advanced_lighting/1.blinn_phong/shaders/blinn_phong.fs
blob: 5c66b3e27135e77f0028aa427d72795d373fe463 (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
#version 330 core

in VS_OUT {
    vec3 frag_pos;
    vec3 normal;
    vec2 tex_coords;
} fs_in;

out vec4 frag_color;

uniform sampler2D texture0;
uniform vec3 light_pos;
uniform vec3 view_pos;
uniform bool blinn;

void
main(void)
{
    vec3 light_dir = normalize(light_pos-fs_in.frag_pos);
    vec3 view_dir = normalize(view_pos-fs_in.frag_pos);
    vec3 normal = normalize(fs_in.normal);

    vec3 texture_color = texture(texture0, fs_in.tex_coords).rgb;

    float diff = max(dot(normal, light_dir), 0.0);

    float spec = 0.0;
    if (blinn)
    {
        vec3 halfway_dir = normalize(light_dir+view_dir);
        spec = pow(max(dot(normal, halfway_dir), 0.0), 16.0);
    }
    else
    {
        vec3 reflect_dir = reflect(-light_dir, normal);
        spec = pow(max(dot(view_dir, reflect_dir), 0.0), 8.0);
    }

    vec3 ambient = 0.05*texture_color;
    vec3 diffuse = diff*texture_color;
    vec3 specular = vec3(0.3)*spec;

    frag_color = vec4(ambient+diffuse+specular, 1.0);
}