Skip to content

Commit e617c01

Browse files
committed
Source changes
1 parent e9662fc commit e617c01

File tree

7 files changed

+71
-71
lines changed

7 files changed

+71
-71
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090

9191
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
9292

93-
for (int j = image_height-1; j >= 0; --j) {
93+
for (int j = 0; j < image_height; ++j) {
9494
for (int i = 0; i < image_width; ++i) {
9595
auto r = double(i) / (image_width-1);
9696
auto g = double(j) / (image_height-1);
@@ -189,9 +189,9 @@
189189
instead write to the logging output stream (`std::clog`):
190190

191191
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
192-
for (int j = image_height-1; j >= 0; --j) {
192+
for (int j = 0; j < image_height; ++j) {
193193
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
194-
std::clog << "\rScanlines remaining: " << j << ' ' << std::flush;
194+
std::clog << "\rScanlines remaining: " << (image_height - j) << ' ' << std::flush;
195195
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
196196
for (int i = 0; i < image_width; ++i) {
197197
auto r = double(i) / (image_width-1);
@@ -401,8 +401,8 @@
401401

402402
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
403403

404-
for (int j = image_height-1; j >= 0; --j) {
405-
std::clog << "\rScanlines remaining: " << j << ' ' << std::flush;
404+
for (int j = 0; j < image_height; ++j) {
405+
std::clog << "\rScanlines remaining: " << (image_height - j) << ' ' << std::flush;
406406
for (int i = 0; i < image_width; ++i) {
407407
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
408408
color pixel_color(double(i)/(image_width-1), double(j)/(image_height-1), 0.25);
@@ -484,6 +484,7 @@
484484
often, so I’ll use a non-square image. For now we'll use a 16:9 aspect ratio, since that's so
485485
common.
486486

487+
487488
In addition to setting up the pixel dimensions for the rendered image, we also need to set up a
488489
virtual viewport through which to pass our scene rays. For the standard pixel spacing (equally
489490
spaced in both the horizontal and vertical direction), the viewport's aspect ratio should be the
@@ -547,13 +548,13 @@
547548

548549
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
549550

550-
for (int j = image_height-1; j >= 0; --j) {
551-
std::clog << "\rScanlines remaining: " << j << ' ' << std::flush;
551+
for (int j = 0; j < image_height; ++j) {
552+
std::clog << "\rScanlines remaining: " << (image_height - j) << ' ' << std::flush;
552553
for (int i = 0; i < image_width; ++i) {
553554
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
554-
auto u = double(i) / (image_width-1);
555-
auto v = double(j) / (image_height-1);
556-
ray r(origin, lower_left_corner + u*horizontal + v*vertical - origin);
555+
auto s = double(i) / (image_width-1);
556+
auto t = double(j) / (image_height-1);
557+
ray r(origin, lower_left_corner + s*horizontal + (1-t)*vertical - origin);
557558
color pixel_color = ray_color(r);
558559
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
559560
write_color(std::cout, pixel_color);
@@ -1253,12 +1254,12 @@
12531254

12541255
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
12551256

1256-
for (int j = image_height-1; j >= 0; --j) {
1257-
std::clog << "\rScanlines remaining: " << j << ' ' << std::flush;
1257+
for (int j = 0; j < image_height; ++j) {
1258+
std::clog << "\rScanlines remaining: " << (image_height - j) << ' ' << std::flush;
12581259
for (int i = 0; i < image_width; ++i) {
1259-
auto u = double(i) / (image_width-1);
1260-
auto v = double(j) / (image_height-1);
1261-
ray r(origin, lower_left_corner + u*horizontal + v*vertical);
1260+
auto s = double(i) / (image_width-1);
1261+
auto t = double(j) / (image_height-1);
1262+
ray r(origin, lower_left_corner + s*horizontal + (1-t)*vertical);
12621263
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
12631264
color pixel_color = ray_color(r, world);
12641265
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -1489,7 +1490,11 @@
14891490
}
14901491

14911492
ray get_ray(double s, double t) const {
1492-
return ray(origin, lower_left_corner + s*horizontal + t*vertical - origin);
1493+
// Return the ray from the projection point to the indicated pixel. Coordinates s,t are
1494+
// the normalized image-based coordinates of the pixel. Image left is s=0, image right
1495+
// is s=1, image top is t=0, image bottom is t=1.
1496+
1497+
return ray(origin, lower_left_corner + s*horizontal + (1-t)*vertical - origin);
14931498
}
14941499

14951500
private:
@@ -1590,15 +1595,16 @@
15901595

15911596
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
15921597

1593-
for (int j = image_height-1; j >= 0; --j) {
1594-
std::clog << "\rScanlines remaining: " << j << ' ' << std::flush;
1598+
for (int j = 0; j < image_height; ++j) {
1599+
std::clog << "\rScanlines remaining: " << (image_height - j) << ' ' << std::flush;
1600+
auto t = (j + random_double()) / (image_height-1);
1601+
15951602
for (int i = 0; i < image_width; ++i) {
15961603
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
15971604
color pixel_color(0, 0, 0);
15981605
for (int sample = 0; sample < samples_per_pixel; ++sample) {
1599-
auto u = (i + random_double()) / (image_width-1);
1600-
auto v = (j + random_double()) / (image_height-1);
1601-
ray r = cam.get_ray(u, v);
1606+
auto s = (i + random_double()) / (image_width-1);
1607+
ray r = cam.get_ray(s, t);
16021608
pixel_color += ray_color(r, world);
16031609
}
16041610
write_color(std::cout, pixel_color, samples_per_pixel);
@@ -1769,14 +1775,15 @@
17691775

17701776
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
17711777

1772-
for (int j = image_height-1; j >= 0; --j) {
1773-
std::clog << "\rScanlines remaining: " << j << ' ' << std::flush;
1778+
for (int j = 0; j < image_height; ++j) {
1779+
std::clog << "\rScanlines remaining: " << (image_height - j) << ' ' << std::flush;
1780+
auto t = (j + random_double()) / (image_height-1);
1781+
17741782
for (int i = 0; i < image_width; ++i) {
17751783
color pixel_color(0, 0, 0);
17761784
for (int sample = 0; sample < samples_per_pixel; ++sample) {
1777-
auto u = (i + random_double()) / (image_width-1);
1778-
auto v = (j + random_double()) / (image_height-1);
1779-
ray r = cam.get_ray(u, v);
1785+
auto s = (i + random_double()) / (image_width-1);
1786+
ray r = cam.get_ray(s, t);
17801787
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
17811788
pixel_color += ray_color(r, world, max_depth);
17821789
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -2367,21 +2374,7 @@
23672374

23682375
// Render
23692376

2370-
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
2371-
2372-
for (int j = image_height-1; j >= 0; --j) {
2373-
std::clog << "\rScanlines remaining: " << j << ' ' << std::flush;
2374-
for (int i = 0; i < image_width; ++i) {
2375-
color pixel_color(0, 0, 0);
2376-
for (int sample = 0; sample < samples_per_pixel; ++sample) {
2377-
auto u = (i + random_double()) / (image_width-1);
2378-
auto v = (j + random_double()) / (image_height-1);
2379-
ray r = cam.get_ray(u, v);
2380-
pixel_color += ray_color(r, world, max_depth);
2381-
}
2382-
write_color(std::cout, pixel_color, samples_per_pixel);
2383-
}
2384-
}
2377+
...
23852378

23862379
std::clog << "\nDone.\n";
23872380
}
@@ -2828,14 +2821,15 @@
28282821
void render() {
28292822
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
28302823

2831-
for (int j = image_height-1; j >= 0; --j) {
2832-
std::clog << "\rScanlines remaining: " << j << ' ' << std::flush;
2824+
for (int j = 0; j < image_height; ++j) {
2825+
std::clog << "\rScanlines remaining: " << (image_height - j) << ' ' << std::flush;
2826+
auto t = (j + random_double()) / (image_height-1);
2827+
28332828
for (int i = 0; i < image_width; ++i) {
28342829
color pixel_color(0,0,0);
28352830
for (int sample = 0; sample < samples_per_pixel; ++sample) {
2836-
auto u = (i + random_double()) / (image_width-1);
2837-
auto v = (j + random_double()) / (image_height-1);
2838-
ray r = cam.get_ray(u, v);
2831+
auto s = (i + random_double()) / (image_width-1);
2832+
ray r = cam.get_ray(s, t);
28392833
pixel_color += ray_color(r, max_depth);
28402834
}
28412835
write_color(std::cout, pixel_color, samples_per_pixel);

books/RayTracingTheRestOfYourLife.html

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,17 +338,16 @@
338338

339339
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
340340
int sqrt_spp = int(sqrt(samples_per_pixel));
341-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
342-
for (int j = image_height-1; j >= 0; --j) {
343-
std::clog << "\rScanlines remaining: " << j << ' ' << std::flush;
341+
for (int j = 0; j < image_height; ++j) {
342+
std::clog << "\rScanlines remaining: " << (image_height - j) << ' ' << std::flush;
343+
344344
for (int i = 0; i < image_width; ++i) {
345345
color pixel_color(0,0,0);
346-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
347346
for (int s_j = 0; s_j < sqrt_spp; ++s_j) {
347+
auto t = (j + (s_j + random_double()) / sqrt_spp) / (image_height-1);
348348
for (int s_i = 0; s_i < sqrt_spp; ++s_i) {
349-
auto u = (i + (s_i + random_double()) / sqrt_spp) / (image_width-1);
350-
auto v = (j + (s_j + random_double()) / sqrt_spp) / (image_height-1);
351-
ray r = cam.get_ray(u, v);
349+
auto s = (i + (s_i + random_double()) / sqrt_spp) / (image_width-1);
350+
ray r = cam.get_ray(s, t);
352351
pixel_color += ray_color(r, max_depth);
353352
}
354353
}

images/img-1.01-first-ppm-image.png

51 Bytes
Loading

src/InOneWeekend/scene.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ class scene {
2626

2727
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
2828

29-
for (int j = image_height-1; j >= 0; --j) {
30-
std::clog << "\rScanlines remaining: " << j << ' ' << std::flush;
29+
for (int j = 0; j < image_height; ++j) {
30+
std::clog << "\rScanlines remaining: " << (image_height - j) << ' ' << std::flush;
31+
auto t = (j + random_double()) / (image_height-1);
32+
3133
for (int i = 0; i < image_width; ++i) {
3234
color pixel_color(0,0,0);
3335
for (int sample = 0; sample < samples_per_pixel; ++sample) {
34-
auto u = (i + random_double()) / (image_width-1);
35-
auto v = (j + random_double()) / (image_height-1);
36-
ray r = cam.get_ray(u, v);
36+
auto s = (i + random_double()) / (image_width-1);
37+
ray r = cam.get_ray(s, t);
3738
pixel_color += ray_color(r, max_depth);
3839
}
3940
write_color(std::cout, pixel_color, samples_per_pixel);
@@ -69,8 +70,8 @@ class scene {
6970
}
7071

7172
vec3 unit_direction = unit_vector(r.direction());
72-
auto t = 0.5*(unit_direction.y() + 1.0);
73-
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
73+
auto a = 0.5*(unit_direction.y() + 1.0);
74+
return (1.0-a)*color(1.0, 1.0, 1.0) + a*color(0.5, 0.7, 1.0);
7475
}
7576
};
7677

src/TheNextWeek/scene.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ class scene {
2626

2727
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
2828

29-
for (int j = image_height-1; j >= 0; --j) {
30-
std::clog << "\rScanlines remaining: " << j << ' ' << std::flush;
29+
for (int j = 0; j < image_height; ++j) {
30+
std::clog << "\rScanlines remaining: " << (image_height - j) << ' ' << std::flush;
31+
auto t = (j + random_double()) / (image_height-1);
32+
3133
for (int i = 0; i < image_width; ++i) {
3234
color pixel_color(0,0,0);
3335
for (int sample = 0; sample < samples_per_pixel; ++sample) {
34-
auto u = (i + random_double()) / (image_width-1);
35-
auto v = (j + random_double()) / (image_height-1);
36-
ray r = cam.get_ray(u, v);
36+
auto s = (i + random_double()) / (image_width-1);
37+
ray r = cam.get_ray(s, t);
3738
pixel_color += ray_color(r, max_depth);
3839
}
3940
write_color(std::cout, pixel_color, samples_per_pixel);

src/TheRestOfYourLife/scene.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ class scene {
2525
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
2626

2727
int sqrt_spp = int(sqrt(samples_per_pixel));
28-
for (int j = image_height-1; j >= 0; --j) {
29-
std::clog << "\rScanlines remaining: " << j << ' ' << std::flush;
28+
for (int j = 0; j < image_height; ++j) {
29+
std::clog << "\rScanlines remaining: " << (image_height - j) << ' ' << std::flush;
30+
3031
for (int i = 0; i < image_width; ++i) {
3132
color pixel_color(0,0,0);
3233
for (int s_j = 0; s_j < sqrt_spp; ++s_j) {
34+
auto t = (j + (s_j + random_double()) / sqrt_spp) / (image_height-1);
3335
for (int s_i = 0; s_i < sqrt_spp; ++s_i) {
34-
auto u = (i + (s_i + random_double()) / sqrt_spp) / (image_width-1);
35-
auto v = (j + (s_j + random_double()) / sqrt_spp) / (image_height-1);
36-
ray r = cam.get_ray(u, v);
36+
auto s = (i + (s_i + random_double()) / sqrt_spp) / (image_width-1);
37+
ray r = cam.get_ray(s, t);
3738
pixel_color += ray_color(r, max_depth);
3839
}
3940
}

src/common/camera.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ class camera {
3535
}
3636

3737
ray get_ray(double s, double t) const {
38+
// Return the ray from the projection point to the indicated pixel. Coordinates s,t are
39+
// the normalized image-based coordinates of the pixel. Image left is s=0, image right
40+
// is s=1, image top is t=0, image bottom is t=1.
41+
3842
vec3 rd = lens_radius * random_in_unit_disk();
3943
vec3 offset = u * rd.x() + v * rd.y();
4044
const auto ray_time = random_double(0.0, 1.0);
4145

4246
return ray(
4347
origin + offset,
44-
lower_left_corner + s*horizontal + t*vertical - origin - offset,
48+
lower_left_corner + s*horizontal + (1-t)*vertical - origin - offset,
4549
ray_time
4650
);
4751
}

0 commit comments

Comments
 (0)