Skip to content

Commit ee4e295

Browse files
authored
Merge pull request #487 from RayTracing/image-code-updates
Update image generation code
2 parents a645eab + d82cce7 commit ee4e295

File tree

6 files changed

+43
-41
lines changed

6 files changed

+43
-41
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,9 @@
466466
}
467467

468468
int main() {
469-
const int image_width = 768;
470-
const auto aspect_ratio = 9.0 / 16.0;
471-
const int image_height = static_cast<int>(aspect_ratio * image_width);
469+
const auto aspect_ratio = 16.0 / 9.0;
470+
const int image_width = 384;
471+
const int image_height = static_cast<int>(image_width / aspect_ratio);
472472

473473
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
474474

@@ -1180,8 +1180,9 @@
11801180
}
11811181

11821182
int main() {
1183-
const int image_width = 200;
1184-
const int image_height = 100;
1183+
const auto aspect_ratio = 16.0 / 9.0;
1184+
const int image_width = 384;
1185+
const int image_height = static_cast<int>(image_width / aspect_ratio);
11851186

11861187
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
11871188

@@ -1373,8 +1374,9 @@
13731374

13741375
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
13751376
int main() {
1376-
const int image_width = 200;
1377-
const int image_height = 100;
1377+
const auto aspect_ratio = 16.0 / 9.0;
1378+
const int image_width = 384;
1379+
const int image_height = static_cast<int>(image_width / aspect_ratio);
13781380
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
13791381
const int samples_per_pixel = 100;
13801382
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -1553,8 +1555,9 @@
15531555
...
15541556

15551557
int main() {
1556-
const int image_width = 200;
1557-
const int image_height = 100;
1558+
const auto aspect_ratio = 16.0 / 9.0;
1559+
const int image_width = 384;
1560+
const int image_height = static_cast<int>(image_width / aspect_ratio);
15581561
const int samples_per_pixel = 100;
15591562
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
15601563
const int max_depth = 50;
@@ -2076,8 +2079,9 @@
20762079

20772080
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
20782081
int main() {
2079-
const int image_width = 200;
2080-
const int image_height = 100;
2082+
const auto aspect_ratio = 16.0 / 9.0;
2083+
const int image_width = 384;
2084+
const int image_height = static_cast<int>(image_width / aspect_ratio);
20812085
const int samples_per_pixel = 100;
20822086
const int max_depth = 50;
20832087

@@ -2563,14 +2567,14 @@
25632567
public:
25642568
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
25652569
camera(
2566-
double vfov, // top to bottom, in degrees
2567-
double aspect
2570+
double vfov, // vertical field-of-view in degrees
2571+
double aspect_ratio
25682572
) {
25692573
origin = point3(0.0, 0.0, 0.0);
25702574

25712575
auto theta = degrees_to_radians(vfov);
25722576
auto half_height = tan(theta/2);
2573-
auto half_width = aspect * half_height;
2577+
auto half_width = aspect_ratio * half_height;
25742578

25752579
lower_left_corner = point3(-half_width, -half_height, -1.0);
25762580

@@ -2646,8 +2650,8 @@
26462650
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
26472651
point3 lookfrom, point3 lookat, vec3 vup,
26482652
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2649-
double vfov, // top to bottom, in degrees
2650-
double aspect
2653+
double vfov, // vertical field-of-view in degrees
2654+
double aspect_ratio
26512655
) {
26522656
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
26532657
origin = lookfrom;
@@ -2656,7 +2660,7 @@
26562660

26572661
auto theta = degrees_to_radians(vfov);
26582662
auto half_height = tan(theta/2);
2659-
auto half_width = aspect * half_height;
2663+
auto half_width = aspect_ratio * half_height;
26602664

26612665
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
26622666
w = unit_vector(lookfrom - lookat);
@@ -2687,8 +2691,6 @@
26872691
This allows us to change the viewpoint:
26882692

26892693
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2690-
const auto aspect_ratio = double(image_width) / image_height;
2691-
...
26922694
camera cam(point3(-2,2,1), point3(0,0,-1), vup, 90, aspect_ratio);
26932695
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26942696
[Listing [scene-free-view]: <kbd>[main.cc]</kbd> Scene with alternate viewpoint]
@@ -2781,17 +2783,17 @@
27812783
public:
27822784
camera(
27832785
point3 lookfrom, point3 lookat, vec3 vup,
2784-
double vfov, // top to bottom, in degrees
2786+
double vfov, // vertical field-of-view in degrees
27852787
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2786-
double aspect, double aperture, double focus_dist
2788+
double aspect_ratio, double aperture, double focus_dist
27872789
) {
27882790
origin = lookfrom;
27892791
lens_radius = aperture / 2;
27902792
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
27912793

27922794
auto theta = degrees_to_radians(vfov);
27932795
auto half_height = tan(theta/2);
2794-
auto half_width = aspect * half_height;
2796+
auto half_width = aspect_ratio * half_height;
27952797

27962798
w = unit_vector(lookfrom - lookat);
27972799
u = unit_vector(cross(vup, w));
@@ -2837,8 +2839,6 @@
28372839
Using a big aperture:
28382840

28392841
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2840-
const auto aspect_ratio = double(image_width) / image_height;
2841-
...
28422842
point3 lookfrom(3,3,2);
28432843
point3 lookat(0,0,-1);
28442844
vec3 vup(0,1,0);

books/RayTracingTheNextWeek.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,10 @@
109109
public:
110110
camera(
111111
point3 lookfrom, point3 lookat, vec3 vup,
112-
double vfov, // top to bottom, in degrees
112+
double vfov, // vertical field-of-view in degrees
113+
double aspect_ratio, double aperture, double focus_dist,
113114
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
114-
double aspect, double aperture, double focus_dist, double t0 = 0, double t1 = 0
115+
double t0 = 0, double t1 = 0
115116
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
116117
) {
117118
origin = lookfrom;

src/InOneWeekend/main.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ hittable_list random_scene() {
9090

9191

9292
int main() {
93+
const auto aspect_ratio = 16.0 / 9.0;
9394
const int image_width = 1200;
94-
const int image_height = 800;
95+
const int image_height = static_cast<int>(image_width / aspect_ratio);
9596
const int samples_per_pixel = 10;
9697
const int max_depth = 50;
97-
const auto aspect_ratio = double(image_width) / image_height;
9898

9999
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
100100

@@ -113,8 +113,8 @@ int main() {
113113
for (int i = 0; i < image_width; ++i) {
114114
color pixel_color;
115115
for (int s = 0; s < samples_per_pixel; ++s) {
116-
auto u = (i + random_double()) / image_width;
117-
auto v = (j + random_double()) / image_height;
116+
auto u = (i + random_double()) / (image_width-1);
117+
auto v = (j + random_double()) / (image_height-1);
118118
ray r = cam.get_ray(u, v);
119119
pixel_color += ray_color(r, world, max_depth);
120120
}

src/TheNextWeek/main.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,9 @@ hittable_list final_scene() {
339339

340340

341341
int main() {
342+
const auto aspect_ratio = 1.0 / 1.0;
342343
const int image_width = 600;
343-
const int image_height = 600;
344-
const auto aspect_ratio = double(image_width) / image_height;
344+
const int image_height = static_cast<int>(image_width / aspect_ratio);
345345

346346
hittable_list world;
347347

@@ -442,8 +442,8 @@ int main() {
442442
for (int i = 0; i < image_width; ++i) {
443443
color pixel_color;
444444
for (int s = 0; s < samples_per_pixel; ++s) {
445-
auto u = (i + random_double()) / image_width;
446-
auto v = (j + random_double()) / image_height;
445+
auto u = (i + random_double()) / (image_width-1);
446+
auto v = (j + random_double()) / (image_height-1);
447447
ray r = cam.get_ray(u, v);
448448
pixel_color += ray_color(r, background, world, max_depth);
449449
}

src/TheRestOfYourLife/main.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ hittable_list cornell_box(camera& cam, double aspect) {
100100

101101

102102
int main() {
103+
const auto aspect_ratio = 1.0 / 1.0;
103104
const int image_width = 600;
104-
const int image_height = 600;
105+
const int image_height = static_cast<int>(image_width / aspect_ratio);
105106
const int samples_per_pixel = 100;
106107
const int max_depth = 50;
107-
const auto aspect_ratio = double(image_width) / image_height;
108108

109109
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
110110

@@ -122,8 +122,8 @@ int main() {
122122
for (int i = 0; i < image_width; ++i) {
123123
color pixel_color;
124124
for (int s = 0; s < samples_per_pixel; ++s) {
125-
auto u = (i + random_double()) / image_width;
126-
auto v = (j + random_double()) / image_height;
125+
auto u = (i + random_double()) / (image_width-1);
126+
auto v = (j + random_double()) / (image_height-1);
127127
ray r = cam.get_ray(u, v);
128128
pixel_color += ray_color(r, background, world, lights, max_depth);
129129
}

src/common/camera.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ class camera {
2020

2121
camera(
2222
point3 lookfrom, point3 lookat, vec3 vup,
23-
double vfov, // top to bottom, in degrees
24-
double aspect, double aperture, double focus_dist, double t0 = 0, double t1 = 0
23+
double vfov, // vertical field-of-view in degrees
24+
double aspect_ratio, double aperture, double focus_dist,
25+
double t0 = 0, double t1 = 0
2526
) {
2627
origin = lookfrom;
2728
lens_radius = aperture / 2;
@@ -30,7 +31,7 @@ class camera {
3031

3132
auto theta = degrees_to_radians(vfov);
3233
auto half_height = tan(theta/2);
33-
auto half_width = aspect * half_height;
34+
auto half_width = aspect_ratio * half_height;
3435

3536
w = unit_vector(lookfrom - lookat);
3637
u = unit_vector(cross(vup, w));

0 commit comments

Comments
 (0)