-
Notifications
You must be signed in to change notification settings - Fork 0
/
constant_medium.h
82 lines (65 loc) · 2.33 KB
/
constant_medium.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Subsurface
#ifndef CONSTANT_MEDIUM_H
#define CONSTANT_MEDIUM_H
#include "rtweekend.h"
#include "hittable.h"
#include "material.h"
#include "texture.h"
class constant_medium : public hittable {
public:
shared_ptr<hittable> boundary;
shared_ptr<material> phase_function;
double neg_inv_density;
public:
constant_medium(shared_ptr<hittable> b, double d, shared_ptr<texture> a)
: boundary(b),
neg_inv_density(-1/d),
phase_function(make_shared<isotropic>(a)) {}
constant_medium(shared_ptr<hittable> b, double d, color c)
: boundary(b),
neg_inv_density(-1/d),
phase_function(make_shared<isotropic>(c)) {}
virtual bool hit(const ray& r, double t_min, double t_max,
hit_record& rec) const override;
virtual bool bounding_box(double time0, double time1, aabb& output_box)
const override {
return boundary->bounding_box(time0, time1, output_box);
}
};
bool constant_medium::hit(const ray& r, double t_min, double t_max,
hit_record& rec) const {
// Print occasional samples when debugging.
// To enable, set enableDebug true.
const bool enableDebug = false;
const bool debugging = enableDebug && random_double() < 0.00001;
hit_record rec1, rec2;
if (!boundary->hit(r, -infinity, infinity, rec1))
return false;
if (!boundary->hit(r, rec1.t + 0.0001, infinity, rec2))
return false;
if (debugging)
std::cerr << "\nt_min=" << rec1.t << ", t_max=" << rec2.t << '\n';
if (rec1.t < t_min) rec1.t = t_min;
if (rec2.t > t_max) rec2.t = t_max;
if (rec1.t > rec2.t)
return false;
if (rec1.t < 0)
rec1.t = 0;
const auto ray_length = r.direction().length();
const auto distance_inside_boundary = (rec2.t - rec1.t) * ray_length;
const auto hit_distance = neg_inv_density * log(random_double());
if (hit_distance > distance_inside_boundary)
return false;
rec.t = rec1.t + hit_distance / ray_length;
rec.p = r.at(rec.t);
if (debugging) {
std::cerr << "hit_distance = " << hit_distance << '\n'
<< "rec.t = " << rec.t << '\n'
<< "rec.p = " << rec.p << '\n';
}
rec.normal = vec3(1, 0, 0); // arbitrary
rec.front_face = true; // also arbitrary
rec.mat_ptr = phase_function;
return true;
}
#endif