-
Notifications
You must be signed in to change notification settings - Fork 0
/
bipath.cpp
579 lines (466 loc) · 15.5 KB
/
bipath.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
#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>
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());
}
};
class Shape {
public:
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;
};
class Renderer {
public:
Scene scene;
V3* buffer;
int pixels;
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);
}
}
~Renderer(void) {
delete[] buffer;
}
void iterate() {
int i = 0;
// 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);
V3 color = trace(ray, 0);
// cout << color.x << ", " << color.y << ", " << color.z << endl;
buffer[i++].iadd(color);
}
}
}
V3 trace(Ray ray, int n) {
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));
}
Ray newray = Ray(point, direction);
return trace(newray, n+1).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));
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.4L, 4.0L, 2.0L), 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.0L, 2.1L), 0.1L);
Material right_light_mat = Material(V3(0.0L, 0.0L, 0.0L), V3(1.9L, 1.77L, 1.59L));
Body right_light = Body(&right_light_sphere, &right_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);
Body* bodies[] = {&glass, &chrome, &green, &red, &blue, &pyramid, &floor, &back, &left, &right, &top_light, &front, &top_left_divider, &top_right_divider, &right_light};
scene.objects = bodies;
scene.body_count = 15;
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;
}