|
344 | 344 | std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
345 | 345 | for (int i = 0; i < image_width; ++i) {
|
346 | 346 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
347 |
| - vec3 color(double(i)/(image_width-1), double(j)/(image_height-1), 0.25); |
348 |
| - color.write_color(std::cout); |
| 347 | + vec3 pixel_color(double(i)/(image_width-1), double(j)/(image_height-1), 0.25); |
| 348 | + pixel_color.write_color(std::cout); |
349 | 349 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
350 | 350 | }
|
351 | 351 | }
|
|
411 | 411 | through pixels and computes the color seen in the direction of those rays. The involved steps are
|
412 | 412 | (1) calculate the ray from the eye to the pixel, (2) determine which objects the ray intersects, and
|
413 | 413 | (3) compute a color for that intersection point. When first developing a ray tracer, I always do a
|
414 |
| -simple camera for getting the code up and running. I also make a simple `color(ray)` function that |
415 |
| -returns the color of the background (a simple gradient). |
| 414 | +simple camera for getting the code up and running. I also make a simple `ray_color(ray)` function |
| 415 | +that returns the color of the background (a simple gradient). |
416 | 416 |
|
417 | 417 | I’ve often gotten into trouble using square images for debugging because I transpose $x$ and $y$ too
|
418 | 418 | often, so I’ll stick with a 200×100 image. I’ll put the “eye” (or camera center if you think of a
|
|
459 | 459 | auto u = double(i) / (image_width-1);
|
460 | 460 | auto v = double(j) / (image_height-1);
|
461 | 461 | ray r(origin, lower_left_corner + u*horizontal + v*vertical);
|
462 |
| - vec3 color = ray_color(r); |
| 462 | + vec3 pixel_color = ray_color(r); |
463 | 463 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
464 |
| - color.write_color(std::cout); |
| 464 | + pixel_color.write_color(std::cout); |
465 | 465 | }
|
466 | 466 | }
|
467 | 467 |
|
|
1154 | 1154 |
|
1155 | 1155 |
|
1156 | 1156 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1157 |
| - vec3 color = ray_color(r, world); |
| 1157 | + vec3 pixel_color = ray_color(r, world); |
1158 | 1158 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1159 | 1159 |
|
1160 |
| - color.write_color(std::cout); |
| 1160 | + pixel_color.write_color(std::cout); |
1161 | 1161 | }
|
1162 | 1162 | }
|
1163 | 1163 |
|
|
1334 | 1334 | std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
1335 | 1335 | for (int i = 0; i < image_width; ++i) {
|
1336 | 1336 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1337 |
| - vec3 color(0, 0, 0); |
| 1337 | + vec3 pixel_color(0, 0, 0); |
1338 | 1338 | for (int s = 0; s < samples_per_pixel; ++s) {
|
1339 | 1339 | auto u = (i + random_double()) / (image_width-1);
|
1340 | 1340 | auto v = (j + random_double()) / (image_height-1);
|
1341 | 1341 | ray r = cam.get_ray(u, v);
|
1342 |
| - color += ray_color(r, world); |
| 1342 | + pixel_color += ray_color(r, world); |
1343 | 1343 | }
|
1344 |
| - color.write_color(std::cout, samples_per_pixel); |
| 1344 | + pixel_color.write_color(std::cout, samples_per_pixel); |
1345 | 1345 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1346 | 1346 | }
|
1347 | 1347 | }
|
|
1499 | 1499 | for (int j = image_height-1; j >= 0; --j) {
|
1500 | 1500 | std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
1501 | 1501 | for (int i = 0; i < image_width; ++i) {
|
1502 |
| - vec3 color(0, 0, 0); |
| 1502 | + vec3 pixel_color(0, 0, 0); |
1503 | 1503 | for (int s = 0; s < samples_per_pixel; ++s) {
|
1504 | 1504 | auto u = (i + random_double()) / (image_width-1);
|
1505 | 1505 | auto v = (j + random_double()) / (image_height-1);
|
1506 | 1506 | ray r = cam.get_ray(u, v);
|
1507 | 1507 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
1508 |
| - color += ray_color(r, world, max_depth); |
| 1508 | + pixel_color += ray_color(r, world, max_depth); |
1509 | 1509 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1510 | 1510 | }
|
1511 |
| - color.write_color(std::cout, samples_per_pixel); |
| 1511 | + pixel_color.write_color(std::cout, samples_per_pixel); |
1512 | 1512 | }
|
1513 | 1513 | }
|
1514 | 1514 |
|
|
1811 | 1811 | `hit_record` is just a way to stuff a bunch of arguments into a struct so we can send them as a
|
1812 | 1812 | group. When a ray hits a surface (a particular sphere for example), the material pointer in the
|
1813 | 1813 | `hit_record` will be set to point at the material pointer the sphere was given when it was set up in
|
1814 |
| -`main()` when we start. When the `color()` routine gets the `hit_record` it can call member |
| 1814 | +`main()` when we start. When the `ray_color()` routine gets the `hit_record` it can call member |
1815 | 1815 | functions of the material pointer to find out what ray, if any, is scattered.
|
1816 | 1816 |
|
1817 | 1817 | <div class='together'>
|
|
1951 | 1951 | </div>
|
1952 | 1952 |
|
1953 | 1953 | <div class='together'>
|
1954 |
| -We need to modify the color function to use this: |
| 1954 | +We need to modify the `ray_color()` function to use this: |
1955 | 1955 |
|
1956 | 1956 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1957 | 1957 | vec3 ray_color(const ray& r, const hittable& world, int depth) {
|
|
2009 | 2009 | for (int j = image_height-1; j >= 0; --j) {
|
2010 | 2010 | std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
2011 | 2011 | for (int i = 0; i < image_width; ++i) {
|
2012 |
| - vec3 color(0, 0, 0); |
| 2012 | + vec3 pixel_color(0, 0, 0); |
2013 | 2013 | for (int s = 0; s < samples_per_pixel; ++s) {
|
2014 | 2014 | auto u = (i + random_double()) / (image_width-1);
|
2015 | 2015 | auto v = (j + random_double()) / (image_height-1);
|
2016 | 2016 | ray r = cam.get_ray(u, v);
|
2017 |
| - color += ray_color(r, world, max_depth); |
| 2017 | + pixel_color += ray_color(r, world, max_depth); |
2018 | 2018 | }
|
2019 |
| - color.write_color(std::cout, samples_per_pixel); |
| 2019 | + pixel_color.write_color(std::cout, samples_per_pixel); |
2020 | 2020 | }
|
2021 | 2021 | }
|
2022 | 2022 |
|
|
0 commit comments