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
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#version 330 core
#define MAX_TEXTURES 32
struct Material {
sampler2D diffuse[MAX_TEXTURES];
sampler2D specular[MAX_TEXTURES];
float shininess;
};
struct DirectionalLight {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct PointLight {
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
// attentuation factors
float kC;
float kL;
float kQ;
};
struct SpotLight {
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
// attenuation factors
float kC;
float kL;
float kQ;
// vector for the direction directly in front of the spotlight
vec3 front;
// spot radius
float radius_inner;
float radius_outer; // to smooth out the light
};
// this is the result of a light creation. This contains the multipliers for each kind of a light we want
// to have.
struct LightFactor {
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
in vec2 TexCoords;
in vec3 FragNormal;
in vec3 VertexWorldPos;
uniform Material material;
uniform PointLight pointLight;
uniform DirectionalLight dirLight;
uniform vec3 cameraPosition;
out vec4 FragColor;
LightFactor make_directional_light(DirectionalLight light, vec3 CONST_viewDir) {
LightFactor res;
vec3 DL_lightDir = normalize(-light.direction);
res.ambient = light.ambient;
float DL_diffuseStrength = max(dot(DL_lightDir, FragNormal), 0.0);
res.diffuse = light.diffuse * DL_diffuseStrength;
vec3 DL_reflectDir = reflect(-DL_lightDir, FragNormal);
float DL_specularity = max(dot(CONST_viewDir, DL_reflectDir), 0.0);
float DL_shinePower = pow(DL_specularity, material.shininess);
res.specular = light.specular * DL_shinePower;
return res;
};
LightFactor make_point_light(PointLight light, vec3 CONST_viewDir) {
LightFactor res;
float PL_lightDistance = length(light.position - VertexWorldPos);
float PL_attenuationFactor = 1.0 /
(light.kC + (light.kL * PL_lightDistance) + (light.kQ * PL_lightDistance * PL_lightDistance));
res.ambient = PL_attenuationFactor * light.ambient;
vec3 PL_lightDir = normalize(light.position - VertexWorldPos);
float PL_diffuseStrength = max(dot(PL_lightDir, FragNormal), 0.0);
res.diffuse = PL_attenuationFactor * light.diffuse * PL_diffuseStrength;
vec3 PL_reflectDir = reflect(-PL_lightDir, FragNormal);
float PL_specularity = max(dot(CONST_viewDir, PL_reflectDir), 0.0);
float PL_shinePower = pow(PL_specularity, material.shininess);
res.specular = PL_attenuationFactor * PL_shinePower * light.specular;
return res;
}
LightFactor make_spot_light(SpotLight light, vec3 CONST_viewDir) {
LightFactor res;
float SL_lightDistance = length(light.position - VertexWorldPos);
float SL_attenuationFactor = 1.0 /
(light.kC + (light.kL * SL_lightDistance) + (light.kQ * SL_lightDistance * SL_lightDistance));
vec3 SL_lightDir = normalize(light.position - VertexWorldPos);
res.ambient = SL_attenuationFactor * light.ambient;
float SL_diffAmount = dot(SL_lightDir, normalize(-light.front));
float SL_spotLightFadeFactor = clamp((SL_diffAmount - light.radius_outer)/(light.radius_inner - light.radius_outer), 0.0f, 1.0f);
float SL_diffuseStrength = max(dot(SL_lightDir, FragNormal), 0.0);
res.diffuse = SL_spotLightFadeFactor * SL_attenuationFactor * light.diffuse * SL_diffuseStrength;
vec3 SL_reflectDir = reflect(-SL_lightDir, FragNormal);
float SL_specularity = max(dot(CONST_viewDir, SL_reflectDir), 0.0);
float SL_shinePower = pow(SL_specularity, material.shininess);
res.specular = SL_spotLightFadeFactor * SL_attenuationFactor * SL_shinePower * light.specular;
return res;
}
void main() {
//vec3 CONST_viewDir = normalize(cameraPosition - VertexWorldPos);
//vec3 combinedAmbience = vec3(0.0);
//vec3 combinedDiffuse = vec3(0.0);
//vec3 combinedSpecular = vec3(0.0);
//LightFactor DL_factors = make_directional_light(dirLight, CONST_viewDir);
//combinedAmbience += DL_factors.ambient;
//combinedDiffuse += DL_factors.diffuse;
//combinedSpecular += DL_factors.specular;
//LightFactor PL_factors = make_point_light(pointLight, CONST_viewDir);
//combinedAmbience += PL_factors.ambient;
//combinedDiffuse += PL_factors.diffuse;
//combinedSpecular += PL_factors.specular;
vec3 diffuseLight = vec3(texture(material.diffuse[0], TexCoords));
vec3 color = diffuseLight;
FragColor = vec4(color, 1.0);
};
|