summaryrefslogtreecommitdiff
path: root/demo.fs
blob: 65700b74cf8e9dfc1b94b3b2b9c8d2092f167fce (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
#version 330 core

in vec3 normal;
in vec2 texture_coordinate;
in vec3 fragment_position;

uniform vec3 ambient_color;

uniform vec3 light_color;
uniform vec3 light_position;

uniform sampler2D box_texture;
uniform sampler2D happy_texture;

uniform mat4 model;
uniform mat4 view;

out vec4 fragment_color;

void main()
{
  /* diffuse light */
  vec3 norm = normalize(normal);
  vec3 light_pos = vec3(view * vec4(light_position.xyz, 1.0));
  vec3 light_direction = normalize(light_pos - fragment_position);
  float diffuse = max( dot(norm, light_direction), 0 );
  vec3 diffuse_light = light_color * diffuse;

  /* specular reflection */
  vec3 view_direction = normalize(-fragment_position);
  vec3 reflection_direction = reflect(-light_direction, norm);
  float specular = pow(max(dot(view_direction, reflection_direction), 0), 32);
  vec3 specular_light = 1.0 * specular * light_color;

  vec4 total_light = vec4((ambient_color + diffuse_light + specular_light).xyz, 1.0);
  fragment_color = total_light * texture(box_texture, texture_coordinate);
}