This repository has been archived by the owner on Nov 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
integrator.cc
76 lines (72 loc) · 2.09 KB
/
integrator.cc
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
// Copyright (c) 2016 Kai Luo. All rights reserved.
#include "integrator.h"
#include "scene.h"
#include "spectrum.h"
namespace zLi {
Spectrum PathIntegrator::Li(const Scene &scene, const Ray &r, int maxBounces) {
Spectrum L(0);
Spectrum F(1);
Ray ray(r);
for (int i = 0; i < maxBounces; ++i) {
auto ri = scene.Intersect(ray);
if (!ri) {
break;
}
// auto pos = (*ri).ray((*ri).t);
// DEBUG("bounce %d, intersection found at position (%f, %f, %f)", i, pos.x,
// pos.y, pos.z);
L += F * scene.DirectLight(*ri);
// bsdf
auto bsdf = (*ri).g.bsdf();
if (bsdf.btdf) {
auto n = (*ri).g.Normal(ray((*ri).t));
auto rfr = bsdf.btdf->pdf(n, ray.d);
Float pdf = std::get<0>(rfr);
if (pdf > 0) {
auto wo = std::get<1>(rfr);
ray = (*ri).SpawnRay(wo);
Spectrum tmp = PathIntegrator::Li(scene, ray, maxBounces - i);
Float f = bsdf.btdf->f(n, ray.d, wo);
L += F * (*ri).g.R() * (f * std::abs(n * wo) / pdf) * tmp;
}
}
if (bsdf.brdf) {
if (bsdf.brdf->type() == BRDF::Type::Specular) {
auto n = (*ri).g.Normal(ray((*ri).t));
n = ray.d * n <= 0 ? n : -n;
auto rfl = bsdf.brdf->pdf(n, ray.d);
Float pdf = std::get<0>(rfl);
if (pdf <= 0) {
break;
}
auto wo = std::get<1>(rfl);
Float f = bsdf.brdf->f(n, ray.d, wo);
F *= f * std::abs(n * wo) / pdf;
F *= (*ri).g.R();
ray = (*ri).SpawnRay(wo);
} else if (bsdf.brdf->type() == BRDF::Type::Diffuse) {
auto n = (*ri).g.Normal(ray((*ri).t));
auto rfl = bsdf.brdf->pdf(n, ray.d);
Float pdf = std::get<0>(rfl);
if (pdf <= 0) {
break;
}
auto wo = std::get<1>(rfl);
Float f = bsdf.brdf->f(n, ray.d, wo);
F *= f * std::abs(n * wo) / pdf;
F *= (*ri).g.R();
ray = (*ri).SpawnRay(wo);
}
}
// russian roulette
if (i > 3) {
Float q = F.ToxyY().Y;
if (UniformSample() <= q) {
break;
}
F /= 1 - q;
}
}
return L;
}
} // namespace zLi