aboutsummaryrefslogtreecommitdiffstats
path: root/dev/a5-artrender/shaders/phong.frag
blob: 5c51159c21045e6fdc9a3bb0b773e005c9dcf0bf (plain) (blame)
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
#version 330

in vec3 position_in_eye_space;
in vec3 normal_in_eye_space;

out vec4 color;

uniform vec3 light_in_eye_space;
uniform vec4 Ia, Id, Is;

uniform vec4 ka, kd, ks;
uniform float s;


void main() {
    // transoform the vertex position into "eye space"
    vec3 v = position_in_eye_space;

    // unit vector from the vertex to the light
    vec3 l = normalize(light_in_eye_space - v);
    
    // unit vector from the vertex to the eye point, which is at 0,0,0 in "eye space"
    vec3 e = normalize(vec3(0,0,0) - v);

    // normal transformed into "eye space"
    vec3 n = normalize(normalize(normal_in_eye_space));
    // halfway vector
    vec3 h = normalize(e+l);

    // calculate color using the light intensity equation
    vec4 ambient = ka * Ia;
    vec4 diffuse = kd * Id * max(dot(n, l), 0);
    vec4 specular = ks * Is * pow(max(dot(h, n), 0), s);
    
    color = ambient + diffuse + specular;
}