-
Notifications
You must be signed in to change notification settings - Fork 2
/
object.frag
76 lines (56 loc) · 1.88 KB
/
object.frag
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#version 330 core
// This is a sample fragment shader.
struct Material {
vec3 ambient;
vec3 diffuse;
vec3 specular;
float shininess;
};
struct DirLight {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
// Inputs to the fragment shader are the outputs of the same name from the vertex shader.
// Note that you do not have access to the vertex shader's default output, gl_Position.
in vec3 FragPos;
in vec3 Normal;
// You can output many things. The first vec4 type output determines the color of the fragment
out vec4 color;
// Forward declaration
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);
// Uniform
uniform DirLight dirLight;
uniform Material material;
uniform mat4 view;
void main()
{
// Color everything a hot pink color. An alpha of 1.0f means it is not transparent.
vec3 norm = normalize(Normal);
vec3 result = material.ambient;
float viewx = view[3][0];
float viewy = view[3][1];
float viewz = view[3][2];
vec3 viewPos = {viewx, viewy, viewz};
vec3 viewDir = normalize(viewPos - FragPos);
// Directional
result = CalcDirLight(dirLight, norm, viewDir) * result;
// Output
color = vec4(result, 1.0f);
}
// Calculates the color when using a directional light.
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
{
vec3 lightDir = normalize(-light.direction);
// Diffuse shading
float diff = max(dot(normal, lightDir), 0.0);
// Specular shading
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
// Combine results
vec3 ambient = light.ambient * material.diffuse;
vec3 diffuse = light.diffuse * diff * material.diffuse;
vec3 specular = light.specular * spec * material.specular;
return (ambient + diffuse + specular);
}