-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.frag
137 lines (92 loc) · 3.4 KB
/
default.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#version 330 core
out vec4 FragColor;
in vec3 currentPos;
in vec3 normal;
// inputs the color from vertex shader
// must have same name as in the vertex shader
in vec3 color;
in vec2 texCoord;
// Textures
uniform sampler2D diffuse0;
uniform sampler2D specular0;
uniform vec4 lightColor;
uniform vec3 lightPos;
uniform vec3 camPos;
vec4 PointLight()
{
vec3 lightVec = lightPos - currentPos;
float dist = length(lightVec);
float a = 1.0f;
float b = 0.04f;
float intensity = 1.0f / (a * dist * dist + b * dist + 1.0f);
float ambient = 0.2f;
vec3 normalVec = normalize(normal);
vec3 lightDirection = normalize(lightVec);
float diffuse = max(dot(normalVec, lightDirection), 0.0f);
float specularLight = 0.5f;
vec3 viewDirection = normalize(camPos - currentPos);
vec3 reflectionDirection = reflect(-lightDirection, normal);
float specAmount = pow(max(dot(viewDirection, reflectionDirection), 0.0f), 2);
float specular = specAmount * specularLight;
return (texture(diffuse0, texCoord) * (diffuse * intensity + ambient) + texture(specular0, texCoord).r * specular * intensity) * lightColor;
}
vec4 DirectionalLight()
{
vec3 lightVec = lightPos - currentPos;
float dist = length(lightVec);
float a = 1.0f;
float b = 0.04f;
float intensity = 1.0f / (a * dist * dist + b * dist + 1.0f);
float ambient = 0.2f;
vec3 normalVec = normalize(normal);
vec3 lightDirection = normalize(vec3(1.0f, 1.0f, 0.0f));
float diffuse = max(dot(normalVec, lightDirection), 0.0f);
float specularLight = 0.5f;
vec3 viewDirection = normalize(camPos - currentPos);
vec3 reflectionDirection = reflect(-lightDirection, normal);
float specAmount = pow(max(dot(viewDirection, reflectionDirection), 0.0f), 2);
float specular = specAmount * specularLight;
return (texture(diffuse0, texCoord) * (diffuse + ambient) + texture(specular0, texCoord).r * specular) * lightColor;
}
vec4 SpotLight()
{
// Cosine of angles
float outerCone = 0.9f;
float innerCone = 0.95f;
float ambient = 0.2f;
vec3 normalVec = normalize(normal);
vec3 lightDirection = normalize(lightPos - currentPos);
float diffuse = max(dot(normalVec, lightDirection), 0.0f);
float specularLight = 0.5f;
vec3 viewDirection = normalize(camPos - currentPos);
vec3 reflectionDirection = reflect(-lightDirection, normal);
float specAmount = pow(max(dot(viewDirection, reflectionDirection), 0.0f), 2);
float specular = specAmount * specularLight;
float angle = dot(vec3(0.0f, -1.0f, 0.0f), -lightDirection);
float intensity = clamp((angle - outerCone) / (innerCone - outerCone), 0.0f, 1.0f);
return (texture(diffuse0, texCoord) * (diffuse * intensity + ambient) + texture(specular0, texCoord).r * specular * intensity) * lightColor;
}
vec4 rDirectionalLight()
{
vec3 lightVec = lightPos - currentPos;
float dist = length(lightVec);
float a = 1.0f;
float b = 0.04f;
float intensity = 1.0f / (a * dist * dist + b * dist + 1.0f);
// ambient
float ambientStrength = 0.3;
vec4 ambient = lightColor * ambientStrength;
// diffuse
vec3 norm = normalize(normal);
vec3 lightDir = normalize(lightPos - currentPos);
float diff = max(dot(norm, lightDir), 0.0);
vec4 diffuse = lightColor * diff;
return (diffuse + ambient) * vec4(color,1.0f) ;
}
void main()
{
FragColor = vec4(color,1.0);
//FragColor = rDirectionalLight();
//FragColor = vec4(currentPos, 1.0);
// FragColor = texture(diffuse0, texCoord) * (diffuse + ambient) * lightColor;
}