-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader_Lights.fs
200 lines (156 loc) · 6.13 KB
/
shader_Lights.fs
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#version 330 core
out vec4 FragColor;
#define NUMBER 26
in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;
//uniform sampler2D texture_diffuse1;
//struct Material
//{
// sampler2D diffuse;
// sampler2D specular;
// float shininess;
//};
struct DirLight
{
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct PointLight
{
vec3 position;
float constant;
float linear;
float quadratic;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct SpotLight
{
vec3 position;
vec3 direction;
float cutOff;
float outerCutOff;
float constant;
float linear;
float quadratic;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
uniform vec3 viewPos;
uniform DirLight dirLight;
uniform PointLight pointLight[NUMBER];
uniform SpotLight spotLight;
//uniform Material material;
uniform sampler2D material_diffuse;
uniform sampler2D material_specular;
uniform float material_shininess;
// Function prototypes
vec3 CalcDirLight( DirLight light, vec3 normal, vec3 viewDir );
vec3 CalcPointLight( PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir );
vec4 CalcSpotLight( SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir );
void main()
{
//Properties needed to lighting
vec3 viewDir = normalize(viewPos - FragPos);
vec3 norm = normalize(Normal);
//Directional lighting
vec3 result = CalcDirLight(dirLight, norm, viewDir);
//Point Light
for(int i = 0; i < NUMBER; i++)
{
result += CalcPointLight(pointLight[i], norm, FragPos, viewDir);
}
// Spot light
//result += CalcSpotLight( spotLight, norm, FragPos, viewDir );
vec4 texColor = vec4( result,texture( material_diffuse, TexCoords).a );
if(texColor.a < 0.1)
discard;
FragColor = texColor;
}
// 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 * vec3( texture( material_diffuse, TexCoords ).rgb );
vec3 diffuse = light.diffuse * diff * vec3( texture( material_diffuse, TexCoords ).rgb );
vec3 specular = light.specular * spec * vec3( texture( material_specular, TexCoords ).rgb );
vec3 result = ambient + diffuse + specular;
return (result);
}
// Calculates the color when using a point light.
vec3 CalcPointLight( PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir )
{
vec3 lightDir = normalize( light.position - fragPos );
// 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 );
// Attenuation
float distance = length( light.position - fragPos );
float attenuation = 1.0f / ( light.constant + light.linear * distance + light.quadratic * ( distance * distance ) );
// Combine results
vec3 ambient = light.ambient * vec3( texture( material_diffuse, TexCoords ).rgb );
vec3 diffuse = light.diffuse * diff * vec3( texture( material_diffuse, TexCoords ).rgb );
vec3 specular = light.specular * spec * vec3( texture( material_specular, TexCoords ).rgb );
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
vec3 result=ambient + diffuse + specular;
return (result);
}
// Calculates the color when using a spot light.
vec4 CalcSpotLight( SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir )
{
vec3 lightDir = normalize(light.position - FragPos);
// check if lighting is inside the spotlight cone
float theta = dot(lightDir, normalize(-light.direction));
if(theta > light.cutOff) // remember that we're working with angles as cosines instead of degrees so a '>' is used.
{
// ambient
vec3 ambient = light.ambient * texture(material_diffuse, TexCoords).rgb;
// diffuse
vec3 norm = normalize(Normal);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light.diffuse * diff * texture(material_diffuse, TexCoords).rgb;
// specular
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material_shininess);
vec3 specular = light.specular * spec * texture(material_specular, TexCoords).rgb;
// attenuation
float distance = length(light.position - FragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
// ambient *= attenuation; // remove attenuation from ambient, as otherwise at large distances the light would be darker inside than outside the spotlight due the ambient term in the else branche
diffuse *= attenuation;
specular *= attenuation;
//vec3 result = ambient + diffuse + specular;
//FragColor = vec4(result, 1.0);
vec4 result = vec4(ambient + diffuse + specular,texture(material_diffuse, TexCoords).a) ;
if(result.a < 0.1)
discard;
FragColor = result;
return (result);
}
else
{
// else, use ambient light so scene isn't completely dark outside the spotlight.
//FragColor = vec4(light.ambient * texture(material_diffuse, TexCoords).rgb, 1.0);
vec4 result = vec4(light.ambient * texture(material_diffuse, TexCoords).rgb, texture(material_diffuse, TexCoords).a);
if(result.a < 0.1)
discard;
FragColor = result;
return (result);
}
}