|
757 | 757 |
|
758 | 758 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
759 | 759 | ... |
760 | | - color ray_color( |
| 760 | + color ray_color(...) { |
761 | 761 | ... |
| 762 | + } |
762 | 763 |
|
763 | 764 | hittable_list cornell_box() { |
764 | | - hittable_list world; |
| 765 | + hittable_list objects; |
765 | 766 |
|
766 | 767 | auto red = make_shared<lambertian>(color(.65, .05, .05)); |
767 | 768 | auto white = make_shared<lambertian>(color(.73, .73, .73)); |
768 | 769 | auto green = make_shared<lambertian>(color(.12, .45, .15)); |
769 | 770 | auto light = make_shared<diffuse_light>(color(15, 15, 15)); |
770 | 771 |
|
771 | | - world.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green)); |
772 | | - world.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red)); |
773 | | - world.add(make_shared<xz_rect>(213, 343, 227, 332, 554, light)); |
774 | | - world.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white)); |
775 | | - world.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white)); |
776 | | - world.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white)); |
| 772 | + objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green)); |
| 773 | + objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red)); |
| 774 | + objects.add(make_shared<xz_rect>(213, 343, 227, 332, 554, light)); |
| 775 | + objects.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white)); |
| 776 | + objects.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white)); |
| 777 | + objects.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white)); |
777 | 778 |
|
778 | 779 | shared_ptr<hittable> box1 = make_shared<box>(point3(0,0,0), point3(165,330,165), white); |
779 | 780 | box1 = make_shared<rotate_y>(box1, 15); |
780 | 781 | box1 = make_shared<translate>(box1, vec3(265,0,295)); |
781 | | - world.add(box1); |
| 782 | + objects.add(box1); |
782 | 783 |
|
783 | 784 | shared_ptr<hittable> box2 = make_shared<box>(point3(0,0,0), point3(165,165,165), white); |
784 | 785 | box2 = make_shared<rotate_y>(box2, -18); |
785 | 786 | box2 = make_shared<translate>(box2, vec3(130,0,65)); |
786 | | - world.add(box2); |
| 787 | + objects.add(box2); |
787 | 788 |
|
788 | | - return world; |
| 789 | + return objects; |
789 | 790 | } |
790 | 791 |
|
791 | 792 | int main() { |
|
799 | 800 |
|
800 | 801 | // World |
801 | 802 |
|
802 | | - auto lights = make_shared<hittable_list>(); |
803 | | - lights->add(make_shared<xz_rect>(213, 343, 227, 332, 554, shared_ptr<material>())); |
804 | | - lights->add(make_shared<sphere>(point3(190, 90, 190), 90, shared_ptr<material>())); |
805 | | - |
806 | 803 | auto world = cornell_box(); |
807 | 804 |
|
808 | 805 | color background(0,0,0); |
|
1524 | 1521 |
|
1525 | 1522 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
1526 | 1523 | hittable_list cornell_box() { |
1527 | | - hittable_list world; |
| 1524 | + hittable_list objects; |
1528 | 1525 |
|
1529 | 1526 | auto red = make_shared<lambertian>(color(.65, .05, .05)); |
1530 | 1527 | auto white = make_shared<lambertian>(color(.73, .73, .73)); |
1531 | 1528 | auto green = make_shared<lambertian>(color(.12, .45, .15)); |
1532 | 1529 | auto light = make_shared<diffuse_light>(color(15, 15, 15)); |
1533 | 1530 |
|
1534 | | - world.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green)); |
1535 | | - world.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red)); |
| 1531 | + objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green)); |
| 1532 | + objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red)); |
1536 | 1533 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
1537 | | - world.add(make_shared<flip_face>(make_shared<xz_rect>(213, 343, 227, 332, 554, light))); |
| 1534 | + objects.add(make_shared<flip_face>(make_shared<xz_rect>(213, 343, 227, 332, 554, light))); |
1538 | 1535 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
1539 | | - world.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white)); |
1540 | | - world.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white)); |
1541 | | - world.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white)); |
| 1536 | + objects.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white)); |
| 1537 | + objects.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white)); |
| 1538 | + objects.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white)); |
1542 | 1539 |
|
1543 | 1540 | ... |
1544 | 1541 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
1690 | 1687 |
|
1691 | 1688 | return emitted |
1692 | 1689 | + albedo * rec.mat_ptr->scattering_pdf(r, rec, scattered) |
1693 | | - * ray_color(scattered, background, world, depth-1) |
1694 | | - / pdf_val; |
| 1690 | + * ray_color(scattered, background, world, depth-1) / pdf_val; |
1695 | 1691 | } |
1696 | 1692 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
1697 | 1693 | [Listing [ray-color-cos-pdf]: <kbd>[main.cc]</kbd> The ray_color function, using cosine pdf] |
|
1821 | 1817 |
|
1822 | 1818 | return emitted |
1823 | 1819 | + albedo * rec.mat_ptr->scattering_pdf(r, rec, scattered) |
1824 | | - * ray_color(scattered, background, world, depth-1) |
1825 | | - / pdf_val; |
| 1820 | + * ray_color(scattered, background, world, depth-1) / pdf_val; |
1826 | 1821 | } |
1827 | 1822 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
1828 | 1823 | [Listing [ray-color-hittable-pdf]: <kbd>[main.cc]</kbd> ray_color function with hittable PDF] |
|
1872 | 1867 | <div class='together'> |
1873 | 1868 | And plugging it into `ray_color()`: |
1874 | 1869 |
|
1875 | | - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
1876 | | - color ray_color( |
1877 | | - const ray& r, |
1878 | | - const color& background, |
1879 | | - const hittable& world, |
1880 | | - shared_ptr<hittable> lights, |
1881 | | - int depth |
1882 | | - ) { |
1883 | 1870 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
| 1871 | + color ray_color(const ray& r, const color& background, const hittable& world, int depth) { |
1884 | 1872 | hit_record rec; |
1885 | 1873 |
|
1886 | 1874 | // If we've exceeded the ray bounce limit, no more light is gathered. |
|
1912 | 1900 |
|
1913 | 1901 | return emitted |
1914 | 1902 | + albedo * rec.mat_ptr->scattering_pdf(r, rec, scattered) |
1915 | | - * ray_color(scattered, background, world, lights, depth-1) |
1916 | | - / pdf_val; |
| 1903 | + * ray_color(scattered, background, world, depth-1) / pdf_val; |
1917 | 1904 | } |
1918 | 1905 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
1919 | 1906 | [Listing [ray-color-mixture]: <kbd>[main.cc]</kbd> The ray_color function, using mixture PDF] |
|
1972 | 1959 |
|
1973 | 1960 |
|
1974 | 1961 |
|
1975 | | -Cleaning Up PDF Management. |
| 1962 | +Cleaning Up PDF Management |
1976 | 1963 | ==================================================================================================== |
1977 | 1964 |
|
1978 | 1965 | <div class='together'> |
|
2077 | 2064 | <div class='together'> |
2078 | 2065 | And `ray_color()` changes are small: |
2079 | 2066 |
|
2080 | | - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
| 2067 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
2081 | 2068 | color ray_color( |
2082 | 2069 | const ray& r, |
2083 | 2070 | const color& background, |
2084 | 2071 | const hittable& world, |
2085 | 2072 | shared_ptr<hittable> lights, |
2086 | 2073 | int depth |
2087 | 2074 | ) { |
| 2075 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
2088 | 2076 | hit_record rec; |
2089 | 2077 |
|
2090 | 2078 | // If we've exceeded the ray bounce limit, no more light is gathered. |
|
2109 | 2097 |
|
2110 | 2098 | return emitted |
2111 | 2099 | + srec.attenuation * rec.mat_ptr->scattering_pdf(r, rec, scattered) |
2112 | | - * ray_color(scattered, background, world, lights, depth-1) |
2113 | | - / pdf_val; |
| 2100 | + * ray_color(scattered, background, world, lights, depth-1) / pdf_val; |
2114 | 2101 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
2115 | 2102 | } |
| 2103 | + |
| 2104 | + ... |
| 2105 | + |
| 2106 | + int main() { |
| 2107 | + ... |
| 2108 | + // World |
| 2109 | + |
| 2110 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
| 2111 | + auto lights = make_shared<hittable_list>(); |
| 2112 | + lights->add(make_shared<xz_rect>(213, 343, 227, 332, 554, shared_ptr<material>())); |
| 2113 | + lights->add(make_shared<sphere>(point3(190, 90, 190), 90, shared_ptr<material>())); |
| 2114 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
| 2115 | + |
| 2116 | + auto world = cornell_box(); |
| 2117 | + |
| 2118 | + color background(0,0,0); |
| 2119 | + |
| 2120 | + for (int j = image_height-1; j >= 0; --j) { |
| 2121 | + std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush; |
| 2122 | + for (int i = 0; i < image_width; ++i) { |
| 2123 | + ... |
| 2124 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
| 2125 | + pixel_color += ray_color(r, background, world, lights, max_depth); |
2116 | 2126 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
2117 | 2127 | [Listing [ray-color-scatter]: <kbd>[main.cc]</kbd> Ray color with scatter] |
2118 | 2128 | </div> |
|
2218 | 2228 |
|
2219 | 2229 | return emitted + srec.attenuation |
2220 | 2230 | * rec.mat_ptr->scattering_pdf(r, rec, scattered) |
2221 | | - * ray_color(scattered, background, world, lights, depth-1) |
2222 | | - / pdf_val; |
| 2231 | + * ray_color(scattered, background, world, lights, depth-1) / pdf_val; |
2223 | 2232 | } |
2224 | 2233 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
2225 | 2234 | [Listing [ray-color-implicit]: <kbd>[main.cc]</kbd> |
2226 | 2235 | Ray color function with implicitly-sampled rays] |
2227 | 2236 | </div> |
2228 | 2237 |
|
2229 | 2238 | <div class='together'> |
2230 | | -We also need to change the block to metal. |
| 2239 | +We also need to change the block to metal. We'll also swap out the short block for a glass sphere. |
2231 | 2240 |
|
2232 | 2241 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
2233 | | - hittable_list cornell_box(camera& cam, double aspect) { |
2234 | | - hittable_list world; |
| 2242 | + hittable_list cornell_box() { |
| 2243 | + hittable_list objects; |
2235 | 2244 |
|
2236 | 2245 | auto red = make_shared<lambertian>(color(.65, .05, .05)); |
2237 | 2246 | auto white = make_shared<lambertian>(color(.73, .73, .73)); |
2238 | 2247 | auto green = make_shared<lambertian>(color(.12, .45, .15)); |
2239 | 2248 | auto light = make_shared<diffuse_light>(color(15, 15, 15)); |
2240 | 2249 |
|
2241 | | - world.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green)); |
2242 | | - world.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red)); |
2243 | | - world.add(make_shared<xz_rect>(213, 343, 227, 332, 554, light)); |
2244 | | - world.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white)); |
2245 | | - world.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white)); |
2246 | | - world.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white)); |
| 2250 | + objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green)); |
| 2251 | + objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red)); |
| 2252 | + objects.add(make_shared<flip_face>(make_shared<xz_rect>(213, 343, 227, 332, 554, light))); |
| 2253 | + objects.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white)); |
| 2254 | + objects.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white)); |
| 2255 | + objects.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white)); |
2247 | 2256 |
|
2248 | 2257 |
|
2249 | 2258 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
|
2252 | 2261 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
2253 | 2262 | box1 = make_shared<rotate_y>(box1, 15); |
2254 | 2263 | box1 = make_shared<translate>(box1, vec3(265,0,295)); |
2255 | | - world.add(box1); |
2256 | | - |
2257 | | - shared_ptr<hittable> box2 = make_shared<box>(point3(0,0,0), point3(165,165,165), white); |
2258 | | - box2 = make_shared<rotate_y>(box2, -18); |
2259 | | - box2 = make_shared<translate>(box2, vec3(130,0,65); |
2260 | | - world.add(box2); |
| 2264 | + objects.add(box1); |
2261 | 2265 |
|
2262 | | - point3 lookfrom(278, 278, -800); |
2263 | | - point3 lookat(278, 278, 0); |
2264 | | - vec3 vup(0, 1, 0); |
2265 | | - auto dist_to_focus = 10.0; |
2266 | | - auto aperture = 0.0; |
2267 | | - auto vfov = 40.0; |
2268 | | - auto time0 = 0.0; |
2269 | | - auto time1 = 1.0; |
2270 | 2266 |
|
2271 | | - cam = camera(lookfrom, lookat, vup, vfov, aspect, aperture, dist_to_focus, time0, time1); |
| 2267 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
| 2268 | + auto glass = make_shared<dielectric>(1.5); |
| 2269 | + objects.add(make_shared<sphere>(point3(190,90,190), 90 , glass)); |
| 2270 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
2272 | 2271 |
|
2273 | | - return world; |
| 2272 | + return objects; |
2274 | 2273 | } |
2275 | 2274 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
2276 | 2275 | [Listing [scene-cornell-al]: <kbd>[main.cc]</kbd> Cornell box scene with aluminum material] |
|
2511 | 2510 | auto g = pixel_color.y(); |
2512 | 2511 | auto b = pixel_color.z(); |
2513 | 2512 |
|
| 2513 | + |
2514 | 2514 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
2515 | 2515 | // Replace NaN components with zero. See explanation in Ray Tracing: The Rest of Your Life. |
2516 | 2516 | if (r != r) r = 0.0; |
|
0 commit comments