Skip to content

Commit 65004c7

Browse files
committed
Changes made from book 2 image 8 - image 15
1 parent f61f152 commit 65004c7

15 files changed

+139
-17
lines changed

books/RayTracingTheNextWeek.html

Lines changed: 139 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@
708708
...
709709
virtual bool hit(const ray& r, double tmin, double tmax, hit_record& rec) const;
710710
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
711-
virtual bool sphere::bounding_box(double t0, double t1, aabb& output_box) const;
711+
virtual bool bounding_box(double t0, double t1, aabb& output_box) const;
712712
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
713713
...
714714
};
@@ -1536,6 +1536,8 @@
15361536
To make it smooth, we can linearly interpolate:
15371537

15381538
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1539+
1540+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
15391541
inline double trilinear_interp(double c[2][2][2], double u, double v, double w) {
15401542
auto accum = 0.0;
15411543
for (int i=0; i < 2; i++)
@@ -1548,13 +1550,17 @@
15481550
return accum;
15491551
}
15501552

1553+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1554+
15511555
class perlin {
15521556
public:
15531557
...
15541558
double noise(point3 vec3& p) const {
15551559
auto u = p.x() - floor(p.x());
15561560
auto v = p.y() - floor(p.y());
15571561
auto w = p.z() - floor(p.z());
1562+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1563+
15581564
int i = floor(p.x());
15591565
int j = floor(p.y());
15601566
int k = floor(p.z());
@@ -1570,6 +1576,7 @@
15701576
];
15711577

15721578
return trilinear_interp(c, u, v, w);
1579+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
15731580
}
15741581
...
15751582
}
@@ -1581,7 +1588,7 @@
15811588
And we get:
15821589

15831590
![Image 8: Perlin texture with trilinear interpolation
1584-
](../images/img-2.08-perlin-trilerp.jpg class=pixel)
1591+
](../images/img-2.08-perlin-trilerp.png class=pixel)
15851592

15861593
</div>
15871594

@@ -1620,7 +1627,7 @@
16201627
This gives a smoother looking image:
16211628

16221629
![Image 9: Perlin texture, trilinearly interpolated, smoothed
1623-
](../images/img-2.09-perlin-trilerp-smooth.jpg class=pixel)
1630+
](../images/img-2.09-perlin-trilerp-smooth.png class=pixel)
16241631

16251632
</div>
16261633

@@ -1634,7 +1641,9 @@
16341641
class noise_texture : public texture {
16351642
public:
16361643
noise_texture() {}
1644+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
16371645
noise_texture(double sc) : scale(sc) {}
1646+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
16381647

16391648
virtual color value(double u, double v, const point3& p) const {
16401649
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -1644,14 +1653,36 @@
16441653

16451654
public:
16461655
perlin noise;
1656+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
16471657
double scale;
1658+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
16481659
};
16491660
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16501661
[Listing [perlin-smoothed-2]: <kbd>[texture.h]</kbd> Perlin smoothed, higher frequency]
1662+
</div>
1663+
1664+
<div class='together'>
1665+
We then add that scale to the `two_perlin_spheres()` scene description::
1666+
1667+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1668+
hittable_list two_perlin_spheres() {
1669+
hittable_list objects;
1670+
1671+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1672+
auto pertext = make_shared<noise_texture>(4);
1673+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1674+
objects.add(make_shared<sphere>(point3(0,-1000,0), 1000, make_shared<lambertian>(pertext)));
1675+
objects.add(make_shared<sphere>(point3(0, 2, 0), 2, make_shared<lambertian>(pertext)));
1676+
1677+
return objects;
1678+
}
1679+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1680+
[Listing [scale-perlin]: <kbd>[main.cc]</kbd> Perlin-textured spheres with a scale to the noise]
1681+
</div>
16511682

16521683
which gives:
16531684

1654-
![Image 10: Perlin texture, higher frequency](../images/img-2.10-perlin-hifreq.jpg class=pixel)
1685+
![Image 10: Perlin texture, higher frequency](../images/img-2.10-perlin-hifreq.png class=pixel)
16551686

16561687
</div>
16571688

@@ -1669,10 +1700,13 @@
16691700
class perlin {
16701701
public:
16711702
perlin() {
1703+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
16721704
ranvec = new vec3[point_count];
1673-
1705+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
16741706
for (int i = 0; i < point_count; ++i) {
1707+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
16751708
ranvec[i] = unit_vector(vec3::random(-1,1));
1709+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
16761710
}
16771711

16781712
perm_x = perlin_generate_perm();
@@ -1681,14 +1715,19 @@
16811715
}
16821716

16831717
~perlin() {
1718+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
16841719
delete[] ranvec;
1720+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
16851721
delete[] perm_x;
16861722
delete[] perm_y;
16871723
delete[] perm_z;
16881724
}
16891725
...
16901726
private:
1727+
static const int point_count = 256;
1728+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
16911729
vec3* ranvec;
1730+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
16921731
int* perm_x;
16931732
int* perm_y;
16941733
int* perm_z;
@@ -1706,24 +1745,33 @@
17061745
public:
17071746
...
17081747
double noise(const point3& p) const {
1748+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
17091749
auto u = p.x() - floor(p.x());
17101750
auto v = p.y() - floor(p.y());
17111751
auto w = p.z() - floor(p.z());
1752+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
17121753
int i = floor(p.x());
17131754
int j = floor(p.y());
17141755
int k = floor(p.z());
1756+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
17151757
vec3 c[2][2][2];
1758+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
17161759

17171760
for (int di=0; di < 2; di++)
17181761
for (int dj=0; dj < 2; dj++)
17191762
for (int dk=0; dk < 2; dk++)
1763+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
17201764
c[di][dj][dk] = ranvec[
17211765
perm_x[(i+di) & 255] ^
17221766
perm_y[(j+dj) & 255] ^
17231767
perm_z[(k+dk) & 255]
17241768
];
1769+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1770+
17251771

1772+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
17261773
return perlin_interp(c, u, v, w);
1774+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
17271775
}
17281776
...
17291777
}
@@ -1739,7 +1787,7 @@
17391787
...
17401788
private:
17411789
...
1742-
inline double perlin_interp(vec3 c[2][2][2], double u, double v, double w) {
1790+
inline double perlin_interp(vec3 c[2][2][2], double u, double v, double w) const {
17431791
auto uu = u*u*(3-2*u);
17441792
auto vv = v*v*(3-2*v);
17451793
auto ww = w*w*(3-2*w);
@@ -1792,7 +1840,7 @@
17921840
This finally gives something more reasonable looking:
17931841

17941842
![Image 11: Perlin texture, shifted off integer values
1795-
](../images/img-2.11-perlin-shift.jpg class=pixel)
1843+
](../images/img-2.11-perlin-shift.png class=pixel)
17961844

17971845
</div>
17981846

@@ -1828,10 +1876,31 @@
18281876

18291877
Here `fabs()` is the absolute value function defined in `<cmath>`.
18301878

1879+
<div class='together'>
1880+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1881+
class noise_texture : public texture {
1882+
public:
1883+
noise_texture() {}
1884+
noise_texture(double sc) : scale(sc) {}
1885+
1886+
virtual color value(double u, double v, const point3& p) const {
1887+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1888+
return color(1,1,1) * noise.turb(scale * p);
1889+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1890+
}
1891+
1892+
public:
1893+
perlin noise;
1894+
double scale;
1895+
};
1896+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1897+
[Listing [noise-tex-2]: <kbd>[texture.h]</kbd> Noise texture with turbulence]
1898+
</div>
1899+
18311900
<div class='together'>
18321901
Used directly, turbulence gives a sort of camouflage netting appearance:
18331902

1834-
![Image 12: Perlin texture with turbulence](../images/img-2.12-perlin-turb.jpg class=pixel)
1903+
![Image 12: Perlin texture with turbulence](../images/img-2.12-perlin-turb.png class=pixel)
18351904

18361905
</div>
18371906

@@ -1862,11 +1931,11 @@
18621931
double scale;
18631932
};
18641933
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1865-
[Listing [noise-tex-2]: <kbd>[texture.h]</kbd> Noise texture with turbulence]
1934+
[Listing [noise-tex-3]: <kbd>[texture.h]</kbd> Noise texture with marbled texture]
18661935

18671936
Which yields:
18681937

1869-
![Image 13: Perlin noise, marbled texture](../images/img-2.13-perlin-marble.jpg class=pixel)
1938+
![Image 13: Perlin noise, marbled texture](../images/img-2.13-perlin-marble.png class=pixel)
18701939

18711940
</div>
18721941

@@ -1904,9 +1973,12 @@
19041973
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
19051974
#include "rtweekend.h"
19061975
#include "rtw_stb_image.h"
1976+
#include "perlin.h"
19071977

19081978
#include <iostream>
19091979

1980+
...
1981+
19101982
class image_texture : public texture {
19111983
public:
19121984
const static int bytes_per_pixel = 3;
@@ -1965,13 +2037,34 @@
19652037

19662038
<div class='together'>
19672039
The representation of a packed array in that order is pretty standard. Thankfully, the [stb_image][]
1968-
package makes that super simple -- just include the header `rtw_stb_image.h` (found in the project
1969-
source code at `src/common/rtw_stb_image.h`) in `main.h`:
2040+
package makes that super simple -- just write a header called `rtw_stb_image.h` that also deals with
2041+
some compiler warnings:
19702042

19712043
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1972-
#include "rtw_stb_image.h"
2044+
#ifndef RTWEEKEND_STB_IMAGE_H
2045+
#define RTWEEKEND_STB_IMAGE_H
2046+
2047+
// Disable pedantic warnings for this external library.
2048+
#ifdef _MSC_VER
2049+
// Microsoft Visual C++ Compiler
2050+
#pragma warning (push, 0)
2051+
#endif
2052+
2053+
#define STB_IMAGE_IMPLEMENTATION
2054+
#include "external/stb_image.h"
2055+
2056+
// Restore warning levels.
2057+
#ifdef _MSC_VER
2058+
// Microsoft Visual C++ Compiler
2059+
#pragma warning (pop)
2060+
#endif
2061+
2062+
#endif
19732063
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1974-
[Listing [incl-stb-img]: Including the STB image package]
2064+
[Listing [rtw-stb-image]: <kbd>[rtw_stb_image.h]</kbd> Include the stb_image package]
2065+
2066+
The above assumes that you have copied the `stb_image.h` into a folder called `export`. Adjust as per
2067+
your directory structure.
19752068
</div>
19762069

19772070

@@ -2003,10 +2096,39 @@
20032096
to the lambertian material, and lambertian doesn’t need to be aware of it.
20042097

20052098
<div class='together'>
2006-
To test this, assign it to a sphere, and then temporarily cripple the `ray_color()` function in main
2007-
to just return attenuation. You should get something like:
2099+
To test this, throw it into main:
2100+
2101+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2102+
int main() {
2103+
...
2104+
switch (0) {
2105+
...
2106+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ delete
2107+
default:
2108+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2109+
case 3:
2110+
...
2111+
2112+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2113+
default:
2114+
case 4:
2115+
world = earth();
2116+
background = color(0.70, 0.80, 1.00);
2117+
lookfrom = point3(13,2,3);
2118+
lookat = point3(0,0,0);
2119+
vfov = 20.0;
2120+
break;
2121+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2122+
}
2123+
...
2124+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2125+
[Listing [scene-perlin-view]: <kbd>[main.cc]</kbd> Viewing parameters]
2126+
2127+
If the photo comes back with a large cyan sphere in the middle, then stb_image failed to find your
2128+
Earth map photo. The program will look for the file in the same directory as the executable. Make
2129+
sure to copy the Earth into your build directory, or rewrite `earth()` to point somewhere else.
20082130

2009-
![Image 15: Earth-mapped sphere](../images/img-2.15-earth-sphere.jpg class=pixel)
2131+
![Image 15: Earth-mapped sphere](../images/img-2.15-earth-sphere.png class=pixel)
20102132

20112133
</div>
20122134

images/img-2.08-perlin-trilerp.jpg

-18.5 KB
Binary file not shown.

images/img-2.08-perlin-trilerp.png

120 KB
Loading
-19.3 KB
Binary file not shown.
123 KB
Loading

images/img-2.10-perlin-hifreq.jpg

-31 KB
Binary file not shown.

images/img-2.10-perlin-hifreq.png

142 KB
Loading

images/img-2.11-perlin-shift.jpg

-25.3 KB
Binary file not shown.

images/img-2.11-perlin-shift.png

118 KB
Loading

images/img-2.12-perlin-turb.jpg

-50.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)