Skip to content

Commit ef89561

Browse files
Steve Hollaschhollasch
authored andcommitted
Fix scattering_pdf implementations
Multiple implementations of the `scattering_pdf()` function in The Rest Of Your Life returned bool, though the function is declared as returning float. Changed all `return false` to `return 0` and verified uses. Resolves #189
1 parent 15b978c commit ef89561

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

books/RayTracingTheRestOfYourLife.html

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -753,14 +753,17 @@
753753
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
754754
class material {
755755
public:
756+
756757
virtual bool scatter(const ray& r_in,
757758
const hit_record& rec, vec3& albedo, ray& scattered, float& pdf) const {
758759
return false;
759760
}
760-
virtual float scattering_pdf(const ray& r_in,
761-
const hit_record& rec, const ray& scattered) const {
762-
return false;
761+
762+
virtual float scattering_pdf(const ray& r_in, const hit_record& rec,
763+
const ray& scattered) const {
764+
return 0;
763765
}
766+
764767
virtual vec3 emitted(float u, float v, const vec3& p) const {
765768
return vec3(0,0,0);
766769
}
@@ -775,13 +778,15 @@
775778
class lambertian : public material {
776779
public:
777780
lambertian(texture *a) : albedo(a) {}
781+
778782
float scattering_pdf(const ray& r_in,
779783
const hit_record& rec, const ray& scattered) const {
780784
float cosine = dot(rec.normal, unit_vector(scattered.direction()));
781785
if (cosine < 0)
782786
return 0;
783787
return cosine / M_PI;
784788
}
789+
785790
bool scatter(const ray& r_in,
786791
const hit_record& rec, vec3& alb, ray& scattered, float& pdf) const {
787792
vec3 target = rec.p + rec.normal + random_in_unit_sphere();
@@ -790,6 +795,7 @@
790795
pdf = dot(rec.normal, scattered.direction()) / M_PI;
791796
return true;
792797
}
798+
793799
texture *albedo;
794800
};
795801
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1671,9 +1677,15 @@
16711677
class material {
16721678
public:
16731679
virtual bool scatter(const ray& r_in, const hit_record& hrec,
1674-
scatter_record& srec) const { return false; }
1680+
scatter_record& srec) const {
1681+
return false;
1682+
}
1683+
16751684
virtual float scattering_pdf(const ray& r_in, const hit_record& rec,
1676-
const ray& scattered) const { return false; }
1685+
const ray& scattered) const {
1686+
return 0;
1687+
}
1688+
16771689
virtual vec3 emitted(const ray& r_in, const hit_record& rec,
16781690
float u, float v, const vec3& p) const { return vec3(0,0,0); }
16791691
};
@@ -1686,14 +1698,18 @@
16861698
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
16871699
class lambertian : public material {
16881700
public:
1701+
16891702
lambertian(texture *a) : albedo(a) {}
1703+
16901704
float scattering_pdf(const ray& r_in, const hit_record& rec,
1691-
const ray& scattered) const {
1705+
const ray& scattered) const {
16921706
float cosine = dot(rec.normal, unit_vector(scattered.direction()));
16931707
if (cosine < 0)
16941708
return 0;
16951709
return cosine / M_PI;
16961710
}
1711+
1712+
16971713
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
16981714
bool scatter(const ray& r_in, const hit_record& hrec,
16991715
scatter_record& srec) const {
@@ -1703,6 +1719,7 @@
17031719
return true;
17041720
}
17051721
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1722+
17061723
texture *albedo;
17071724
};
17081725
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/TheRestOfYourLife/main.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ vec3 color(const ray& r, hittable *world, hittable *light_shape, int depth) {
5656
ray scattered = ray(hrec.p, p.generate(), r.time());
5757
float pdf_val = p.value(scattered.direction());
5858
delete srec.pdf_ptr;
59-
return emitted + srec.attenuation*hrec.mat_ptr->scattering_pdf(r, hrec, scattered)*color(scattered, world, light_shape, depth+1) / pdf_val;
59+
return emitted
60+
+ srec.attenuation * hrec.mat_ptr->scattering_pdf(r, hrec, scattered)
61+
* color(scattered, world, light_shape, depth+1)
62+
/ pdf_val;
6063
}
6164
}
6265
else

src/TheRestOfYourLife/material.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,14 @@ struct scatter_record
5454
class material {
5555
public:
5656
virtual bool scatter(const ray& r_in, const hit_record& hrec, scatter_record& srec) const {
57-
return false;}
57+
return false;
58+
}
5859
virtual float scattering_pdf(const ray& r_in, const hit_record& rec, const ray& scattered) const {
59-
return false;}
60-
virtual vec3 emitted(const ray& r_in, const hit_record& rec, float u, float v, const vec3& p) const { return vec3(0,0,0); }
60+
return 0;
61+
}
62+
virtual vec3 emitted(const ray& r_in, const hit_record& rec, float u, float v, const vec3& p) const {
63+
return vec3(0,0,0);
64+
}
6165
};
6266

6367
class dielectric : public material {

0 commit comments

Comments
 (0)