|
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 | 765 | hittable_list objects;
|
|
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);
|
|
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 ...) { |
| 2121 | + for (int i ...) { |
| 2122 | + ... |
| 2123 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
| 2124 | + pixel_color += ray_color(r, background, world, lights, max_depth); |
2116 | 2125 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2117 | 2126 | [Listing [ray-color-scatter]: <kbd>[main.cc]</kbd> Ray color with scatter]
|
2118 | 2127 | </div>
|
|
2218 | 2227 |
|
2219 | 2228 | return emitted + srec.attenuation
|
2220 | 2229 | * rec.mat_ptr->scattering_pdf(r, rec, scattered)
|
2221 |
| - * ray_color(scattered, background, world, lights, depth-1) |
2222 |
| - / pdf_val; |
| 2230 | + * ray_color(scattered, background, world, lights, depth-1) / pdf_val; |
2223 | 2231 | }
|
2224 | 2232 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2225 | 2233 | [Listing [ray-color-implicit]: <kbd>[main.cc]</kbd>
|
|
2254 | 2262 | box1 = make_shared<translate>(box1, vec3(265,0,295));
|
2255 | 2263 | objects.add(box1);
|
2256 | 2264 |
|
| 2265 | + |
2257 | 2266 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
2258 | 2267 | auto glass = make_shared<dielectric>(1.5);
|
2259 | 2268 | objects.add(make_shared<sphere>(point3(190,90,190), 90 , glass));
|
|
2500 | 2509 | auto g = pixel_color.y();
|
2501 | 2510 | auto b = pixel_color.z();
|
2502 | 2511 |
|
| 2512 | + |
2503 | 2513 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
|
2504 | 2514 | // Replace NaN components with zero. See explanation in Ray Tracing: The Rest of Your Life.
|
2505 | 2515 | if (r != r) r = 0.0;
|
|
0 commit comments