Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a7b2e9d

Browse files
committed
Refactor emitted() to use hit_rec values directly
This simplifies the `material::emitted()` function. Resolves #1605
1 parent f3656ea commit a7b2e9d

File tree

7 files changed

+25
-29
lines changed

7 files changed

+25
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Change Log / Ray Tracing in One Weekend
99
- Fix --
1010
- New --
1111

12+
- Change -- Changed `material::emitted()` function to use passed `hit_rec` values directly (#1605)
13+
1214
### In One Weekend
1315

1416
### The Next Week

books/RayTracingTheNextWeek.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3202,8 +3202,8 @@
32023202
diffuse_light(shared_ptr<texture> tex) : tex(tex) {}
32033203
diffuse_light(const color& emit) : tex(make_shared<solid_color>(emit)) {}
32043204

3205-
color emitted(double u, double v, const point3& p) const override {
3206-
return tex->value(u, v, p);
3205+
color emitted(const hit_record& rec) const override {
3206+
return tex->value(rec.u, rec.v, rec.p);
32073207
}
32083208

32093209
private:
@@ -3223,7 +3223,7 @@
32233223

32243224

32253225
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
3226-
virtual color emitted(double u, double v, const point3& p) const {
3226+
virtual color emitted(const hit_record& rec) const {
32273227
return color(0,0,0);
32283228
}
32293229
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -3276,7 +3276,7 @@
32763276

32773277
ray scattered;
32783278
color attenuation;
3279-
color color_from_emission = rec.mat->emitted(rec.u, rec.v, rec.p);
3279+
color color_from_emission = rec.mat->emitted(rec);
32803280

32813281
if (!rec.mat->scatter(r, rec, attenuation, scattered))
32823282
return color_from_emission;

books/RayTracingTheRestOfYourLife.html

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,7 +1742,7 @@
17421742

17431743
ray scattered;
17441744
color attenuation;
1745-
color color_from_emission = rec.mat->emitted(rec.u, rec.v, rec.p);
1745+
color color_from_emission = rec.mat->emitted(rec);
17461746

17471747
if (!rec.mat->scatter(r, rec, attenuation, scattered))
17481748
return color_from_emission;
@@ -1797,7 +1797,7 @@
17971797

17981798
ray scattered;
17991799
color attenuation;
1800-
color color_from_emission = rec.mat->emitted(rec.u, rec.v, rec.p);
1800+
color color_from_emission = rec.mat->emitted(rec);
18011801

18021802
if (!rec.mat->scatter(r, rec, attenuation, scattered))
18031803
return color_from_emission;
@@ -2399,7 +2399,7 @@
23992399
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
24002400
double pdf_value;
24012401
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2402-
color color_from_emission = rec.mat->emitted(rec.u, rec.v, rec.p);
2402+
color color_from_emission = rec.mat->emitted(rec);
24032403

24042404

24052405
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -2540,7 +2540,7 @@
25402540
ray scattered;
25412541
color attenuation;
25422542
double pdf_value;
2543-
color color_from_emission = rec.mat->emitted(rec.u, rec.v, rec.p);
2543+
color color_from_emission = rec.mat->emitted(rec);
25442544

25452545
if (!rec.mat->scatter(r, rec, attenuation, scattered, pdf_value))
25462546
return color_from_emission;
@@ -2614,9 +2614,7 @@
26142614

26152615

26162616
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2617-
virtual color emitted(
2618-
const ray& r_in, const hit_record& rec, double u, double v, const point3& p
2619-
) const {
2617+
virtual color emitted(const ray& r_in, const hit_record& rec) const {
26202618
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
26212619
return color(0,0,0);
26222620
}
@@ -2629,11 +2627,10 @@
26292627

26302628

26312629
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2632-
color emitted(const ray& r_in, const hit_record& rec, double u, double v, const point3& p)
2633-
const override {
2630+
color emitted(const ray& r_in, const hit_record& rec) const override {
26342631
if (!rec.front_face)
26352632
return color(0,0,0);
2636-
return tex->value(u, v, p);
2633+
return tex->value(rec.u, rec.v, rec.p);
26372634
}
26382635
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
26392636

@@ -2652,7 +2649,7 @@
26522649

26532650

26542651
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2655-
color color_from_emission = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p);
2652+
color color_from_emission = rec.mat->emitted(r, rec);
26562653
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
26572654

26582655
...
@@ -2802,7 +2799,7 @@
28022799
ray scattered;
28032800
color attenuation;
28042801
double pdf_value;
2805-
color color_from_emission = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p);
2802+
color color_from_emission = rec.mat->emitted(r, rec);
28062803

28072804
if (!rec.mat->scatter(r, rec, attenuation, scattered, pdf_value))
28082805
return color_from_emission;
@@ -3040,7 +3037,7 @@
30403037
ray scattered;
30413038
color attenuation;
30423039
double pdf_value;
3043-
color color_from_emission = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p);
3040+
color color_from_emission = rec.mat->emitted(r, rec);
30443041

30453042
if (!rec.mat->scatter(r, rec, attenuation, scattered, pdf_value))
30463043
return color_from_emission;
@@ -3466,7 +3463,7 @@
34663463
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
34673464
scatter_record srec;
34683465
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
3469-
color color_from_emission = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p);
3466+
color color_from_emission = rec.mat->emitted(r, rec);
34703467

34713468

34723469
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight

src/TheNextWeek/camera.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class camera {
146146

147147
ray scattered;
148148
color attenuation;
149-
color color_from_emission = rec.mat->emitted(rec.u, rec.v, rec.p);
149+
color color_from_emission = rec.mat->emitted(rec);
150150

151151
if (!rec.mat->scatter(r, rec, attenuation, scattered))
152152
return color_from_emission;

src/TheNextWeek/material.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class material {
2121
public:
2222
virtual ~material() = default;
2323

24-
virtual color emitted(double u, double v, const point3& p) const {
24+
virtual color emitted(const hit_record& rec) const {
2525
return color(0,0,0);
2626
}
2727

@@ -119,8 +119,8 @@ class diffuse_light : public material {
119119
diffuse_light(shared_ptr<texture> tex) : tex(tex) {}
120120
diffuse_light(const color& emit) : tex(make_shared<solid_color>(emit)) {}
121121

122-
color emitted(double u, double v, const point3& p) const override {
123-
return tex->value(u, v, p);
122+
color emitted(const hit_record& rec) const override {
123+
return tex->value(rec.u, rec.v, rec.p);
124124
}
125125

126126
private:

src/TheRestOfYourLife/camera.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class camera {
163163
return background;
164164

165165
scatter_record srec;
166-
color color_from_emission = rec.mat->emitted(r, rec, rec.u, rec.v, rec.p);
166+
color color_from_emission = rec.mat->emitted(r, rec);
167167

168168
if (!rec.mat->scatter(r, rec, srec))
169169
return color_from_emission;

src/TheRestOfYourLife/material.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ class material {
3131
public:
3232
virtual ~material() = default;
3333

34-
virtual color emitted(
35-
const ray& r_in, const hit_record& rec, double u, double v, const point3& p
36-
) const {
34+
virtual color emitted(const ray& r_in, const hit_record& rec) const {
3735
return color(0,0,0);
3836
}
3937

@@ -138,11 +136,10 @@ class diffuse_light : public material {
138136
diffuse_light(shared_ptr<texture> tex) : tex(tex) {}
139137
diffuse_light(const color& emit) : tex(make_shared<solid_color>(emit)) {}
140138

141-
color emitted(const ray& r_in, const hit_record& rec, double u, double v, const point3& p)
142-
const override {
139+
color emitted(const ray& r_in, const hit_record& rec) const override {
143140
if (!rec.front_face)
144141
return color(0,0,0);
145-
return tex->value(u, v, p);
142+
return tex->value(rec.u, rec.v, rec.p);
146143
}
147144

148145
private:

0 commit comments

Comments
 (0)