-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
reflection.cpp
805 lines (698 loc) · 30.8 KB
/
reflection.cpp
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*
pbrt source code is Copyright(c) 1998-2016
Matt Pharr, Greg Humphreys, and Wenzel Jakob.
This file is part of pbrt.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// core/reflection.cpp*
#include "reflection.h"
#include "spectrum.h"
#include "sampler.h"
#include "sampling.h"
#include "interpolation.h"
#include "scene.h"
#include "interaction.h"
#include "stats.h"
#include <stdarg.h>
namespace pbrt {
// BxDF Utility Functions
Float FrDielectric(Float cosThetaI, Float etaI, Float etaT) {
cosThetaI = Clamp(cosThetaI, -1, 1);
// Potentially swap indices of refraction
bool entering = cosThetaI > 0.f;
if (!entering) {
std::swap(etaI, etaT);
cosThetaI = std::abs(cosThetaI);
}
// Compute _cosThetaT_ using Snell's law
Float sinThetaI = std::sqrt(std::max((Float)0, 1 - cosThetaI * cosThetaI));
Float sinThetaT = etaI / etaT * sinThetaI;
// Handle total internal reflection
if (sinThetaT >= 1) return 1;
Float cosThetaT = std::sqrt(std::max((Float)0, 1 - sinThetaT * sinThetaT));
Float Rparl = ((etaT * cosThetaI) - (etaI * cosThetaT)) /
((etaT * cosThetaI) + (etaI * cosThetaT));
Float Rperp = ((etaI * cosThetaI) - (etaT * cosThetaT)) /
((etaI * cosThetaI) + (etaT * cosThetaT));
return (Rparl * Rparl + Rperp * Rperp) / 2;
}
// https://seblagarde.wordpress.com/2013/04/29/memo-on-fresnel-equations/
Spectrum FrConductor(Float cosThetaI, const Spectrum &etai,
const Spectrum &etat, const Spectrum &k) {
cosThetaI = Clamp(cosThetaI, -1, 1);
Spectrum eta = etat / etai;
Spectrum etak = k / etai;
Float cosThetaI2 = cosThetaI * cosThetaI;
Float sinThetaI2 = 1. - cosThetaI2;
Spectrum eta2 = eta * eta;
Spectrum etak2 = etak * etak;
Spectrum t0 = eta2 - etak2 - sinThetaI2;
Spectrum a2plusb2 = Sqrt(t0 * t0 + 4 * eta2 * etak2);
Spectrum t1 = a2plusb2 + cosThetaI2;
Spectrum a = Sqrt(0.5f * (a2plusb2 + t0));
Spectrum t2 = (Float)2 * cosThetaI * a;
Spectrum Rs = (t1 - t2) / (t1 + t2);
Spectrum t3 = cosThetaI2 * a2plusb2 + sinThetaI2 * sinThetaI2;
Spectrum t4 = t2 * sinThetaI2;
Spectrum Rp = Rs * (t3 - t4) / (t3 + t4);
return 0.5 * (Rp + Rs);
}
// BxDF Method Definitions
Spectrum ScaledBxDF::f(const Vector3f &wo, const Vector3f &wi) const {
return scale * bxdf->f(wo, wi);
}
Spectrum ScaledBxDF::Sample_f(const Vector3f &wo, Vector3f *wi,
const Point2f &sample, Float *pdf,
BxDFType *sampledType) const {
Spectrum f = bxdf->Sample_f(wo, wi, sample, pdf, sampledType);
return scale * f;
}
Float ScaledBxDF::Pdf(const Vector3f &wo, const Vector3f &wi) const {
return bxdf->Pdf(wo, wi);
}
std::string ScaledBxDF::ToString() const {
return std::string("[ ScaledBxDF bxdf: ") + bxdf->ToString() +
std::string(" scale: ") + scale.ToString() + std::string(" ]");
}
Fresnel::~Fresnel() {}
Spectrum FresnelConductor::Evaluate(Float cosThetaI) const {
return FrConductor(std::abs(cosThetaI), etaI, etaT, k);
}
std::string FresnelConductor::ToString() const {
return std::string("[ FresnelConductor etaI: ") + etaI.ToString() +
std::string(" etaT: ") + etaT.ToString() + std::string(" k: ") +
k.ToString() + std::string(" ]");
}
Spectrum FresnelDielectric::Evaluate(Float cosThetaI) const {
return FrDielectric(cosThetaI, etaI, etaT);
}
std::string FresnelDielectric::ToString() const {
return StringPrintf("[ FrenselDielectric etaI: %f etaT: %f ]", etaI, etaT);
}
Spectrum SpecularReflection::Sample_f(const Vector3f &wo, Vector3f *wi,
const Point2f &sample, Float *pdf,
BxDFType *sampledType) const {
// Compute perfect specular reflection direction
*wi = Vector3f(-wo.x, -wo.y, wo.z);
*pdf = 1;
return fresnel->Evaluate(CosTheta(*wi)) * R / AbsCosTheta(*wi);
}
std::string SpecularReflection::ToString() const {
return std::string("[ SpecularReflection R: ") + R.ToString() +
std::string(" fresnel: ") + fresnel->ToString() + std::string(" ]");
}
Spectrum SpecularTransmission::Sample_f(const Vector3f &wo, Vector3f *wi,
const Point2f &sample, Float *pdf,
BxDFType *sampledType) const {
// Figure out which $\eta$ is incident and which is transmitted
bool entering = CosTheta(wo) > 0;
Float etaI = entering ? etaA : etaB;
Float etaT = entering ? etaB : etaA;
// Compute ray direction for specular transmission
if (!Refract(wo, Faceforward(Normal3f(0, 0, 1), wo), etaI / etaT, wi))
return 0;
*pdf = 1;
Spectrum ft = T * (Spectrum(1.) - fresnel.Evaluate(CosTheta(*wi)));
// Account for non-symmetry with transmission to different medium
if (mode == TransportMode::Radiance) ft *= (etaI * etaI) / (etaT * etaT);
return ft / AbsCosTheta(*wi);
}
std::string SpecularTransmission::ToString() const {
return std::string("[ SpecularTransmission: T: ") + T.ToString() +
StringPrintf(" etaA: %f etaB: %f ", etaA, etaB) +
std::string(" fresnel: ") + fresnel.ToString() +
std::string(" mode : ") +
(mode == TransportMode::Radiance ? std::string("RADIANCE")
: std::string("IMPORTANCE")) +
std::string(" ]");
}
Spectrum LambertianReflection::f(const Vector3f &wo, const Vector3f &wi) const {
return R * InvPi;
}
std::string LambertianReflection::ToString() const {
return std::string("[ LambertianReflection R: ") + R.ToString() +
std::string(" ]");
}
Spectrum LambertianTransmission::f(const Vector3f &wo,
const Vector3f &wi) const {
return T * InvPi;
}
std::string LambertianTransmission::ToString() const {
return std::string("[ LambertianTransmission T: ") + T.ToString() +
std::string(" ]");
}
Spectrum OrenNayar::f(const Vector3f &wo, const Vector3f &wi) const {
Float sinThetaI = SinTheta(wi);
Float sinThetaO = SinTheta(wo);
// Compute cosine term of Oren-Nayar model
Float maxCos = 0;
if (sinThetaI > 1e-4 && sinThetaO > 1e-4) {
Float sinPhiI = SinPhi(wi), cosPhiI = CosPhi(wi);
Float sinPhiO = SinPhi(wo), cosPhiO = CosPhi(wo);
Float dCos = cosPhiI * cosPhiO + sinPhiI * sinPhiO;
maxCos = std::max((Float)0, dCos);
}
// Compute sine and tangent terms of Oren-Nayar model
Float sinAlpha, tanBeta;
if (AbsCosTheta(wi) > AbsCosTheta(wo)) {
sinAlpha = sinThetaO;
tanBeta = sinThetaI / AbsCosTheta(wi);
} else {
sinAlpha = sinThetaI;
tanBeta = sinThetaO / AbsCosTheta(wo);
}
return R * InvPi * (A + B * maxCos * sinAlpha * tanBeta);
}
std::string OrenNayar::ToString() const {
return std::string("[ OrenNayar R: ") + R.ToString() +
StringPrintf(" A: %f B: %f ]", A, B);
}
Spectrum MicrofacetReflection::f(const Vector3f &wo, const Vector3f &wi) const {
Float cosThetaO = AbsCosTheta(wo), cosThetaI = AbsCosTheta(wi);
Vector3f wh = wi + wo;
// Handle degenerate cases for microfacet reflection
if (cosThetaI == 0 || cosThetaO == 0) return Spectrum(0.);
if (wh.x == 0 && wh.y == 0 && wh.z == 0) return Spectrum(0.);
wh = Normalize(wh);
// For the Fresnel call, make sure that wh is in the same hemisphere
// as the surface normal, so that TIR is handled correctly.
Spectrum F = fresnel->Evaluate(Dot(wi, Faceforward(wh, Vector3f(0,0,1))));
return R * distribution->D(wh) * distribution->G(wo, wi) * F /
(4 * cosThetaI * cosThetaO);
}
std::string MicrofacetReflection::ToString() const {
return std::string("[ MicrofacetReflection R: ") + R.ToString() +
std::string(" distribution: ") + distribution->ToString() +
std::string(" fresnel: ") + fresnel->ToString() + std::string(" ]");
}
Spectrum MicrofacetTransmission::f(const Vector3f &wo,
const Vector3f &wi) const {
if (SameHemisphere(wo, wi)) return 0; // transmission only
Float cosThetaO = CosTheta(wo);
Float cosThetaI = CosTheta(wi);
if (cosThetaI == 0 || cosThetaO == 0) return Spectrum(0);
// Compute $\wh$ from $\wo$ and $\wi$ for microfacet transmission
Float eta = CosTheta(wo) > 0 ? (etaB / etaA) : (etaA / etaB);
Vector3f wh = Normalize(wo + wi * eta);
if (wh.z < 0) wh = -wh;
// Same side?
if (Dot(wo, wh) * Dot(wi, wh) > 0) return Spectrum(0);
Spectrum F = fresnel.Evaluate(Dot(wo, wh));
Float sqrtDenom = Dot(wo, wh) + eta * Dot(wi, wh);
Float factor = (mode == TransportMode::Radiance) ? (1 / eta) : 1;
return (Spectrum(1.f) - F) * T *
std::abs(distribution->D(wh) * distribution->G(wo, wi) * eta * eta *
AbsDot(wi, wh) * AbsDot(wo, wh) * factor * factor /
(cosThetaI * cosThetaO * sqrtDenom * sqrtDenom));
}
std::string MicrofacetTransmission::ToString() const {
return std::string("[ MicrofacetTransmission T: ") + T.ToString() +
std::string(" distribution: ") + distribution->ToString() +
StringPrintf(" etaA: %f etaB: %f", etaA, etaB) +
std::string(" fresnel: ") + fresnel.ToString() +
std::string(" mode : ") +
(mode == TransportMode::Radiance ? std::string("RADIANCE")
: std::string("IMPORTANCE")) +
std::string(" ]");
}
FresnelBlend::FresnelBlend(const Spectrum &Rd, const Spectrum &Rs,
MicrofacetDistribution *distribution)
: BxDF(BxDFType(BSDF_REFLECTION | BSDF_GLOSSY)),
Rd(Rd),
Rs(Rs),
distribution(distribution) {}
Spectrum FresnelBlend::f(const Vector3f &wo, const Vector3f &wi) const {
auto pow5 = [](Float v) { return (v * v) * (v * v) * v; };
Spectrum diffuse = (28.f / (23.f * Pi)) * Rd * (Spectrum(1.f) - Rs) *
(1 - pow5(1 - .5f * AbsCosTheta(wi))) *
(1 - pow5(1 - .5f * AbsCosTheta(wo)));
Vector3f wh = wi + wo;
if (wh.x == 0 && wh.y == 0 && wh.z == 0) return Spectrum(0);
wh = Normalize(wh);
Spectrum specular =
distribution->D(wh) /
(4 * AbsDot(wi, wh) * std::max(AbsCosTheta(wi), AbsCosTheta(wo))) *
SchlickFresnel(Dot(wi, wh));
return diffuse + specular;
}
std::string FresnelBlend::ToString() const {
return std::string("[ FresnelBlend Rd: ") + Rd.ToString() +
std::string(" Rs: ") + Rs.ToString() +
std::string(" distribution: ") + distribution->ToString() +
std::string(" ]");
}
Spectrum FourierBSDF::f(const Vector3f &wo, const Vector3f &wi) const {
// Find the zenith angle cosines and azimuth difference angle
Float muI = CosTheta(-wi), muO = CosTheta(wo);
Float cosPhi = CosDPhi(-wi, wo);
// Compute Fourier coefficients $a_k$ for $(\mui, \muo)$
// Determine offsets and weights for $\mui$ and $\muo$
int offsetI, offsetO;
Float weightsI[4], weightsO[4];
if (!bsdfTable.GetWeightsAndOffset(muI, &offsetI, weightsI) ||
!bsdfTable.GetWeightsAndOffset(muO, &offsetO, weightsO))
return Spectrum(0.f);
// Allocate storage to accumulate _ak_ coefficients
Float *ak = ALLOCA(Float, bsdfTable.mMax * bsdfTable.nChannels);
memset(ak, 0, bsdfTable.mMax * bsdfTable.nChannels * sizeof(Float));
// Accumulate weighted sums of nearby $a_k$ coefficients
int mMax = 0;
for (int b = 0; b < 4; ++b) {
for (int a = 0; a < 4; ++a) {
// Add contribution of _(a, b)_ to $a_k$ values
Float weight = weightsI[a] * weightsO[b];
if (weight != 0) {
int m;
const Float *ap = bsdfTable.GetAk(offsetI + a, offsetO + b, &m);
mMax = std::max(mMax, m);
for (int c = 0; c < bsdfTable.nChannels; ++c)
for (int k = 0; k < m; ++k)
ak[c * bsdfTable.mMax + k] += weight * ap[c * m + k];
}
}
}
// Evaluate Fourier expansion for angle $\phi$
Float Y = std::max((Float)0, Fourier(ak, mMax, cosPhi));
Float scale = muI != 0 ? (1 / std::abs(muI)) : (Float)0;
// Update _scale_ to account for adjoint light transport
if (mode == TransportMode::Radiance && muI * muO > 0) {
float eta = muI > 0 ? 1 / bsdfTable.eta : bsdfTable.eta;
scale *= eta * eta;
}
if (bsdfTable.nChannels == 1)
return Spectrum(Y * scale);
else {
// Compute and return RGB colors for tabulated BSDF
Float R = Fourier(ak + 1 * bsdfTable.mMax, mMax, cosPhi);
Float B = Fourier(ak + 2 * bsdfTable.mMax, mMax, cosPhi);
Float G = 1.39829f * Y - 0.100913f * B - 0.297375f * R;
Float rgb[3] = {R * scale, G * scale, B * scale};
return Spectrum::FromRGB(rgb).Clamp();
}
}
std::string FourierBSDF::ToString() const {
return StringPrintf("[ FourierBSDF eta: %f mMax: %d nChannels: %d nMu: %d ",
bsdfTable.eta, bsdfTable.mMax, bsdfTable.nChannels,
bsdfTable.nMu) +
std::string(" mode : ") +
(mode == TransportMode::Radiance ? std::string("RADIANCE")
: std::string("IMPORTANCE")) +
std::string(" ]");
}
bool FourierBSDFTable::GetWeightsAndOffset(Float cosTheta, int *offset,
Float weights[4]) const {
return CatmullRomWeights(nMu, mu, cosTheta, offset, weights);
}
Spectrum BxDF::Sample_f(const Vector3f &wo, Vector3f *wi, const Point2f &u,
Float *pdf, BxDFType *sampledType) const {
// Cosine-sample the hemisphere, flipping the direction if necessary
*wi = CosineSampleHemisphere(u);
if (wo.z < 0) wi->z *= -1;
*pdf = Pdf(wo, *wi);
return f(wo, *wi);
}
Float BxDF::Pdf(const Vector3f &wo, const Vector3f &wi) const {
return SameHemisphere(wo, wi) ? AbsCosTheta(wi) * InvPi : 0;
}
Spectrum LambertianTransmission::Sample_f(const Vector3f &wo, Vector3f *wi,
const Point2f &u, Float *pdf,
BxDFType *sampledType) const {
*wi = CosineSampleHemisphere(u);
if (wo.z > 0) wi->z *= -1;
*pdf = Pdf(wo, *wi);
return f(wo, *wi);
}
Float LambertianTransmission::Pdf(const Vector3f &wo,
const Vector3f &wi) const {
return !SameHemisphere(wo, wi) ? AbsCosTheta(wi) * InvPi : 0;
}
Spectrum MicrofacetReflection::Sample_f(const Vector3f &wo, Vector3f *wi,
const Point2f &u, Float *pdf,
BxDFType *sampledType) const {
// Sample microfacet orientation $\wh$ and reflected direction $\wi$
if (wo.z == 0) return 0.;
Vector3f wh = distribution->Sample_wh(wo, u);
if (Dot(wo, wh) < 0) return 0.; // Should be rare
*wi = Reflect(wo, wh);
if (!SameHemisphere(wo, *wi)) return Spectrum(0.f);
// Compute PDF of _wi_ for microfacet reflection
*pdf = distribution->Pdf(wo, wh) / (4 * Dot(wo, wh));
return f(wo, *wi);
}
Float MicrofacetReflection::Pdf(const Vector3f &wo, const Vector3f &wi) const {
if (!SameHemisphere(wo, wi)) return 0;
Vector3f wh = Normalize(wo + wi);
return distribution->Pdf(wo, wh) / (4 * Dot(wo, wh));
}
Spectrum MicrofacetTransmission::Sample_f(const Vector3f &wo, Vector3f *wi,
const Point2f &u, Float *pdf,
BxDFType *sampledType) const {
if (wo.z == 0) return 0.;
Vector3f wh = distribution->Sample_wh(wo, u);
if (Dot(wo, wh) < 0) return 0.; // Should be rare
Float eta = CosTheta(wo) > 0 ? (etaA / etaB) : (etaB / etaA);
if (!Refract(wo, (Normal3f)wh, eta, wi)) return 0;
*pdf = Pdf(wo, *wi);
return f(wo, *wi);
}
Float MicrofacetTransmission::Pdf(const Vector3f &wo,
const Vector3f &wi) const {
if (SameHemisphere(wo, wi)) return 0;
// Compute $\wh$ from $\wo$ and $\wi$ for microfacet transmission
Float eta = CosTheta(wo) > 0 ? (etaB / etaA) : (etaA / etaB);
Vector3f wh = Normalize(wo + wi * eta);
if (Dot(wo, wh) * Dot(wi, wh) > 0) return 0;
// Compute change of variables _dwh\_dwi_ for microfacet transmission
Float sqrtDenom = Dot(wo, wh) + eta * Dot(wi, wh);
Float dwh_dwi =
std::abs((eta * eta * Dot(wi, wh)) / (sqrtDenom * sqrtDenom));
return distribution->Pdf(wo, wh) * dwh_dwi;
}
Spectrum FresnelBlend::Sample_f(const Vector3f &wo, Vector3f *wi,
const Point2f &uOrig, Float *pdf,
BxDFType *sampledType) const {
Point2f u = uOrig;
if (u[0] < .5) {
u[0] = std::min(2 * u[0], OneMinusEpsilon);
// Cosine-sample the hemisphere, flipping the direction if necessary
*wi = CosineSampleHemisphere(u);
if (wo.z < 0) wi->z *= -1;
} else {
u[0] = std::min(2 * (u[0] - .5f), OneMinusEpsilon);
// Sample microfacet orientation $\wh$ and reflected direction $\wi$
Vector3f wh = distribution->Sample_wh(wo, u);
*wi = Reflect(wo, wh);
if (!SameHemisphere(wo, *wi)) return Spectrum(0.f);
}
*pdf = Pdf(wo, *wi);
return f(wo, *wi);
}
Float FresnelBlend::Pdf(const Vector3f &wo, const Vector3f &wi) const {
if (!SameHemisphere(wo, wi)) return 0;
Vector3f wh = Normalize(wo + wi);
Float pdf_wh = distribution->Pdf(wo, wh);
return .5f * (AbsCosTheta(wi) * InvPi + pdf_wh / (4 * Dot(wo, wh)));
}
Spectrum FresnelSpecular::Sample_f(const Vector3f &wo, Vector3f *wi,
const Point2f &u, Float *pdf,
BxDFType *sampledType) const {
Float F = FrDielectric(CosTheta(wo), etaA, etaB);
if (u[0] < F) {
// Compute specular reflection for _FresnelSpecular_
// Compute perfect specular reflection direction
*wi = Vector3f(-wo.x, -wo.y, wo.z);
if (sampledType)
*sampledType = BxDFType(BSDF_SPECULAR | BSDF_REFLECTION);
*pdf = F;
return F * R / AbsCosTheta(*wi);
} else {
// Compute specular transmission for _FresnelSpecular_
// Figure out which $\eta$ is incident and which is transmitted
bool entering = CosTheta(wo) > 0;
Float etaI = entering ? etaA : etaB;
Float etaT = entering ? etaB : etaA;
// Compute ray direction for specular transmission
if (!Refract(wo, Faceforward(Normal3f(0, 0, 1), wo), etaI / etaT, wi))
return 0;
Spectrum ft = T * (1 - F);
// Account for non-symmetry with transmission to different medium
if (mode == TransportMode::Radiance)
ft *= (etaI * etaI) / (etaT * etaT);
if (sampledType)
*sampledType = BxDFType(BSDF_SPECULAR | BSDF_TRANSMISSION);
*pdf = 1 - F;
return ft / AbsCosTheta(*wi);
}
}
std::string FresnelSpecular::ToString() const {
return std::string("[ FresnelSpecular R: ") + R.ToString() +
std::string(" T: ") + T.ToString() +
StringPrintf(" etaA: %f etaB: %f ", etaA, etaB) +
std::string(" mode : ") +
(mode == TransportMode::Radiance ? std::string("RADIANCE")
: std::string("IMPORTANCE")) +
std::string(" ]");
}
Spectrum FourierBSDF::Sample_f(const Vector3f &wo, Vector3f *wi,
const Point2f &u, Float *pdf,
BxDFType *sampledType) const {
// Sample zenith angle component for _FourierBSDF_
Float muO = CosTheta(wo);
Float pdfMu;
Float muI = SampleCatmullRom2D(bsdfTable.nMu, bsdfTable.nMu, bsdfTable.mu,
bsdfTable.mu, bsdfTable.a0, bsdfTable.cdf,
muO, u[1], nullptr, &pdfMu);
// Compute Fourier coefficients $a_k$ for $(\mui, \muo)$
// Determine offsets and weights for $\mui$ and $\muo$
int offsetI, offsetO;
Float weightsI[4], weightsO[4];
if (!bsdfTable.GetWeightsAndOffset(muI, &offsetI, weightsI) ||
!bsdfTable.GetWeightsAndOffset(muO, &offsetO, weightsO))
return Spectrum(0.f);
// Allocate storage to accumulate _ak_ coefficients
Float *ak = ALLOCA(Float, bsdfTable.mMax * bsdfTable.nChannels);
memset(ak, 0, bsdfTable.mMax * bsdfTable.nChannels * sizeof(Float));
// Accumulate weighted sums of nearby $a_k$ coefficients
int mMax = 0;
for (int b = 0; b < 4; ++b) {
for (int a = 0; a < 4; ++a) {
// Add contribution of _(a, b)_ to $a_k$ values
Float weight = weightsI[a] * weightsO[b];
if (weight != 0) {
int m;
const Float *ap = bsdfTable.GetAk(offsetI + a, offsetO + b, &m);
mMax = std::max(mMax, m);
for (int c = 0; c < bsdfTable.nChannels; ++c)
for (int k = 0; k < m; ++k)
ak[c * bsdfTable.mMax + k] += weight * ap[c * m + k];
}
}
}
// Importance sample the luminance Fourier expansion
Float phi, pdfPhi;
Float Y = SampleFourier(ak, bsdfTable.recip, mMax, u[0], &pdfPhi, &phi);
*pdf = std::max((Float)0, pdfPhi * pdfMu);
// Compute the scattered direction for _FourierBSDF_
Float sin2ThetaI = std::max((Float)0, 1 - muI * muI);
Float norm = std::sqrt(sin2ThetaI / Sin2Theta(wo));
if (std::isinf(norm)) norm = 0;
Float sinPhi = std::sin(phi), cosPhi = std::cos(phi);
*wi = -Vector3f(norm * (cosPhi * wo.x - sinPhi * wo.y),
norm * (sinPhi * wo.x + cosPhi * wo.y), muI);
// Mathematically, wi will be normalized (if wo was). However, in
// practice, floating-point rounding error can cause some error to
// accumulate in the computed value of wi here. This can be
// catastrophic: if the ray intersects an object with the FourierBSDF
// again and the wo (based on such a wi) is nearly perpendicular to the
// surface, then the wi computed at the next intersection can end up
// being substantially (like 4x) longer than normalized, which leads to
// all sorts of errors, including negative spectral values. Therefore,
// we normalize again here.
*wi = Normalize(*wi);
// Evaluate remaining Fourier expansions for angle $\phi$
Float scale = muI != 0 ? (1 / std::abs(muI)) : (Float)0;
if (mode == TransportMode::Radiance && muI * muO > 0) {
float eta = muI > 0 ? 1 / bsdfTable.eta : bsdfTable.eta;
scale *= eta * eta;
}
if (bsdfTable.nChannels == 1) return Spectrum(Y * scale);
Float R = Fourier(ak + 1 * bsdfTable.mMax, mMax, cosPhi);
Float B = Fourier(ak + 2 * bsdfTable.mMax, mMax, cosPhi);
Float G = 1.39829f * Y - 0.100913f * B - 0.297375f * R;
Float rgb[3] = {R * scale, G * scale, B * scale};
return Spectrum::FromRGB(rgb).Clamp();
}
Float FourierBSDF::Pdf(const Vector3f &wo, const Vector3f &wi) const {
// Find the zenith angle cosines and azimuth difference angle
Float muI = CosTheta(-wi), muO = CosTheta(wo);
Float cosPhi = CosDPhi(-wi, wo);
// Compute luminance Fourier coefficients $a_k$ for $(\mui, \muo)$
int offsetI, offsetO;
Float weightsI[4], weightsO[4];
if (!bsdfTable.GetWeightsAndOffset(muI, &offsetI, weightsI) ||
!bsdfTable.GetWeightsAndOffset(muO, &offsetO, weightsO))
return 0;
Float *ak = ALLOCA(Float, bsdfTable.mMax);
memset(ak, 0, bsdfTable.mMax * sizeof(Float));
int mMax = 0;
for (int o = 0; o < 4; ++o) {
for (int i = 0; i < 4; ++i) {
Float weight = weightsI[i] * weightsO[o];
if (weight == 0) continue;
int order;
const Float *coeffs =
bsdfTable.GetAk(offsetI + i, offsetO + o, &order);
mMax = std::max(mMax, order);
for (int k = 0; k < order; ++k) ak[k] += *coeffs++ * weight;
}
}
// Evaluate probability of sampling _wi_
Float rho = 0;
for (int o = 0; o < 4; ++o) {
if (weightsO[o] == 0) continue;
rho +=
weightsO[o] *
bsdfTable.cdf[(offsetO + o) * bsdfTable.nMu + bsdfTable.nMu - 1] *
(2 * Pi);
}
Float Y = Fourier(ak, mMax, cosPhi);
return (rho > 0 && Y > 0) ? (Y / rho) : 0;
}
Spectrum BxDF::rho(const Vector3f &w, int nSamples, const Point2f *u) const {
Spectrum r(0.);
for (int i = 0; i < nSamples; ++i) {
// Estimate one term of $\rho_\roman{hd}$
Vector3f wi;
Float pdf = 0;
Spectrum f = Sample_f(w, &wi, u[i], &pdf);
if (pdf > 0) r += f * AbsCosTheta(wi) / pdf;
}
return r / nSamples;
}
Spectrum BxDF::rho(int nSamples, const Point2f *u1, const Point2f *u2) const {
Spectrum r(0.f);
for (int i = 0; i < nSamples; ++i) {
// Estimate one term of $\rho_\roman{hh}$
Vector3f wo, wi;
wo = UniformSampleHemisphere(u1[i]);
Float pdfo = UniformHemispherePdf(), pdfi = 0;
Spectrum f = Sample_f(wo, &wi, u2[i], &pdfi);
if (pdfi > 0)
r += f * AbsCosTheta(wi) * AbsCosTheta(wo) / (pdfo * pdfi);
}
return r / (Pi * nSamples);
}
// BSDF Method Definitions
Spectrum BSDF::f(const Vector3f &woW, const Vector3f &wiW,
BxDFType flags) const {
ProfilePhase pp(Prof::BSDFEvaluation);
Vector3f wi = WorldToLocal(wiW), wo = WorldToLocal(woW);
if (wo.z == 0) return 0.;
bool reflect = Dot(wiW, ng) * Dot(woW, ng) > 0;
Spectrum f(0.f);
for (int i = 0; i < nBxDFs; ++i)
if (bxdfs[i]->MatchesFlags(flags) &&
((reflect && (bxdfs[i]->type & BSDF_REFLECTION)) ||
(!reflect && (bxdfs[i]->type & BSDF_TRANSMISSION))))
f += bxdfs[i]->f(wo, wi);
return f;
}
Spectrum BSDF::rho(int nSamples, const Point2f *samples1,
const Point2f *samples2, BxDFType flags) const {
Spectrum ret(0.f);
for (int i = 0; i < nBxDFs; ++i)
if (bxdfs[i]->MatchesFlags(flags))
ret += bxdfs[i]->rho(nSamples, samples1, samples2);
return ret;
}
Spectrum BSDF::rho(const Vector3f &woWorld, int nSamples, const Point2f *samples,
BxDFType flags) const {
Vector3f wo = WorldToLocal(woWorld);
Spectrum ret(0.f);
for (int i = 0; i < nBxDFs; ++i)
if (bxdfs[i]->MatchesFlags(flags))
ret += bxdfs[i]->rho(wo, nSamples, samples);
return ret;
}
Spectrum BSDF::Sample_f(const Vector3f &woWorld, Vector3f *wiWorld,
const Point2f &u, Float *pdf, BxDFType type,
BxDFType *sampledType) const {
ProfilePhase pp(Prof::BSDFSampling);
// Choose which _BxDF_ to sample
int matchingComps = NumComponents(type);
if (matchingComps == 0) {
*pdf = 0;
if (sampledType) *sampledType = BxDFType(0);
return Spectrum(0);
}
int comp =
std::min((int)std::floor(u[0] * matchingComps), matchingComps - 1);
// Get _BxDF_ pointer for chosen component
BxDF *bxdf = nullptr;
int count = comp;
for (int i = 0; i < nBxDFs; ++i)
if (bxdfs[i]->MatchesFlags(type) && count-- == 0) {
bxdf = bxdfs[i];
break;
}
CHECK(bxdf != nullptr);
VLOG(2) << "BSDF::Sample_f chose comp = " << comp << " / matching = " <<
matchingComps << ", bxdf: " << bxdf->ToString();
// Remap _BxDF_ sample _u_ to $[0,1)^2$
Point2f uRemapped(std::min(u[0] * matchingComps - comp, OneMinusEpsilon),
u[1]);
// Sample chosen _BxDF_
Vector3f wi, wo = WorldToLocal(woWorld);
if (wo.z == 0) return 0.;
*pdf = 0;
if (sampledType) *sampledType = bxdf->type;
Spectrum f = bxdf->Sample_f(wo, &wi, uRemapped, pdf, sampledType);
VLOG(2) << "For wo = " << wo << ", sampled f = " << f << ", pdf = "
<< *pdf << ", ratio = " << ((*pdf > 0) ? (f / *pdf) : Spectrum(0.))
<< ", wi = " << wi;
if (*pdf == 0) {
if (sampledType) *sampledType = BxDFType(0);
return 0;
}
*wiWorld = LocalToWorld(wi);
// Compute overall PDF with all matching _BxDF_s
if (!(bxdf->type & BSDF_SPECULAR) && matchingComps > 1)
for (int i = 0; i < nBxDFs; ++i)
if (bxdfs[i] != bxdf && bxdfs[i]->MatchesFlags(type))
*pdf += bxdfs[i]->Pdf(wo, wi);
if (matchingComps > 1) *pdf /= matchingComps;
// Compute value of BSDF for sampled direction
if (!(bxdf->type & BSDF_SPECULAR)) {
bool reflect = Dot(*wiWorld, ng) * Dot(woWorld, ng) > 0;
f = 0.;
for (int i = 0; i < nBxDFs; ++i)
if (bxdfs[i]->MatchesFlags(type) &&
((reflect && (bxdfs[i]->type & BSDF_REFLECTION)) ||
(!reflect && (bxdfs[i]->type & BSDF_TRANSMISSION))))
f += bxdfs[i]->f(wo, wi);
}
VLOG(2) << "Overall f = " << f << ", pdf = " << *pdf << ", ratio = "
<< ((*pdf > 0) ? (f / *pdf) : Spectrum(0.));
return f;
}
Float BSDF::Pdf(const Vector3f &woWorld, const Vector3f &wiWorld,
BxDFType flags) const {
ProfilePhase pp(Prof::BSDFPdf);
if (nBxDFs == 0.f) return 0.f;
Vector3f wo = WorldToLocal(woWorld), wi = WorldToLocal(wiWorld);
if (wo.z == 0) return 0.;
Float pdf = 0.f;
int matchingComps = 0;
for (int i = 0; i < nBxDFs; ++i)
if (bxdfs[i]->MatchesFlags(flags)) {
++matchingComps;
pdf += bxdfs[i]->Pdf(wo, wi);
}
Float v = matchingComps > 0 ? pdf / matchingComps : 0.f;
return v;
}
std::string BSDF::ToString() const {
std::string s = StringPrintf("[ BSDF eta: %f nBxDFs: %d", eta, nBxDFs);
for (int i = 0; i < nBxDFs; ++i)
s += StringPrintf("\n bxdfs[%d]: ", i) + bxdfs[i]->ToString();
return s + std::string(" ]");
}
} // namespace pbrt