-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
422 lines (347 loc) · 14.5 KB
/
main.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
#include <iostream>
#include <fstream>
#include <vector>
#include <limits>
#include <atomic>
#include <omp.h>
#include "Common.hpp"
#include "Color.hpp"
#include "math/Real.hpp"
#include "math/Vector3.hpp"
#include "math/Ray.hpp"
#include "math/PDF.hpp"
#include "geometry/HittableList.hpp"
#include "geometry/Sphere.hpp"
#include "geometry/AARect.hpp"
#include "geometry/Box.hpp"
#include "geometry/ConstantMedium.hpp"
#include "Camera.hpp"
#include "Material.hpp"
#include "BVH.hpp"
/* Write an image to the disk */
void CreateImage(std::shared_ptr<Vector3> data, const std::string& file_name, int width, int height, int samples_per_pixel) {
std::cout << "Writing image file... " << std::flush;
std::ofstream file;
file.open(file_name);
file << "P3\n" << width << ' ' << height << "\n255\n";
for (int j = height - 1; j >= 0; --j) {
for (int i = 0; i < width; ++i) {
write_color(file, data.get()[j * width + i], samples_per_pixel);
}
}
file.close();
std::cout << "DONE" << std::endl;
}
Color ray_color(const Ray& r, const Color& background, const Hittable& world, shared_ptr<Hittable> lights, int depth) {
if (depth <= 0)
return Color(0, 0, 0);
HitRecord rec;
if (!world.hit(r, 0.001, infinity, rec)) {
return background;
}
ScatterRecord srec;
Color emitted = rec.mat_->emitted(r, rec, rec.u_, rec.v_, rec.p_);
if (!rec.mat_->scatter(r, rec, srec))
return emitted;
if (srec.is_specular_) {
return srec.attenuation_
* ray_color(srec.specular_ray_, background, world, lights, depth - 1);
}
auto light_ptr = make_shared<HittablePDF>(lights, rec.p_);
MixturePDF p(light_ptr, srec.pdf_);
Ray scattered = Ray(rec.p_, p.generate(), r.Time());
auto pdf_val = p.value(scattered.direction());
return emitted + srec.attenuation_
* rec.mat_->scattering_pdf(r, rec, scattered) * ray_color(scattered, background, world, lights, depth - 1) / pdf_val;
}
/* Create scenes */
HittableList two_spheres() {
HittableList objects;
auto checker_texture = make_shared<CheckerTexture>(Color(0.2, 0.3, 0.1), Color(0.9, 0.9, 0.9));
objects.add(make_shared<Sphere>(Point3(0, -10, 0), 10, make_shared<Lambertian>(checker_texture)));
objects.add(make_shared<Sphere>(Point3(0, 10, 0), 10, make_shared<Lambertian>(checker_texture)));
return objects;
}
HittableList random_scene() {
HittableList world;
auto checker_texture = make_shared<CheckerTexture>(Color(0.2, 0.3, 0.1), Color(0.9, 0.9, 0.9));
world.add(make_shared<Sphere>(Point3(0, -1000, 0), 1000, make_shared<Lambertian>(checker_texture)));
for (int a = -11; a < 11; a++) {
for (int b = -11; b < 11; b++) {
auto choose_mat = random_double();
Point3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());
if ((center - Point3(4, 0.2, 0)).length() > 0.9) {
shared_ptr<Material> sphere_material;
if (choose_mat < 0.8) {
// diffuse
auto albedo = Color::random() * Color::random();
sphere_material = make_shared<Lambertian>(albedo);
auto center2 = center + Vector3(0, random_double(0, .5), 0);
world.add(make_shared<MovingSphere>(
center, center2, 0.0, 1.0, 0.2, sphere_material));
}
else if (choose_mat < 0.95) {
// metal
auto albedo = Color::random(0.5, 1.0);
auto fuzz = random_double(0, 0.5);
sphere_material = make_shared<Metal>(albedo, fuzz);
world.add(make_shared<Sphere>(center, 0.2, sphere_material));
}
else {
// glass
sphere_material = make_shared<Dielectric>(1.5);
world.add(make_shared<Sphere>(center, 0.2, sphere_material));
}
}
}
}
auto material1 = make_shared<Dielectric>(1.5);
world.add(make_shared<Sphere>(Point3(0, 1, 0), 1.0, material1));
auto material2 = make_shared<Lambertian>(Color(0.4, 0.2, 0.1));
world.add(make_shared<Sphere>(Point3(-4, 1, 0), 1.0, material2));
auto material3 = make_shared<Metal>(Color(0.7, 0.6, 0.5), 0.0);
world.add(make_shared<Sphere>(Point3(4, 1, 0), 1.0, material3));
return world;
}
HittableList two_perlin_spheres() {
HittableList objects;
auto pertext = make_shared<NoiseTexture>(16);
objects.add(make_shared<Sphere>(Point3(0, -1000, 0), 1000, make_shared<Lambertian>(pertext)));
objects.add(make_shared<Sphere>(Point3(0, 2, 0), 2, make_shared<Lambertian>(pertext)));
return objects;
}
HittableList earth() {
auto earth_texture = make_shared<ImageTexture>("earthmap.jpg");
auto earth_surface = make_shared<Lambertian>(earth_texture);
auto globe = make_shared<Sphere>(Point3(0, 0, 0), 2, earth_surface);
return HittableList(globe);
}
HittableList simple_lights() {
HittableList objects;
auto pertext = make_shared<NoiseTexture>(4);
objects.add(make_shared<Sphere>(Point3(0, -1000, 0), 1000, make_shared<Lambertian>(pertext)));
objects.add(make_shared<Sphere>(Point3(0, 2, 0), 2, make_shared<Lambertian>(pertext)));
auto difflight = make_shared<DiffuseLight>(Color(4, 4, 4));
objects.add(make_shared<XYRect>(3, 5, 1, 3, -2, difflight));
auto difflight_2 = make_shared<DiffuseLight>(Color(4, 0, 0));
objects.add(make_shared<Sphere>(Point3(0, 6, 0), 1.5, difflight_2));
return objects;
}
HittableList cornell_box(shared_ptr<Hittable>& lights) {
HittableList objects;
auto red = make_shared<Lambertian>(Color(.65, .05, .05));
auto white = make_shared<Lambertian>(Color(.73, .73, .73));
auto green = make_shared<Lambertian>(Color(.12, .45, .15));
auto light = make_shared<DiffuseLight>(Color(15, 15, 15));
objects.add(make_shared<YZRect>(0, 555, 0, 555, 555, green));
objects.add(make_shared<YZRect>(0, 555, 0, 555, 0, red));
HittableList samplers;
shared_ptr<Hittable> light_rec = make_shared<XZRect>(213, 343, 227, 332, 554, light);
samplers.add(light_rec);
objects.add(make_shared<FlipFace>(light_rec));
objects.add(make_shared<XZRect>(0, 555, 0, 555, 0, white));
objects.add(make_shared<XZRect>(0, 555, 0, 555, 555, white));
objects.add(make_shared<XYRect>(0, 555, 0, 555, 555, white));
shared_ptr<Hittable> box1 = make_shared<Box>(Point3(0, 0, 0), Point3(165, 330, 165), white);
box1 = make_shared<RotateY>(box1, 15);
box1 = make_shared<Translate>(box1, Vector3(265, 0, 295));
objects.add(box1);
shared_ptr<Material> aluminum = make_shared<Metal>(Color(0.8, 0.85, 0.88), 0.0);
shared_ptr<Hittable> sphere = make_shared<Sphere>(Point3(165, 82.5, 82.5), 82.5, make_shared<Dielectric>(1.5));
objects.add(sphere);
samplers.add(sphere);
lights = make_shared<HittableList>(samplers);
return objects;
}
HittableList cornell_smoke() {
HittableList objects;
auto red = make_shared<Lambertian>(Color(.65, .05, .05));
auto white = make_shared<Lambertian>(Color(.73, .73, .73));
auto green = make_shared<Lambertian>(Color(.12, .45, .15));
auto light = make_shared<DiffuseLight>(Color(7, 7, 7));
objects.add(make_shared<YZRect>(0, 555, 0, 555, 555, green));
objects.add(make_shared<YZRect>(0, 555, 0, 555, 0, red));
objects.add(make_shared<XZRect>(113, 443, 127, 432, 554, light));
objects.add(make_shared<XZRect>(0, 555, 0, 555, 0, white));
objects.add(make_shared<XZRect>(0, 555, 0, 555, 555, white));
objects.add(make_shared<XYRect>(0, 555, 0, 555, 555, white));
shared_ptr<Hittable> box1 = make_shared<Box>(Point3(0, 0, 0), Point3(165, 330, 165), white);
box1 = make_shared<RotateY>(box1, 15);
box1 = make_shared<Translate>(box1, Vector3(265, 0, 295));
shared_ptr<Hittable> box2 = make_shared<Box>(Point3(0, 0, 0), Point3(165, 165, 165), white);
box2 = make_shared<RotateY>(box2, -18);
box2 = make_shared<Translate>(box2, Vector3(130, 0, 65));
objects.add(make_shared<ConstantMedium>(box1, 0.01, Color(0, 0, 0)));
objects.add(make_shared<ConstantMedium>(box2, 0.01, Color(1, 1, 1)));
//HittableList scene;
//scene.add(make_shared<BVHNode>(objects));
return objects;
}
HittableList final_scene() {
HittableList boxes1;
auto ground = make_shared<Lambertian>(Color(0.48, 0.83, 0.53));
const int boxes_per_side = 20;
for (int i = 0; i < boxes_per_side; i++) {
for (int j = 0; j < boxes_per_side; j++) {
auto w = 100.0;
auto x0 = -1000.0 + i * w;
auto z0 = -1000.0 + j * w;
auto y0 = 0.0;
auto x1 = x0 + w;
auto y1 = random_double(1, 101);
auto z1 = z0 + w;
boxes1.add(make_shared<Box>(Point3(x0, y0, z0), Point3(x1, y1, z1), ground));
}
}
HittableList objects;
objects.add(make_shared<BVHNode>(boxes1, 0, 1));
auto light = make_shared<DiffuseLight>(Color(7, 7, 7));
objects.add(make_shared<XZRect>(123, 423, 147, 412, 554, light));
auto center1 = Point3(400, 400, 200);
auto center2 = center1 + Vector3(30, 0, 0);
auto moving_sphere_material = make_shared<Lambertian>(Color(0.7, 0.3, 0.1));
objects.add(make_shared<MovingSphere>(center1, center2, 0, 1, 50, moving_sphere_material));
objects.add(make_shared<Sphere>(Point3(260, 150, 45), 50, make_shared<Dielectric>(1.5)));
objects.add(make_shared<Sphere>(
Point3(0, 150, 145), 50, make_shared<Metal>(Color(0.8, 0.8, 0.9), 1.0)
));
auto boundary = make_shared<Sphere>(Point3(360, 150, 145), 70, make_shared<Dielectric>(1.5));
objects.add(boundary);
objects.add(make_shared<ConstantMedium>(boundary, 0.2, Color(0.2, 0.4, 0.9)));
boundary = make_shared<Sphere>(Point3(0, 0, 0), 5000, make_shared<Dielectric>(1.5));
objects.add(make_shared<ConstantMedium>(boundary, .0001, Color(1, 1, 1)));
auto emat = make_shared<Lambertian>(make_shared<ImageTexture>("earthmap.jpg"));
objects.add(make_shared<Sphere>(Point3(400, 200, 400), 100, emat));
auto pertext = make_shared<NoiseTexture>(0.1);
objects.add(make_shared<Sphere>(Point3(220, 280, 300), 80, make_shared<Lambertian>(pertext)));
HittableList boxes2;
auto white = make_shared<Lambertian>(Color(.73, .73, .73));
int ns = 1000;
for (int j = 0; j < ns; j++) {
boxes2.add(make_shared<Sphere>(Point3::random(0, 165), 10, white));
}
objects.add(make_shared<Translate>(
make_shared<RotateY>(
make_shared<BVHNode>(boxes2, 0.0, 1.0), 15),
Vector3(-100, 270, 395)
)
);
return objects;
}
int main() {
/* Default image parameters */
Real aspect_ratio = 16.0 / 9.0;
size_t image_width = 256;
int samples_per_pixel = 100;
/* Computation parameters */
const int max_depth = 50;
const int threads = 5;
/* Scene and camera parameters */
HittableList world;
shared_ptr<Hittable> lights;
Point3 lookfrom;
Point3 lookat;
auto aperture = 0.0;
auto vfov = 40.0;
size_t time_start = 0;
size_t time_end = 0;
Vector3 vup(0, 1, 0);
auto dist_to_focus = 10.0;
Color background(0, 0, 0);
/* Create scenes, and set scene specific parameters */
switch (6) {
case 1:
world = random_scene();
lookfrom = Point3(13, 2, 3);
background = Color(0.70, 0.80, 1.00);
lookat = Point3(0, 0, 0);
vfov = 20.0;
aperture = 0.1;
break;
case 2:
world = two_spheres();
background = Color(0.70, 0.80, 1.00);
lookfrom = Point3(13, 2, 3);
lookat = Point3(0, 0, 0);
vfov = 20.0;
break;
case 3:
world = two_perlin_spheres();
background = Color(0.70, 0.80, 1.00);
lookfrom = Point3(13, 2, 3);
lookat = Point3(0, 0, 0);
vfov = 20.0;
break;
case 4:
world = earth();
lookfrom = Point3(13, 2, 3);
lookat = Point3(0, 0, 0);
vfov = 20.0;
break;
case 5:
world = simple_lights();
samples_per_pixel = 400;
background = Color(0, 0, 0);
lookfrom = Point3(26, 3, 6);
lookat = Point3(0, 2, 0);
vfov = 20.0;
break;
case 6:
world = cornell_box(lights);
aspect_ratio = 1.0;
image_width = 600;
samples_per_pixel = 1000;
background = Color(0, 0, 0);
lookfrom = Point3(278, 278, -800);
lookat = Point3(278, 278, 0);
vfov = 40.0;
break;
case 7:
world = cornell_smoke();
aspect_ratio = 1.0;
image_width = 256;
samples_per_pixel = 100;
lookfrom = Point3(278, 278, -800);
lookat = Point3(278, 278, 0);
vfov = 40.0;
break;
default:
case 8:
world = final_scene();
aspect_ratio = 1.0;
image_width = 256;
samples_per_pixel = 10000;
background = Color(0, 0, 0);
lookfrom = Point3(478, 278, -600);
lookat = Point3(278, 278, 0);
vfov = 40.0;
time_start = 0;
time_end = 1;
break;
}
/* Create a camera */
Camera camera(lookfrom, lookat, vup, vfov, aspect_ratio, aperture, dist_to_focus, time_start, time_end);
/* Create the image data */
size_t image_height = static_cast<size_t>(image_width / aspect_ratio);
std::shared_ptr<Vector3> image_data(new Vector3[image_width * image_height], std::default_delete<Vector3[]>());
std::atomic<int> lines_remaining = { (int)image_height };
#pragma omp parallel for num_threads(threads) shared(image_data) schedule(dynamic, 2)
for (int j = image_height - 1; j >= 0; --j) {
for (int i = 0; i < image_width; ++i) {
Color pixel_color(0, 0, 0);
for (int s = 0; s < samples_per_pixel; ++s) {
auto u = (i + random_double()) / (image_width - 1);
auto v = (j + random_double()) / (image_height - 1);
Ray r = camera.get_ray(u, v);
pixel_color += ray_color(r, background, world, lights, max_depth);
}
image_data.get()[j * image_width + i] = pixel_color;
}
std::atomic_fetch_sub(&lines_remaining, 1);
/* First thread to report the progress */
if (omp_get_thread_num() == 0) std::cerr << "\rImage lines remaining: " << lines_remaining.load() << ' ' << std::flush;
}
std::cerr << std::endl;
CreateImage(image_data, "test.ppm", image_width, image_height, samples_per_pixel);
system("pause");
}