Skip to content

Commit 8d3e88d

Browse files
committed
Refactor all material::dielectric classes
- Moved global function `schlick` to private static `reflectance` method in material::dielectric. This function is used nowhere else. In addition, `reflectance` is a more clear name of what this function actually computes. Added a comment that it uses Schlick's approximation. - Simplified the branching between reflection and refraction.
1 parent eccb004 commit 8d3e88d

File tree

3 files changed

+40
-28
lines changed

3 files changed

+40
-28
lines changed

src/InOneWeekend/material.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,6 @@ class metal : public material {
6363

6464

6565
class dielectric : public material {
66-
private:
67-
static double reflectance(double cosine, double ref_idx) {
68-
// Use Schlick's approximation for reflectance.
69-
auto r0 = (1-ref_idx) / (1+ref_idx);
70-
r0 = r0*r0;
71-
return r0 + (1-r0)*pow((1 - cosine),5);
72-
}
73-
7466
public:
7567
dielectric(double index_of_refraction) : ir(index_of_refraction) {}
7668

@@ -85,17 +77,27 @@ class dielectric : public material {
8577
double sin_theta = sqrt(1.0 - cos_theta*cos_theta);
8678

8779
bool cannot_refract = refraction_ratio * sin_theta > 1.0;
80+
vec3 direction;
8881

8982
if (cannot_refract || reflectance(cos_theta, refraction_ratio) > random_double())
90-
scattered = ray(rec.p, reflect(unit_direction, rec.normal));
83+
direction = reflect(unit_direction, rec.normal);
9184
else
92-
scattered = ray(rec.p, refract(unit_direction, rec.normal, refraction_ratio));
85+
direction = refract(unit_direction, rec.normal, refraction_ratio);
9386

87+
scattered = ray(rec.p, direction);
9488
return true;
9589
}
9690

9791
public:
9892
double ir; // Index of Refraction
93+
94+
private:
95+
static double reflectance(double cosine, double ref_idx) {
96+
// Use Schlick's approximation for reflectance.
97+
auto r0 = (1-ref_idx) / (1+ref_idx);
98+
r0 = r0*r0;
99+
return r0 + (1-r0)*pow((1 - cosine),5);
100+
}
99101
};
100102

101103

src/TheNextWeek/material.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,27 @@ class dielectric : public material {
8989
double sin_theta = sqrt(1.0 - cos_theta*cos_theta);
9090

9191
bool cannot_refract = refraction_ratio * sin_theta > 1.0;
92+
vec3 direction;
9293

93-
// If the ray cannot refract, or if it probabilistically reflects because of its
94-
// grazing angle, then return the reflected path.
95-
if (cannot_refract || random_double() < schlick(cos_theta, refraction_ratio)) {
96-
vec3 reflected = reflect(unit_direction, rec.normal);
97-
scattered = ray(rec.p, reflected, r_in.time());
98-
return true;
99-
}
94+
if (cannot_refract || reflectance(cos_theta, refraction_ratio) > random_double())
95+
direction = reflect(unit_direction, rec.normal);
96+
else
97+
direction = refract(unit_direction, rec.normal, refraction_ratio);
10098

101-
vec3 refracted = refract(unit_direction, rec.normal, refraction_ratio);
102-
scattered = ray(rec.p, refracted, r_in.time());
99+
scattered = ray(rec.p, direction, r_in.time());
103100
return true;
104101
}
105102

106103
public:
107104
double ir; // Index of Refraction
105+
106+
private:
107+
static double reflectance(double cosine, double ref_idx) {
108+
// Use Schlick's approximation for reflectance.
109+
auto r0 = (1-ref_idx) / (1+ref_idx);
110+
r0 = r0*r0;
111+
return r0 + (1-r0)*pow((1 - cosine),5);
112+
}
108113
};
109114

110115

src/TheRestOfYourLife/material.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,22 +119,27 @@ class dielectric : public material {
119119
double sin_theta = sqrt(1.0 - cos_theta*cos_theta);
120120

121121
bool cannot_refract = refraction_ratio * sin_theta > 1.0;
122+
vec3 direction;
122123

123-
// If the ray cannot refract, or if it probabilistically reflects because of its
124-
// grazing angle, then return the reflected path.
125-
if (cannot_refract || random_double() < schlick(cos_theta, refraction_ratio)) {
126-
vec3 reflected = reflect(unit_direction, rec.normal);
127-
srec.specular_ray = ray(rec.p, reflected, r_in.time());
128-
return true;
129-
}
124+
if (cannot_refract || reflectance(cos_theta, refraction_ratio) > random_double())
125+
direction = reflect(unit_direction, rec.normal);
126+
else
127+
direction = refract(unit_direction, rec.normal, refraction_ratio);
130128

131-
vec3 refracted = refract(unit_direction, rec.normal, refraction_ratio);
132-
srec.specular_ray = ray(rec.p, refracted, r_in.time());
129+
srec.specular_ray = ray(rec.p, direction, r_in.time());
133130
return true;
134131
}
135132

136133
public:
137134
double ir; // Index of Refraction
135+
136+
private:
137+
static double reflectance(double cosine, double ref_idx) {
138+
// Use Schlick's approximation for reflectance.
139+
auto r0 = (1-ref_idx) / (1+ref_idx);
140+
r0 = r0*r0;
141+
return r0 + (1-r0)*pow((1 - cosine),5);
142+
}
138143
};
139144

140145

0 commit comments

Comments
 (0)