Skip to content

Commit 1db47f9

Browse files
committed
Update book2 to shared_ptr
1 parent 391e3ce commit 1db47f9

File tree

9 files changed

+312
-195
lines changed

9 files changed

+312
-195
lines changed

books/RayTracingTheNextWeek.html

Lines changed: 265 additions & 184 deletions
Large diffs are not rendered by default.

src/TheNextWeek/aarect.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class xy_rect: public hittable {
3030
return true;
3131
}
3232

33+
public:
3334
shared_ptr<material> mp;
3435
double x0, x1, y0, y1, k;
3536
};
@@ -49,6 +50,7 @@ class xz_rect: public hittable {
4950
return true;
5051
}
5152

53+
public:
5254
shared_ptr<material> mp;
5355
double x0, x1, z0, z1, k;
5456
};
@@ -68,6 +70,7 @@ class yz_rect: public hittable {
6870
return true;
6971
}
7072

73+
public:
7174
shared_ptr<material> mp;
7275
double y0, y1, z0, z1, k;
7376
};

src/TheNextWeek/constant_medium.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class constant_medium : public hittable {
3131
return boundary->bounding_box(t0, t1, output_box);
3232
}
3333

34+
public:
3435
shared_ptr<hittable> boundary;
3536
double density;
3637
shared_ptr<material> phase_function;

src/TheNextWeek/hittable.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ struct hit_record {
4040
}
4141
};
4242

43+
4344
class hittable {
4445
public:
4546
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const = 0;
4647
virtual bool bounding_box(double t0, double t1, aabb& output_box) const = 0;
4748
};
4849

50+
4951
class flip_face : public hittable {
5052
public:
5153
flip_face(shared_ptr<hittable> p) : ptr(p) {}
@@ -67,10 +69,12 @@ class flip_face : public hittable {
6769
shared_ptr<hittable> ptr;
6870
};
6971

72+
7073
class translate : public hittable {
7174
public:
7275
translate(shared_ptr<hittable> p, const vec3& displacement)
7376
: ptr(p), offset(displacement) {}
77+
7478
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const;
7579
virtual bool bounding_box(double t0, double t1, aabb& output_box) const;
7680

@@ -79,6 +83,7 @@ class translate : public hittable {
7983
vec3 offset;
8084
};
8185

86+
8287
bool translate::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
8388
ray moved_r(r.origin() - offset, r.direction(), r.time());
8489
if (ptr->hit(moved_r, t_min, t_max, rec)) {
@@ -90,6 +95,7 @@ bool translate::hit(const ray& r, double t_min, double t_max, hit_record& rec) c
9095
return false;
9196
}
9297

98+
9399
bool translate::bounding_box(double t0, double t1, aabb& output_box) const {
94100
if (ptr->bounding_box(t0, t1, output_box)) {
95101
output_box = aabb(
@@ -101,22 +107,26 @@ bool translate::bounding_box(double t0, double t1, aabb& output_box) const {
101107
return false;
102108
}
103109

110+
104111
class rotate_y : public hittable {
105112
public:
106113
rotate_y(shared_ptr<hittable> p, double angle);
114+
107115
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const;
108116
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
109117
output_box = bbox;
110118
return hasbox;
111119
}
112120

121+
public:
113122
shared_ptr<hittable> ptr;
114123
double sin_theta;
115124
double cos_theta;
116125
bool hasbox;
117126
aabb bbox;
118127
};
119128

129+
120130
rotate_y::rotate_y(shared_ptr<hittable> p, double angle) : ptr(p) {
121131
auto radians = degrees_to_radians(angle);
122132
sin_theta = sin(radians);
@@ -145,6 +155,7 @@ rotate_y::rotate_y(shared_ptr<hittable> p, double angle) : ptr(p) {
145155
bbox = aabb(min, max);
146156
}
147157

158+
148159
bool rotate_y::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
149160
vec3 origin = r.origin();
150161
vec3 direction = r.direction();

src/TheNextWeek/main.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ vec3 ray_color(const ray& r, hittable& world, int depth) {
4646

4747

4848
hittable_list random_scene() {
49-
hittable_list objects;
49+
hittable_list world;
5050

5151
auto checker = make_shared<checker_texture>(
5252
make_shared<constant_texture>(vec3(0.2, 0.3, 0.1)),
5353
make_shared<constant_texture>(vec3(0.9, 0.9, 0.9))
5454
);
5555

56-
objects.add(make_shared<sphere>(vec3(0,-1000,0), 1000, make_shared<lambertian>(checker)));
56+
world.add(make_shared<sphere>(vec3(0,-1000,0), 1000, make_shared<lambertian>(checker)));
5757

5858
for (int a = -10; a < 10; a++) {
5959
for (int b = -10; b < 10; b++) {
@@ -63,33 +63,33 @@ hittable_list random_scene() {
6363
if (choose_mat < 0.8) {
6464
// diffuse
6565
auto albedo = vec3::random() * vec3::random();
66-
objects.add(make_shared<moving_sphere>(
66+
world.add(make_shared<moving_sphere>(
6767
center, center + vec3(0, random_double(0,.5), 0), 0.0, 1.0, 0.2,
6868
make_shared<lambertian>(make_shared<constant_texture>(albedo))
6969
));
7070
} else if (choose_mat < 0.95) {
7171
// metal
7272
auto albedo = vec3::random(.5, 1);
7373
auto fuzz = random_double(0, .5);
74-
objects.add(
74+
world.add(
7575
make_shared<sphere>(center, 0.2, make_shared<metal>(albedo, fuzz)));
7676
} else {
7777
// glass
78-
objects.add(make_shared<sphere>(center, 0.2, make_shared<dielectric>(1.5)));
78+
world.add(make_shared<sphere>(center, 0.2, make_shared<dielectric>(1.5)));
7979
}
8080
}
8181
}
8282
}
8383

84-
objects.add(make_shared<sphere>(vec3(0, 1, 0), 1.0, make_shared<dielectric>(1.5)));
85-
objects.add(make_shared<sphere>(
84+
world.add(make_shared<sphere>(vec3(0, 1, 0), 1.0, make_shared<dielectric>(1.5)));
85+
world.add(make_shared<sphere>(
8686
vec3(-4, 1, 0), 1.0,
8787
make_shared<lambertian>(make_shared<constant_texture>(vec3(0.4, 0.2, 0.1)))
8888
));
89-
objects.add(
89+
world.add(
9090
make_shared<sphere>(vec3(4, 1, 0), 1.0, make_shared<metal>(vec3(0.7, 0.6, 0.5), 0.0)));
9191

92-
return hittable_list(make_shared<bvh_node>(objects, 0.0, 1.0));
92+
return hittable_list(make_shared<bvh_node>(world, 0.0, 1.0));
9393
}
9494

9595

@@ -121,7 +121,7 @@ hittable_list two_perlin_spheres() {
121121

122122
hittable_list earth() {
123123
int nx, ny, nn;
124-
unsigned char *texture_data = stbi_load("earthmap.jpg", &nx, &ny, &nn, 0);
124+
unsigned char* texture_data = stbi_load("earthmap.jpg", &nx, &ny, &nn, 0);
125125

126126
auto earth_surface =
127127
make_shared<lambertian>(make_shared<image_texture>(texture_data, nx, ny));

src/TheNextWeek/material.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class dielectric : public material {
6767
return true;
6868
}
6969

70+
public:
7071
double ref_idx;
7172
};
7273

@@ -85,6 +86,7 @@ class diffuse_light : public material {
8586
return emit->value(u, v, p);
8687
}
8788

89+
public:
8890
shared_ptr<texture> emit;
8991
};
9092

@@ -101,6 +103,7 @@ class isotropic : public material {
101103
return true;
102104
}
103105

106+
public:
104107
shared_ptr<texture> albedo;
105108
};
106109

@@ -118,6 +121,7 @@ class lambertian : public material {
118121
return true;
119122
}
120123

124+
public:
121125
shared_ptr<texture> albedo;
122126
};
123127

@@ -135,6 +139,7 @@ class metal : public material {
135139
return (dot(scattered.direction(), rec.normal) > 0);
136140
}
137141

142+
public:
138143
vec3 albedo;
139144
double fuzz;
140145
};

src/TheNextWeek/moving_sphere.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "hittable.h"
1616

1717

18-
class moving_sphere: public hittable {
18+
class moving_sphere : public hittable {
1919
public:
2020
moving_sphere() {}
2121
moving_sphere(
@@ -28,16 +28,19 @@ class moving_sphere: public hittable {
2828

2929
vec3 center(double time) const;
3030

31+
public:
3132
vec3 center0, center1;
3233
double time0, time1;
3334
double radius;
3435
shared_ptr<material> mat_ptr;
3536
};
3637

38+
3739
vec3 moving_sphere::center(double time) const{
3840
return center0 + ((time - time0) / (time1 - time0))*(center1 - center0);
3941
}
4042

43+
4144
bool moving_sphere::bounding_box(double t0, double t1, aabb& output_box) const {
4245
aabb box0(
4346
center(t0) - vec3(radius, radius, radius),

src/common/perlin.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ inline double perlin_interp(vec3 c[2][2][2], double u, double v, double w) {
3030
return accum;
3131
}
3232

33+
3334
class perlin {
3435
public:
3536
double noise(const vec3& p) const {
@@ -48,6 +49,7 @@ class perlin {
4849
];
4950
return perlin_interp(c, u, v, w);
5051
}
52+
5153
double turb(const vec3& p, int depth=7) const {
5254
auto accum = 0.0;
5355
vec3 temp_p = p;
@@ -59,12 +61,15 @@ class perlin {
5961
}
6062
return fabs(accum);
6163
}
64+
65+
public:
6266
static vec3 *ranvec;
6367
static int *perm_x;
6468
static int *perm_y;
6569
static int *perm_z;
6670
};
6771

72+
6873
static vec3* perlin_generate() {
6974
vec3 *p = new vec3[256];
7075
for (int i = 0; i < 256; ++i) {
@@ -73,6 +78,7 @@ static vec3* perlin_generate() {
7378
return p;
7479
}
7580

81+
7682
void permute(int *p, int n) {
7783
for (int i = n-1; i > 0; i--) {
7884
int target = random_int(0,i);
@@ -83,6 +89,7 @@ void permute(int *p, int n) {
8389
return;
8490
}
8591

92+
8693
static int* perlin_generate_perm() {
8794
int * p = new int[256];
8895
for (int i = 0; i < 256; i++)
@@ -91,9 +98,11 @@ static int* perlin_generate_perm() {
9198
return p;
9299
}
93100

101+
94102
vec3 *perlin::ranvec = perlin_generate();
95103
int *perlin::perm_x = perlin_generate_perm();
96104
int *perlin::perm_y = perlin_generate_perm();
97105
int *perlin::perm_z = perlin_generate_perm();
98106

107+
99108
#endif

src/common/texture.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class constant_texture : public texture {
3030
return color;
3131
}
3232

33+
public:
3334
vec3 color;
3435
};
3536

@@ -47,6 +48,7 @@ class checker_texture : public texture {
4748
return even->value(u, v, p);
4849
}
4950

51+
public:
5052
shared_ptr<texture> odd;
5153
shared_ptr<texture> even;
5254
};
@@ -63,6 +65,7 @@ class noise_texture : public texture {
6365
return vec3(1,1,1)*0.5*(1 + sin(scale*p.z() + 10*noise.turb(p)));
6466
}
6567

68+
public:
6669
perlin noise;
6770
double scale;
6871
};
@@ -72,6 +75,7 @@ class image_texture : public texture {
7275
public:
7376
image_texture() {}
7477
image_texture(unsigned char *pixels, int A, int B) : data(pixels), nx(A), ny(B) {}
78+
7579
~image_texture() {
7680
delete data;
7781
}

0 commit comments

Comments
 (0)