-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvertexShaderForGouraudShading.vs
More file actions
163 lines (123 loc) · 4.31 KB
/
Copy pathvertexShaderForGouraudShading.vs
File metadata and controls
163 lines (123 loc) · 4.31 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
150
151
152
153
154
155
156
157
158
159
160
161
162
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
out vec4 LightingColor;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
struct Material {
vec3 ambient;
vec3 diffuse;
vec3 specular;
float shininess;
};
struct DirLight {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
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 cutOff;
float outerCutOff;
float k_c; // attenuation factors
float k_l; // attenuation factors
float k_q; // attenuation factors
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
#define NR_POINT_LIGHTS 4
uniform vec3 viewPos;
uniform DirLight dirLight;
uniform PointLight pointLights[NR_POINT_LIGHTS];
uniform SpotLight spotLight;
uniform Material material;
// function prototypes
vec3 CalcDirLight(Material material, DirLight light, vec3 N, vec3 V);
vec3 CalcPointLight(Material material, PointLight light, vec3 N, vec3 Pos, vec3 V);
vec3 CalcSpotLight(Material material, SpotLight light, vec3 N, vec3 Pos, vec3 V);
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0);
vec3 Pos = vec3(model * vec4(aPos, 1.0));
vec3 Normal = mat3(transpose(inverse(model))) * aNormal;
// properties
vec3 N = normalize(Normal);
vec3 V = normalize(viewPos - Pos);
// directional lighting
vec3 result = CalcDirLight(material, dirLight, N, V);
// point lights
for(int i = 0; i < NR_POINT_LIGHTS; i++)
result += CalcPointLight(material, pointLights[i], N, Pos, V);
// spot light
result += CalcSpotLight(material, spotLight, N, Pos, V);
LightingColor = vec4(result, 1.0);
}
// calculates the color when using a directional light.
vec3 CalcDirLight(Material material, DirLight light, vec3 N, vec3 V)
{
vec3 L = normalize(-light.direction);
vec3 R = reflect(-L, N);
vec3 K_A = material.ambient;
vec3 K_D = material.diffuse;
vec3 K_S = material.specular;
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;
return (ambient + diffuse + specular);
}
// calculates the color when using a point light.
vec3 CalcPointLight(Material material, PointLight light, vec3 N, vec3 Pos, vec3 V)
{
vec3 L = normalize(light.position - Pos);
vec3 R = reflect(-L, N);
vec3 K_A = material.ambient;
vec3 K_D = material.diffuse;
vec3 K_S = material.specular;
// attenuation
float d = length(light.position - Pos);
float attenuation = 1.0 / (light.k_c + light.k_l * d + light.k_q * (d * d));
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;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
}
// calculates the color when using a spot light.
vec3 CalcSpotLight(Material material, SpotLight light, vec3 N, vec3 Pos, vec3 V)
{
vec3 L = normalize(light.position - Pos);
vec3 R = reflect(-L, N);
vec3 K_A = material.ambient;
vec3 K_D = material.diffuse;
vec3 K_S = material.specular;
// attenuation
float d = length(light.position - Pos);
float attenuation = 1.0 / (light.k_c + light.k_l * d + light.k_q * (d * d));
// spotlight intensity
float theta = dot(L, normalize(-light.direction));
float epsilon = light.cutOff - light.outerCutOff;
float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
// combine results
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);
}