Skip to content

Commit 47a8035

Browse files
authored
Merge pull request #478 from RayTracing/tdb/refactor_book_1
Edits made for sections of book 1
2 parents a20c5d9 + 64bf8bc commit 47a8035

File tree

1 file changed

+52
-29
lines changed

1 file changed

+52
-29
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@
111111
later when we internally use high dynamic range, but before output we will tone map to the zero
112112
to one range, so this code won’t change.
113113

114-
4. Red goes from black to fully on from left to right, and green goes from black at the bottom to
115-
fully on at the top. Red and green together make yellow so we should expect the upper right
116-
corner to be yellow.
114+
4. Red goes from fully off (black) to fully on (bright red) from left to right, and green goes
115+
from black at the bottom to fully on at the top. Red and green together make yellow so we
116+
should expect the upper right corner to be yellow.
117117

118118

119119
Creating an Image File
@@ -231,6 +231,9 @@
231231
Here’s the top part of my `vec3` class:
232232

233233
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
234+
#ifndef VEC3_H
235+
#define VEC3_H
236+
234237
#include <iostream>
235238

236239
class vec3 {
@@ -286,8 +289,10 @@
286289
// Type aliases for vec3
287290
using point3 = vec3; // 3D point
288291
using color = vec3; // RGB color
292+
293+
#endif
289294
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
290-
[Listing [vec3-class]: <kbd>[vec3.h]</kbd> `vec3` class]
295+
[Listing [vec3-class]: <kbd>[vec3.h]</kbd> vec3 class]
291296
</div>
292297

293298
We use `double` here, but some ray tracers use `float`. Either one is fine -- follow your own
@@ -374,7 +379,7 @@
374379
std::cerr << "\nDone.\n";
375380
}
376381
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
377-
[Listing [ppm-2]: <kbd>[main.cc]</kbd> Final code for the first PPM image
382+
[Listing [ppm-2]: <kbd>[main.cc]</kbd> Final code for the first PPM image]
378383
</div>
379384

380385

@@ -765,7 +770,7 @@
765770
An Abstraction for Hittable Objects
766771
------------------------------------
767772
Now, how about several spheres? While it is tempting to have an array of spheres, a very clean
768-
solution is the make an “abstract class” for anything a ray might hit and make both a sphere and a
773+
solution is to make an “abstract class” for anything a ray might hit and make both a sphere and a
769774
list of spheres just something you can hit. What that class should be called is something of a
770775
quandary -- calling it an “object” would be good if not for “object oriented” programming. “Surface”
771776
is often used, with the weakness being maybe we will want volumes. “hittable” emphasizes the member
@@ -884,7 +889,7 @@
884889
If we decide to have the normals always point out, then we will need to determine which side the
885890
ray is on when we color it. We can figure this out by comparing the ray with the normal. If the ray
886891
and the normal face in the same direction, the ray is inside the object, if the ray and the normal
887-
face in the opposide direction, then the ray is outside the object. This can be determined by
892+
face in the opposite direction, then the ray is outside the object. This can be determined by
888893
taking the dot product of the two vectors, where if their dot is positive, the ray is inside the
889894
sphere.
890895

@@ -993,15 +998,16 @@
993998
return false;
994999
}
9951000
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
996-
[Listing [sphere-final]: <kbd>[sphere.h]</kbd> The `sphere` class with normal determination]
1001+
[Listing [sphere-final]: <kbd>[sphere.h]</kbd> The sphere class with normal determination]
9971002

9981003
</div>
9991004

10001005

10011006
A List of Hittable Objects
10021007
---------------------------
10031008
<div class='together'>
1004-
We add a list of objects:
1009+
We have a generic object called a `hittable` that the ray can intersect with. We now add a class
1010+
that stores a list of `hittable`s:
10051011

10061012
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
10071013
#ifndef HITTABLE_LIST_H
@@ -1069,7 +1075,7 @@
10691075
shared_ptr<vec3> vec3_ptr = make_shared<vec3>(1.414214, 2.718281, 1.618034);
10701076
shared_ptr<sphere> sphere_ptr = make_shared<sphere>(point3(0,0,0), 1.0);
10711077
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1072-
[Listing [shared-ptr]: An example allocation using `shared_ptr`]
1078+
[Listing [shared-ptr]: An example allocation using shared_ptr]
10731079

10741080
`make_shared<thing>(thing_constructor_params ...)` allocates a new instance of type `thing`, using
10751081
the constructor parameters. It returns a `shared_ptr<thing>`.
@@ -1082,7 +1088,7 @@
10821088
auto vec3_ptr = make_shared<vec3>(1.414214, 2.718281, 1.618034);
10831089
auto sphere_ptr = make_shared<sphere>(point3(0,0,0), 1.0);
10841090
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1085-
[Listing [shared-ptr-auto]: An example allocation using `shared_ptr` with `auto` type]
1091+
[Listing [shared-ptr-auto]: An example allocation using shared_ptr with auto type]
10861092

10871093
</div>
10881094

@@ -1217,7 +1223,7 @@
12171223
std::cerr << "\nDone.\n";
12181224
}
12191225
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1220-
[Listing [main-with-rtweekend-h]: <kbd>[main.cc]</kbd> desc]
1226+
[Listing [main-with-rtweekend-h]: <kbd>[main.cc]</kbd> The new main with hittables]
12211227
</div>
12221228

12231229
<div class='together'>
@@ -1248,7 +1254,7 @@
12481254
Some Random Number Utilities
12491255
-----------------------------
12501256
One thing we need is a random number generator that returns real random numbers. We need a function
1251-
that returns a canonical random number which by convention returns random real in the range
1257+
that returns a canonical random number which by convention returns a random real in the range
12521258
$0 ≤ r < 1$. The “less than” before the 1 is important as we will sometimes take advantage of that.
12531259

12541260
<div class='together'>
@@ -1290,7 +1296,7 @@
12901296
return rand_generator();
12911297
}
12921298
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1293-
[Listing [random-double-alt]: <kbd>[file]</kbd> random_double(), alternate implemenation]
1299+
[Listing [random-double-alt]: <kbd>[rtweekend.h]</kbd> random_double(), alternate implemenation]
12941300
</div>
12951301

12961302

@@ -1430,11 +1436,11 @@
14301436
====================================================================================================
14311437

14321438
Now that we have objects and multiple rays per pixel, we can make some realistic looking materials.
1433-
We’ll start with diffuse (matte) materials. One question is whether we can mix and match shapes and
1434-
materials (so we assign a sphere a material) or if it’s put together so the geometry and material
1435-
are tightly bound (that could be useful for procedural objects where the geometry and material are
1436-
linked). We’ll go with separate -- which is usual in most renderers -- but do be aware of the
1437-
limitation.
1439+
We’ll start with diffuse (matte) materials. One question is whether we mix and match geometry and
1440+
materials (so we can assign a material to multiple spheres, or vice versa) or if geometry and
1441+
material are tightly bound (that could be useful for procedural objects where the geometry and
1442+
material are linked). We’ll go with separate -- which is usual in most renderers -- but do be aware
1443+
of the limitation.
14381444

14391445
A Simple Diffuse Material
14401446
--------------------------
@@ -1488,7 +1494,7 @@
14881494
return vec3(random_double(min,max), random_double(min,max), random_double(min,max));
14891495
}
14901496
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1491-
[Listing [vec-rand-util]: <kbd>[vec3.h]</kbd> `vec3` random utility functions]
1497+
[Listing [vec-rand-util]: <kbd>[vec3.h]</kbd> vec3 random utility functions]
14921498

14931499
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
14941500
vec3 random_in_unit_sphere() {
@@ -1535,14 +1541,19 @@
15351541

15361542
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
15371543
color ray_color(const ray& r, const hittable& world, int depth) {
1544+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
15381545
hit_record rec;
15391546

1547+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
15401548
// If we've exceeded the ray bounce limit, no more light is gathered.
15411549
if (depth <= 0)
15421550
return color(0,0,0);
1551+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1552+
15431553

15441554
if (world.hit(r, 0, infinity, rec)) {
15451555
point3 target = rec.p + rec.normal + random_in_unit_sphere();
1556+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
15461557
return 0.5 * ray_color(ray(rec.p, target - rec.p), world, depth-1);
15471558
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
15481559
}
@@ -1664,10 +1675,9 @@
16641675
<div class='together'>
16651676
The rejection method presented here produces random points in the unit ball offset along the surface
16661677
normal. This corresponds to picking directions on the hemisphere with high probability close to the
1667-
normal, and a lower probability of scattering rays at grazing angles. The distribution present
1668-
scales by the $\cos^3 (\phi)$ where $\phi$ is the angle from the normal. This is useful since light
1669-
arriving at shallow angles spreads over a larger area, and thus has a lower contribution to the
1670-
final color.
1678+
normal, and a lower probability of scattering rays at grazing angles. This distribution scales by
1679+
the $\cos^3 (\phi)$ where $\phi$ is the angle from the normal. This is useful since light arriving
1680+
at shallow angles spreads over a larger area, and thus has a lower contribution to the final color.
16711681

16721682
However, we are interested in a Lambertian distribution, which has a distribution of $\cos (\phi)$.
16731683
True Lambertian has the probability higher for ray scattering close to the normal, but the
@@ -1837,12 +1847,17 @@
18371847
This suggests the abstract class:
18381848

18391849
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1850+
#ifndef MATERIAL_H
1851+
#define MATERIAL_H
1852+
18401853
class material {
18411854
public:
18421855
virtual bool scatter(
18431856
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered
18441857
) const = 0;
18451858
};
1859+
1860+
#endif
18461861
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18471862
[Listing [material-initial]: <kbd>[material.h]</kbd> The material class]
18481863
</div>
@@ -2453,8 +2468,8 @@
24532468
----------------------
24542469
<div class='together'>
24552470
Now real glass has reflectivity that varies with angle -- look at a window at a steep angle and it
2456-
becomes a mirror. There is a big ugly equation for that, but almost everybody uses a simple and
2457-
surprisingly simple polynomial approximation by Christophe Schlick:
2471+
becomes a mirror. There is a big ugly equation for that, but almost everybody uses a cheap and
2472+
surprisingly accurate polynomial approximation by Christophe Schlick:
24582473

24592474
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
24602475
double schlick(double cosine, double ref_idx) {
@@ -2634,13 +2649,14 @@
26342649

26352650
We can actually use any up vector we want, and simply project it onto this plane to get an up vector
26362651
for the camera. I use the common convention of naming a “view up” (_vup_) vector. A couple of cross
2637-
products, and we now have a complete orthonormal basis (u,v,w) to describe our camera’s orientation.
2652+
products, and we now have a complete orthonormal basis $(u,v,w)$ to describe our camera’s
2653+
orientation.
26382654

26392655
![Figure [cam-up]: Camera view up direction](../images/fig.cam-up.jpg)
26402656

26412657
Remember that `vup`, `v`, and `w` are all in the same plane. Note that, like before when our fixed
26422658
camera faced -Z, our arbitrary view camera faces -w. And keep in mind that we can -- but we don’t
2643-
have to -- use world up (0,1,0) to specify vup. This is convenient and will naturally keep your
2659+
have to -- use world up $(0,1,0)$ to specify vup. This is convenient and will naturally keep your
26442660
camera horizontally level until you decide to experiment with crazy camera angles.
26452661

26462662
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -2703,7 +2719,14 @@
27032719

27042720
</div>
27052721

2706-
And we can change field of view to get:
2722+
And we can change field of view:
2723+
2724+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2725+
camera cam(point3(-2,2,1), point3(0,0,-1), vup, 20, aspect_ratio);
2726+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2727+
[Listing [change-field-view]: <kbd>[main.cc]</kbd> Change field of view]
2728+
2729+
to get:
27072730

27082731
<div class="render">
27092732

0 commit comments

Comments
 (0)