@@ -23,23 +23,24 @@ class sphere: public hittable {
23
23
: center(cen), radius(r), mat_ptr(m) {};
24
24
virtual bool hit (const ray& r, double tmin, double tmax, hit_record& rec) const ;
25
25
virtual bool bounding_box (double t0, double t1, aabb& output_box) const ;
26
- virtual double pdf_value (const vec3& o, const vec3& v) const ;
26
+ virtual double pdf_value (const vec3& o, const vec3& v) const ;
27
27
virtual vec3 random (const vec3& o) const ;
28
28
29
+ public:
29
30
vec3 center;
30
31
double radius;
31
32
shared_ptr<material> mat_ptr;
32
33
};
33
34
34
35
double sphere::pdf_value (const vec3& o, const vec3& v) const {
35
36
hit_record rec;
36
- if (this ->hit (ray (o, v), 0.001 , infinity, rec)) {
37
- auto cos_theta_max = sqrt (1 - radius*radius/(center-o).length_squared ());
38
- auto solid_angle = 2 *pi*(1 -cos_theta_max);
39
- return 1 / solid_angle;
40
- }
41
- else
37
+ if (!this ->hit (ray (o, v), 0.001 , infinity, rec))
42
38
return 0 ;
39
+
40
+ auto cos_theta_max = sqrt (1 - radius*radius/(center-o).length_squared ());
41
+ auto solid_angle = 2 *pi*(1 -cos_theta_max);
42
+
43
+ return 1 / solid_angle;
43
44
}
44
45
45
46
vec3 sphere::random (const vec3& o) const {
@@ -63,6 +64,7 @@ bool sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) cons
63
64
auto a = r.direction ().length_squared ();
64
65
auto half_b = dot (oc, r.direction ());
65
66
auto c = oc.length_squared () - radius*radius;
67
+
66
68
auto discriminant = half_b*half_b - a*c;
67
69
68
70
if (discriminant > 0 ) {
@@ -90,6 +92,7 @@ bool sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) cons
90
92
return true ;
91
93
}
92
94
}
95
+
93
96
return false ;
94
97
}
95
98
0 commit comments