Skip to content

Commit fc908ec

Browse files
committed
Use shared_ptr for materials and objects
For src/inOneWeekend code only.
1 parent 0afae2e commit fc908ec

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

src/InOneWeekend/hittable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class material;
1919
struct hit_record {
2020
vec3 p;
2121
vec3 normal;
22-
material *mat_ptr;
22+
shared_ptr<material> mat_ptr;
2323
double t;
2424
bool front_face;
2525

src/InOneWeekend/hittable_list.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
class hittable_list: public hittable {
1919
public:
2020
hittable_list() {}
21-
hittable_list(hittable* object) { add(object); }
21+
hittable_list(shared_ptr<hittable> object) { add(object); }
2222

23-
void clear() { objects.clear(); }
24-
void add(hittable* object) { objects.push_back(object); }
23+
void clear() { objects.clear(); }
24+
void add(shared_ptr<hittable> object) { objects.push_back(object); }
2525

2626
virtual bool hit(const ray& r, double tmin, double tmax, hit_record& rec) const;
2727

2828
public:
29-
std::vector<hittable*> objects;
29+
std::vector<shared_ptr<hittable>> objects;
3030
};
3131

3232

src/InOneWeekend/main.cc

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ vec3 ray_color(const ray& r, hittable& world, int depth) {
4141
hittable_list random_scene() {
4242
hittable_list objects;
4343

44-
objects.add(new sphere(vec3(0,-1000,0), 1000, new lambertian(vec3(0.5, 0.5, 0.5))));
44+
objects.add(
45+
make_shared<sphere>(vec3(0,-1000,0), 1000, make_shared<lambertian>(vec3(0.5, 0.5, 0.5)))
46+
);
4547

4648
int i = 1;
4749
for (int a = -11; a < 11; a++) {
@@ -52,23 +54,28 @@ hittable_list random_scene() {
5254
if (choose_mat < 0.8) {
5355
// diffuse
5456
auto albedo = vec3::random() * vec3::random();
55-
objects.add(new sphere(center, 0.2, new lambertian(albedo)));
57+
objects.add(
58+
make_shared<sphere>(center, 0.2, make_shared<lambertian>(albedo)));
5659
} else if (choose_mat < 0.95) {
5760
// metal
5861
auto albedo = vec3::random(.5, 1);
5962
auto fuzz = random_double(0, .5);
60-
objects.add(new sphere(center, 0.2, new metal(albedo, fuzz)));
63+
objects.add(
64+
make_shared<sphere>(center, 0.2, make_shared<metal>(albedo, fuzz)));
6165
} else {
6266
// glass
63-
objects.add(new sphere(center, 0.2, new dielectric(1.5)));
67+
objects.add(make_shared<sphere>(center, 0.2, make_shared<dielectric>(1.5)));
6468
}
6569
}
6670
}
6771
}
6872

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)));
73+
objects.add(
74+
make_shared<sphere>(vec3(0, 1, 0), 1.0, make_shared<dielectric>(1.5)));
75+
objects.add(
76+
make_shared<sphere>(vec3(-4, 1, 0), 1.0, make_shared<lambertian>(vec3(0.4, 0.2, 0.1))));
77+
objects.add(
78+
make_shared<sphere>(vec3(4, 1, 0), 1.0, make_shared<metal>(vec3(0.7, 0.6, 0.5), 0.0)));
7279

7380
return objects;
7481
}

src/InOneWeekend/sphere.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@
1818
class sphere: public hittable {
1919
public:
2020
sphere() {}
21-
sphere(vec3 cen, double r, material *m) : center(cen), radius(r), mat_ptr(m) {};
21+
22+
sphere(vec3 cen, double r, shared_ptr<material> m)
23+
: center(cen), radius(r), mat_ptr(m) {};
24+
2225
virtual bool hit(const ray& r, double tmin, double tmax, hit_record& rec) const;
26+
27+
public:
2328
vec3 center;
2429
double radius;
25-
material *mat_ptr;
30+
shared_ptr<material> mat_ptr;
2631
};
2732

2833

src/common/rtweekend.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@
1212
#include <cstdlib>
1313
#include <limits>
1414
#include <cmath>
15+
#include <memory>
1516

1617

18+
// Usings
19+
20+
using std::shared_ptr;
21+
using std::make_shared;
22+
1723
// Constants
1824

1925
const double infinity = std::numeric_limits<double>::infinity();

0 commit comments

Comments
 (0)