Skip to content

Commit 719e579

Browse files
trevordblackhollasch
authored andcommitted
Moved to a color_from_* syntax for photon addition in scene.h
1 parent e5e34bc commit 719e579

File tree

4 files changed

+66
-48
lines changed

4 files changed

+66
-48
lines changed

books/RayTracingTheNextWeek.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2882,7 +2882,7 @@
28822882
--------------------------------------------------
28832883
Next, we want a pure black background so the only light in the scene is coming from the emitters. To
28842884
do this, we’ll add a background color parameter to our `ray_color` function, and pay attention to
2885-
the new `emitted` value.
2885+
the new `color_from_emission` value.
28862886

28872887
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
28882888
class scene {
@@ -2917,12 +2917,14 @@
29172917

29182918
ray scattered;
29192919
color attenuation;
2920-
color emitted = rec.mat->emitted(rec.u, rec.v, rec.p);
2920+
color color_from_emission = rec.mat->emitted(rec.u, rec.v, rec.p);
29212921

29222922
if (!rec.mat->scatter(r, rec, attenuation, scattered))
2923-
return emitted;
2923+
return color_from_emission;
29242924

2925-
return emitted + attenuation * ray_color(scattered, depth-1);
2925+
color color_from_scatter = attenuation * ray_color(scattered, depth-1);
2926+
2927+
return color_from_emission + color_from_scatter;
29262928
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
29272929
}
29282930
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

books/RayTracingTheRestOfYourLife.html

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,21 +1680,21 @@
16801680

16811681
ray scattered;
16821682
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);
16841684

16851685
if (!rec.mat->scatter(r, rec, attenuation, scattered))
1686-
return emitted;
1686+
return color_from_emission;
16871687

16881688

16891689
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
16901690
double scattering_pdf = rec.mat->scattering_pdf(r, rec, scattered);
16911691
double pdf = scattering_pdf;
16921692

1693-
color scattered_color =
1693+
color color_from_scatter =
16941694
(attenuation * scattering_pdf * ray_color(scattered, depth-1)) / pdf;
1695-
1696-
return emitted + scattered_color;
16971695
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1696+
1697+
return color_from_emission + color_from_scatter;
16981698
}
16991699
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17001700
[Listing [ray-color-impsample]: <kbd>[scene.h]</kbd>
@@ -1734,21 +1734,21 @@
17341734

17351735
ray scattered;
17361736
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);
17381738

17391739
if (!rec.mat->scatter(r, rec, attenuation, scattered))
1740-
return emitted;
1740+
return color_from_emission;
17411741

17421742
double scattering_pdf = rec.mat->scattering_pdf(r, rec, scattered);
17431743
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
17441744
double pdf = 1 / (2*pi);
17451745
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
17461746

1747-
color scattered_color =
1747+
color color_from_scatter =
17481748
(attenuation * scattering_pdf * ray_color(scattered, depth-1)) / pdf;
17491749

17501750

1751-
return emitted + scattered_color;
1751+
return color_from_emission + color_from_scatter;
17521752
}
17531753
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17541754
[Listing [ray-color-uniform]: <kbd>[scene.h]</kbd>
@@ -2412,12 +2412,12 @@
24122412

24132413
ray scattered;
24142414
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);
24162416
double pdf;
24172417
color albedo;
24182418

24192419
if (!rec.mat->scatter(r, rec, albedo, scattered, pdf))
2420-
return emitted;
2420+
return color_from_emission;
24212421

24222422

24232423
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -2427,20 +2427,22 @@
24272427
to_light = unit_vector(to_light);
24282428

24292429
if (dot(to_light, rec.normal) < 0)
2430-
return emitted;
2430+
return color_from_emission;
24312431

24322432
double light_area = (343-213)*(332-227);
24332433
auto light_cosine = fabs(to_light.y());
24342434
if (light_cosine < 0.000001)
2435-
return emitted;
2435+
return color_from_emission;
24362436

24372437
pdf = distance_squared / (light_cosine * light_area);
24382438
scattered = ray(rec.p, to_light, r.time());
24392439
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
24402440

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;
24442446
}
24452447
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24462448
[Listing [ray-color-lights]: <kbd>[scene.h]</kbd> Ray color with light sampling]
@@ -2626,12 +2628,12 @@
26262628

26272629
ray scattered;
26282630
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);
26302632
double pdf_val;
26312633
color albedo;
26322634

26332635
if (!rec.mat->scatter(r, rec, albedo, scattered, pdf_val))
2634-
return emitted;
2636+
return color_from_emission;
26352637

26362638

26372639
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -2640,9 +2642,11 @@
26402642
pdf_val = surface_pdf.value(scattered.direction());
26412643
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
26422644

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;
26462650
}
26472651
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26482652
[Listing [ray-color-cos-pdf]: <kbd>[scene.h]</kbd> The ray_color function, using cosine pdf]
@@ -2802,12 +2806,12 @@
28022806

28032807
ray scattered;
28042808
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);
28062810
double pdf_val;
28072811
color albedo;
28082812

28092813
if (!rec.mat->scatter(r, rec, albedo, scattered, pdf_val))
2810-
return emitted;
2814+
return color_from_emission;
28112815

28122816

28132817
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -2816,9 +2820,11 @@
28162820
pdf_val = light_pdf.value(scattered.direction());
28172821
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
28182822

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;
28222828
}
28232829
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28242830
[Listing [ray-color-lights]: <kbd>[scene.h]</kbd> ray_color function with light PDF]
@@ -2935,12 +2941,12 @@
29352941

29362942
ray scattered;
29372943
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);
29392945
double pdf_val;
29402946
color albedo;
29412947

29422948
if (!rec.mat->scatter(r, rec, albedo, scattered, pdf_val))
2943-
return emitted;
2949+
return color_from_emission;
29442950

29452951

29462952
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -2952,9 +2958,11 @@
29522958
pdf_val = mixed_pdf.value(scattered.direction());
29532959
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
29542960

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;
29582966
}
29592967
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29602968
[Listing [ray-color-mixture]: <kbd>[scene.h]</kbd> The ray_color function, using mixture PDF]
@@ -3161,21 +3169,22 @@
31613169

31623170
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
31633171
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+
31653174
if (!rec.mat->scatter(r, rec, srec))
3166-
return emitted;
3175+
return color_from_emission;
31673176

31683177
auto light_ptr = make_shared<hittable_pdf>(lights, rec.p);
31693178
mixture_pdf p(light_ptr, srec.pdf_ptr);
31703179

31713180
ray scattered = ray(rec.p, p.generate(), r.time());
31723181
auto pdf_val = p.value(scattered.direction());
31733182

3174-
color scattered_color = (srec.attenuation
3183+
color color_from_scatter = (srec.attenuation
31753184
* rec.mat->scattering_pdf(r, rec, scattered)
31763185
* ray_color(scattered, depth-1)) / pdf_val;
31773186

3178-
return emitted + scattered_color;
3187+
return color_from_emission + color_from_scatter;
31793188
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
31803189
}
31813190
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -3266,10 +3275,12 @@
32663275
private:
32673276
color ray_color(const ray& r, int depth) {
32683277
...
3278+
32693279
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+
32713282
if (!rec.mat->scatter(r, rec, srec))
3272-
return emitted;
3283+
return color_from_emission;
32733284

32743285

32753286
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -3278,6 +3289,9 @@
32783289
}
32793290
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
32803291

3292+
auto light_ptr = make_shared<hittable_pdf>(lights, rec.p);
3293+
mixture_pdf p(light_ptr, srec.pdf_ptr);
3294+
32813295
...
32823296
}
32833297
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/TheNextWeek/scene.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ class scene {
6767

6868
ray scattered;
6969
color attenuation;
70-
color emitted = rec.mat->emitted(rec.u, rec.v, rec.p);
70+
color color_from_emission = rec.mat->emitted(rec.u, rec.v, rec.p);
7171

7272
if (!rec.mat->scatter(r, rec, attenuation, scattered))
73-
return emitted;
73+
return color_from_emission;
7474

75-
return emitted + attenuation * ray_color(scattered, depth-1);
75+
color color_from_scatter = attenuation * ray_color(scattered, depth-1);
76+
77+
return color_from_emission + color_from_scatter;
7678
}
7779
};
7880

src/TheRestOfYourLife/scene.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ class scene {
6565
return background;
6666

6767
scatter_record srec;
68-
color emitted = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p);
68+
color color_from_emission = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p);
6969

7070
if (!rec.mat->scatter(r, rec, srec))
71-
return emitted;
71+
return color_from_emission;
7272

7373
if (srec.skip_pdf) {
7474
return srec.attenuation * ray_color(srec.skip_pdf_ray, depth-1);
@@ -79,11 +79,11 @@ class scene {
7979
ray scattered = ray(rec.p, p.generate(), r.time());
8080
auto pdf_val = p.value(scattered.direction());
8181

82-
color scattered_color = (srec.attenuation
82+
color color_from_scatter = (srec.attenuation
8383
* rec.mat->scattering_pdf(r, rec, scattered)
8484
* ray_color(scattered, depth-1)) / pdf_val;
8585

86-
return emitted + scattered_color;
86+
return color_from_emission + color_from_scatter;
8787
}
8888
};
8989

0 commit comments

Comments
 (0)