/
light_source.cpp
464 lines (388 loc) · 11.2 KB
/
light_source.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
#include <cmath>
#include <iostream>
#include <assert.h>
#include "light_source.h"
#include "photonmap.h"
#include "raytracer.h"
#include "random.h"
#include "config.h"
SquarePhotonLight::SquarePhotonLight(Colour col, Raytracer *raytracer ) :
col(col), raytracer(raytracer), icache(ICACHE_TOLERANCE,
ICACHE_MIN_SPACING)
{
light_col = col;
}
SquarePhotonLight::~SquarePhotonLight() {
destroyPhotonMap(bmap);
destroyPhotonMap(cmap);
}
/* seems like this is where Matrix4x4 is used */
void SquarePhotonLight::initTransformMatrix(Matrix4x4 mat, Vector3D& w)
{
Vector3D u, v;
if ((fabs(w.v[0]) < fabs(w.v[1])) && (fabs(w.v[0]) < fabs(w.v[2]))) {
v.v[0] = 0;
v.v[1] = w.v[2];
v.v[2] = -w.v[1];
} else if (fabs(w.v[1]) < fabs(w.v[2])) {
v.v[0] = w.v[2];
v.v[1] = 0;
v.v[2] = -w.v[0];
} else {
v.v[0] = w.v[1];
v.v[1] = -w.v[0];
v.v[2] = 0;
}
v.normalize();
u = v.cross(w);
mat[0][0] = u.v[0];
mat[1][0] = u.v[1];
mat[2][0] = u.v[2];
mat[0][1] = v.v[0];
mat[1][1] = v.v[1];
mat[2][1] = v.v[2];
mat[0][2] = w.v[0];
mat[1][2] = w.v[1];
mat[2][2] = w.v[2];
}
void SquarePhotonLight::globalIllumination(Ray3D& ray, bool getDirectly)
{
// If we're not already in the irradiance cache, compute the irradiance via
// monte carlo methods.
if (!icache.getIrradiance(ray.intersection.point, ray.intersection.normal, &ray.col)) {
Colour c;
ray.col = Colour(0, 0, 0);
int N = MONTE_CARLO_STRATIFICATION_N;
int M = MONTE_CARLO_STRATIFICATION_M;
int hits = 0;
float r0 = 0;
Matrix4x4 basis;
initTransformMatrix(basis, ray.intersection.normal);
// Stratification
for (int i = 0; i < N; i++) {
float phi = 2 * M_PI * ((float) i + r.Random1()) / N;
float sinPhi = sin(phi);
float cosPhi = cos(phi);
for (int j = 0; j < M; j++) {
float cosTheta = rsqrtss_sqrt(1 - (((float) j + r.Random1()) / M));
float theta = acos(cosTheta);
float sinTheta = sin(theta);
Vector3D v = Vector3D(cosPhi * sinTheta, sinPhi * sinTheta, cosTheta);
v = v.transform(basis);
v.normalize();
Ray3D new_ray = Ray3D(ray.intersection.point, v);
raytracer->traverseEntireScene(new_ray, true);
if (!new_ray.intersection.none) {
raytracer->computeShading(new_ray, 3, true);
ray.col += new_ray.col;
r0 += 1 / new_ray.intersection.t_value;
hits++;
}
}
}
ray.col *= 1.0 / hits;
r0 = 1 / r0;
if (hits == N * M) {
icache.insert(ray.intersection.point, ray.intersection.normal, r0, ray.col);
}
}
ray.col *= ray.intersection.mat->diffuse;
}
void SquarePhotonLight::causticIllumination(Ray3D& ray)
{
// Caustics
Colour caus_col;
Vector3D normal = ray.intersection.normal;
normal.normalize();
if (raytracer->soft_shadows) {
irradianceEstimate(cmap,
&caus_col,
ray.intersection.point,
normal,
CAUSTICS_SOFT_MAX_DISTANCE,
CAUSTIC_SOFT_MAX_PHOTONS);
} else {
irradianceEstimate(cmap,
&caus_col,
ray.intersection.point,
normal,
CAUSTICS_MAX_DISTANCE,
CAUSTIC_MAX_PHOTONS);
}
float cosTheta12 = rsqrtss_sqrt(-(ray.dir.dot(ray.intersection.normal)));
caus_col *= ray.intersection.mat->diffuse;
ray.col += caus_col * cosTheta12;
}
void SquarePhotonLight::directIllumination(Ray3D& ray)
{
// Direct illumination
int N = 1;
int M = 1;
if (raytracer->soft_shadows) {
N = NUM_SOFT_SHADOW_RAYS_IN_EACH_DIM;
M = NUM_SOFT_SHADOW_RAYS_IN_EACH_DIM;
}
Colour direct_col = Colour(0, 0, 0);
float dx = 1.0 / (N + 1);
float dz = 1.0 / (M + 1);
// Loop for soft shadows
for (int i = 1; i <= N; i++) {
float x;
if (raytracer->soft_shadows) {
float rand = r.Random1();
float dx_rand = rand - floor(rand);
x = ((i + dx_rand) * dx) * 30 - 15;
} else {
x = i * dx * 30 - 15;
}
for (int j = 1; j <= M; j++) {
float z;
if (raytracer->soft_shadows) {
float rand = r.Random1();
float dz_rand = rand - floor(rand);
z = (((float) j + dz_rand) * dz) * 30 - 15;
} else {
z = i * dz * 30 - 15;
}
Vector3D L = Point3D(0 + x, 50, 0 + z) - ray.intersection.point;
float l = rsqrtss_sqrt(L.v[0] * L.v[0] + L.v[1] * L.v[1] + L.v[2] * L.v[2]);
L.normalize();
Vector3D LN = Vector3D(0, -1, 0);
float scale = -LN.dot(L);
scale *= (scale >= 0);
// scale = scale < 0 ? 0 : scale;
scale /= l * l * 1.5 * M_PI;
Ray3D new_ray = Ray3D(ray.intersection.point, L);
raytracer->traverseEntireScene(new_ray, false);
if (!new_ray.intersection.none && new_ray.intersection.mat->light) {
Vector3D R = 2.0 * ray.intersection.normal.dot(L) * ray.intersection.normal - L;
float NdotL = L.dot(ray.intersection.normal);
float RdotV = -(R.dot(ray.dir));
NdotL *= (NdotL >= 0);
// NdotL = NdotL < 0 ? 0 : NdotL;
RdotV *= (RdotV >= 0);
// RdotV = RdotV < 0 ? 0 : RdotV;
RdotV *= ray.dir.dot(ray.intersection.normal) <= 0;
/*
if (ray.dir.dot(ray.intersection.normal) > 0) {
RdotV = 0;
}
*/
direct_col += (light_col * scale * (ray.intersection.mat->diffuse * NdotL + ray.intersection.mat->specular * pow(RdotV, ray.intersection.mat->specular_exp)));
}
}
}
direct_col = direct_col / (N * M);
ray.col += direct_col;
}
void SquarePhotonLight::shade(Ray3D& ray, bool getDirectly)
{
// Don't bother shading lights, just give it the color of the light source.
if (ray.intersection.mat->light) {
ray.col = ray.intersection.mat->diffuse;
return;
}
// getDirectly means we visualize the photon map directly.
if (getDirectly) {
Vector3D normal = ray.intersection.normal;
normal.normalize();
irradianceEstimate(bmap,
&ray.col,
ray.intersection.point,
normal,
INDIRECT_MAX_DISTANCE,
INDIRECT_MAX_PHOTONS);
ray.col = ray.col * ray.intersection.mat->diffuse;
return;
}
// Only look at objects in the photon map
if (ray.intersection.mat->isDiffuse) {
if (raytracer->global_illumination)
globalIllumination(ray, getDirectly);
if (raytracer->caustics)
causticIllumination(ray);
}
if (raytracer->direct_illumination)
directIllumination(ray);
}
Vector3D SquarePhotonLight::getRandLambertianDir(Vector3D& normal)
{
Vector3D v;
float phi = 2 * M_PI * r.Random1();
float sinPhi = sin(phi);
float cosPhi = cos(phi);
float cosTheta = rsqrtss_sqrt(r.Random1());
float theta = acos(cosTheta);
float sinTheta = sin(theta);
Matrix4x4 basis;
initTransformMatrix(basis, normal);
v = Vector3D(cosPhi * sinTheta, sinPhi * sinTheta, cosTheta);
v = v.transform(basis);
v.normalize();
return v;
}
void SquarePhotonLight::tracePhotons(int num, int caustics_num)
{
// Flatten scene before render
raytracer->flattenEntireScene();
PhotonMap *map = createPhotonMap(num * 4);
cout << "Emitting global illumination photons: 0%% ";
int i = 0;
while (i < num) {
// Calculate the start pos and direction of the photon
Point3D p = Point3D(r.Random2() * 16, 49.99, r.Random2() * 16);
Vector3D v = Vector3D(0, -1, 0);
v = getRandLambertianDir(v);
Ray3D ray = Ray3D(p, v);
ray.col = col;
int count = 0;
while (ray.col.max() > 0.1 && count++ < 100) {
raytracer->traverseEntireScene(ray, true);
if (ray.intersection.none) {
i--;
break;
}
Vector3D dir_norm = ray.dir;
dir_norm.normalize();
if (ray.intersection.mat->isDiffuse) {
storePhoton(map,
ray.col,
ray.intersection.point,
dir_norm);
}
float ran = r.Random1();
Colour c = ray.col * ray.intersection.mat->diffuse;
float P = c.max() / ray.col.max();
if (ran < P) {
// Diffuse reflection
v = getRandLambertianDir(ray.intersection.normal);
} else {
ran -= P;
c = ray.col * ray.intersection.mat->specular;
P = c.max() / ray.col.max();
if (ran < P) {
// Specular reflection
v = ray.dir - (2 * ray.dir.dot(ray.intersection.normal)) *
ray.intersection.normal;
} else {
ran -= P;
c = ray.col * ray.intersection.mat->refractive;
P = c.max() / ray.col.max();
if (ran < P) {
// Refraction
float n;
if (ray.dir.dot(ray.intersection.normal) < 0) {
n = 1 / ray.intersection.mat->refr_index;
} else {
ray.intersection.normal = -ray.intersection.normal;
n = ray.intersection.mat->refr_index;
}
float cosI = ray.intersection.normal.dot(ray.dir);
float sinT2 = n * n * (1.0 - cosI * cosI);
if (sinT2 < 1.0) {
v = n * ray.dir - (n * cosI + rsqrtss_sqrt(1.0 - sinT2)) *
ray.intersection.normal;
} else {
// Total internal reflection
v = ray.dir - (2 * ray.dir.dot(ray.intersection.normal)) *
ray.intersection.normal;
}
} else {
// Absorption
break;
}
}
}
ray.origin = ray.intersection.point;
ray.dir = v;
ray.col = c / P;
ray.intersection.none = true;
ray.intersection.t_value = FLT_MAX;
}
raytracer->printProgress((int) (((i + 1) * 100.0) / num));
i++;
}
scalePhotonPower(map, 1.0 / i);
bmap = balancePhotonMap(map);
// Caustics
map = createPhotonMap(caustics_num);
int emitted = 0;
i = 0;
cout << endl << "Emitting caustics illumination photons: 0%% ";
while (i < caustics_num) {
// Calculate the start pos and direction of the photon
Point3D p;
if (raytracer->soft_shadows) {
p = Point3D(r.Random2() * 18, 49.99, r.Random2() * 18);
} else {
p = Point3D(0, 49.99, 0);
}
Vector3D v = Vector3D(0, -1, 0);
v = getRandLambertianDir(v);
Ray3D ray = Ray3D(p, v);
ray.col = col;
emitted++;
raytracer->traverseEntireScene(ray, true);
if (!ray.intersection.none && ray.intersection.mat->isSpecular) {
while (1) {
raytracer->traverseEntireScene(ray, true);
if (ray.intersection.none) break;
Vector3D dir_norm = ray.dir;
dir_norm.normalize();
if (ray.intersection.mat->isDiffuse) {
storePhoton(map,
ray.col,
ray.intersection.point,
dir_norm);
i++;
break;
}
float ran = r.Random1();
Colour c = ray.col * ray.intersection.mat->specular;
float P = c.max() / ray.col.max();
// Specular reflection
if (ran < P) {
ray.intersection.normal = -ray.intersection.normal;
v = ray.dir - (2 * ray.dir.dot(ray.intersection.normal)) *
ray.intersection.normal;
} else {
ran -= P;
c = ray.col * ray.intersection.mat->refractive;
P = c.max() / ray.col.max();
if (ran < P) {
// Refraction
float n;
if (ray.dir.dot(ray.intersection.normal) < 0) {
n = 1 / ray.intersection.mat->refr_index;
} else {
ray.intersection.normal = -ray.intersection.normal;
n = ray.intersection.mat->refr_index;
}
float cosI = ray.intersection.normal.dot(ray.dir);
float sinT2 = n * n * (1.0 - cosI * cosI);
if (sinT2 < 1.0) {
v = n * ray.dir - (n * cosI + rsqrtss_sqrt(1.0 - sinT2)) *
ray.intersection.normal;
} else { // Total internal reflection.
v = ray.dir - (2 * ray.dir.dot(ray.intersection.normal)) *
ray.intersection.normal;
}
} else {
// Absorption
i++;
break;
}
}
ray.origin = ray.intersection.point;
ray.dir = v;
ray.col = c / P;
ray.intersection.none = true;
ray.intersection.t_value = FLT_MAX;
}
raytracer->printProgress((int) (((i + 1) * 100.0) / caustics_num));
}
}
cout << endl;
scalePhotonPower(map, 1.0 / emitted);
cmap = balancePhotonMap(map);
}