summaryrefslogtreecommitdiff
path: root/engine/light.glsl
blob: 90539a12c785f4ceb686a2a19923354a6f4a5380 (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 330 core

#ifndef LIGHT_GLSL
#define LIGHT_GLSL

struct Material {
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
    float shininess;
}; 

struct Light {
	vec3 position;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
}; 
  
uniform Material material;
uniform Light light;

vec3 calc_ambient_light()
{
	return light.ambient * material.ambient;
}

vec3 calc_diffuse_light(in vec3 light_dir, in vec3 norm)
{
	float diff = max(dot(norm, light_dir), 0.0);

	return light.diffuse * (diff * material.diffuse);
}

vec3 calc_specular_light(
	in vec3 light_dir,
	in vec3 norm,
	in vec3 view_pos,
	in vec3 frag_pos
)
{
	vec3 view_direction = normalize(view_pos - frag_pos);

	vec3 reflect_direction = reflect(-light_dir, norm);
	
	float spec =
		pow(max(dot(view_direction, reflect_direction), 0.0), material.shininess);

	return light.specular * (spec * material.specular);
}

#endif