Skip to content

Commit 2108dec

Browse files
committed
hitvec oneWeekend: hittable_list uses vector
1 parent d3cf9db commit 2108dec

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

src/InOneWeekend/hittable_list.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,39 @@
1212
//==============================================================================================
1313

1414
#include "hittable.h"
15+
#include <vector>
1516

1617

1718
class hittable_list: public hittable {
1819
public:
1920
hittable_list() {}
20-
hittable_list(hittable **l, int n) { list = l; list_size = n; }
21+
hittable_list(hittable* object) { add(object); }
22+
23+
void clear() { objects.clear(); }
24+
void add(hittable* object) { objects.push_back(object); }
25+
2126
virtual bool hit(const ray& r, double tmin, double tmax, hit_record& rec) const;
22-
hittable **list;
23-
int list_size;
27+
28+
public:
29+
std::vector<hittable*> objects;
2430
};
2531

32+
2633
bool hittable_list::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
2734
hit_record temp_rec;
28-
bool hit_anything = false;
29-
double closest_so_far = t_max;
30-
for (int i = 0; i < list_size; i++) {
31-
if (list[i]->hit(r, t_min, closest_so_far, temp_rec)) {
35+
auto hit_anything = false;
36+
auto closest_so_far = t_max;
37+
38+
for (auto object : objects) {
39+
if (object->hit(r, t_min, closest_so_far, temp_rec)) {
3240
hit_anything = true;
3341
closest_so_far = temp_rec.t;
3442
rec = temp_rec;
3543
}
3644
}
45+
3746
return hit_anything;
3847
}
3948

49+
4050
#endif

src/InOneWeekend/main.cc

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414
#include "hittable_list.h"
1515
#include "material.h"
1616
#include "sphere.h"
17-
1817
#include <iostream>
1918

2019

21-
vec3 ray_color(const ray& r, hittable *world, int depth) {
20+
vec3 ray_color(const ray& r, hittable& world, int depth) {
2221
hit_record rec;
23-
if (world->hit(r, 0.001, infinity, rec)) {
24-
if (depth <= 0)
25-
return vec3(0,0,0);
22+
23+
// If we've exceeded the ray bounce limit, no more light is gathered.
24+
if (depth <= 0)
25+
return vec3(0,0,0);
26+
27+
if (world.hit(r, 0.001, infinity, rec)) {
2628
ray scattered;
2729
vec3 attenuation;
2830
if (rec.mat_ptr->scatter(r, rec, attenuation, scattered))
@@ -36,10 +38,11 @@ vec3 ray_color(const ray& r, hittable *world, int depth) {
3638
}
3739

3840

39-
hittable *random_scene() {
40-
size_t n = 500;
41-
hittable **list = new hittable*[n+1];
42-
list[0] = new sphere(vec3(0,-1000,0), 1000, new lambertian(vec3(0.5, 0.5, 0.5)));
41+
hittable_list random_scene() {
42+
hittable_list objects;
43+
44+
objects.add(new sphere(vec3(0,-1000,0), 1000, new lambertian(vec3(0.5, 0.5, 0.5))));
45+
4346
int i = 1;
4447
for (int a = -11; a < 11; a++) {
4548
for (int b = -11; b < 11; b++) {
@@ -49,25 +52,25 @@ hittable *random_scene() {
4952
if (choose_mat < 0.8) {
5053
// diffuse
5154
auto albedo = vec3::random() * vec3::random();
52-
list[i++] = new sphere(center, 0.2, new lambertian(albedo));
55+
objects.add(new sphere(center, 0.2, new lambertian(albedo)));
5356
} else if (choose_mat < 0.95) {
5457
// metal
5558
auto albedo = vec3::random(.5, 1);
5659
auto fuzz = random_double(0, .5);
57-
list[i++] = new sphere(center, 0.2, new metal(albedo, fuzz));
60+
objects.add(new sphere(center, 0.2, new metal(albedo, fuzz)));
5861
} else {
5962
// glass
60-
list[i++] = new sphere(center, 0.2, new dielectric(1.5));
63+
objects.add(new sphere(center, 0.2, new dielectric(1.5)));
6164
}
6265
}
6366
}
6467
}
6568

66-
list[i++] = new sphere(vec3(0, 1, 0), 1.0, new dielectric(1.5));
67-
list[i++] = new sphere(vec3(-4, 1, 0), 1.0, new lambertian(vec3(0.4, 0.2, 0.1)));
68-
list[i++] = new sphere(vec3(4, 1, 0), 1.0, new metal(vec3(0.7, 0.6, 0.5), 0.0));
69+
objects.add(new sphere(vec3(0, 1, 0), 1.0, new dielectric(1.5)));
70+
objects.add(new sphere(vec3(-4, 1, 0), 1.0, new lambertian(vec3(0.4, 0.2, 0.1))));
71+
objects.add(new sphere(vec3(4, 1, 0), 1.0, new metal(vec3(0.7, 0.6, 0.5), 0.0)));
6972

70-
return new hittable_list(list,i);
73+
return objects;
7174
}
7275

7376

@@ -79,7 +82,7 @@ int main() {
7982

8083
std::cout << "P3\n" << nx << ' ' << ny << "\n255\n";
8184

82-
hittable *world = random_scene();
85+
auto world = random_scene();
8386

8487
vec3 lookfrom(13,2,3);
8588
vec3 lookat(0,0,0);

0 commit comments

Comments
 (0)