Skip to content

Commit b731cc7

Browse files
committed
Get rid of all unnecessary time parameters
The current design of render time intervals is incomplete, so we've decided to scale this back. Time will be mangaged only by the camera (by specifying ray time), and by objects that respond to time (moving sphere). See #799
1 parent b9873b9 commit b731cc7

17 files changed

+72
-91
lines changed

src/TheNextWeek/aarect.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class xy_rect : public hittable {
4444
return true;
4545
}
4646

47-
bool bounding_box(double time_start, double time_end, aabb& output_box) const override {
47+
bool bounding_box(aabb& output_box) const override {
4848
// The bounding box must have non-zero width in each dimension, so pad the Z
4949
// dimension a small amount.
5050
output_box = aabb(point3(x0, y0, k-0.0001), point3(x1, y1, k+0.0001));
@@ -85,7 +85,7 @@ class xz_rect : public hittable {
8585
return true;
8686
}
8787

88-
bool bounding_box(double time_start, double time_end, aabb& output_box) const override {
88+
bool bounding_box(aabb& output_box) const override {
8989
// The bounding box must have non-zero width in each dimension, so pad the Y
9090
// dimension a small amount.
9191
output_box = aabb(point3(x0, k-0.0001, z0), point3(x1, k+0.0001, z1));
@@ -126,7 +126,7 @@ class yz_rect : public hittable {
126126
return true;
127127
}
128128

129-
bool bounding_box(double time_start, double time_end, aabb& output_box) const override {
129+
bool bounding_box(aabb& output_box) const override {
130130
// The bounding box must have non-zero width in each dimension, so pad the X
131131
// dimension a small amount.
132132
output_box = aabb(point3(k-0.0001, y0, z0), point3(k+0.0001, y1, z1));

src/TheNextWeek/box.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class box : public hittable {
3939
return sides.hit(r, ray_t, rec);
4040
}
4141

42-
bool bounding_box(double time_start, double time_end, aabb& output_box) const override {
42+
bool bounding_box(aabb& output_box) const override {
4343
output_box = aabb(box_min, box_max);
4444
return true;
4545
}

src/TheNextWeek/bvh.h

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,9 @@ class bvh_node : public hittable {
2323
public:
2424
bvh_node();
2525

26-
bvh_node(const hittable_list& list, double time0, double time1)
27-
: bvh_node(list.objects, 0, list.objects.size(), time0, time1)
28-
{}
26+
bvh_node(const hittable_list& list) : bvh_node(list.objects, 0, list.objects.size()) {}
2927

30-
bvh_node(
31-
const std::vector<shared_ptr<hittable>>& src_objects,
32-
size_t start, size_t end, double time0, double time1
33-
) {
28+
bvh_node(const std::vector<shared_ptr<hittable>>& src_objects, size_t start, size_t end) {
3429
auto objects = src_objects; // Create a modifiable array of the source scene objects
3530

3631
int axis = random_int(0,2);
@@ -54,15 +49,13 @@ class bvh_node : public hittable {
5449
std::sort(objects.begin() + start, objects.begin() + end, comparator);
5550

5651
auto mid = start + object_span/2;
57-
left = make_shared<bvh_node>(objects, start, mid, time0, time1);
58-
right = make_shared<bvh_node>(objects, mid, end, time0, time1);
52+
left = make_shared<bvh_node>(objects, start, mid);
53+
right = make_shared<bvh_node>(objects, mid, end);
5954
}
6055

6156
aabb box_left, box_right;
6257

63-
if ( !left->bounding_box (time0, time1, box_left)
64-
|| !right->bounding_box(time0, time1, box_right)
65-
)
58+
if (!left->bounding_box(box_left) || !right->bounding_box(box_right))
6659
std::cerr << "No bounding box in bvh_node constructor.\n";
6760

6861
box = aabb(box_left, box_right);
@@ -78,7 +71,7 @@ class bvh_node : public hittable {
7871
return hit_left || hit_right;
7972
}
8073

81-
bool bounding_box(double time0, double time1, aabb& output_box) const override {
74+
bool bounding_box(aabb& output_box) const override {
8275
output_box = box;
8376
return true;
8477
}
@@ -95,7 +88,7 @@ class bvh_node : public hittable {
9588
aabb box_a;
9689
aabb box_b;
9790

98-
if (!a->bounding_box(0,0, box_a) || !b->bounding_box(0,0, box_b))
91+
if (!a->bounding_box(box_a) || !b->bounding_box(box_b))
9992
std::cerr << "No bounding box in bvh_node constructor.\n";
10093

10194
return box_a.axis(axis_index).min < box_b.axis(axis_index).min;

src/TheNextWeek/constant_medium.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ class constant_medium : public hittable {
7575
return true;
7676
}
7777

78-
bool bounding_box(double time_start, double time_end, aabb& output_box) const override {
79-
return boundary->bounding_box(time_start, time_end, output_box);
78+
bool bounding_box(aabb& output_box) const override {
79+
return boundary->bounding_box(output_box);
8080
}
8181

8282
public:

src/TheNextWeek/hittable.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class hittable {
4040
public:
4141
virtual bool hit(const ray& r, interval ray_t, hit_record& rec) const = 0;
4242

43-
virtual bool bounding_box(double time_start, double time_end, aabb& output_box) const = 0;
43+
virtual bool bounding_box(aabb& output_box) const = 0;
4444
};
4545

4646

@@ -60,12 +60,11 @@ class translate : public hittable {
6060
return true;
6161
}
6262

63-
bool bounding_box(double time_start, double time_end, aabb& output_box) const override {
64-
if (!ptr->bounding_box(time_start, time_end, output_box))
63+
bool bounding_box(aabb& output_box) const override {
64+
if (!ptr->bounding_box(output_box))
6565
return false;
6666

6767
output_box += offset;
68-
6968
return true;
7069
}
7170

@@ -81,7 +80,7 @@ class rotate_y : public hittable {
8180
auto radians = degrees_to_radians(angle);
8281
sin_theta = sin(radians);
8382
cos_theta = cos(radians);
84-
hasbox = ptr->bounding_box(0, 1, bbox);
83+
hasbox = ptr->bounding_box(bbox);
8584

8685
point3 min( infinity, infinity, infinity);
8786
point3 max(-infinity, -infinity, -infinity);
@@ -139,7 +138,7 @@ class rotate_y : public hittable {
139138
return true;
140139
}
141140

142-
bool bounding_box(double time_start, double time_end, aabb& output_box) const override {
141+
bool bounding_box(aabb& output_box) const override {
143142
output_box = bbox;
144143
return hasbox;
145144
}

src/TheNextWeek/hittable_list.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ class hittable_list : public hittable {
4444
return hit_anything;
4545
}
4646

47-
bool bounding_box(double time_start, double time_end, aabb& output_box) const override {
47+
bool bounding_box(aabb& output_box) const override {
4848
if (objects.empty()) return false;
4949

5050
aabb temp_box;
5151
bool first_box = true;
5252

5353
for (const auto& object : objects) {
54-
if (!object->bounding_box(time_start, time_end, temp_box)) return false;
54+
if (!object->bounding_box(temp_box)) return false;
5555
output_box = first_box ? temp_box : aabb(output_box, temp_box);
5656
first_box = false;
5757
}

src/TheNextWeek/main.cc

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ void random_spheres(scene& scene_desc) {
3636
scene_desc.cam.aperture = 0.1;
3737
scene_desc.cam.focus_dist = 10.0;
3838

39-
scene_desc.cam.time_start = 0.0;
40-
scene_desc.cam.time_end = 1.0;
41-
4239
hittable_list& world = scene_desc.world;
4340

4441
auto checker = make_shared<checker_texture>(0.32, color(.2, .3, .1), color(.9, .9, .9));
@@ -58,7 +55,7 @@ void random_spheres(scene& scene_desc) {
5855
sphere_material = make_shared<lambertian>(albedo);
5956
auto center2 = center + vec3(0, random_double(0,.5), 0);
6057
world.add(make_shared<moving_sphere>(
61-
center, center2, 0.2, sphere_material, 0.0, 1.0));
58+
center, center2, 0.2, sphere_material));
6259
} else if (choose_mat < 0.95) {
6360
// metal
6461
auto albedo = color::random(0.5, 1);
@@ -83,7 +80,7 @@ void random_spheres(scene& scene_desc) {
8380
auto material3 = make_shared<metal>(color(0.7, 0.6, 0.5), 0.0);
8481
world.add(make_shared<sphere>(point3(4, 1, 0), 1.0, material3));
8582

86-
scene_desc.world = hittable_list(make_shared<bvh_node>(world, 0.0, 1.0));
83+
scene_desc.world = hittable_list(make_shared<bvh_node>(world));
8784
}
8885

8986

@@ -271,15 +268,15 @@ void final_scene(scene& scene_desc) {
271268

272269
hittable_list& world = scene_desc.world;
273270

274-
world.add(make_shared<bvh_node>(boxes1, 0, 1));
271+
world.add(make_shared<bvh_node>(boxes1));
275272

276273
auto light = make_shared<diffuse_light>(color(7, 7, 7));
277274
world.add(make_shared<xz_rect>(123, 423, 147, 412, 554, light));
278275

279276
auto center1 = point3(400, 400, 200);
280277
auto center2 = center1 + vec3(30,0,0);
281278
auto moving_sphere_material = make_shared<lambertian>(color(0.7, 0.3, 0.1));
282-
world.add(make_shared<moving_sphere>(center1, center2, 50, moving_sphere_material, 0, 1));
279+
world.add(make_shared<moving_sphere>(center1, center2, 50, moving_sphere_material));
283280

284281
world.add(make_shared<sphere>(point3(260, 150, 45), 50, make_shared<dielectric>(1.5)));
285282
world.add(make_shared<sphere>(
@@ -306,7 +303,7 @@ void final_scene(scene& scene_desc) {
306303

307304
world.add(make_shared<translate>(
308305
make_shared<rotate_y>(
309-
make_shared<bvh_node>(boxes2, 0.0, 1.0), 15),
306+
make_shared<bvh_node>(boxes2), 15),
310307
vec3(-100,270,395)
311308
)
312309
);
@@ -325,11 +322,8 @@ int main() {
325322
scene scene_desc;
326323

327324
scene_desc.background = color(0.70, 0.80, 1.00);
328-
329325
scene_desc.cam.vup = vec3(0,1,0);
330326
scene_desc.cam.focus_dist = 10.0;
331-
scene_desc.cam.time_start = 0;
332-
scene_desc.cam.time_end = 1;
333327

334328
switch (0) {
335329
case 1: random_spheres(scene_desc); break;

src/TheNextWeek/moving_sphere.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
class moving_sphere : public hittable {
2020
public:
2121
moving_sphere() {}
22-
moving_sphere(
23-
point3 ctr0, point3 ctr1, double r, shared_ptr<material> m,
24-
double time_start, double time_end)
25-
: center0(ctr0), center1(ctr1), radius(r), mat_ptr(m), time0(time_start), time1(time_end)
26-
{};
22+
moving_sphere(point3 c0, point3 c1, double r, shared_ptr<material> m)
23+
: center0(c0), center1(c1), center_vec(c1 - c0), radius(r), mat_ptr(m)
24+
{
25+
const auto rvec = vec3(radius, radius, radius);
26+
const aabb box0(center0 - rvec, center0 + rvec);
27+
const aabb box1(center1 - rvec, center1 + rvec);
28+
bounds = aabb(box0, box1);
29+
};
2730

2831
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
2932
vec3 oc = r.origin() - center(r.time());
@@ -52,26 +55,23 @@ class moving_sphere : public hittable {
5255
return true;
5356
}
5457

55-
bool bounding_box(double time_start, double time_end, aabb& output_box) const override {
56-
aabb box0(
57-
center(time_start) - vec3(radius, radius, radius),
58-
center(time_start) + vec3(radius, radius, radius));
59-
aabb box1(
60-
center(time_end) - vec3(radius, radius, radius),
61-
center(time_end) + vec3(radius, radius, radius));
62-
output_box = aabb(box0, box1);
58+
bool bounding_box(aabb& output_box) const override {
59+
output_box = bounds;
6360
return true;
6461
}
6562

6663
point3 center(double time) const {
67-
return center0 + ((time - time0) / (time1 - time0))*(center1 - center0);
64+
// Linearly interpolate from center0 to center1 according to time, where t=0 yields
65+
// center0, and t=1 yields center1.
66+
return center0 + time * center_vec;
6867
}
6968

7069
public:
7170
point3 center0, center1;
72-
double time0, time1;
71+
vec3 center_vec;
7372
double radius;
7473
shared_ptr<material> mat_ptr;
74+
aabb bounds;
7575
};
7676

7777

src/TheNextWeek/sphere.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
class sphere : public hittable {
2020
public:
2121
sphere() {}
22-
sphere(point3 ctr, double r, shared_ptr<material> m)
23-
: center(ctr), radius(r), mat_ptr(m) {};
22+
sphere(point3 ctr, double r, shared_ptr<material> m) : center(ctr), radius(r), mat_ptr(m) {
23+
const auto rvec = vec3(radius, radius, radius);
24+
bbox = aabb(center - rvec, center + rvec);
25+
};
2426

2527
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
2628
vec3 oc = r.origin() - center;
@@ -50,17 +52,16 @@ class sphere : public hittable {
5052
return true;
5153
}
5254

53-
bool bounding_box(double time_start, double time_end, aabb& output_box) const override {
54-
output_box = aabb(
55-
center - vec3(radius, radius, radius),
56-
center + vec3(radius, radius, radius));
55+
bool bounding_box(aabb& output_box) const override {
56+
output_box = bbox;
5757
return true;
5858
}
5959

6060
public:
6161
point3 center;
6262
double radius;
6363
shared_ptr<material> mat_ptr;
64+
aabb bbox;
6465

6566
private:
6667
static void get_sphere_uv(const point3& p, double& u, double& v) {

src/TheRestOfYourLife/aarect.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class xy_rect : public hittable {
4444
return true;
4545
}
4646

47-
bool bounding_box(double time_start, double time_end, aabb& output_box) const override {
47+
bool bounding_box(aabb& output_box) const override {
4848
// The bounding box must have non-zero width in each dimension, so pad the Z
4949
// dimension a small amount.
5050
output_box = aabb(point3(x0, y0, k-0.0001), point3(x1, y1, k+0.0001));
@@ -85,7 +85,7 @@ class xz_rect : public hittable {
8585
return true;
8686
}
8787

88-
bool bounding_box(double time_start, double time_end, aabb& output_box) const override {
88+
bool bounding_box(aabb& output_box) const override {
8989
// The bounding box must have non-zero width in each dimension, so pad the Y
9090
// dimension a small amount.
9191
output_box = aabb(point3(x0, k-0.0001, z0), point3(x1, k+0.0001, z1));
@@ -143,7 +143,7 @@ class yz_rect : public hittable {
143143
return true;
144144
}
145145

146-
bool bounding_box(double time_start, double time_end, aabb& output_box) const override {
146+
bool bounding_box(aabb& output_box) const override {
147147
// The bounding box must have non-zero width in each dimension, so pad the X
148148
// dimension a small amount.
149149
output_box = aabb(point3(k-0.0001, y0, z0), point3(k+0.0001, y1, z1));

src/TheRestOfYourLife/box.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class box : public hittable {
3939
return sides.hit(r, ray_t, rec);
4040
}
4141

42-
bool bounding_box(double time_start, double time_end, aabb& output_box) const override {
42+
bool bounding_box(aabb& output_box) const override {
4343
output_box = aabb(box_min, box_max);
4444
return true;
4545
}

src/TheRestOfYourLife/bvh.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ class bvh_node : public hittable {
6060

6161
aabb box_left, box_right;
6262

63-
if ( !left->bounding_box (time0, time1, box_left)
64-
|| !right->bounding_box(time0, time1, box_right)
65-
)
63+
if (!left->bounding_box(box_left) || !right->bounding_box(box_right))
6664
std::cerr << "No bounding box in bvh_node constructor.\n";
6765

6866
box = surrounding_box(box_left, box_right);
@@ -78,7 +76,7 @@ class bvh_node : public hittable {
7876
return hit_left || hit_right;
7977
}
8078

81-
bool bounding_box(double time0, double time1, aabb& output_box) const override {
79+
bool bounding_box(aabb& output_box) const override {
8280
output_box = box;
8381
return true;
8482
}
@@ -95,7 +93,7 @@ class bvh_node : public hittable {
9593
aabb box_a;
9694
aabb box_b;
9795

98-
if (!a->bounding_box(0,0, box_a) || !b->bounding_box(0,0, box_b))
96+
if (!a->bounding_box(box_a) || !b->bounding_box(box_b))
9997
std::cerr << "No bounding box in bvh_node constructor.\n";
10098

10199
return box_a.axis(axis_index).min < box_b.axis(axis_index).min;

0 commit comments

Comments
 (0)