Debian Codegraph @

tests/manual/clip-planes-qml/light.inc.frag

raw
1 const int MAX_LIGHTS = 8;
2 const int TYPE_POINT = 0;
3 const int TYPE_DIRECTIONAL = 1;
4 const int TYPE_SPOT = 2;
5 struct Light {
6 int type;
7 vec3 position;
8 vec3 color;
9 float intensity;
10 vec3 direction;
11 float constantAttenuation;
12 float linearAttenuation;
13 float quadraticAttenuation;
14 float cutOffAngle;
15 };
16 uniform Light lights[MAX_LIGHTS];
17 uniform int lightCount;
18
19 // Pre-convolved environment maps
20 struct EnvironmentLight {
21 samplerCube irradiance; // For diffuse contribution
22 samplerCube specular; // For specular contribution
23 };
24 uniform EnvironmentLight envLight;
25 uniform int envLightCount = 0;
26
27 void adsModelNormalMapped(const in vec3 worldPos,
28 const in vec3 tsNormal,
29 const in vec3 worldEye,
30 const in float shininess,
31 const in mat3 tangentMatrix,
32 out vec3 diffuseColor,
33 out vec3 specularColor)
34 {
35 diffuseColor = vec3(0.0);
36 specularColor = vec3(0.0);
37
38 // We perform all work in tangent space, so we convert quantities from world space
39 vec3 tsPos = tangentMatrix * worldPos;
40 vec3 n = normalize(tsNormal);
41 vec3 v = normalize(tangentMatrix * (worldEye - worldPos));
42 vec3 s = vec3(0.0);
43
44 for (int i = 0; i < lightCount; ++i) {
45 float att = 1.0;
46 float sDotN = 0.0;
47
48 if (lights[i].type != TYPE_DIRECTIONAL) {
49 // Point and Spot lights
50
51 // Transform the light position from world to tangent space
52 vec3 tsLightPos = tangentMatrix * lights[i].position;
53 vec3 sUnnormalized = tsLightPos - tsPos;
54 s = normalize(sUnnormalized); // Light direction in tangent space
55
56 // Calculate the attenuation factor
57 sDotN = dot(s, n);
58 if (sDotN > 0.0) {
59 if (lights[i].constantAttenuation != 0.0
60 || lights[i].linearAttenuation != 0.0
61 || lights[i].quadraticAttenuation != 0.0) {
62 float dist = length(sUnnormalized);
63 att = 1.0 / (lights[i].constantAttenuation +
64 lights[i].linearAttenuation * dist +
65 lights[i].quadraticAttenuation * dist * dist);
66 }
67
68 // The light direction is in world space, convert to tangent space
69 if (lights[i].type == TYPE_SPOT) {
70 // Check if fragment is inside or outside of the spot light cone
71 vec3 tsLightDirection = tangentMatrix * lights[i].direction;
72 if (degrees(acos(dot(-s, tsLightDirection))) > lights[i].cutOffAngle)
73 sDotN = 0.0;
74 }
75 }
76 } else {
77 // Directional lights
78 // The light direction is in world space, convert to tangent space
79 s = normalize(tangentMatrix * -lights[i].direction);
80 sDotN = dot(s, n);
81 }
82
83 // Calculate the diffuse factor
84 float diffuse = max(sDotN, 0.0);
85
86 // Calculate the specular factor
87 float specular = 0.0;
88 if (diffuse > 0.0 && shininess > 0.0) {
89 float normFactor = (shininess + 2.0) / 2.0;
90 vec3 r = reflect(-s, n); // Reflection direction in tangent space
91 specular = normFactor * pow(max(dot(r, v), 0.0), shininess);
92 }
93
94 // Accumulate the diffuse and specular contributions
95 diffuseColor += att * lights[i].intensity * diffuse * lights[i].color;
96 specularColor += att * lights[i].intensity * specular * lights[i].color;
97 }
98 }
99
100 void adsModel(const in vec3 worldPos,
101 const in vec3 worldNormal,
102 const in vec3 worldEye,
103 const in float shininess,
104 out vec3 diffuseColor,
105 out vec3 specularColor)
106 {
107 diffuseColor = vec3(0.0);
108 specularColor = vec3(0.0);
109
110 // We perform all work in world space
111 vec3 n = normalize(worldNormal);
112 vec3 v = normalize(worldEye - worldPos);
113 vec3 s = vec3(0.0);
114
115 for (int i = 0; i < lightCount; ++i) {
116 float att = 1.0;
117 float sDotN = 0.0;
118
119 if (lights[i].type != TYPE_DIRECTIONAL) {
120 // Point and Spot lights
121
122 // Light position is already in world space
123 vec3 sUnnormalized = lights[i].position - worldPos;
124 s = normalize(sUnnormalized); // Light direction
125
126 // Calculate the attenuation factor
127 sDotN = dot(s, n);
128 if (sDotN > 0.0) {
129 if (lights[i].constantAttenuation != 0.0
130 || lights[i].linearAttenuation != 0.0
131 || lights[i].quadraticAttenuation != 0.0) {
132 float dist = length(sUnnormalized);
133 att = 1.0 / (lights[i].constantAttenuation +
134 lights[i].linearAttenuation * dist +
135 lights[i].quadraticAttenuation * dist * dist);
136 }
137
138 // The light direction is in world space already
139 if (lights[i].type == TYPE_SPOT) {
140 // Check if fragment is inside or outside of the spot light cone
141 if (degrees(acos(dot(-s, lights[i].direction))) > lights[i].cutOffAngle)
142 sDotN = 0.0;
143 }
144 }
145 } else {
146 // Directional lights
147 // The light direction is in world space already
148 s = normalize(-lights[i].direction);
149 sDotN = dot(s, n);
150 }
151
152 // Calculate the diffuse factor
153 float diffuse = max(sDotN, 0.0);
154
155 // Calculate the specular factor
156 float specular = 0.0;
157 if (diffuse > 0.0 && shininess > 0.0) {
158 float normFactor = (shininess + 2.0) / 2.0;
159 vec3 r = reflect(-s, n); // Reflection direction in world space
160 specular = normFactor * pow(max(dot(r, v), 0.0), shininess);
161 }
162
163 // Accumulate the diffuse and specular contributions
164 diffuseColor += att * lights[i].intensity * diffuse * lights[i].color;
165 specularColor += att * lights[i].intensity * specular * lights[i].color;
166 }
167 }
168
169 void adModel(const in vec3 worldPos,
170 const in vec3 worldNormal,
171 out vec3 diffuseColor)
172 {
173 diffuseColor = vec3(0.0);
174
175 // We perform all work in world space
176 vec3 n = normalize(worldNormal);
177 vec3 s = vec3(0.0);
178
179 for (int i = 0; i < lightCount; ++i) {
180 float att = 1.0;
181 float sDotN = 0.0;
182
183 if (lights[i].type != TYPE_DIRECTIONAL) {
184 // Point and Spot lights
185
186 // Light position is already in world space
187 vec3 sUnnormalized = lights[i].position - worldPos;
188 s = normalize(sUnnormalized); // Light direction
189
190 // Calculate the attenuation factor
191 sDotN = dot(s, n);
192 if (sDotN > 0.0) {
193 if (lights[i].constantAttenuation != 0.0
194 || lights[i].linearAttenuation != 0.0
195 || lights[i].quadraticAttenuation != 0.0) {
196 float dist = length(sUnnormalized);
197 att = 1.0 / (lights[i].constantAttenuation +
198 lights[i].linearAttenuation * dist +
199 lights[i].quadraticAttenuation * dist * dist);
200 }
201
202 // The light direction is in world space already
203 if (lights[i].type == TYPE_SPOT) {
204 // Check if fragment is inside or outside of the spot light cone
205 if (degrees(acos(dot(-s, lights[i].direction))) > lights[i].cutOffAngle)
206 sDotN = 0.0;
207 }
208 }
209 } else {
210 // Directional lights
211 // The light direction is in world space already
212 s = normalize(-lights[i].direction);
213 sDotN = dot(s, n);
214 }
215
216 // Calculate the diffuse factor
217 float diffuse = max(sDotN, 0.0);
218
219 // Accumulate the diffuse contributions
220 diffuseColor += att * lights[i].intensity * diffuse * lights[i].color;
221 }
222 }