-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcube.fs
More file actions
60 lines (50 loc) · 1.42 KB
/
cube.fs
File metadata and controls
60 lines (50 loc) · 1.42 KB
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
#version 330 core
in vec3 normal;
in vec2 texCoords;
in vec3 color;
in vec3 fragPos;
struct PointLight {
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
float constant;
float linear;
float quadratic;
};
uniform PointLight light;
uniform vec3 viewPos;
uniform sampler2D texture;
uniform bool useBlinnPhongShading;
out vec4 fragColor;
vec3 calculateBlinnPhongShading() {
// direction vectors
vec3 lightDir = normalize(light.position - fragPos);
vec3 viewDir = normalize(viewPos - fragPos);
vec3 halfwayDir = normalize(lightDir + viewDir);
// ambient
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * light.ambient;
// diffuse
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = diff * light.diffuse;
// specular
float specularStrength = 1.0;
int shininess = 32;
float spec = pow(max(dot(viewDir, halfwayDir), 0.0), shininess);
vec3 specular = specularStrength * spec * light.specular;
return ambient + diffuse + specular;
};
float calculateAttenuation() {
float distance = length(light.position - fragPos);
return 1.0 / (light.constant + light.linear * distance + light.quadratic * distance * distance);
}
void main() {
if (useBlinnPhongShading) {
vec3 lighting = calculateBlinnPhongShading();
float attenuation = calculateAttenuation();
lighting *= attenuation;
fragColor = vec4(lighting, 1.0) * texture(texture, texCoords);
} else
fragColor = texture(texture, texCoords);
}