Skip to content

Commit 81c5d5c

Browse files
committed
Updates for first PPM image
- Fix gradient computation, scale by width/height - 1 - Set blue component to 1/4, for easier understanding of output PPM - Set resolution to 256x256 for easier understanding of output PPM - Include updated image
1 parent 6d179e5 commit 81c5d5c

File tree

2 files changed

+38
-31
lines changed

2 files changed

+38
-31
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,21 @@
7676
#include <iostream>
7777

7878
int main() {
79-
const int image_width = 200;
80-
const int image_height = 100;
79+
const int image_width = 256;
80+
const int image_height = 256;
8181

8282
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
8383

8484
for (int j = image_height-1; j >= 0; --j) {
8585
for (int i = 0; i < image_width; ++i) {
86-
auto r = double(i) / image_width;
87-
auto g = double(j) / image_height;
88-
auto b = 0.2;
86+
auto r = double(i) / (image_width-1);
87+
auto g = double(j) / (image_height-1);
88+
auto b = 0.25;
89+
8990
int ir = static_cast<int>(255.999 * r);
9091
int ig = static_cast<int>(255.999 * g);
9192
int ib = static_cast<int>(255.999 * b);
93+
9294
std::cout << ir << ' ' << ig << ' ' << ib << '\n';
9395
}
9496
}
@@ -144,16 +146,19 @@
144146

145147
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
146148
P3
147-
200 100
149+
256 256
148150
255
149-
0 253 51
150-
1 253 51
151-
2 253 51
152-
3 253 51
153-
5 253 51
154-
6 253 51
155-
7 253 51
156-
8 253 51
151+
0 255 63
152+
1 255 63
153+
2 255 63
154+
3 255 63
155+
4 255 63
156+
5 255 63
157+
6 255 63
158+
7 255 63
159+
8 255 63
160+
9 255 63
161+
...
157162
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
158163
[Listing [first-img]: First image output]
159164
</div>
@@ -180,12 +185,14 @@
180185
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
181186
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
182187
for (int i = 0; i < image_width; ++i) {
183-
auto r = double(i) / image_width;
184-
auto g = double(j) / image_height;
185-
auto b = 0.2;
188+
auto r = double(i) / (image_width-1);
189+
auto g = double(j) / (image_height-1);
190+
auto b = 0.25;
191+
186192
int ir = static_cast<int>(255.999 * r);
187193
int ig = static_cast<int>(255.999 * g);
188194
int ib = static_cast<int>(255.999 * b);
195+
189196
std::cout << ir << ' ' << ig << ' ' << ib << '\n';
190197
}
191198
}
@@ -328,16 +335,16 @@
328335
#include <iostream>
329336

330337
int main() {
331-
const int image_width = 200;
332-
const int image_height = 100;
338+
const int image_width = 256;
339+
const int image_height = 256;
333340

334341
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
335342

336343
for (int j = image_height-1; j >= 0; --j) {
337344
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
338345
for (int i = 0; i < image_width; ++i) {
339346
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
340-
vec3 color(double(i)/image_width, double(j)/image_height, 0.2);
347+
vec3 color(double(i)/(image_width-1), double(j)/(image_height-1), 0.25);
341348
color.write_color(std::cout);
342349
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
343350
}
@@ -346,7 +353,7 @@
346353
std::cerr << "\nDone.\n";
347354
}
348355
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
349-
[Listing [main-gradient]: <kbd>[main.cc]</kbd> Creating a color gradient image]
356+
[Listing [ppm-2]: <kbd>[main.cc]</kbd> Final code for the first PPM image
350357
</div>
351358

352359

@@ -448,8 +455,8 @@
448455
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
449456
for (int i = 0; i < image_width; ++i) {
450457
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
451-
auto u = double(i) / image_width;
452-
auto v = double(j) / image_height;
458+
auto u = double(i) / (image_width-1);
459+
auto v = double(j) / (image_height-1);
453460
ray r(origin, lower_left_corner + u*horizontal + v*vertical);
454461
vec3 color = ray_color(r);
455462
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -1138,8 +1145,8 @@
11381145
for (int j = image_height-1; j >= 0; --j) {
11391146
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
11401147
for (int i = 0; i < image_width; ++i) {
1141-
auto u = double(i) / image_width;
1142-
auto v = double(j) / image_height;
1148+
auto u = double(i) / (image_width-1);
1149+
auto v = double(j) / (image_height-1);
11431150
ray r(origin, lower_left_corner + u*horizontal + v*vertical);
11441151

11451152

@@ -1326,8 +1333,8 @@
13261333
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
13271334
vec3 color(0, 0, 0);
13281335
for (int s = 0; s < samples_per_pixel; ++s) {
1329-
auto u = (i + random_double()) / image_width;
1330-
auto v = (j + random_double()) / image_height;
1336+
auto u = (i + random_double()) / (image_width-1);
1337+
auto v = (j + random_double()) / (image_height-1);
13311338
ray r = cam.get_ray(u, v);
13321339
color += ray_color(r, world);
13331340
}
@@ -1491,8 +1498,8 @@
14911498
for (int i = 0; i < image_width; ++i) {
14921499
vec3 color(0, 0, 0);
14931500
for (int s = 0; s < samples_per_pixel; ++s) {
1494-
auto u = (i + random_double()) / image_width;
1495-
auto v = (j + random_double()) / image_height;
1501+
auto u = (i + random_double()) / (image_width-1);
1502+
auto v = (j + random_double()) / (image_height-1);
14961503
ray r = cam.get_ray(u, v);
14971504
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
14981505
color += ray_color(r, world, max_depth);
@@ -2001,8 +2008,8 @@
20012008
for (int i = 0; i < image_width; ++i) {
20022009
vec3 color(0, 0, 0);
20032010
for (int s = 0; s < samples_per_pixel; ++s) {
2004-
auto u = (i + random_double()) / image_width;
2005-
auto v = (j + random_double()) / image_height;
2011+
auto u = (i + random_double()) / (image_width-1);
2012+
auto v = (j + random_double()) / (image_height-1);
20062013
ray r = cam.get_ray(u, v);
20072014
color += ray_color(r, world, max_depth);
20082015
}

images/img.first-ppm-image.png

268 Bytes
Loading

0 commit comments

Comments
 (0)