|
70 | 70 | #include <iostream>
|
71 | 71 |
|
72 | 72 | int main() {
|
73 |
| - int nx = 200; |
74 |
| - int ny = 100; |
| 73 | + int image_width = 200; |
| 74 | + 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 | + int image_width = 200; |
| 322 | + 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 | + int image_width = 200; |
| 423 | + int image_height = 100; |
| 424 | + std::cout << "P3\n" << image_width << " " << image_height << "\n255\n"; |
425 | 425 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
426 | 426 | vec3 lower_left_corner(-2.0, -1.0, -1.0);
|
427 | 427 | vec3 horizontal(4.0, 0.0, 0.0);
|
428 | 428 | vec3 vertical(0.0, 2.0, 0.0);
|
429 | 429 | vec3 origin(0.0, 0.0, 0.0);
|
430 | 430 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
431 |
| - for (int j = ny-1; j >= 0; --j) { |
| 431 | + for (int j = image_height-1; j >= 0; --j) { |
432 | 432 | std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
433 |
| - for (int i = 0; i < nx; ++i) { |
| 433 | + for (int i = 0; i < image_width; ++i) { |
434 | 434 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
435 |
| - auto u = double(i) / nx; |
436 |
| - auto v = double(j) / ny; |
| 435 | + auto u = double(i) / image_width; |
| 436 | + auto v = double(j) / image_height; |
437 | 437 | ray r(origin, lower_left_corner + u*horizontal + v*vertical);
|
438 | 438 | vec3 color = ray_color(r);
|
439 | 439 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
|
1078 | 1078 | }
|
1079 | 1079 |
|
1080 | 1080 | int main() {
|
1081 |
| - int nx = 200; |
1082 |
| - int ny = 100; |
| 1081 | + int image_width = 200; |
| 1082 | + int image_height = 100; |
1083 | 1083 |
|
1084 |
| - std::cout << "P3\n" << nx << ' ' << ny << "\n255\n"; |
| 1084 | + std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n"; |
1085 | 1085 |
|
1086 | 1086 | vec3 lower_left_corner(-2.0, -1.0, -1.0);
|
1087 | 1087 | vec3 horizontal(4.0, 0.0, 0.0);
|
|
1095 | 1095 | world.add(make_shared<sphere>(vec3(0,-100.5,-1), 100));
|
1096 | 1096 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1097 | 1097 |
|
1098 |
| - for (int j = ny-1; j >= 0; --j) { |
| 1098 | + for (int j = image_height-1; j >= 0; --j) { |
1099 | 1099 | 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; |
| 1100 | + for (int i = 0; i < image_width; ++i) { |
| 1101 | + auto u = double(i) / image_width; |
| 1102 | + auto v = double(j) / image_height; |
1103 | 1103 | ray r(origin, lower_left_corner + u*horizontal + v*vertical);
|
1104 | 1104 |
|
1105 | 1105 |
|
|
1260 | 1260 |
|
1261 | 1261 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1262 | 1262 | int main() {
|
1263 |
| - int nx = 200; |
1264 |
| - int ny = 100; |
| 1263 | + int image_width = 200; |
| 1264 | + int image_height = 100; |
1265 | 1265 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1266 | 1266 | int num_samples = 100;
|
1267 | 1267 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1268 | 1268 |
|
1269 |
| - std::cout << "P3\n" << nx << " " << ny << "\n255\n"; |
| 1269 | + std::cout << "P3\n" << image_width << " " << image_height << "\n255\n"; |
1270 | 1270 |
|
1271 | 1271 | hittable_list world;
|
1272 | 1272 | world.add(make_shared<sphere>(vec3(0,0,-1), 0.5));
|
|
1275 | 1275 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1276 | 1276 | camera cam;
|
1277 | 1277 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1278 |
| - for (int j = ny-1; j >= 0; --j) { |
| 1278 | + for (int j = image_height-1; j >= 0; --j) { |
1279 | 1279 | std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
1280 |
| - for (int i = 0; i < nx; ++i) { |
| 1280 | + for (int i = 0; i < image_width; ++i) { |
1281 | 1281 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1282 | 1282 | vec3 color(0, 0, 0);
|
1283 | 1283 | for (int s = 0; s < num_samples; ++s) {
|
1284 |
| - auto u = (i + random_double()) / nx; |
1285 |
| - auto v = (j + random_double()) / ny; |
| 1284 | + auto u = (i + random_double()) / image_width; |
| 1285 | + auto v = (j + random_double()) / image_height; |
1286 | 1286 | ray r = cam.get_ray(u, v);
|
1287 | 1287 | color += ray_color(r, world);
|
1288 | 1288 | }
|
|
1425 | 1425 | }
|
1426 | 1426 | ...
|
1427 | 1427 | int main() {
|
1428 |
| - int nx = 200; |
1429 |
| - int ny = 100; |
| 1428 | + int image_width = 200; |
| 1429 | + int image_height = 100; |
1430 | 1430 | int num_samples = 100;
|
1431 | 1431 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1432 | 1432 | int max_depth = 50;
|
1433 | 1433 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1434 | 1434 |
|
1435 | 1435 | ...
|
1436 |
| - for (int j = ny-1; j >= 0; --j) { |
| 1436 | + for (int j = image_height-1; j >= 0; --j) { |
1437 | 1437 | std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
1438 |
| - for (int i = 0; i < nx; ++i) { |
| 1438 | + for (int i = 0; i < image_width; ++i) { |
1439 | 1439 | vec3 color(0, 0, 0);
|
1440 | 1440 | for (int s = 0; s < num_samples; ++s) {
|
1441 |
| - auto u = (i + random_double()) / nx; |
1442 |
| - auto v = (j + random_double()) / ny; |
| 1441 | + auto u = (i + random_double()) / image_width; |
| 1442 | + auto v = (j + random_double()) / image_height; |
1443 | 1443 | ray r = cam.get_ray(u, v);
|
1444 | 1444 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1445 | 1445 | color += ray_color(r, world, max_depth);
|
|
1897 | 1897 |
|
1898 | 1898 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1899 | 1899 | int main() {
|
1900 |
| - int nx = 200; |
1901 |
| - int ny = 100; |
| 1900 | + int image_width = 200; |
| 1901 | + int image_height = 100; |
1902 | 1902 | int num_samples = 100;
|
1903 | 1903 | int max_depth = 50;
|
1904 |
| - std::cout << "P3\n" << nx << " " << ny << "\n255\n"; |
| 1904 | + std::cout << "P3\n" << image_width << " " << image_height << "\n255\n"; |
1905 | 1905 |
|
1906 | 1906 |
|
1907 | 1907 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
|
1918 | 1918 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1919 | 1919 |
|
1920 | 1920 | camera cam;
|
1921 |
| - for (int j = ny-1; j >= 0; --j) { |
| 1921 | + for (int j = image_height-1; j >= 0; --j) { |
1922 | 1922 | std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
1923 |
| - for (int i = 0; i < nx; ++i) { |
| 1923 | + for (int i = 0; i < image_width; ++i) { |
1924 | 1924 | vec3 color(0, 0, 0);
|
1925 | 1925 | for (int s = 0; s < num_samples; ++s) {
|
1926 |
| - auto u = (i + random_double()) / nx; |
1927 |
| - auto v = (j + random_double()) / ny; |
| 1926 | + auto u = (i + random_double()) / image_width; |
| 1927 | + auto v = (j + random_double()) / image_height; |
1928 | 1928 | ray r = cam.get_ray(u, v);
|
1929 | 1929 | color += ray_color(r, world, max_depth);
|
1930 | 1930 | }
|
|
2363 | 2363 | </div>
|
2364 | 2364 |
|
2365 | 2365 | <div class='together'>
|
2366 |
| -When calling it with camera `cam(90, double(nx)/ny)` and these spheres: |
| 2366 | +When calling it with camera `cam(90, double(image_width)/image_height)` and these spheres: |
2367 | 2367 |
|
2368 | 2368 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
2369 | 2369 | auto R = cos(pi/4);
|
|
2449 | 2449 | This allows us to change the viewpoint:
|
2450 | 2450 |
|
2451 | 2451 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
2452 |
| - camera cam(vec3(-2,2,1), vec3(0,0,-1), vec3(0,1,0), 90, double(nx)/ny); |
| 2452 | + auto aspect_ratio = double(image_width) / image_height; |
| 2453 | + camera cam(vec3(-2,2,1), vec3(0,0,-1), vec3(0,1,0), 90, aspect_ratio); |
2453 | 2454 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2454 | 2455 | [Listing [scene-free-view]: <kbd>[main.cc]</kbd> Scene with alternate viewpoint]
|
2455 | 2456 |
|
|
2587 | 2588 | auto dist_to_focus = (lookfrom-lookat).length();
|
2588 | 2589 | auto aperture = 2.0;
|
2589 | 2590 |
|
2590 |
| - camera cam(lookfrom, lookat, vec3(0,1,0), 20, double(nx)/ny, aperture, dist_to_focus); |
| 2591 | + auto aspect_ratio = double(image_width) / image_height; |
| 2592 | + camera cam(lookfrom, lookat, vec3(0,1,0), 20, aspect_ratio, aperture, dist_to_focus); |
2591 | 2593 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2592 | 2594 | [Listing [scene-camera-dof]: <kbd>[main.cc]</kbd> Scene camera with depth-of-field]
|
2593 | 2595 |
|
|
0 commit comments