|
70 | 70 | #include <iostream>
|
71 | 71 |
|
72 | 72 | int main() {
|
73 |
| - int nx = 200; |
74 |
| - int ny = 100; |
| 73 | + const int image_width = 200; |
| 74 | + const int image_height = 100; |
75 | 75 |
|
76 |
| - std::cout << "P3\n" << nx << ' ' << ny << "\n255\n"; |
| 76 | + std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n"; |
77 | 77 |
|
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; |
82 | 82 | auto b = 0.2;
|
83 | 83 | int ir = static_cast<int>(255.999 * r);
|
84 | 84 | int ig = static_cast<int>(255.999 * g);
|
|
165 | 165 | instead write to the error output stream (`std::cerr`):
|
166 | 166 |
|
167 | 167 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
168 |
| - for (int j = ny-1; j >= 0; --j) { |
| 168 | + for (int j = image_height-1; j >= 0; --j) { |
169 | 169 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
170 | 170 | std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
171 | 171 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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; |
175 | 175 | auto b = 0.2;
|
176 | 176 | int ir = static_cast<int>(255.999 * r);
|
177 | 177 | int ig = static_cast<int>(255.999 * g);
|
|
318 | 318 | #include <iostream>
|
319 | 319 |
|
320 | 320 | int main() {
|
321 |
| - int nx = 200; |
322 |
| - int ny = 100; |
| 321 | + const int image_width = 200; |
| 322 | + const int image_height = 100; |
323 | 323 |
|
324 |
| - std::cout << "P3\n" << nx << ' ' << ny << "\n255\n"; |
| 324 | + std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n"; |
325 | 325 |
|
326 |
| - for (int j = ny-1; j >= 0; --j) { |
| 326 | + for (int j = image_height-1; j >= 0; --j) { |
327 | 327 | std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
328 |
| - for (int i = 0; i < nx; ++i) { |
| 328 | + for (int i = 0; i < image_width; ++i) { |
329 | 329 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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); |
331 | 331 | color.write_color(std::cout);
|
332 | 332 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
333 | 333 | }
|
|
419 | 419 | }
|
420 | 420 |
|
421 | 421 | 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 | + |
425 | 427 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
426 | 428 | vec3 lower_left_corner(-2.0, -1.0, -1.0);
|
427 | 429 | vec3 horizontal(4.0, 0.0, 0.0);
|
428 | 430 | vec3 vertical(0.0, 2.0, 0.0);
|
429 | 431 | vec3 origin(0.0, 0.0, 0.0);
|
430 | 432 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
431 |
| - for (int j = ny-1; j >= 0; --j) { |
| 433 | + for (int j = image_height-1; j >= 0; --j) { |
432 | 434 | std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
433 |
| - for (int i = 0; i < nx; ++i) { |
| 435 | + for (int i = 0; i < image_width; ++i) { |
434 | 436 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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; |
437 | 439 | ray r(origin, lower_left_corner + u*horizontal + v*vertical);
|
438 | 440 | vec3 color = ray_color(r);
|
439 | 441 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
|
1078 | 1080 | }
|
1079 | 1081 |
|
1080 | 1082 | int main() {
|
1081 |
| - int nx = 200; |
1082 |
| - int ny = 100; |
| 1083 | + const int image_width = 200; |
| 1084 | + const int image_height = 100; |
1083 | 1085 |
|
1084 |
| - std::cout << "P3\n" << nx << ' ' << ny << "\n255\n"; |
| 1086 | + std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n"; |
1085 | 1087 |
|
1086 | 1088 | vec3 lower_left_corner(-2.0, -1.0, -1.0);
|
1087 | 1089 | vec3 horizontal(4.0, 0.0, 0.0);
|
|
1095 | 1097 | world.add(make_shared<sphere>(vec3(0,-100.5,-1), 100));
|
1096 | 1098 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1097 | 1099 |
|
1098 |
| - for (int j = ny-1; j >= 0; --j) { |
| 1100 | + for (int j = image_height-1; j >= 0; --j) { |
1099 | 1101 | 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; |
1103 | 1105 | ray r(origin, lower_left_corner + u*horizontal + v*vertical);
|
1104 | 1106 |
|
1105 | 1107 |
|
|
1240 | 1242 | [Listing [clamp]: <kbd>[rtweekend.h]</kbd> The clamp() utility function]
|
1241 | 1243 |
|
1242 | 1244 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1243 |
| - void write_color(std::ostream &out, int num_samples) { |
| 1245 | + void write_color(std::ostream &out, int samples_per_pixel) { |
1244 | 1246 | // Divide the color total by the number of samples.
|
1245 |
| - auto scale = 1.0 / num_samples; |
| 1247 | + auto scale = 1.0 / samples_per_pixel; |
1246 | 1248 | auto r = scale * e[0];
|
1247 | 1249 | auto g = scale * e[1];
|
1248 | 1250 | auto b = scale * e[2];
|
|
1260 | 1262 |
|
1261 | 1263 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1262 | 1264 | int main() {
|
1263 |
| - int nx = 200; |
1264 |
| - int ny = 100; |
| 1265 | + const int image_width = 200; |
| 1266 | + const int image_height = 100; |
1265 | 1267 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1266 |
| - int num_samples = 100; |
| 1268 | + const int samples_per_pixel = 100; |
1267 | 1269 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1268 | 1270 |
|
1269 |
| - std::cout << "P3\n" << nx << " " << ny << "\n255\n"; |
| 1271 | + std::cout << "P3\n" << image_width << " " << image_height << "\n255\n"; |
1270 | 1272 |
|
1271 | 1273 | hittable_list world;
|
1272 | 1274 | world.add(make_shared<sphere>(vec3(0,0,-1), 0.5));
|
|
1275 | 1277 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1276 | 1278 | camera cam;
|
1277 | 1279 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1278 |
| - for (int j = ny-1; j >= 0; --j) { |
| 1280 | + for (int j = image_height-1; j >= 0; --j) { |
1279 | 1281 | std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
1280 |
| - for (int i = 0; i < nx; ++i) { |
| 1282 | + for (int i = 0; i < image_width; ++i) { |
1281 | 1283 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1282 | 1284 | 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; |
1286 | 1288 | ray r = cam.get_ray(u, v);
|
1287 | 1289 | color += ray_color(r, world);
|
1288 | 1290 | }
|
1289 |
| - color.write_color(std::cout, num_samples); |
| 1291 | + color.write_color(std::cout, samples_per_pixel); |
1290 | 1292 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1291 | 1293 | }
|
1292 | 1294 | }
|
|
1425 | 1427 | }
|
1426 | 1428 | ...
|
1427 | 1429 | 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; |
1431 | 1433 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1432 |
| - int max_depth = 50; |
| 1434 | + const int max_depth = 50; |
1433 | 1435 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1434 | 1436 |
|
1435 | 1437 | ...
|
1436 |
| - for (int j = ny-1; j >= 0; --j) { |
| 1438 | + for (int j = image_height-1; j >= 0; --j) { |
1437 | 1439 | std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
1438 |
| - for (int i = 0; i < nx; ++i) { |
| 1440 | + for (int i = 0; i < image_width; ++i) { |
1439 | 1441 | 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; |
1443 | 1445 | ray r = cam.get_ray(u, v);
|
1444 | 1446 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1445 | 1447 | color += ray_color(r, world, max_depth);
|
1446 | 1448 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1447 | 1449 | }
|
1448 |
| - color.write_color(std::cout, num_samples); |
| 1450 | + color.write_color(std::cout, samples_per_pixel); |
1449 | 1451 | }
|
1450 | 1452 | }
|
1451 | 1453 |
|
|
1473 | 1475 | square-root:
|
1474 | 1476 |
|
1475 | 1477 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1476 |
| - void write_color(std::ostream &out, int num_samples) { |
| 1478 | + void write_color(std::ostream &out, int samples_per_pixel) { |
1477 | 1479 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1478 | 1480 | // Divide the color total by the number of samples and gamma-correct
|
1479 | 1481 | // for a gamma value of 2.0.
|
1480 |
| - auto scale = 1.0 / num_samples; |
| 1482 | + auto scale = 1.0 / samples_per_pixel; |
1481 | 1483 | auto r = sqrt(scale * e[0]);
|
1482 | 1484 | auto g = sqrt(scale * e[1]);
|
1483 | 1485 | auto b = sqrt(scale * e[2]);
|
|
1897 | 1899 |
|
1898 | 1900 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1899 | 1901 | 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"; |
1905 | 1908 |
|
1906 | 1909 |
|
1907 | 1910 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
|
1918 | 1921 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1919 | 1922 |
|
1920 | 1923 | camera cam;
|
1921 |
| - for (int j = ny-1; j >= 0; --j) { |
| 1924 | + for (int j = image_height-1; j >= 0; --j) { |
1922 | 1925 | std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
1923 |
| - for (int i = 0; i < nx; ++i) { |
| 1926 | + for (int i = 0; i < image_width; ++i) { |
1924 | 1927 | 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; |
1928 | 1931 | ray r = cam.get_ray(u, v);
|
1929 | 1932 | color += ray_color(r, world, max_depth);
|
1930 | 1933 | }
|
1931 |
| - color.write_color(std::cout, num_samples); |
| 1934 | + color.write_color(std::cout, samples_per_pixel); |
1932 | 1935 | }
|
1933 | 1936 | }
|
1934 | 1937 |
|
|
2363 | 2366 | </div>
|
2364 | 2367 |
|
2365 | 2368 | <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: |
2367 | 2370 |
|
2368 | 2371 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
2369 | 2372 | auto R = cos(pi/4);
|
|
2449 | 2452 | This allows us to change the viewpoint:
|
2450 | 2453 |
|
2451 | 2454 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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); |
2453 | 2458 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2454 | 2459 | [Listing [scene-free-view]: <kbd>[main.cc]</kbd> Scene with alternate viewpoint]
|
2455 | 2460 |
|
|
2582 | 2587 | Using a big aperture:
|
2583 | 2588 |
|
2584 | 2589 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
| 2590 | + const auto aspect_ratio = double(image_width) / image_height; |
| 2591 | + ... |
2585 | 2592 | vec3 lookfrom(3,3,2);
|
2586 | 2593 | vec3 lookat(0,0,-1);
|
| 2594 | + vec3 vup(0,1,0); |
2587 | 2595 | auto dist_to_focus = (lookfrom-lookat).length();
|
2588 | 2596 | auto aperture = 2.0;
|
2589 | 2597 |
|
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); |
2591 | 2599 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2592 | 2600 | [Listing [scene-camera-dof]: <kbd>[main.cc]</kbd> Scene camera with depth-of-field]
|
2593 | 2601 |
|
|
0 commit comments