Skip to content

Commit fe725ac

Browse files
authored
Merge pull request #330 from RayTracing/scene
Scene
2 parents 08ec4ab + 8d2ce9a commit fe725ac

File tree

7 files changed

+303
-185
lines changed

7 files changed

+303
-185
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 77 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@
7070
#include <iostream>
7171

7272
int main() {
73-
int nx = 200;
74-
int ny = 100;
73+
const int image_width = 200;
74+
const int image_height = 100;
7575

76-
std::cout << "P3\n" << nx << ' ' << ny << "\n255\n";
76+
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
7777

78-
for (int j = ny-1; j >= 0; --j) {
79-
for (int i = 0; i < nx; ++i) {
80-
auto r = double(i) / nx;
81-
auto g = double(j) / ny;
78+
for (int j = image_height-1; j >= 0; --j) {
79+
for (int i = 0; i < image_width; ++i) {
80+
auto r = double(i) / image_width;
81+
auto g = double(j) / image_height;
8282
auto b = 0.2;
8383
int ir = static_cast<int>(255.999 * r);
8484
int ig = static_cast<int>(255.999 * g);
@@ -165,13 +165,13 @@
165165
instead write to the error output stream (`std::cerr`):
166166

167167
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
168-
for (int j = ny-1; j >= 0; --j) {
168+
for (int j = image_height-1; j >= 0; --j) {
169169
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
170170
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
171171
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
172-
for (int i = 0; i < nx; ++i) {
173-
auto r = double(i) / nx;
174-
auto g = double(j) / ny;
172+
for (int i = 0; i < image_width; ++i) {
173+
auto r = double(i) / image_width;
174+
auto g = double(j) / image_height;
175175
auto b = 0.2;
176176
int ir = static_cast<int>(255.999 * r);
177177
int ig = static_cast<int>(255.999 * g);
@@ -318,16 +318,16 @@
318318
#include <iostream>
319319

320320
int main() {
321-
int nx = 200;
322-
int ny = 100;
321+
const int image_width = 200;
322+
const int image_height = 100;
323323

324-
std::cout << "P3\n" << nx << ' ' << ny << "\n255\n";
324+
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
325325

326-
for (int j = ny-1; j >= 0; --j) {
326+
for (int j = image_height-1; j >= 0; --j) {
327327
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
328-
for (int i = 0; i < nx; ++i) {
328+
for (int i = 0; i < image_width; ++i) {
329329
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
330-
vec3 color(double(i)/nx, double(j)/ny, 0.2);
330+
vec3 color(double(i)/image_width, double(j)/image_height, 0.2);
331331
color.write_color(std::cout);
332332
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
333333
}
@@ -419,21 +419,23 @@
419419
}
420420

421421
int main() {
422-
int nx = 200;
423-
int ny = 100;
424-
std::cout << "P3\n" << nx << " " << ny << "\n255\n";
422+
const int image_width = 200;
423+
const int image_height = 100;
424+
425+
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
426+
425427
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
426428
vec3 lower_left_corner(-2.0, -1.0, -1.0);
427429
vec3 horizontal(4.0, 0.0, 0.0);
428430
vec3 vertical(0.0, 2.0, 0.0);
429431
vec3 origin(0.0, 0.0, 0.0);
430432
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
431-
for (int j = ny-1; j >= 0; --j) {
433+
for (int j = image_height-1; j >= 0; --j) {
432434
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
433-
for (int i = 0; i < nx; ++i) {
435+
for (int i = 0; i < image_width; ++i) {
434436
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
435-
auto u = double(i) / nx;
436-
auto v = double(j) / ny;
437+
auto u = double(i) / image_width;
438+
auto v = double(j) / image_height;
437439
ray r(origin, lower_left_corner + u*horizontal + v*vertical);
438440
vec3 color = ray_color(r);
439441
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -1078,10 +1080,10 @@
10781080
}
10791081

10801082
int main() {
1081-
int nx = 200;
1082-
int ny = 100;
1083+
const int image_width = 200;
1084+
const int image_height = 100;
10831085

1084-
std::cout << "P3\n" << nx << ' ' << ny << "\n255\n";
1086+
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
10851087

10861088
vec3 lower_left_corner(-2.0, -1.0, -1.0);
10871089
vec3 horizontal(4.0, 0.0, 0.0);
@@ -1095,11 +1097,11 @@
10951097
world.add(make_shared<sphere>(vec3(0,-100.5,-1), 100));
10961098
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
10971099

1098-
for (int j = ny-1; j >= 0; --j) {
1100+
for (int j = image_height-1; j >= 0; --j) {
10991101
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
1100-
for (int i = 0; i < nx; ++i) {
1101-
auto u = double(i) / nx;
1102-
auto v = double(j) / ny;
1102+
for (int i = 0; i < image_width; ++i) {
1103+
auto u = double(i) / image_width;
1104+
auto v = double(j) / image_height;
11031105
ray r(origin, lower_left_corner + u*horizontal + v*vertical);
11041106

11051107

@@ -1240,9 +1242,9 @@
12401242
[Listing [clamp]: <kbd>[rtweekend.h]</kbd> The clamp() utility function]
12411243

12421244
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1243-
void write_color(std::ostream &out, int num_samples) {
1245+
void write_color(std::ostream &out, int samples_per_pixel) {
12441246
// Divide the color total by the number of samples.
1245-
auto scale = 1.0 / num_samples;
1247+
auto scale = 1.0 / samples_per_pixel;
12461248
auto r = scale * e[0];
12471249
auto g = scale * e[1];
12481250
auto b = scale * e[2];
@@ -1260,13 +1262,13 @@
12601262

12611263
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
12621264
int main() {
1263-
int nx = 200;
1264-
int ny = 100;
1265+
const int image_width = 200;
1266+
const int image_height = 100;
12651267
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1266-
int num_samples = 100;
1268+
const int samples_per_pixel = 100;
12671269
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
12681270

1269-
std::cout << "P3\n" << nx << " " << ny << "\n255\n";
1271+
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
12701272

12711273
hittable_list world;
12721274
world.add(make_shared<sphere>(vec3(0,0,-1), 0.5));
@@ -1275,18 +1277,18 @@
12751277
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
12761278
camera cam;
12771279
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1278-
for (int j = ny-1; j >= 0; --j) {
1280+
for (int j = image_height-1; j >= 0; --j) {
12791281
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
1280-
for (int i = 0; i < nx; ++i) {
1282+
for (int i = 0; i < image_width; ++i) {
12811283
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
12821284
vec3 color(0, 0, 0);
1283-
for (int s = 0; s < num_samples; ++s) {
1284-
auto u = (i + random_double()) / nx;
1285-
auto v = (j + random_double()) / ny;
1285+
for (int s = 0; s < samples_per_pixel; ++s) {
1286+
auto u = (i + random_double()) / image_width;
1287+
auto v = (j + random_double()) / image_height;
12861288
ray r = cam.get_ray(u, v);
12871289
color += ray_color(r, world);
12881290
}
1289-
color.write_color(std::cout, num_samples);
1291+
color.write_color(std::cout, samples_per_pixel);
12901292
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
12911293
}
12921294
}
@@ -1425,27 +1427,27 @@
14251427
}
14261428
...
14271429
int main() {
1428-
int nx = 200;
1429-
int ny = 100;
1430-
int num_samples = 100;
1430+
const int image_width = 200;
1431+
const int image_height = 100;
1432+
const int samples_per_pixel = 100;
14311433
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1432-
int max_depth = 50;
1434+
const int max_depth = 50;
14331435
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
14341436

14351437
...
1436-
for (int j = ny-1; j >= 0; --j) {
1438+
for (int j = image_height-1; j >= 0; --j) {
14371439
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
1438-
for (int i = 0; i < nx; ++i) {
1440+
for (int i = 0; i < image_width; ++i) {
14391441
vec3 color(0, 0, 0);
1440-
for (int s = 0; s < num_samples; ++s) {
1441-
auto u = (i + random_double()) / nx;
1442-
auto v = (j + random_double()) / ny;
1442+
for (int s = 0; s < samples_per_pixel; ++s) {
1443+
auto u = (i + random_double()) / image_width;
1444+
auto v = (j + random_double()) / image_height;
14431445
ray r = cam.get_ray(u, v);
14441446
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
14451447
color += ray_color(r, world, max_depth);
14461448
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
14471449
}
1448-
color.write_color(std::cout, num_samples);
1450+
color.write_color(std::cout, samples_per_pixel);
14491451
}
14501452
}
14511453

@@ -1473,11 +1475,11 @@
14731475
square-root:
14741476

14751477
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1476-
void write_color(std::ostream &out, int num_samples) {
1478+
void write_color(std::ostream &out, int samples_per_pixel) {
14771479
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
14781480
// Divide the color total by the number of samples and gamma-correct
14791481
// for a gamma value of 2.0.
1480-
auto scale = 1.0 / num_samples;
1482+
auto scale = 1.0 / samples_per_pixel;
14811483
auto r = sqrt(scale * e[0]);
14821484
auto g = sqrt(scale * e[1]);
14831485
auto b = sqrt(scale * e[2]);
@@ -1897,11 +1899,12 @@
18971899

18981900
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
18991901
int main() {
1900-
int nx = 200;
1901-
int ny = 100;
1902-
int num_samples = 100;
1903-
int max_depth = 50;
1904-
std::cout << "P3\n" << nx << " " << ny << "\n255\n";
1902+
const int image_width = 200;
1903+
const int image_height = 100;
1904+
const int samples_per_pixel = 100;
1905+
const int max_depth = 50;
1906+
1907+
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
19051908

19061909

19071910
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -1918,17 +1921,17 @@
19181921
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
19191922

19201923
camera cam;
1921-
for (int j = ny-1; j >= 0; --j) {
1924+
for (int j = image_height-1; j >= 0; --j) {
19221925
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
1923-
for (int i = 0; i < nx; ++i) {
1926+
for (int i = 0; i < image_width; ++i) {
19241927
vec3 color(0, 0, 0);
1925-
for (int s = 0; s < num_samples; ++s) {
1926-
auto u = (i + random_double()) / nx;
1927-
auto v = (j + random_double()) / ny;
1928+
for (int s = 0; s < samples_per_pixel; ++s) {
1929+
auto u = (i + random_double()) / image_width;
1930+
auto v = (j + random_double()) / image_height;
19281931
ray r = cam.get_ray(u, v);
19291932
color += ray_color(r, world, max_depth);
19301933
}
1931-
color.write_color(std::cout, num_samples);
1934+
color.write_color(std::cout, samples_per_pixel);
19321935
}
19331936
}
19341937

@@ -2363,7 +2366,7 @@
23632366
</div>
23642367

23652368
<div class='together'>
2366-
When calling it with camera `cam(90, double(nx)/ny)` and these spheres:
2369+
When calling it with camera `cam(90, double(image_width)/image_height)` and these spheres:
23672370

23682371
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
23692372
auto R = cos(pi/4);
@@ -2449,7 +2452,9 @@
24492452
This allows us to change the viewpoint:
24502453

24512454
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2452-
camera cam(vec3(-2,2,1), vec3(0,0,-1), vec3(0,1,0), 90, double(nx)/ny);
2455+
const auto aspect_ratio = double(image_width) / image_height;
2456+
...
2457+
camera cam(vec3(-2,2,1), vec3(0,0,-1), vup, 90, aspect_ratio);
24532458
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24542459
[Listing [scene-free-view]: <kbd>[main.cc]</kbd> Scene with alternate viewpoint]
24552460

@@ -2582,12 +2587,15 @@
25822587
Using a big aperture:
25832588

25842589
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2590+
const auto aspect_ratio = double(image_width) / image_height;
2591+
...
25852592
vec3 lookfrom(3,3,2);
25862593
vec3 lookat(0,0,-1);
2594+
vec3 vup(0,1,0);
25872595
auto dist_to_focus = (lookfrom-lookat).length();
25882596
auto aperture = 2.0;
25892597

2590-
camera cam(lookfrom, lookat, vec3(0,1,0), 20, double(nx)/ny, aperture, dist_to_focus);
2598+
camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus);
25912599
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25922600
[Listing [scene-camera-dof]: <kbd>[main.cc]</kbd> Scene camera with depth-of-field]
25932601

0 commit comments

Comments
 (0)