-
Notifications
You must be signed in to change notification settings - Fork 41
/
SphericalHarmonics.h
386 lines (305 loc) · 10.1 KB
/
SphericalHarmonics.h
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#pragma once
#include "Math.h"
namespace Probulator
{
// https://graphics.stanford.edu/papers/envmap/envmap.pdf
template <typename T, size_t L>
struct SphericalHarmonicsT
{
T data[(L + 1)*(L + 1)];
const T& operator [] (size_t i) const { return data[i]; }
T& operator [] (size_t i) { return data[i]; }
T& at(int l, int m) { return data[l * l + l + m]; }
const T& at(int l, int m) const { return data[l * l + l + m]; }
};
typedef SphericalHarmonicsT<float, 1> SphericalHarmonicsL1;
typedef SphericalHarmonicsT<float, 2> SphericalHarmonicsL2;
typedef SphericalHarmonicsT<vec3, 1> SphericalHarmonicsL1RGB;
typedef SphericalHarmonicsT<vec3, 2> SphericalHarmonicsL2RGB;
template <typename T, size_t L>
SphericalHarmonicsL1 shEvaluateL1(vec3 p);
SphericalHarmonicsL2 shEvaluateL2(vec3 p);
inline size_t shSize(size_t L) { return (L + 1)*(L + 1); }
template <typename Ta, typename Tb, typename Tw, size_t L>
inline void shAddWeighted(SphericalHarmonicsT<Ta, L>& accumulatorSh, const SphericalHarmonicsT<Tb, L>& sh, const Tw& weight)
{
for (size_t i = 0; i < shSize(L); ++i)
{
accumulatorSh[i] += sh[i] * weight;
}
}
template <typename Ta, typename Tb, size_t L>
inline Ta shDot(const SphericalHarmonicsT<Ta, L>& shA, const SphericalHarmonicsT<Tb, L>& shB)
{
Ta result = Ta(0);
for (size_t i = 0; i < shSize(L); ++i)
{
result += shA[i] * shB[i];
}
return result;
}
template <size_t L>
inline SphericalHarmonicsT<float, L> shEvaluate(vec3 p)
{
// From Peter-Pike Sloan's Stupid SH Tricks
// http://www.ppsloan.org/publications/StupidSH36.pdf
// https://github.com/dariomanesku/cmft/blob/master/src/cmft/cubemapfilter.cpp#L130
static_assert(L<=4, "Spherical Harmonics above L4 are not supported");
SphericalHarmonicsT<float, L> result;
const float x = -p.x;
const float y = -p.y;
const float z = p.z;
const float x2 = x*x;
const float y2 = y*y;
const float z2 = z*z;
const float z3 = z2*z;
const float x4 = x2*x2;
const float y4 = y2*y2;
const float z4 = z2*z2;
const float sqrtPi = sqrt(pi);
size_t i = 0;
result[i++] = 1.0f/(2.0f*sqrtPi);
if (L >= 1)
{
result[i++] = -sqrt(3.0f/(4.0f*pi))*y;
result[i++] = sqrt(3.0f/(4.0f*pi))*z;
result[i++] = -sqrt(3.0f/(4.0f*pi))*x;
}
if (L >= 2)
{
result[i++] = sqrt(15.0f/(4.0f*pi))*y*x;
result[i++] = -sqrt(15.0f/(4.0f*pi))*y*z;
result[i++] = sqrt(5.0f/(16.0f*pi))*(3.0f*z2-1.0f);
result[i++] = -sqrt(15.0f/(4.0f*pi))*x*z;
result[i++] = sqrt(15.0f/(16.0f*pi))*(x2-y2);
}
if (L >= 3)
{
result[i++] = -sqrt( 70.0f/(64.0f*pi))*y*(3.0f*x2-y2);
result[i++] = sqrt(105.0f/ (4.0f*pi))*y*x*z;
result[i++] = -sqrt( 21.0f/(16.0f*pi))*y*(-1.0f+5.0f*z2);
result[i++] = sqrt( 7.0f/(16.0f*pi))*(5.0f*z3-3.0f*z);
result[i++] = -sqrt( 21.0f/(64.0f*pi))*x*(-1.0f+5.0f*z2);
result[i++] = sqrt(105.0f/(16.0f*pi))*(x2-y2)*z;
result[i++] = -sqrt( 70.0f/(64.0f*pi))*x*(x2-3.0f*y2);
}
if (L >= 4)
{
result[i++] = 3.0f*sqrt(35.0f/(16.0f*pi))*x*y*(x2-y2);
result[i++] = -3.0f*sqrt(70.0f/(64.0f*pi))*y*z*(3.0f*x2-y2);
result[i++] = 3.0f*sqrt( 5.0f/(16.0f*pi))*y*x*(-1.0f+7.0f*z2);
result[i++] = -3.0f*sqrt(10.0f/(64.0f*pi))*y*z*(-3.0f+7.0f*z2);
result[i++] = (105.0f*z4-90.0f*z2+9.0f)/(16.0f*sqrtPi);
result[i++] = -3.0f*sqrt(10.0f/(64.0f*pi))*x*z*(-3.0f+7.0f*z2);
result[i++] = 3.0f*sqrt( 5.0f/(64.0f*pi))*(x2-y2)*(-1.0f+7.0f*z2);
result[i++] = -3.0f*sqrt(70.0f/(64.0f*pi))*x*z*(x2-3.0f*y2);
result[i++] = 3.0f*sqrt(35.0f/(4.0f*(64.0f*pi)))*(x4-6.0f*y2*x2+y4);
}
return result;
}
inline SphericalHarmonicsL1 shEvaluateL1(vec3 p)
{
return shEvaluate<1>(p);
}
inline SphericalHarmonicsL2 shEvaluateL2(vec3 p)
{
return shEvaluate<2>(p);
}
inline float shEvaluateDiffuseL1Geomerics(const SphericalHarmonicsL1& sh, const vec3& n)
{
// http://www.geomerics.com/wp-content/uploads/2015/08/CEDEC_Geomerics_ReconstructingDiffuseLighting1.pdf
float R0 = sh[0];
vec3 R1 = 0.5f * vec3(sh[3], sh[1], sh[2]);
float lenR1 = length(R1);
float q = 0.5f * (1.0f + dot(R1 / lenR1, n));
float p = 1.0f + 2.0f * lenR1 / R0;
float a = (1.0f - lenR1 / R0) / (1.0f + lenR1 / R0);
return R0 * (a + (1.0f - a) * (p + 1.0f) * pow(q, p));
}
template <typename T, size_t L>
inline SphericalHarmonicsT<T, L> shConvolveDiffuse(SphericalHarmonicsT<T, L>& sh)
{
SphericalHarmonicsT<T, L> result;
// https://cseweb.ucsd.edu/~ravir/papers/envmap/envmap.pdf equation 8
const float A[5] = {
pi,
pi * 2.0f / 3.0f,
pi * 1.0f / 4.0f,
0.0f,
-pi * 1.0f / 24.0f
};
int i = 0;
for (int l = 0; l <= (int)L; ++l)
{
for (int m = -l; m <= l; ++m)
{
result[i] = sh[i] * A[l];
++i;
}
}
return result;
}
template <typename T, size_t L>
inline T shEvaluateDiffuse(const SphericalHarmonicsT<T, L>& sh, const vec3& direction)
{
static_assert(L<=4, "Spherical Harmonics above L4 are not supported");
SphericalHarmonicsT<float, L> directionSh = shEvaluate<L>(direction);
// https://cseweb.ucsd.edu/~ravir/papers/envmap/envmap.pdf equation 8
const float A[5] = {
pi,
pi * 2.0f / 3.0f,
pi * 1.0f / 4.0f,
0.0f,
-pi * 1.0f / 24.0f
};
size_t i = 0;
T result = sh[i] * directionSh[i] * A[0]; ++i;
if (L >= 1)
{
result += sh[i] * directionSh[i] * A[1]; ++i;
result += sh[i] * directionSh[i] * A[1]; ++i;
result += sh[i] * directionSh[i] * A[1]; ++i;
}
if (L >= 2)
{
result += sh[i] * directionSh[i] * A[2]; ++i;
result += sh[i] * directionSh[i] * A[2]; ++i;
result += sh[i] * directionSh[i] * A[2]; ++i;
result += sh[i] * directionSh[i] * A[2]; ++i;
result += sh[i] * directionSh[i] * A[2]; ++i;
}
// L3 and other odd bands > 1 have 0 factor
if (L >= 4)
{
i = 16;
result += sh[i] * directionSh[i] * A[4]; ++i;
result += sh[i] * directionSh[i] * A[4]; ++i;
result += sh[i] * directionSh[i] * A[4]; ++i;
result += sh[i] * directionSh[i] * A[4]; ++i;
result += sh[i] * directionSh[i] * A[4]; ++i;
result += sh[i] * directionSh[i] * A[4]; ++i;
result += sh[i] * directionSh[i] * A[4]; ++i;
result += sh[i] * directionSh[i] * A[4]; ++i;
result += sh[i] * directionSh[i] * A[4]; ++i;
}
return result;
}
template <typename T>
inline T shEvaluateDiffuseL1(const SphericalHarmonicsT<T, 1>& sh, const vec3& direction)
{
return shEvaluateDiffuse<T, 1>(sh, direction);
}
template <typename T>
inline T shEvaluateDiffuseL2(const SphericalHarmonicsT<T, 2>& sh, const vec3& direction)
{
return shEvaluateDiffuse<T, 2>(sh, direction);
}
inline vec3 shEvaluateDiffuseL1ZH3Hallucinate(const SphericalHarmonicsL1RGB& sh, const vec3& n)
{
// From "ZH3: Quadratic Zonal Harmonics" - https://torust.me/ZH3.pdf
const vec3 lumCoefficients = vec3(0.2126f, 0.7152f, 0.0722f);
const vec3 zonalAxis = normalize(vec3(glm::dot(sh[3], lumCoefficients), glm::dot(sh[1], lumCoefficients), glm::dot(sh[2], lumCoefficients)));
const vec3 ratio = vec3(glm::abs(glm::dot(vec3(sh[3].x, sh[1].x, sh[2].x), zonalAxis)),
glm::abs(glm::dot(vec3(sh[3].y, sh[1].y, sh[2].y), zonalAxis)),
glm::abs(glm::dot(vec3(sh[3].z, sh[1].z, sh[2].z), zonalAxis))) / sh[0];
const vec3 zonalL2Coeff = sh[0] * (0.08f * ratio + 0.6f * ratio * ratio);
const float fZ = glm::dot(zonalAxis, n);
const float zhDir = sqrt(5.0f / (16.0f * pi)) * (3.0f * fZ * fZ - 1.0f);
const vec3 baseIrradiance = shEvaluateDiffuseL1(sh, n);
return baseIrradiance + (pi * 0.25f * zonalL2Coeff * zhDir);
}
template <size_t L>
float shFindWindowingLambda(const SphericalHarmonicsT<float, L>& sh, float maxLaplacian)
{
// http://www.ppsloan.org/publications/StupidSH36.pdf
// Appendix A7 Solving for Lambda to Reduce the Squared Laplacian
float tableL[L + 1];
float tableB[L + 1];
tableL[0] = 0.0f;
tableB[0] = 0.0f;
for (int l = 1; l <= (int)L; ++l)
{
tableL[l] = float(sqr(l) * sqr(l + 1));
float B = 0.0f;
for (int m = -1; m <= l; ++m)
{
B += sqr(sh.at(l, m));
}
tableB[l] = B;
}
float squaredLaplacian = 0.0f;
for (int l = 1; l <= (int)L; ++l)
{
squaredLaplacian += tableL[l] * tableB[l];
}
const float targetSquaredLaplacian = maxLaplacian * maxLaplacian;
if (squaredLaplacian <= targetSquaredLaplacian)
{
return 0.0f;
}
float lambda = 0.0f;
const u32 iterationLimit = 10000000;
for (u32 i = 0; i < iterationLimit; ++i)
{
float f = 0.0f;
float fd = 0.0f;
for (int l = 1; l <= (int)L; ++l)
{
f += tableL[l] * tableB[l] / sqr(1.0f + lambda * tableL[l]);
fd += (2.0f * sqr(tableL[l]) * tableB[l]) / cube(1.0f + lambda * tableL[l]);
}
f = targetSquaredLaplacian - f;
float delta = -f / fd;
lambda += delta;
if (abs(delta) < 1e-6f)
{
break;
}
}
return lambda;
}
template <typename T, size_t L>
void shApplyWindowing(SphericalHarmonicsT<T, L>& sh, float lambda)
{
// From Peter-Pike Sloan's Stupid SH Tricks
// http://www.ppsloan.org/publications/StupidSH36.pdf
int i = 0;
for(int l = 0; l <= (int)L; ++l)
{
float s = 1.0f / (1.0f + lambda * l * l * (l + 1.0f) * (l + 1.0f));
for(int m = -l; m <= l; ++m)
{
sh[i++] *= s;
}
}
}
template <typename T, size_t L>
inline T shMeanSquareError(const SphericalHarmonicsT<T, L>& sh, const std::vector<RadianceSample>& radianceSamples)
{
T errorSquaredSum = T(0.0f);
for (const RadianceSample& sample : radianceSamples)
{
auto directionSh = shEvaluate<L>(sample.direction);
auto reconstructedValue = shDot(sh, directionSh);
auto error = sample.value - reconstructedValue;
errorSquaredSum += error*error;
}
float sampleWeight = 1.0f / radianceSamples.size();
return errorSquaredSum * sampleWeight;
}
template <typename T, size_t L>
inline float shMeanSquareErrorScalar(const SphericalHarmonicsT<T, L>& sh, const std::vector<RadianceSample>& radianceSamples)
{
return dot(shMeanSquareError(sh, radianceSamples), T(1.0f / 3.0f));
}
template <size_t L>
inline SphericalHarmonicsT<float, L> shLuminance(const SphericalHarmonicsT<vec3, L>& sh)
{
SphericalHarmonicsT<float, L> result;
for (size_t i = 0; i < shSize(L); ++i)
{
result[i] = rgbLuminance(sh[i]);
}
return result;
}
}