|
1680 | 1680 |
|
1681 | 1681 | ray scattered; |
1682 | 1682 | color attenuation; |
1683 | | - color emitted = rec.mat->emitted(rec.u, rec.v, rec.p); |
| 1683 | + color color_from_emission = rec.mat->emitted(rec.u, rec.v, rec.p); |
1684 | 1684 |
|
1685 | 1685 | if (!rec.mat->scatter(r, rec, attenuation, scattered)) |
1686 | | - return emitted; |
| 1686 | + return color_from_emission; |
1687 | 1687 |
|
1688 | 1688 |
|
1689 | 1689 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
1690 | 1690 | double scattering_pdf = rec.mat->scattering_pdf(r, rec, scattered); |
1691 | 1691 | double pdf = scattering_pdf; |
1692 | 1692 |
|
1693 | | - color scattered_color = |
| 1693 | + color color_from_scatter = |
1694 | 1694 | (attenuation * scattering_pdf * ray_color(scattered, depth-1)) / pdf; |
1695 | | - |
1696 | | - return emitted + scattered_color; |
1697 | 1695 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
| 1696 | + |
| 1697 | + return color_from_emission + color_from_scatter; |
1698 | 1698 | } |
1699 | 1699 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
1700 | 1700 | [Listing [ray-color-impsample]: <kbd>[scene.h]</kbd> |
|
1734 | 1734 |
|
1735 | 1735 | ray scattered; |
1736 | 1736 | color attenuation; |
1737 | | - color emitted = rec.mat->emitted(rec.u, rec.v, rec.p); |
| 1737 | + color color_from_emission = rec.mat->emitted(rec.u, rec.v, rec.p); |
1738 | 1738 |
|
1739 | 1739 | if (!rec.mat->scatter(r, rec, attenuation, scattered)) |
1740 | | - return emitted; |
| 1740 | + return color_from_emission; |
1741 | 1741 |
|
1742 | 1742 | double scattering_pdf = rec.mat->scattering_pdf(r, rec, scattered); |
1743 | 1743 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
1744 | 1744 | double pdf = 1 / (2*pi); |
1745 | 1745 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
1746 | 1746 |
|
1747 | | - color scattered_color = |
| 1747 | + color color_from_scatter = |
1748 | 1748 | (attenuation * scattering_pdf * ray_color(scattered, depth-1)) / pdf; |
1749 | 1749 |
|
1750 | 1750 |
|
1751 | | - return emitted + scattered_color; |
| 1751 | + return color_from_emission + color_from_scatter; |
1752 | 1752 | } |
1753 | 1753 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
1754 | 1754 | [Listing [ray-color-uniform]: <kbd>[scene.h]</kbd> |
|
2412 | 2412 |
|
2413 | 2413 | ray scattered; |
2414 | 2414 | color attenuation; |
2415 | | - color emitted = rec.mat->emitted(rec.u, rec.v, rec.p); |
| 2415 | + color color_from_emission = rec.mat->emitted(rec.u, rec.v, rec.p); |
2416 | 2416 | double pdf; |
2417 | 2417 | color albedo; |
2418 | 2418 |
|
2419 | 2419 | if (!rec.mat->scatter(r, rec, albedo, scattered, pdf)) |
2420 | | - return emitted; |
| 2420 | + return color_from_emission; |
2421 | 2421 |
|
2422 | 2422 |
|
2423 | 2423 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
|
2427 | 2427 | to_light = unit_vector(to_light); |
2428 | 2428 |
|
2429 | 2429 | if (dot(to_light, rec.normal) < 0) |
2430 | | - return emitted; |
| 2430 | + return color_from_emission; |
2431 | 2431 |
|
2432 | 2432 | double light_area = (343-213)*(332-227); |
2433 | 2433 | auto light_cosine = fabs(to_light.y()); |
2434 | 2434 | if (light_cosine < 0.000001) |
2435 | | - return emitted; |
| 2435 | + return color_from_emission; |
2436 | 2436 |
|
2437 | 2437 | pdf = distance_squared / (light_cosine * light_area); |
2438 | 2438 | scattered = ray(rec.p, to_light, r.time()); |
2439 | 2439 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
2440 | 2440 |
|
2441 | | - return emitted |
2442 | | - + albedo * rec.mat->scattering_pdf(r, rec, scattered) |
2443 | | - * ray_color(scattered, depth-1) / pdf; |
| 2441 | + color color_from_scatter = |
| 2442 | + albedo * rec.mat->scattering_pdf(r, rec, scattered) |
| 2443 | + * ray_color(scattered, depth-1) / pdf |
| 2444 | + |
| 2445 | + return color_from_emission + color_from_scatter; |
2444 | 2446 | } |
2445 | 2447 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
2446 | 2448 | [Listing [ray-color-lights]: <kbd>[scene.h]</kbd> Ray color with light sampling] |
|
2626 | 2628 |
|
2627 | 2629 | ray scattered; |
2628 | 2630 | color attenuation; |
2629 | | - color emitted = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p); |
| 2631 | + color color_from_emission = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p); |
2630 | 2632 | double pdf_val; |
2631 | 2633 | color albedo; |
2632 | 2634 |
|
2633 | 2635 | if (!rec.mat->scatter(r, rec, albedo, scattered, pdf_val)) |
2634 | | - return emitted; |
| 2636 | + return color_from_emission; |
2635 | 2637 |
|
2636 | 2638 |
|
2637 | 2639 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
|
2640 | 2642 | pdf_val = surface_pdf.value(scattered.direction()); |
2641 | 2643 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
2642 | 2644 |
|
2643 | | - return emitted |
2644 | | - + albedo * rec.mat->scattering_pdf(r, rec, scattered) |
2645 | | - * ray_color(scattered, depth-1) / pdf_val; |
| 2645 | + color color_from_scatter = albedo |
| 2646 | + * rec.mat->scattering_pdf(r, rec, scattered) |
| 2647 | + * ray_color(scattered, depth-1) / pdf_val; |
| 2648 | + |
| 2649 | + return color_from_emission + color_from_scatter; |
2646 | 2650 | } |
2647 | 2651 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
2648 | 2652 | [Listing [ray-color-cos-pdf]: <kbd>[scene.h]</kbd> The ray_color function, using cosine pdf] |
|
2802 | 2806 |
|
2803 | 2807 | ray scattered; |
2804 | 2808 | color attenuation; |
2805 | | - color emitted = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p); |
| 2809 | + color color_from_emission = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p); |
2806 | 2810 | double pdf_val; |
2807 | 2811 | color albedo; |
2808 | 2812 |
|
2809 | 2813 | if (!rec.mat->scatter(r, rec, albedo, scattered, pdf_val)) |
2810 | | - return emitted; |
| 2814 | + return color_from_emission; |
2811 | 2815 |
|
2812 | 2816 |
|
2813 | 2817 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
|
2816 | 2820 | pdf_val = light_pdf.value(scattered.direction()); |
2817 | 2821 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
2818 | 2822 |
|
2819 | | - return emitted |
2820 | | - + albedo * rec.mat->scattering_pdf(r, rec, scattered) |
2821 | | - * ray_color(scattered, depth-1) / pdf_val; |
| 2823 | + color color_from_scatter = albedo |
| 2824 | + * rec.mat->scattering_pdf(r, rec, scattered) |
| 2825 | + * ray_color(scattered, depth-1) / pdf_val; |
| 2826 | + |
| 2827 | + return color_from_emission + color_from_scatter; |
2822 | 2828 | } |
2823 | 2829 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
2824 | 2830 | [Listing [ray-color-lights]: <kbd>[scene.h]</kbd> ray_color function with light PDF] |
|
2935 | 2941 |
|
2936 | 2942 | ray scattered; |
2937 | 2943 | color attenuation; |
2938 | | - color emitted = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p); |
| 2944 | + color color_from_emission = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p); |
2939 | 2945 | double pdf_val; |
2940 | 2946 | color albedo; |
2941 | 2947 |
|
2942 | 2948 | if (!rec.mat->scatter(r, rec, albedo, scattered, pdf_val)) |
2943 | | - return emitted; |
| 2949 | + return color_from_emission; |
2944 | 2950 |
|
2945 | 2951 |
|
2946 | 2952 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
|
2952 | 2958 | pdf_val = mixed_pdf.value(scattered.direction()); |
2953 | 2959 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
2954 | 2960 |
|
2955 | | - return emitted |
2956 | | - + albedo * rec.mat->scattering_pdf(r, rec, scattered) |
2957 | | - * ray_color(scattered, depth-1) / pdf_val; |
| 2961 | + color color_from_scatter = albedo |
| 2962 | + * rec.mat->scattering_pdf(r, rec, scattered) |
| 2963 | + * ray_color(scattered, depth-1) / pdf_val; |
| 2964 | + |
| 2965 | + return color_from_emission + color_from_scatter; |
2958 | 2966 | } |
2959 | 2967 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
2960 | 2968 | [Listing [ray-color-mixture]: <kbd>[scene.h]</kbd> The ray_color function, using mixture PDF] |
|
3161 | 3169 |
|
3162 | 3170 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
3163 | 3171 | scatter_record srec; |
3164 | | - color emitted = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p); |
| 3172 | + color color_from_emission = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p); |
| 3173 | + |
3165 | 3174 | if (!rec.mat->scatter(r, rec, srec)) |
3166 | | - return emitted; |
| 3175 | + return color_from_emission; |
3167 | 3176 |
|
3168 | 3177 | auto light_ptr = make_shared<hittable_pdf>(lights, rec.p); |
3169 | 3178 | mixture_pdf p(light_ptr, srec.pdf_ptr); |
3170 | 3179 |
|
3171 | 3180 | ray scattered = ray(rec.p, p.generate(), r.time()); |
3172 | 3181 | auto pdf_val = p.value(scattered.direction()); |
3173 | 3182 |
|
3174 | | - color scattered_color = (srec.attenuation |
| 3183 | + color color_from_scatter = (srec.attenuation |
3175 | 3184 | * rec.mat->scattering_pdf(r, rec, scattered) |
3176 | 3185 | * ray_color(scattered, depth-1)) / pdf_val; |
3177 | 3186 |
|
3178 | | - return emitted + scattered_color; |
| 3187 | + return color_from_emission + color_from_scatter; |
3179 | 3188 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
3180 | 3189 | } |
3181 | 3190 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
3266 | 3275 | private: |
3267 | 3276 | color ray_color(const ray& r, int depth) { |
3268 | 3277 | ... |
| 3278 | + |
3269 | 3279 | scatter_record srec; |
3270 | | - color emitted = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p); |
| 3280 | + color color_from_emission = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p); |
| 3281 | + |
3271 | 3282 | if (!rec.mat->scatter(r, rec, srec)) |
3272 | | - return emitted; |
| 3283 | + return color_from_emission; |
3273 | 3284 |
|
3274 | 3285 |
|
3275 | 3286 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight |
|
3278 | 3289 | } |
3279 | 3290 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ |
3280 | 3291 |
|
| 3292 | + auto light_ptr = make_shared<hittable_pdf>(lights, rec.p); |
| 3293 | + mixture_pdf p(light_ptr, srec.pdf_ptr); |
| 3294 | + |
3281 | 3295 | ... |
3282 | 3296 | } |
3283 | 3297 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
0 commit comments