-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfragmentShaderForPhongShadingWithTexture.fs
More file actions
149 lines (116 loc) · 4.38 KB
/
Copy pathfragmentShaderForPhongShadingWithTexture.fs
File metadata and controls
149 lines (116 loc) · 4.38 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
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
#version 330 core
out vec4 FragColor;
struct Material {
sampler2D diffuse;
sampler2D specular;
float shininess;
};
struct PointLight {
vec3 position;
float k_c; // attenuation factors
float k_l; // attenuation factors
float k_q; // attenuation factors
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct SpotLight {
vec3 position;
vec3 direction;
float inner_circle;
float outer_circle;
float k_c; // attenuation factors
float k_l; // attenuation factors
float k_q; // attenuation factors
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct DirectionalLight {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
bool on;
};
#define NR_POINT_LIGHTS 64
#define NR_SPOT_LIGHTS 5
in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;
uniform vec3 viewPos;
uniform PointLight pointLights[NR_POINT_LIGHTS];
uniform SpotLight spotLights[NR_SPOT_LIGHTS];
uniform Material material;
uniform bool directionLightOn = true;
uniform DirectionalLight directionalLight;
// function prototypes
vec3 CalcPointLight(Material material, PointLight light, vec3 N, vec3 fragPos, vec3 V);
vec3 CalcDirectionalLight(Material material, DirectionalLight light, vec3 N, vec3 V);
vec3 CalcSpotLight(Material material, SpotLight light, vec3 N, vec3 fragPos, vec3 V);
void main()
{
// properties
vec3 N = normalize(Normal);
vec3 V = normalize(viewPos - FragPos);
vec3 result;
// point lights
for(int i = 0; i < NR_POINT_LIGHTS; i++){
result += CalcPointLight(material, pointLights[i], N, FragPos, V);
}
if(directionLightOn){
result+=CalcDirectionalLight(material, directionalLight, N, V);
}
for(int i = 0; i < NR_SPOT_LIGHTS; i++){
result += CalcSpotLight(material, spotLights[i], N, FragPos, V);
}
FragColor = vec4(result, 1.0);
}
// calculates the color when using a point light.
vec3 CalcPointLight(Material material, PointLight light, vec3 N, vec3 fragPos, vec3 V)
{
vec3 L = normalize(light.position - fragPos);
vec3 R = reflect(-L, N);
// attenuation
float d = length(light.position - fragPos);
float attenuation = 1.0 / (light.k_c + light.k_l * d + light.k_q * (d * d));
vec3 ambient = vec3(texture(material.diffuse, TexCoords)) * light.ambient;
//vec3 ambient = vec3(1.0f, 0.0f, 0.0f) * light.ambient;
vec3 diffuse = vec3(texture(material.diffuse, TexCoords)) * max(dot(N, L), 0.0) * light.diffuse;
//vec3 diffuse = vec3(1.0f, 0.0f, 0.0f) * max(dot(N, L), 0.0) * light.diffuse;
vec3 specular = vec3(texture(material.specular, TexCoords)) * pow(max(dot(V, R), 0.0), material.shininess) * light.specular;
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
}
vec3 CalcDirectionalLight(Material material, DirectionalLight light, vec3 N, vec3 V)
{
vec3 L = normalize(-light.direction);
vec3 R = reflect(-L, N);
vec3 ambient = vec3(texture(material.diffuse, TexCoords)) * light.ambient;
vec3 diffuse = vec3(texture(material.diffuse, TexCoords)) * max(dot(N, L), 0.0) * light.diffuse;
vec3 specular = vec3(texture(material.specular, TexCoords)) * pow(max(dot(V, R), 0.0), material.shininess) * light.specular;
return (ambient + diffuse + specular);
}
vec3 CalcSpotLight(Material material, SpotLight light, vec3 N, vec3 fragPos, vec3 V)
{
vec3 L = normalize(light.position - fragPos);
vec3 R = reflect(-L, N);
vec3 K_A = vec3(texture(material.diffuse, TexCoords));
vec3 K_D = vec3(texture(material.diffuse, TexCoords));
vec3 K_S = vec3(texture(material.specular, TexCoords));
// attenuation
float d = length(light.position - fragPos);
float attenuation = 1.0 / (light.k_c + light.k_l * d + light.k_q * (d * d));
float cos_alpha = dot(L, normalize(-light.direction));
float cos_theta = light.inner_circle- light.outer_circle;
float intensity = clamp((cos_alpha-light.outer_circle)/cos_theta, 0.0, 1.0);
vec3 ambient = K_A * light.ambient;
vec3 diffuse = K_D * max(dot(N, L), 0.0) * light.diffuse;
vec3 specular = K_S * pow(max(dot(V, R), 0.0), material.shininess) * light.specular;
ambient *= attenuation * intensity;
diffuse *= attenuation * intensity;
specular *= attenuation * intensity;
return (ambient + diffuse + specular);
}