Skip to content

Commit 0baf3d6

Browse files
committed
Distribute src/common to individual book sources
With the exception of the invariant STB image library and associated wrapper, this change moves all of the files out from common to each book that requires each file. In addition, common headers that included changes from subsequent books have been reverted to their state at the end of each book. This change also fixes up some issues found during this exercise.
1 parent ab21898 commit 0baf3d6

24 files changed

+1077
-26
lines changed

CMakeLists.txt

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,66 @@ set ( CMAKE_CXX_STANDARD_REQUIRED ON )
1414
set ( CMAKE_CXX_EXTENSIONS OFF )
1515

1616
# Source
17-
set ( COMMON_ALL
18-
src/common/rtweekend.h
19-
src/common/color.h
20-
src/common/interval.h
21-
src/common/ray.h
22-
src/common/vec3.h
23-
)
2417

2518
set ( SOURCE_ONE_WEEKEND
26-
${COMMON_ALL}
2719
src/InOneWeekend/camera.h
20+
src/InOneWeekend/color.h
2821
src/InOneWeekend/hittable.h
2922
src/InOneWeekend/hittable_list.h
23+
src/InOneWeekend/interval.h
3024
src/InOneWeekend/material.h
25+
src/InOneWeekend/ray.h
26+
src/InOneWeekend/rtweekend.h
3127
src/InOneWeekend/sphere.h
28+
src/InOneWeekend/vec3.h
29+
3230
src/InOneWeekend/main.cc
3331
)
3432

3533
set ( SOURCE_NEXT_WEEK
36-
${COMMON_ALL}
37-
src/common/aabb.h
3834
src/common/external/stb_image.h
39-
src/common/perlin.h
4035
src/common/rtw_stb_image.h
41-
src/common/texture.h
36+
src/TheNextWeek/aabb.h
4237
src/TheNextWeek/bvh.h
4338
src/TheNextWeek/camera.h
39+
src/TheNextWeek/color.h
4440
src/TheNextWeek/constant_medium.h
4541
src/TheNextWeek/hittable.h
4642
src/TheNextWeek/hittable_list.h
43+
src/TheNextWeek/interval.h
4744
src/TheNextWeek/material.h
45+
src/TheNextWeek/perlin.h
4846
src/TheNextWeek/quad.h
47+
src/TheNextWeek/ray.h
48+
src/TheNextWeek/rtweekend.h
4949
src/TheNextWeek/sphere.h
50+
src/TheNextWeek/texture.h
51+
src/TheNextWeek/vec3.h
52+
5053
src/TheNextWeek/main.cc
5154
)
5255

5356
set ( SOURCE_REST_OF_YOUR_LIFE
54-
${COMMON_ALL}
55-
src/common/aabb.h
5657
src/common/external/stb_image.h
57-
src/common/perlin.h
5858
src/common/rtw_stb_image.h
59-
src/common/texture.h
60-
src/TheRestOfYourLife/bvh.h
59+
src/TheRestOfYourLife/aabb.h
6160
src/TheRestOfYourLife/camera.h
61+
src/TheRestOfYourLife/color.h
62+
src/TheRestOfYourLife/constant_medium.h
6263
src/TheRestOfYourLife/hittable.h
6364
src/TheRestOfYourLife/hittable_list.h
65+
src/TheRestOfYourLife/interval.h
6466
src/TheRestOfYourLife/material.h
6567
src/TheRestOfYourLife/onb.h
6668
src/TheRestOfYourLife/pdf.h
69+
src/TheRestOfYourLife/perlin.h
6770
src/TheRestOfYourLife/quad.h
71+
src/TheRestOfYourLife/ray.h
72+
src/TheRestOfYourLife/rtweekend.h
6873
src/TheRestOfYourLife/sphere.h
74+
src/TheRestOfYourLife/texture.h
75+
src/TheRestOfYourLife/vec3.h
76+
6977
src/TheRestOfYourLife/main.cc
7078
)
7179

books/RayTracingInOneWeekend.html

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,7 @@
467467
public:
468468
ray() {}
469469

470-
ray(const point3& origin, const vec3& direction)
471-
: orig(origin), dir(direction)
472-
{}
470+
ray(const point3& origin, const vec3& direction) : orig(origin), dir(direction) {}
473471

474472
point3 origin() const { return orig; }
475473
vec3 direction() const { return dir; }
@@ -2172,9 +2170,9 @@
21722170

21732171
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
21742172
// Apply a linear to gamma transform for gamma 2
2175-
r = linear_to_gamma(r)
2176-
g = linear_to_gamma(g)
2177-
b = linear_to_gamma(b)
2173+
r = linear_to_gamma(r);
2174+
g = linear_to_gamma(g);
2175+
b = linear_to_gamma(b);
21782176
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
21792177

21802178
// Write the translated [0,255] value of each color component.

books/RayTracingTheRestOfYourLife.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3668,11 +3668,10 @@
36683668

36693669

36703670
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
3671-
// Replace NaN components with zero. See explanation in Ray Tracing: The Rest of Your Life.
3671+
// Replace NaN components with zero.
36723672
if (r != r) r = 0.0;
36733673
if (g != g) g = 0.0;
36743674
if (b != b) b = 0.0;
3675-
36763675
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
36773676

36783677
// Divide the color by the number of samples and gamma-correct for gamma=2.0.

src/InOneWeekend/color.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef COLOR_H
2+
#define COLOR_H
3+
//==============================================================================================
4+
// Originally written in 2020 by Peter Shirley <[email protected]>
5+
//
6+
// To the extent possible under law, the author(s) have dedicated all copyright and related and
7+
// neighboring rights to this software to the public domain worldwide. This software is
8+
// distributed without any warranty.
9+
//
10+
// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication
11+
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
12+
//==============================================================================================
13+
14+
#include "vec3.h"
15+
16+
#include <iostream>
17+
18+
using color = vec3;
19+
20+
inline double linear_to_gamma(double linear_component)
21+
{
22+
return sqrt(linear_component);
23+
}
24+
25+
void write_color(std::ostream &out, color pixel_color, int samples_per_pixel) {
26+
auto r = pixel_color.x();
27+
auto g = pixel_color.y();
28+
auto b = pixel_color.z();
29+
30+
// Divide the color by the number of samples.
31+
auto scale = 1.0 / samples_per_pixel;
32+
r *= scale;
33+
g *= scale;
34+
b *= scale;
35+
36+
// Apply a linear to gamma transform for gamma 2
37+
r = linear_to_gamma(r);
38+
g = linear_to_gamma(g);
39+
b = linear_to_gamma(b);
40+
41+
// Write the translated [0,255] value of each color component.
42+
static const interval intensity(0.000, 0.999);
43+
out << static_cast<int>(256 * intensity.clamp(r)) << ' '
44+
<< static_cast<int>(256 * intensity.clamp(g)) << ' '
45+
<< static_cast<int>(256 * intensity.clamp(b)) << '\n';
46+
}
47+
48+
49+
#endif

src/InOneWeekend/interval.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef INTERVAL_H
2+
#define INTERVAL_H
3+
//==============================================================================================
4+
// To the extent possible under law, the author(s) have dedicated all copyright and related and
5+
// neighboring rights to this software to the public domain worldwide. This software is
6+
// distributed without any warranty.
7+
//
8+
// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication
9+
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
10+
//==============================================================================================
11+
12+
class interval {
13+
public:
14+
double min, max;
15+
16+
interval() : min(+infinity), max(-infinity) {} // Default interval is empty
17+
18+
interval(double _min, double _max) : min(_min), max(_max) {}
19+
20+
double size() const {
21+
return max - min;
22+
}
23+
24+
interval expand(double delta) const {
25+
auto padding = delta/2;
26+
return interval(min - padding, max + padding);
27+
}
28+
29+
bool contains(double x) const {
30+
return min <= x && x <= max;
31+
}
32+
33+
bool surrounds(double x) const {
34+
return min < x && x < max;
35+
}
36+
37+
double clamp(double x) const {
38+
if (x < min) return min;
39+
if (x > max) return max;
40+
return x;
41+
}
42+
43+
static const interval empty, universe;
44+
};
45+
46+
const interval interval::empty = interval(+infinity, -infinity);
47+
const interval interval::universe = interval(-infinity, +infinity);
48+
49+
50+
#endif

src/InOneWeekend/ray.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef RAY_H
2+
#define RAY_H
3+
//==============================================================================================
4+
// Originally written in 2016 by Peter Shirley <[email protected]>
5+
//
6+
// To the extent possible under law, the author(s) have dedicated all copyright and related and
7+
// neighboring rights to this software to the public domain worldwide. This software is
8+
// distributed without any warranty.
9+
//
10+
// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication
11+
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
12+
//==============================================================================================
13+
14+
#include "vec3.h"
15+
16+
17+
class ray {
18+
public:
19+
ray() {}
20+
21+
ray(const point3& origin, const vec3& direction) : orig(origin), dir(direction) {}
22+
23+
point3 origin() const { return orig; }
24+
vec3 direction() const { return dir; }
25+
26+
point3 at(double t) const {
27+
return orig + t*dir;
28+
}
29+
30+
private:
31+
point3 orig;
32+
vec3 dir;
33+
};
34+
35+
#endif

src/InOneWeekend/rtweekend.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#ifndef RTWEEKEND_H
2+
#define RTWEEKEND_H
3+
//==============================================================================================
4+
// To the extent possible under law, the author(s) have dedicated all copyright and related and
5+
// neighboring rights to this software to the public domain worldwide. This software is
6+
// distributed without any warranty.
7+
//
8+
// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication
9+
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
10+
//==============================================================================================
11+
12+
#include <cmath>
13+
#include <cstdlib>
14+
#include <limits>
15+
#include <memory>
16+
17+
18+
// Usings
19+
20+
using std::shared_ptr;
21+
using std::make_shared;
22+
using std::sqrt;
23+
24+
// Constants
25+
26+
const double infinity = std::numeric_limits<double>::infinity();
27+
const double pi = 3.1415926535897932385;
28+
29+
// Utility Functions
30+
31+
inline double degrees_to_radians(double degrees) {
32+
return degrees * pi / 180.0;
33+
}
34+
35+
inline double random_double() {
36+
// Returns a random real in [0,1).
37+
return rand() / (RAND_MAX + 1.0);
38+
}
39+
40+
inline double random_double(double min, double max) {
41+
// Returns a random real in [min,max).
42+
return min + (max-min)*random_double();
43+
}
44+
45+
// Common Headers
46+
47+
#include "interval.h"
48+
#include "ray.h"
49+
#include "vec3.h"
50+
51+
52+
#endif

0 commit comments

Comments
 (0)