Skip to content

Commit 2dc55ce

Browse files
committed
Update render loop with new write_color() function
1 parent 381b977 commit 2dc55ce

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,15 @@
405405

406406
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
407407

408-
for (int j = image_height-1; j >= 0; --j) {
409-
std::clog << "\rScanlines remaining: " << j << ' ' << std::flush;
410-
for (int i = 0; i < image_width; ++i) {
408+
// Scale factors to convert pixel location to the range [0,1].
409+
double xscale = (image_width < 2) ? 1.0 : (1.0 / (image_width - 1));
410+
double yscale = (image_height < 2) ? 1.0 : (1.0 / (image_height - 1));
411+
412+
for (int py = 0; py < image_height; ++py) {
413+
std::clog << "\rScanlines remaining: " << (image_height - py) << ' ' << std::flush;
414+
for (int px = 0; px < image_width; ++px) {
411415
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
412-
color pixel_color(double(i)/(image_width-1), double(j)/(image_height-1), 0.25);
416+
color pixel_color(xscale * px, yscale * py, 0.25);
413417
write_color(std::cout, pixel_color);
414418
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
415419
}

0 commit comments

Comments
 (0)