/
camera.cc
258 lines (221 loc) · 7.09 KB
/
camera.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
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
#include "camera.h"
#include "light.h"
#include <iostream>
#include <chrono>
#ifdef _WIN32
#include <ppl.h>
namespace parallel = Concurrency;
#else
#include <tbb/tbb.h>
namespace parallel = tbb;
#endif
using std::max;
using std::min;
namespace chrono = std::chrono;
Camera::Camera(
const Transform& xform,
const std::vector<const Geom*>& objs,
int ww,
int hh,
float fov,
float len,
float fStop
) : accel(objs), focalLength(len),
lensRadius((len / fStop) * 0.5f), // Diameter = focalLength / fStop.
camToWorldXform(xform),
masterRng(), rowSeeds(size_t(hh)), img(ww, hh), iters(0)
{
// Calculate ray-tracing vectors.
float halfFocalPlaneUp;
float halfFocalPlaneRight;
if (img.w > img.h) {
halfFocalPlaneUp = focalLength * tanf(0.5f * fov);
halfFocalPlaneRight = halfFocalPlaneUp * float(img.w) / float(img.h);
} else {
halfFocalPlaneRight = focalLength * tanf(0.5f * fov);
halfFocalPlaneUp = halfFocalPlaneRight * float(img.h) / float(img.w);
}
focalPlaneUp = -2.0f * halfFocalPlaneUp;
focalPlaneRight = 2.0f * halfFocalPlaneRight;
focalPlaneOrigin = Vec(-halfFocalPlaneRight, halfFocalPlaneUp, -focalLength);
// Refine emitters so we can compute direct illumination.
for (const Geom* g : objs) {
if (g->light) {
g->refine(emitters);
}
}
}
Camera::Camera(const Node& n)
: Camera(math::rotationThenTranslation(
n.getFloat("rotateAngle"),
n.getVec("rotateAxis"),
n.getVec("translate")
),
n.getGeometryList("objects"),
n.getInt("width"), n.getInt("height"),
n.getFloat("fov"), n.getFloat("focalLength"),
n.getFloat("fStop")) {}
void Camera::renderOnce(
std::string name
) {
// Increment iteration count and begin timer.
iters++;
std::cout << "Iteration " << iters;
chrono::steady_clock::time_point startTime = chrono::steady_clock::now();
// Seed the per-row RNGs.
for (int y = 0; y < img.h; ++y) {
rowSeeds[size_t(y)] = masterRng.nextUnsigned();
}
// Trace paths in parallel using TBB (Linux/Mac) or PPL (Windows).
parallel::parallel_for(0, img.h, [&](int y) {
Randomness rng(rowSeeds[size_t(y)]);
std::vector<RenderVertex> sharedEyePath;
sharedEyePath.reserve(INITIAL_PATH_LENGTH);
for (int x = 0; x < img.w; ++x) {
for (int samp = 0; samp < img.samplesPerPixel; ++samp) {
float offsetY = rng.nextFloat(-img.filterWidth, img.filterWidth);
float offsetX = rng.nextFloat(-img.filterWidth, img.filterWidth);
float posY = float(y) + offsetY;
float posX = float(x) + offsetX;
float fracY = posY / (float(img.h) - 1.0f);
float fracX = posX / (float(img.w) - 1.0f);
// Implement depth of field by jittering the eye.
Vec offset(focalPlaneRight * fracX, focalPlaneUp * fracY, 0);
Vec lookAt = focalPlaneOrigin + offset;
Vec eye(0, 0, 0);
math::areaSampleDisk(rng, &eye[0], &eye[1]);
eye = eye * lensRadius;
Vec eyeWorld = camToWorldXform * eye;
Vec lookAtWorld = camToWorldXform * lookAt;
Vec dir = (lookAtWorld - eyeWorld).normalized();
Vec L = trace(rng, Ray(eyeWorld, dir), sharedEyePath);
img.setSample(x, y, posX, posY, samp, L);
}
}
});
// Process and write the output file at the end of this iteration.
img.commitSamples();
img.writeToEXR(name);
// End timer.
chrono::steady_clock::time_point endTime = chrono::steady_clock::now();
chrono::duration<float> runTime =
chrono::duration_cast<chrono::duration<float>>(endTime - startTime);
std::cout << " [" << runTime.count() << " seconds]\n";
}
void Camera::renderMultiple(
std::string name,
int iterations
) {
if (iterations < 0) {
// Run forever.
std::cout << "Rendering infinitely, press Ctrl-c to terminate program\n";
while (true) {
renderOnce(name);
}
} else {
// Run finite iterations.
std::cout << "Rendering " << iterations << " iterations\n";
for (int i = 0; i < iterations; ++i) {
renderOnce(name);
}
}
}
Vec Camera::trace(
Randomness& rng,
const Ray& r,
std::vector<RenderVertex>& sharedEyePath
) const {
sharedEyePath.clear();
randomWalk(rng, r, Vec(1, 1, 1), sharedEyePath);
Vec L(0, 0, 0);
bool didDirectIlluminate = false;
for (const RenderVertex& vtx : sharedEyePath) {
const Material* mat = vtx.geom->mat;
const AreaLight* light = vtx.geom->light;
// Check for lighting.
if (light && !didDirectIlluminate) {
// Accumulate emission normally if we did not direct-illuminate at the
// last vertex. For any _n_, we can only count one _n_-length path per
// sample trace.
L += vtx.beta.cwiseProduct(light->emit(vtx));
}
// Direct-illuminate if possible.
if (mat && mat->shouldDirectIlluminate()) {
#ifndef NO_DIRECT_ILLUM
// Sample direct lighting and then continue path.
L += vtx.beta.cwiseProduct(
uniformSampleOneLight(rng, vtx)
);
didDirectIlluminate = true;
#else
didDirectIlluminate = false;
#endif
} else {
didDirectIlluminate = false;
}
}
L[0] = math::clamp(L[0], 0.0f, BIASED_RADIANCE_CLAMPING);
L[1] = math::clamp(L[1], 0.0f, BIASED_RADIANCE_CLAMPING);
L[2] = math::clamp(L[2], 0.0f, BIASED_RADIANCE_CLAMPING);
return L;
}
void Camera::randomWalk(
Randomness& rng,
Ray r,
Vec beta,
std::vector<RenderVertex>& path
) const {
for (int depth = 0; ; ++depth) {
// Bounce ray and kill if nothing hit.
RenderVertex isect(beta);
if (accel.intersect(r, &isect)) {
path.push_back(isect);
} else {
// End path in empty space.
break;
}
// Check for scattering (reflection/transmission).
if (isect.geom->mat) {
isect.geom->mat->scatter(rng, isect, &r, &beta);
} else {
// Cannot continue path without a material.
break;
}
// Do Russian Roulette if this path is "old".
if (depth >= RUSSIAN_ROULETTE_DEPTH_1 || math::isNearlyZero(beta)) {
float rv = rng.nextUnitFloat();
float probLive;
if (depth >= RUSSIAN_ROULETTE_DEPTH_2) {
// More aggressive ray killing when ray is very old.
probLive = math::clampedLerp(0.25f, 0.75f, math::luminance(beta));
} else {
// Less aggressive ray killing.
probLive = math::clampedLerp(0.25f, 1.00f, math::luminance(beta));
}
if (rv < probLive) {
// The ray lives (more energy = more likely to live).
// Increase its energy to balance out probabilities.
beta = beta / probLive;
} else {
// The ray dies.
break;
}
}
}
}
Vec Camera::uniformSampleOneLight(
Randomness& rng,
const Intersection& isect
) const {
size_t numLights = emitters.size();
if (numLights == 0) {
return Vec(0, 0, 0);
}
size_t lightIdx = size_t(floorf(rng.nextUnitFloat() * numLights));
const Geom* emitter = emitters[min(lightIdx, numLights - 1)];
const AreaLight* areaLight = emitter->light;
// P[this light] = 1 / numLights, so 1 / P[this light] = numLights.
return float(numLights) * areaLight->directIlluminate(
rng, isect, emitter, &accel
);
}