Skip to content

Commit c273e38

Browse files
authored
Merge branch 'dev-minor' into subchapters
2 parents 6e5d86a + 64b0f76 commit c273e38

30 files changed

+656
-620
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ Change Log -- Ray Tracing in One Weekend
55
# v3.1.0 (in progress)
66

77
### Common
8+
- Change: Minor change to use new `point3` and `color` type aliases for `vec3` (#422)
89
- New: subchapters throughout all three books (#267)
10+
- Fix: Scattered improvements to the text.
911

1012
### _In One Weekend_
1113
- Fix: Update image and size for first PPM image
1214
- Fix: Update image and size for blue-to-white gradient image
1315
- Fix: Update image and size for simple red sphere render
1416
- Fix: Update image and size for sphere with normal-vector coloring
1517
- Fix: Improve image size and aspect ratio calculation to make size changes easier
18+
- Fix: Added `t` parameter back into `hit_record` at correct place
1619

1720

1821
----------------------------------------------------------------------------------------------------

books/RayTracingInOneWeekend.html

Lines changed: 146 additions & 137 deletions
Large diffs are not rendered by default.

books/RayTracingTheNextWeek.html

Lines changed: 147 additions & 144 deletions
Large diffs are not rendered by default.

books/RayTracingTheRestOfYourLife.html

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

src/InOneWeekend/hittable.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class material;
1818

1919
struct hit_record {
20-
vec3 p;
20+
point3 p;
2121
vec3 normal;
2222
shared_ptr<material> mat_ptr;
2323
double t;
@@ -29,6 +29,7 @@ struct hit_record {
2929
}
3030
};
3131

32+
3233
class hittable {
3334
public:
3435
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const = 0;

src/InOneWeekend/main.cc

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,49 @@
1919
#include <iostream>
2020

2121

22-
vec3 ray_color(const ray& r, const hittable& world, int depth) {
22+
color ray_color(const ray& r, const hittable& world, int depth) {
2323
hit_record rec;
2424

2525
// If we've exceeded the ray bounce limit, no more light is gathered.
2626
if (depth <= 0)
27-
return vec3(0,0,0);
27+
return color(0,0,0);
2828

2929
if (world.hit(r, 0.001, infinity, rec)) {
3030
ray scattered;
31-
vec3 attenuation;
31+
color attenuation;
3232
if (rec.mat_ptr->scatter(r, rec, attenuation, scattered))
3333
return attenuation * ray_color(scattered, world, depth-1);
34-
return vec3(0,0,0);
34+
return color(0,0,0);
3535
}
3636

3737
vec3 unit_direction = unit_vector(r.direction());
3838
auto t = 0.5*(unit_direction.y() + 1.0);
39-
return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
39+
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
4040
}
4141

4242

4343
hittable_list random_scene() {
4444
hittable_list world;
4545

4646
world.add(
47-
make_shared<sphere>(vec3(0,-1000,0), 1000, make_shared<lambertian>(vec3(0.5, 0.5, 0.5)))
47+
make_shared<sphere>(
48+
point3(0,-1000,0), 1000, make_shared<lambertian>(color(0.5, 0.5, 0.5)))
4849
);
4950

5051
int i = 1;
5152
for (int a = -11; a < 11; a++) {
5253
for (int b = -11; b < 11; b++) {
5354
auto choose_mat = random_double();
54-
vec3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());
55+
point3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());
5556
if ((center - vec3(4, 0.2, 0)).length() > 0.9) {
5657
if (choose_mat < 0.8) {
5758
// diffuse
58-
auto albedo = vec3::random() * vec3::random();
59+
auto albedo = color::random() * color::random();
5960
world.add(
6061
make_shared<sphere>(center, 0.2, make_shared<lambertian>(albedo)));
6162
} else if (choose_mat < 0.95) {
6263
// metal
63-
auto albedo = vec3::random(.5, 1);
64+
auto albedo = color::random(.5, 1);
6465
auto fuzz = random_double(0, .5);
6566
world.add(
6667
make_shared<sphere>(center, 0.2, make_shared<metal>(albedo, fuzz)));
@@ -73,11 +74,16 @@ hittable_list random_scene() {
7374
}
7475

7576
world.add(
76-
make_shared<sphere>(vec3(0, 1, 0), 1.0, make_shared<dielectric>(1.5)));
77+
make_shared<sphere>(
78+
point3(0, 1, 0), 1.0, make_shared<dielectric>(1.5)));
79+
7780
world.add(
78-
make_shared<sphere>(vec3(-4, 1, 0), 1.0, make_shared<lambertian>(vec3(0.4, 0.2, 0.1))));
81+
make_shared<sphere>(
82+
point3(-4, 1, 0), 1.0, make_shared<lambertian>(color(0.4, 0.2, 0.1))));
83+
7984
world.add(
80-
make_shared<sphere>(vec3(4, 1, 0), 1.0, make_shared<metal>(vec3(0.7, 0.6, 0.5), 0.0)));
85+
make_shared<sphere>(
86+
point3(4, 1, 0), 1.0, make_shared<metal>(color(0.7, 0.6, 0.5), 0.0)));
8187

8288
return world;
8389
}
@@ -94,8 +100,8 @@ int main() {
94100

95101
auto world = random_scene();
96102

97-
vec3 lookfrom(13,2,3);
98-
vec3 lookat(0,0,0);
103+
point3 lookfrom(13,2,3);
104+
point3 lookat(0,0,0);
99105
vec3 vup(0,1,0);
100106
auto dist_to_focus = 10.0;
101107
auto aperture = 0.1;
@@ -105,14 +111,14 @@ int main() {
105111
for (int j = image_height-1; j >= 0; --j) {
106112
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
107113
for (int i = 0; i < image_width; ++i) {
108-
vec3 color;
114+
color pixel_color;
109115
for (int s = 0; s < samples_per_pixel; ++s) {
110116
auto u = (i + random_double()) / image_width;
111117
auto v = (j + random_double()) / image_height;
112118
ray r = cam.get_ray(u, v);
113-
color += ray_color(r, world, max_depth);
119+
pixel_color += ray_color(r, world, max_depth);
114120
}
115-
color.write_color(std::cout, samples_per_pixel);
121+
pixel_color.write_color(std::cout, samples_per_pixel);
116122
}
117123
}
118124

src/InOneWeekend/material.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ double schlick(double cosine, double ref_idx) {
2727
class material {
2828
public:
2929
virtual bool scatter(
30-
const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered
30+
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered
3131
) const = 0;
3232
};
3333

@@ -37,9 +37,9 @@ class dielectric : public material {
3737
dielectric(double ri) : ref_idx(ri) {}
3838

3939
virtual bool scatter(
40-
const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered
40+
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered
4141
) const {
42-
attenuation = vec3(1.0, 1.0, 1.0);
42+
attenuation = color(1.0, 1.0, 1.0);
4343
double etai_over_etat = (rec.front_face) ? (1.0 / ref_idx) : (ref_idx);
4444

4545
vec3 unit_direction = unit_vector(r_in.direction());
@@ -71,10 +71,10 @@ class dielectric : public material {
7171

7272
class lambertian : public material {
7373
public:
74-
lambertian(const vec3& a) : albedo(a) {}
74+
lambertian(const color& a) : albedo(a) {}
7575

7676
virtual bool scatter(
77-
const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered
77+
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered
7878
) const {
7979
vec3 scatter_direction = rec.normal + random_unit_vector();
8080
scattered = ray(rec.p, scatter_direction);
@@ -83,16 +83,16 @@ class lambertian : public material {
8383
}
8484

8585
public:
86-
vec3 albedo;
86+
color albedo;
8787
};
8888

8989

9090
class metal : public material {
9191
public:
92-
metal(const vec3& a, double f) : albedo(a), fuzz(f < 1 ? f : 1) {}
92+
metal(const color& a, double f) : albedo(a), fuzz(f < 1 ? f : 1) {}
9393

9494
virtual bool scatter(
95-
const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered
95+
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered
9696
) const {
9797
vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal);
9898
scattered = ray(rec.p, reflected + fuzz*random_in_unit_sphere());
@@ -101,7 +101,7 @@ class metal : public material {
101101
}
102102

103103
public:
104-
vec3 albedo;
104+
color albedo;
105105
double fuzz;
106106
};
107107

src/InOneWeekend/sphere.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ class sphere: public hittable {
2020
public:
2121
sphere() {}
2222

23-
sphere(vec3 cen, double r, shared_ptr<material> m)
23+
sphere(point3 cen, double r, shared_ptr<material> m)
2424
: center(cen), radius(r), mat_ptr(m) {};
2525

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

2828
public:
29-
vec3 center;
29+
point3 center;
3030
double radius;
3131
shared_ptr<material> mat_ptr;
3232
};

src/TheNextWeek/aarect.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class xy_rect: public hittable {
2727
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
2828

2929
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
30-
output_box = aabb(vec3(x0,y0, k-0.0001), vec3(x1, y1, k+0.0001));
30+
output_box = aabb(point3(x0,y0, k-0.0001), point3(x1, y1, k+0.0001));
3131
return true;
3232
}
3333

@@ -47,7 +47,7 @@ class xz_rect: public hittable {
4747
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
4848

4949
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
50-
output_box = aabb(vec3(x0,k-0.0001,z0), vec3(x1, k+0.0001, z1));
50+
output_box = aabb(point3(x0,k-0.0001,z0), point3(x1, k+0.0001, z1));
5151
return true;
5252
}
5353

@@ -67,7 +67,7 @@ class yz_rect: public hittable {
6767
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
6868

6969
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
70-
output_box = aabb(vec3(k-0.0001, y0, z0), vec3(k+0.0001, y1, z1));
70+
output_box = aabb(point3(k-0.0001, y0, z0), point3(k+0.0001, y1, z1));
7171
return true;
7272
}
7373

@@ -89,7 +89,7 @@ bool xy_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const {
8989
rec.u = (x-x0)/(x1-x0);
9090
rec.v = (y-y0)/(y1-y0);
9191
rec.t = t;
92-
vec3 outward_normal = vec3(0, 0, 1);
92+
auto outward_normal = vec3(0, 0, 1);
9393
rec.set_face_normal(r, outward_normal);
9494
rec.mat_ptr = mp;
9595
rec.p = r.at(t);
@@ -110,7 +110,7 @@ bool xz_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const {
110110
rec.u = (x-x0)/(x1-x0);
111111
rec.v = (z-z0)/(z1-z0);
112112
rec.t = t;
113-
vec3 outward_normal = vec3(0, 1, 0);
113+
auto outward_normal = vec3(0, 1, 0);
114114
rec.set_face_normal(r, outward_normal);
115115
rec.mat_ptr = mp;
116116
rec.p = r.at(t);
@@ -131,7 +131,7 @@ bool yz_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const {
131131
rec.u = (y-y0)/(y1-y0);
132132
rec.v = (z-z0)/(z1-z0);
133133
rec.t = t;
134-
vec3 outward_normal = vec3(1, 0, 0);
134+
auto outward_normal = vec3(1, 0, 0);
135135
rec.set_face_normal(r, outward_normal);
136136
rec.mat_ptr = mp;
137137
rec.p = r.at(t);

src/TheNextWeek/box.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
class box: public hittable {
2121
public:
2222
box() {}
23-
box(const vec3& p0, const vec3& p1, shared_ptr<material> ptr);
23+
box(const point3& p0, const point3& p1, shared_ptr<material> ptr);
2424

2525
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
2626

@@ -30,13 +30,13 @@ class box: public hittable {
3030
}
3131

3232
public:
33-
vec3 box_min;
34-
vec3 box_max;
33+
point3 box_min;
34+
point3 box_max;
3535
hittable_list sides;
3636
};
3737

3838

39-
box::box(const vec3& p0, const vec3& p1, shared_ptr<material> ptr) {
39+
box::box(const point3& p0, const point3& p1, shared_ptr<material> ptr) {
4040
box_min = p0;
4141
box_max = p1;
4242

0 commit comments

Comments
 (0)