-
Notifications
You must be signed in to change notification settings - Fork 0
/
bidirectional.cpp
789 lines (637 loc) · 26.1 KB
/
bidirectional.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
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cfloat>
#include <iostream>
#include <boost/thread.hpp>
#include <boost/thread/locks.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <fstream>
#include <vector>
boost::mutex buffer_mutex;
using namespace std;
// Generate a random number between 0 and 1
// return a uniform number in [0,1].
double unifRand()
{
return rand() / double(RAND_MAX);
}
// Generate a random number in a real interval.
// param a one end point of the interval
// param b the other end of the interval
// return a inform rand numberin [a,b].
double unifRand(double a, double b)
{
return (b-a)*unifRand() + a;
}
// Generate a random integer between 1 and a given value.
// param n the largest value
// return a uniform random value in [1,...,n]
long unifRand(long n)
{
if (n < 0) n = -n;
if (n==0) return 0;
/* There is a slight error in that this code can produce a return value of n+1
**
** return long(unifRand()*n) + 1;
*/
//Fixed code
long guard = (long) (unifRand() * n) +1;
return (guard > n)? n : guard;
}
class V3 {
public:
double x, y, z;
V3(void) {
x = 0.0L;
y = 0.0L;
z = 0.0L;
}
V3(double ix, double iy, double iz) {
x = ix;
y = iy;
z = iz;
}
V3 add(V3 v) {
return V3(x + v.x, y + v.y, z + v.z);
}
V3 iadd(V3 v) {
x += v.x;
y += v.y;
z += v.z;
}
V3 sub(V3 v) {
return V3(x - v.x, y - v.y, z - v.z);
}
V3 mul(V3 v) {
return V3(x * v.x, y * v.y, z * v.z);
}
V3 div(V3 v) {
return V3(x / v.x, y / v.y, z / v.z);
}
V3 muls(double s) {
return V3(x * s, y * s, z * s);
}
V3 divs(double s) {
return muls(1.0L / s);
}
double dot(V3 v) {
return x * v.x + y * v.y + z * v.z;
}
V3 normalize(void) {
return divs(sqrt(dot(*this)));
}
};
V3 getRandomNormalInHemisphere(V3 v) {
V3 v2(0.0L, 0.0L, 0.0L);
do {
v2 = V3(unifRand()*2.0L-1.0L, unifRand()*2.0L-1.0L, unifRand()*2.0L-1.0L);
} while (v2.dot(v2) > 1.0L);
v2.normalize();
if (v2.dot(v) < 0.0L) {
return v2.muls(-1.0L);
}
return v2;
}
class Ray {
public:
V3 origin;
V3 direction;
Ray(V3 iorigin, V3 idirection) {
origin = iorigin;
direction = idirection;
}
};
class Camera {
public:
V3 origin;
V3 topleft;
V3 topright;
V3 bottomleft;
V3 xd;
V3 yd;
Camera(void) {
}
Camera(V3 iorigin, V3 itopleft, V3 itopright, V3 ibottomleft) {
origin = iorigin;
topleft = itopleft;
topright = itopright;
bottomleft = ibottomleft;
xd = topright.sub(topleft);
yd = bottomleft.sub(topleft);
}
Ray getRay(double x, double y) {
V3 p = topleft.add(xd.muls(x)).add(yd.muls(y));
return Ray(origin, p.sub(origin).normalize());
}
V3 getPoint(double x, double y) {
return topleft.add(xd.muls(x)).add(yd.muls(y));
}
};
class Shape {
public:
V3 center;
virtual double intersect(Ray) {
// cout << "Shape.intersect()" << endl;
}
virtual V3 getNormal(V3) {}
};
class Plane : public Shape {
public:
V3 center;
V3 normal;
Plane(V3 icenter, V3 inormal) {
center = icenter;
normal = inormal.normalize();
}
double intersect(Ray r) {
double n_dot_u = normal.dot(r.direction);
if ((n_dot_u > -0.00001L) && (n_dot_u < 0.00001L)) {
return -1.0L;
}
double n_dot_p0 = normal.dot(center.sub(r.origin));
return n_dot_p0 / n_dot_u;
}
V3 getNormal(V3 point) {
return normal;
}
};
class Sphere : public Shape {
public:
V3 center;
double radius;
double radius2;
Sphere(V3 icenter, double iradius) {
center = icenter;
radius = iradius;
radius2 = radius * radius;
}
double intersect(Ray r) {
// cout << "Sphere intersect()" << endl;
V3 distance = r.origin.sub(center);
double b = distance.dot(r.direction);
double c = distance.dot(distance) - radius2;
double d = (b * b) - c;
if (d > 0.0L) {
return -b - sqrt(d);
} else {
return -1.0L;
}
}
V3 getNormal(V3 point) {
return point.sub(center).normalize();
}
};
class Material {
public:
V3 color;
V3 emission;
Material(void) {
color = V3(0.0L, 0.0L, 0.0L);
emission = V3(0.0L, 0.0L, 0.0L);
};
Material(V3 icolor, V3 iemission) {
color = icolor;
emission = iemission;
}
Material(V3 icolor) {
color = icolor;
emission = V3(0.0L, 0.0L, 0.0L);
}
virtual V3 bounce(Ray ray, V3 inormal) {
// cout << "material bounce\n";
return getRandomNormalInHemisphere(inormal);
}
};
class Chrome : public Material {
public:
Chrome(V3 icolor) : Material(icolor) {}
V3 bounce(Ray ray, V3 inormal) {
// cout << "glass bounce\n";
double theta1 = fabs(ray.direction.dot(inormal));
return ray.direction.add(inormal.muls(theta1 * 2.0L));
}
};
class Glass : public Material {
public:
double ior;
double reflection;
Glass(V3 icolor, double iior, double ireflection) : Material(icolor) {
ior = iior;
reflection = ireflection;
}
V3 bounce(Ray ray, V3 normal) {
// cout << "chrome bounce\n";
double theta1 = fabs(ray.direction.dot(normal));
double internalIndex, externalIndex;
if (theta1 >= 0.0L) {
internalIndex = ior;
externalIndex = 1.0L;
} else {
internalIndex = 1.0L;
externalIndex = ior;
}
double eta = externalIndex/internalIndex;
double theta2 = sqrt(1.0L - (eta * eta) * (1.0L - (theta1 * theta1)));
double rs = (externalIndex * theta1 - internalIndex * theta2) / (externalIndex*theta1 + internalIndex * theta2);
double rp = (internalIndex * theta1 - externalIndex * theta2) / (internalIndex*theta1 + externalIndex * theta2);
double reflectance = (rs*rs + rp*rp);
//reflection
if(unifRand() < reflectance+reflection) {
return ray.direction.add(normal.muls(theta1*2.0L));
}
// refraction
return (ray.direction.add(normal.muls(theta1)).muls(eta) \
.add(normal.muls(-theta2)));
}
};
class Body {
public:
Shape* shape;
Material* material;
Body(Shape* ishape, Material* imaterial) {
shape = ishape;
material = imaterial;
}
};
struct Scene {
int width;
int height;
Camera camera;
Body** objects;
int body_count;
};
struct HitRecord {
V3 point;
Body* target;
HitRecord(V3 ipoint, Body* itarget) {
point = ipoint;
target = itarget;
}
};
class Renderer {
public:
Scene scene;
V3* buffer;
int pixels;
vector <Body*> lights;
Renderer(Scene iscene) {
scene = iscene;
pixels = scene.width * scene.height;
buffer = new V3[pixels];
for (int i = 0; i < pixels; i++) {
buffer[i] = V3(0.0L, 0.0L, 0.0L);
}
for (int i = 0; i < scene.body_count; i++) {
V3 emission = scene.objects[i]->material->emission;
if ((emission.x >= 1.0L) || (emission.y >= 1.0L) || (emission.z >= 1.0L)) {
lights.push_back(scene.objects[i]);
}
}
}
~Renderer(void) {
delete[] buffer;
}
void iterate() {
int i = 0;
double flux_lookup[5][4] = {
{0, 0, 0, 0},
{1.0L, 0, 0, 0},
{0.5L, 0.5L, 0, 0},
{0.5L, 0.25L, 0.25L, 0},
{0.5L, 0.25L, 0.125L, 0.125L}};
// cout << "Entered iterate()\n";
for (double y = unifRand() / (double)scene.height, ystep = 1.0L / (double)scene.height;
y < 0.99999L;
y += ystep) {
for (double x = unifRand() / (double)scene.width, xstep = 1.0L / (double)scene.width;
x < 0.99999L;
x += xstep) {
// cout << "Iterate! " << x << ", " << y << endl;
Ray ray = scene.camera.getRay(x, y);
vector <HitRecord> gather_hits;
V3 gather_color = trace(ray, 0, &gather_hits);
// cout << color.x << ", " << color.y << ", " << color.z << endl;
// Choose a random light and trace a ray from it into the scene.
Body* light = lights[unifRand(lights.size() - 1)];
// BUGBUG How do we make sure we're shooting a ray
// into the scene instead of out of it?
ray = Ray(
light->shape->center,
getRandomNormalInHemisphere(light->shape->getNormal(light->shape->center)));
vector <HitRecord> shoot_hits;
V3 shoot_color = trace(ray, 0, &shoot_hits);
V3 final_color = V3();
for (int gather_i = gather_hits.size() - 1; gather_i >= 0; gather_i--) {
for (int shoot_i = shoot_hits.size() - 1; shoot_i >= 0; shoot_i--) {
// Ray bi_ray = Ray(
// light->shape->center,
// gather_hits[gather_i].point.sub(light->shape->center).normalize());
Ray bi_ray = Ray(
shoot_hits[shoot_i].point,
gather_hits[gather_i].point.sub(shoot_hits[shoot_i].point).normalize());
Body *hit = NULL;
double mint = DBL_MAX;
// cout << "Body count: " << scene.body_count << endl;
for (int body_i = 0; body_i < scene.body_count; body_i++) {
Body *candidate = scene.objects[body_i];
if (candidate == light) {
continue;
}
double t = candidate->shape->intersect(bi_ray);
if ((t > 0) && (t <= mint)) {
mint = t;
hit = candidate;
}
}
if (hit != gather_hits[gather_i].target) {
continue;
}
// TODO Must decrease the flux over the shooting ray's hits.
// final_color.iadd(
// light->material->emission.mul(
// hit->material->color).add(hit->material->emission).muls((double)(gather_hits.size() - gather_i) / (double)(gather_hits.size() + 1)));
// final_color = final_color.iadd(
// (light->material->emission.mul(
// hit->material->color)).add(hit->material->emission).divs((double)gather_hits.size() + 1.0L));
// HACK Faking the BRDF according to: http://www.cescg.org/CESCG98/PDornbach/index.html
V3 next_point;
if (gather_i >= 1) {
next_point = gather_hits[gather_i - 1].point;
} else {
next_point = scene.camera.getPoint(x, y);
}
// Ray from shoot to gather hits.
Ray shoot_gather = Ray(
shoot_hits[shoot_i].point,
gather_hits[gather_i].point.sub(shoot_hits[shoot_i].point).normalize());
// Ray bi_ray = Ray(
// From gather hit to shoot hit.
V3 gather_brdf = hit->material->bounce(
shoot_gather,
next_point.sub(gather_hits[gather_i].point).normalize());
double gather_flux = fabs(gather_brdf.dot(hit->shape->getNormal(gather_hits[gather_i].point)));
// From light to shoot hit.
V3 shoot_brdf = shoot_hits[shoot_i].target->material->bounce(
bi_ray,
gather_hits[gather_i].point.sub(shoot_hits[shoot_i].point).normalize());
double shoot_flux = fabs(shoot_brdf.dot(shoot_hits[shoot_i].target->shape->getNormal(shoot_hits[shoot_i].point)));
// cout << "flux: " << light_flux << endl;
// final_color = final_color.add(light->material->emission).mul(
// hit->material->color);
// final_color = final_color.add(light_color.mul(light->material->color)).add(hit->material->emission).mul(
// hit->material->color);
// final_color = final_color.add(light->material->emission.mul(light->material->color).muls(light_flux)).add(hit->material->emission).mul(
// hit->material->color);
double flux = flux_lookup[shoot_hits.size()][shoot_i] * gather_flux * shoot_flux;
final_color = final_color.add(light->material->emission.mul(light->material->color).muls(flux)).mul(hit->material->color).add(
hit->material->emission);
}
}
// TODO Change this to the combination of the gather_color and shoot_color;
//buffer[i++].iadd(gather_color);
//buffer[i++].iadd(final_color.divs(((double)(shoot_hits.size() + 1)) * ((double)(gather_hits.size() + 1))));
buffer[i++].iadd(final_color);
}
}
}
void working_brdf() {
// This appears to work reasonably well for next event
// calculations based on the BRDF. Note that instead of a
// cos() we simply use the dot product of the incoming light
// and the hit objects normal at the hit point.
// NOTE Not certain if we should have the fabs() present or not.
// V3 light_brdf = hit->material->bounce(
// bi_ray,
// next_point.sub(gather_hits[gather_i].point).normalize());
// double light_flux = fabs(light_brdf.dot(hit->shape->getNormal(gather_hits[gather_i].point)));
// final_color = final_color.add(light->material->emission.mul(light->material->color).muls(light_flux)).mul(hit->material->color).add(
// hit->material->emission);
}
V3 trace(Ray ray, int n, vector <HitRecord>* hits) {
if (n > 4) {
return V3();
}
Body *hit = NULL;
double mint = DBL_MAX;
// cout << "Body count: " << scene.body_count << endl;
for (int i = 0; i < scene.body_count; i++) {
Body *candidate = scene.objects[i];
// cout << candidate->shape->intersect(ray) << endl;
double t = candidate->shape->intersect(ray);
if ((t > 0) && (t <= mint)) {
mint = t;
hit = candidate;
}
}
if (hit == NULL) {
return V3();
}
// cout << "hit";
V3 point = ray.origin.add(ray.direction.muls(mint));
V3 normal = hit->shape->getNormal(point);
V3 direction = hit->material->bounce(ray, normal);
if (direction.dot(ray.direction) > 0.0f) {
// if the ray is refractedmove the intersection point a bit in
point = ray.origin.add(ray.direction.muls(mint*1.0000001L));
} else {
// otherwise move it out to prevent problems with doubleing point
// accuracy
point = ray.origin.add(ray.direction.muls(mint*0.9999999L));
}
hits->push_back(HitRecord(point, hit));
Ray newray = Ray(point, direction);
return trace(newray, n+1, hits).mul(hit->material->color).add(hit->material->emission);
}
};
void save(V3* buffer, int samples, int width, int height) {
ofstream myfile;
myfile.open ("output.ppm");
myfile << "P3\n" << width << " " << height << endl << "255\n";
V3* pixel = buffer;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int r = (255.0L * pixel->x) / (double)samples;
int g = (255.0L * pixel->y) / (double)samples;
int b = (255.0L * pixel->z) / (double)samples;
if (r > 255) { r = 255; }
if (g > 255) { g = 255; }
if (b > 255) { b = 255; }
myfile << r << " " << g << " " << b << endl;
pixel++;
}
}
myfile.close();
}
void worker(int worker_num, int iterations, Scene* scene, V3* buffer) {
Renderer renderer = Renderer(*scene);
for (int i = 0; i < iterations; i++) {
cout << "Worker " << worker_num << " iteration " << i << endl;
renderer.iterate();
}
boost::mutex::scoped_lock lock(buffer_mutex);
V3* src_pixel = renderer.buffer;
V3* dst_pixel = buffer;
for (int y = 0; y < scene->height; y++) {
for (int x = 0; x < scene->width; x++) {
dst_pixel->x += src_pixel->x;
dst_pixel->y += src_pixel->y;
dst_pixel->z += src_pixel->z;
src_pixel++;
dst_pixel++;
}
}
// V3* renderer_buffer = renderer_buffer;
// for (int i = 0; i < (scene->width * scene->height); i++) {
// buffer->iadd(renderer_buffer->divs((double)iterations));
// buffer++;
// renderer_buffer++;
// }
}
int main(int argc, const char* argv[]) {
srand(time(NULL));
if (argc < 5) {
cout << argv[0] << " <width> <height> <iterations> <thread count>\n";
return 0;
}
int width = strtol(argv[1], NULL, 10);
int height = strtol(argv[2], NULL, 10);
int iterations = strtol(argv[3], NULL, 10);
int thread_count = strtol(argv[4], NULL, 10);
// int width = 320;
// int height = 240;
// int width = 640;
// int height = 480;
Scene scene;
scene.width = width;
scene.height = height;
scene.camera = Camera(
V3(0.0L, -0.5L, 0.0L),
V3(-1.3L, 1.0L, 1.0L),
V3(1.3L, 1.0L, 1.0L),
V3(-1.3L, 1.0L, -1.0L)
);
// Sphere glowing_sphere = Sphere(V3(0.0L, 3.0L, 0.0L), 0.5L);
// Material glowing_mat = Material(V3(0.9L, 0.9L, 0.9L), V3(1.2L, 1.2L, 1.2L));
// Body glowing = Body(&glowing_sphere, &glowing_mat);
Sphere glass_sphere = Sphere(V3(1.0L, 2.0L, 0.0L), 0.5L);
Glass glass_mat = Glass(V3(1.00L, 1.00L, 1.00L), 1.5L, 0.1L);
Body glass = Body(&glass_sphere, &glass_mat);
Sphere chrome_sphere = Sphere(V3(-1.1L, 2.8L, 0.0L), 0.5L);
Chrome chrome_mat = Chrome(V3(0.8L, 0.8L, 0.8L));
Body chrome = Body(&chrome_sphere, &chrome_mat);
Sphere green_sphere = Sphere(V3(-0.2L, 1.6L, -0.4L), 0.1L);
Glass green_mat = Glass(V3(0.0L, 0.8L, 0.0L), 1.0L, 0.2L);
Body green = Body(&green_sphere, &green_mat);
Sphere red_sphere = Sphere(V3(0.0L, 1.11716L, -0.4L), 0.1L);
Glass red_mat = Glass(V3(0.8, 0.0, 0.0), 1.0, 0.2);
Body red = Body(&red_sphere, &red_mat);
Sphere blue_sphere = Sphere(V3(0.2, 1.6, -0.4), 0.1);
Glass blue_mat = Glass(V3(0.0, 0.0, 0.8), 1.0, 0.2);
Body blue = Body(&blue_sphere, &blue_mat);
Sphere pyramid_sphere = Sphere(V3(0.0L, 1.4L, -0.28284L), 0.1L);
Glass pyramid_mat = Glass(V3(1.00L, 1.00L, 1.00L), 1.5L, 0.1L);
Body pyramid = Body(&pyramid_sphere, &pyramid_mat);
// # White ball
// Body(Sphere(V3(0.0, 1.4, -0.6828), 0.1), Glass(V3(0.8, 0.8, 0.8), 1.0, 0.2)),
Material floor_mat = Material(V3(0.9L, 0.9L, 0.9L));
Plane floor_plane = Plane(V3(0.0L, 3.5L, -0.5L), V3(0.0L, 0.0L, 1.0L));
Body floor = Body(&floor_plane, &floor_mat);
Plane back_plane = Plane(V3(0.0L, 4.5L, 0.0L), V3(0.0L, -1.0L, 0.0L));
Material back_mat = Material(V3(0.9L, 0.9L, 0.9L));
Body back = Body(&back_plane, &back_mat);
Plane left_plane = Plane(V3(-1.9L, 0.0L, 0.0L), V3(1.0L, 0.0L, 0.0L));
Material left_mat = Material(V3(0.9L, 0.5L, 0.5L));
Body left = Body(&left_plane, &left_mat);
Plane right_plane = Plane(V3(1.9L, 0.0L, 0.0L), V3(-1.0L, 0.0L, 0.0L));
Material right_mat = Material(V3(0.5L, 0.5L, 0.9L));
Body right = Body(&right_plane, &right_mat);
Plane top_light_plane = Plane(V3(0.0L, 0.0L, 2.5L), V3(0.0L, 0.0L, -1.0L));
Material top_light_mat = Material(V3(0.0L, 0.0L, 0.0L), V3(1.6L, 1.47L, 1.29L));
// Material top_light_mat = Material(V3(0.0L, 0.0L, 0.0L), V3(1.0L, 0.87L, 0.69L));
// Material top_light_mat = Material(V3(0.0L, 0.0L, 0.0L), V3(1.1L, 0.97L, 0.79L));
Body top_light = Body(&top_light_plane, &top_light_mat);
Plane front_plane = Plane(V3(0.0L, -2.5L, 0.0L), V3(0.0L, 1.0L, 0.0L));
Material front_mat = Material(V3(0.9L, 0.9L, 0.9L));
Body front = Body(&front_plane, &front_mat);
Plane top_left_divider_plane = Plane(V3(-1.8L, 4.4L, 2.4L), V3(1.0L, -1.0L, -1.0L).normalize());
Material top_left_divider_mat = Glass(V3(0.0L, 0.8L, 0.0L), 1.0L, 0.2L);
Body top_left_divider = Body(&top_left_divider_plane, &top_left_divider_mat);
Sphere right_light_sphere = Sphere(V3(1.9L, 3.625L, 2.1L), 0.1L);
Material right_light_mat = Material(V3(0.0L, 0.0L, 0.0L), V3(2.0L, 1.87L, 1.69L));
Body right_light = Body(&right_light_sphere, &right_light_mat);
Sphere left_light_sphere = Sphere(V3(-1.9L, 3.625L, 2.1L), 0.1L);
Material left_light_mat = Material(V3(0.0L, 0.0L, 0.0L), V3(2.0L, 1.87L, 1.69L));
Body left_light = Body(&left_light_sphere, &left_light_mat);
Sphere right2_light_sphere = Sphere(V3(1.9L, 2.75L, 2.1L), 0.1L);
Body right2_light = Body(&right2_light_sphere, &right_light_mat);
Sphere left2_light_sphere = Sphere(V3(-1.9L, 2.75L, 2.1L), 0.1L);
Body left2_light = Body(&left2_light_sphere, &left_light_mat);
Sphere right3_light_sphere = Sphere(V3(1.9L, 1.875L, 2.1L), 0.1L);
Body right3_light = Body(&right3_light_sphere, &right_light_mat);
Sphere left3_light_sphere = Sphere(V3(-1.9L, 1.875L, 2.1L), 0.1L);
Body left3_light = Body(&left3_light_sphere, &left_light_mat);
Sphere right4_light_sphere = Sphere(V3(1.9L, 1.0L, 2.1L), 0.1L);
Body right4_light = Body(&right4_light_sphere, &right_light_mat);
Sphere left4_light_sphere = Sphere(V3(-1.9L, 1.0L, 2.1L), 0.1L);
Body left4_light = Body(&left4_light_sphere, &left_light_mat);
Sphere right5_light_sphere = Sphere(V3(1.9L, 0.125L, 2.1L), 0.1L);
Body right5_light = Body(&right5_light_sphere, &right_light_mat);
Sphere left5_light_sphere = Sphere(V3(-1.9L, 0.125L, 2.1L), 0.1L);
Body left5_light = Body(&left5_light_sphere, &left_light_mat);
Sphere right6_light_sphere = Sphere(V3(1.9L, -0.75L, 2.1L), 0.1L);
Body right6_light = Body(&right6_light_sphere, &right_light_mat);
Sphere left6_light_sphere = Sphere(V3(-1.9L, -0.75L, 2.1L), 0.1L);
Body left6_light = Body(&left6_light_sphere, &left_light_mat);
Sphere right7_light_sphere = Sphere(V3(1.9L, -1.625, 2.1L), 0.1L);
Body right7_light = Body(&right7_light_sphere, &right_light_mat);
Sphere left7_light_sphere = Sphere(V3(-1.9L, -1.625L, 2.1L), 0.1L);
Body left7_light = Body(&left7_light_sphere, &left_light_mat);
Sphere back_light_sphere = Sphere(V3(-0.63333L, 4.5L, 2.1L), 0.1L);
Material back_light_mat = Material(V3(0.0L, 0.0L, 0.0L), V3(2.0L, 1.87L, 1.69L));
Body back_light = Body(&back_light_sphere, &back_light_mat);
Sphere back2_light_sphere = Sphere(V3(0.63333L, 4.5L, 2.1L), 0.1L);
Material back2_light_mat = Material(V3(0.0L, 0.0L, 0.0L), V3(2.0L, 1.87L, 1.69L));
Body back2_light = Body(&back2_light_sphere, &back2_light_mat);
Sphere front_light_sphere = Sphere(V3(-0.63333L, -2.5L, 2.1L), 0.1L);
Material front_light_mat = Material(V3(0.0L, 0.0L, 0.0L), V3(2.0L, 1.87L, 1.69L));
Body front_light = Body(&front_light_sphere, &front_light_mat);
Sphere front2_light_sphere = Sphere(V3(0.63333L, -2.5L, 2.1L), 0.1L);
Material front2_light_mat = Material(V3(0.0L, 0.0L, 0.0L), V3(2.0L, 1.87L, 1.69L));
Body front2_light = Body(&front2_light_sphere, &front2_light_mat);
// Plane top_right_divider_plane = Plane(V3(1.4L, 4.0L, 2.0L), V3(-1.0L, -1.0L, -1.0L).normalize());
// Material top_right_divider_mat = Glass(V3(0.8L, 0.0L, 0.0L), 1.0L, 0.2L);
// Body top_right_divider = Body(&top_right_divider_plane, &top_right_divider_mat);
Plane top_right_divider_plane = Plane(V3(1.8L, 4.4L, 2.4L), V3(-1.0L, -1.0L, -1.0L).normalize());
// Material top_right_divider_mat = Material(V3(0.0L, 0.0L, 0.0L), V3(1.6L, 1.47L, 1.29L));
Material top_right_divider_mat = Glass(V3(0.8L, 0.0L, 0.0L), 1.0L, 0.2L);
Body top_right_divider = Body(&top_right_divider_plane, &top_right_divider_mat);
scene.body_count = 32;
Body* bodies[] = {&glass, &chrome, &green, &red, &blue, &pyramid, &floor, &back, &left, &right, &top_light, &front,
&top_left_divider, &top_right_divider, &right_light, &left_light, &back_light, &back2_light, &right2_light, &left2_light,
&right3_light, &left3_light, &right4_light, &left4_light,
&right5_light, &left5_light, &right6_light, &left6_light, &right7_light, &left7_light,
&front_light, &front2_light};
// scene.body_count = 14;
// Body* bodies[] = {&glass, &chrome, &green, &red, &blue, &pyramid, &floor, &back, &left, &right, &top_light, &front, &top_left_divider, &top_right_divider};
scene.objects = bodies;
// // HACK Decrease emissions uniformly.
// for (int i = 0; i < scene.body_count; i++) {
// scene.objects[i]->material->emission = scene.objects[i]->material->emission.muls(0.8L);
// }
V3* buffer = new V3[width * height];
for (int i = 0; i < (width * height); i++) {
buffer[i] = V3(0.0L, 0.0L, 0.0L);
}
boost::thread** threads = new boost::thread*[thread_count];
for (int i = 0; i < thread_count; i++) {
threads[i] = new boost::thread(boost::bind(&worker, i, iterations / thread_count, &scene, buffer));
}
for (int i = 0; i < thread_count; i++) {
threads[i]->join();
}
//worker(iterations, &scene, buffer);
save(buffer, iterations, width, height);
delete[] buffer;
}