Skip to content

Commit efd940d

Browse files
committed
Text updates for image scanning
- Update text for inverted image vertical axis - Update text around blue-to-white gradient compute - Add comment about hoisting invariant image-t
1 parent 5ed8817 commit efd940d

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,19 @@
501501
I will have the y-axis go up, and the x-axis to the right.
502502
In order to respect the convention of a right handed coordinate system, into the screen is the
503503
negative z-axis.
504+
505+
Now the inevitable tricky part.
506+
While our 3D space has the conventions above, this conflicts with the standard _image_ coordinates,
507+
where the Y axis is inverted.
508+
In image coordinates, Y=0 denotes the top of the image, and Y=1 denotes the bottom.
509+
510+
To help keep things straight, we'll compute two normalized (that is, in the range [0,1]) image
511+
coordinates: $s$ (horizontal) and $t$ (vertical).
512+
$s=0$ is image left, $s=1$ is image right, $t=0$ is image top, and $t=1$ is image bottom.
513+
That means that we get our vertical axis flip at the right point by computing $t = 1 - y$, where $y$
514+
is the vertical component of our 3D coordinate system, and $t$ is the flipped vertical component of
515+
our image coordinate system.
516+
504517
I will traverse the screen from the upper left hand corner, and use two offset vectors along the
505518
screen sides to move the ray endpoint across the screen.
506519
Note that I do not make the ray direction a unit length vector because I think not doing that makes
@@ -509,8 +522,7 @@
509522
![Figure [cam-geom]: Camera geometry](../images/fig-1.03-cam-geom.jpg)
510523

511524
<div class='together'>
512-
Below in code, the ray `r` goes to approximately the pixel centers (I won’t worry about exactness
513-
for now because we’ll add antialiasing later):
525+
Below in code, the ray `r` goes to the current image pixel:
514526

515527
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
516528
#include "color.h"
@@ -525,8 +537,8 @@
525537
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
526538
color ray_color(const ray& r) {
527539
vec3 unit_direction = unit_vector(r.direction());
528-
auto t = 0.5*(unit_direction.y() + 1.0);
529-
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
540+
auto a = 0.5*(unit_direction.y() + 1.0);
541+
return (1.0-a)*color(1.0, 1.0, 1.0) + a*color(0.5, 0.7, 1.0);
530542
}
531543
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
532544

@@ -580,17 +592,17 @@
580592
Because we're looking at the $y$ height after normalizing the vector, you'll notice a horizontal
581593
gradient to the color in addition to the vertical gradient.
582594

583-
I then did a standard graphics trick of scaling that to $0.0 ≤ t ≤ 1.0$.
584-
When $t = 1.0$ I want blue.
585-
When $t = 0.0$ I want white.
595+
I then did a standard graphics trick of scaling that to $0.0 ≤ a ≤ 1.0$.
596+
When $a = 1.0$, I want blue.
597+
When $a = 0.0$, I want white.
586598
In between, I want a blend.
587-
This forms a “linear blend”, or “linear interpolation”: commonly referred to as a _lerp_, between
588-
two things.
599+
This forms a “linear blend”, or “linear interpolation”.
600+
This is commonly referred to as a _lerp_ between two values.
589601
A lerp is always of the form
590602

591-
$$ \text{blendedValue} = (1-t)\cdot\text{startValue} + t\cdot\text{endValue}, $$
603+
$$ \text{blendedValue} = (1-a)\cdot\text{startValue} + a\cdot\text{endValue}, $$
592604

593-
with $t$ going from zero to one.
605+
with $a$ going from zero to one.
594606
In our case this produces:
595607

596608
![Image 2: A blue-to-white gradient depending on ray Y coordinate
@@ -697,8 +709,8 @@
697709
return color(1, 0, 0);
698710
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
699711
vec3 unit_direction = unit_vector(r.direction());
700-
auto t = 0.5*(unit_direction.y() + 1.0);
701-
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
712+
auto a = 0.5*(unit_direction.y() + 1.0);
713+
return (1.0-a)*color(1.0, 1.0, 1.0) + a*color(0.5, 0.7, 1.0);
702714
}
703715
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
704716
[Listing [main-red-sphere]: <kbd>[main.cc]</kbd> Rendering a red sphere]
@@ -772,9 +784,9 @@
772784
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
773785
vec3 unit_direction = unit_vector(r.direction());
774786
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
775-
t = 0.5*(unit_direction.y() + 1.0);
787+
a = 0.5*(unit_direction.y() + 1.0);
776788
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
777-
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
789+
return (1.0-a)*color(1.0, 1.0, 1.0) + a*color(0.5, 0.7, 1.0);
778790
}
779791
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
780792
[Listing [render-surface-normal]: <kbd>[main.cc]</kbd> Rendering surface normals on a sphere]
@@ -1230,9 +1242,9 @@
12301242
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
12311243
vec3 unit_direction = unit_vector(r.direction());
12321244
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1233-
auto t = 0.5*(unit_direction.y() + 1.0);
1245+
auto a = 0.5*(unit_direction.y() + 1.0);
12341246
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1235-
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
1247+
return (1.0-a)*color(1.0, 1.0, 1.0) + a*color(0.5, 0.7, 1.0);
12361248
}
12371249

12381250
int main() {
@@ -1562,8 +1574,9 @@
15621574
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15631575
[Listing [write-color-clamped]: <kbd>[color.h]</kbd> The multi-sample write_color() function]
15641576

1565-
<div class='together'>
1566-
Main is also changed:
1577+
Main is also changed.
1578+
Note that in the image scanning loops, we take the opportunity to hoist the computation of the image
1579+
$t$ coordinate out of the inner loop, since it is the same value for the entire scanline.
15671580

15681581
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
15691582
#include "rtweekend.h"
@@ -1630,7 +1643,6 @@
16301643
}
16311644
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16321645
[Listing [main-multi-sample]: <kbd>[main.cc]</kbd> Rendering with multi-sampled pixels]
1633-
</div>
16341646

16351647
Zooming into the image that is produced, we can see the difference in edge pixels.
16361648

@@ -1730,8 +1742,8 @@
17301742
}
17311743

17321744
vec3 unit_direction = unit_vector(r.direction());
1733-
auto t = 0.5*(unit_direction.y() + 1.0);
1734-
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
1745+
auto a = 0.5*(unit_direction.y() + 1.0);
1746+
return (1.0-a)*color(1.0, 1.0, 1.0) + a*color(0.5, 0.7, 1.0);
17351747
}
17361748
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17371749
[Listing [ray-color-random-unit]: <kbd>[main.cc]</kbd> ray_color() using a random ray direction]
@@ -1766,8 +1778,8 @@
17661778
}
17671779

17681780
vec3 unit_direction = unit_vector(r.direction());
1769-
auto t = 0.5*(unit_direction.y() + 1.0);
1770-
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
1781+
auto a = 0.5*(unit_direction.y() + 1.0);
1782+
return (1.0-a)*color(1.0, 1.0, 1.0) + a*color(0.5, 0.7, 1.0);
17711783
}
17721784

17731785
...
@@ -1891,8 +1903,8 @@
18911903
}
18921904

18931905
vec3 unit_direction = unit_vector(r.direction());
1894-
auto t = 0.5*(unit_direction.y() + 1.0);
1895-
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
1906+
auto a = 0.5*(unit_direction.y() + 1.0);
1907+
return (1.0-a)*color(1.0, 1.0, 1.0) + a*color(0.5, 0.7, 1.0);
18961908
}
18971909
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18981910
[Listing [reflect-tolerance]: <kbd>[main.cc]</kbd>
@@ -1953,8 +1965,8 @@
19531965
}
19541966

19551967
vec3 unit_direction = unit_vector(r.direction());
1956-
auto t = 0.5*(unit_direction.y() + 1.0);
1957-
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
1968+
auto a = 0.5*(unit_direction.y() + 1.0);
1969+
return (1.0-a)*color(1.0, 1.0, 1.0) + a*color(0.5, 0.7, 1.0);
19581970
}
19591971
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19601972
[Listing [ray-color-unit-sphere]: <kbd>[main.cc]</kbd> ray_color() with replacement diffuse]
@@ -2035,8 +2047,8 @@
20352047
}
20362048

20372049
vec3 unit_direction = unit_vector(r.direction());
2038-
auto t = 0.5*(unit_direction.y() + 1.0);
2039-
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
2050+
auto a = 0.5*(unit_direction.y() + 1.0);
2051+
return (1.0-a)*color(1.0, 1.0, 1.0) + a*color(0.5, 0.7, 1.0);
20402052
}
20412053
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20422054
[Listing [ray-color-hemisphere]: <kbd>[main.cc]</kbd> ray_color() with hemispherical scattering]
@@ -2327,8 +2339,8 @@
23272339
}
23282340

23292341
vec3 unit_direction = unit_vector(r.direction());
2330-
auto t = 0.5*(unit_direction.y() + 1.0);
2331-
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
2342+
auto a = 0.5*(unit_direction.y() + 1.0);
2343+
return (1.0-a)*color(1.0, 1.0, 1.0) + a*color(0.5, 0.7, 1.0);
23322344
}
23332345
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23342346
[Listing [ray-color-scatter]: <kbd>[main.cc]</kbd> Ray color with scattered reflectance]
@@ -2879,8 +2891,8 @@
28792891
}
28802892

28812893
vec3 unit_direction = unit_vector(r.direction());
2882-
auto t = 0.5*(unit_direction.y() + 1.0);
2883-
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
2894+
auto a = 0.5*(unit_direction.y() + 1.0);
2895+
return (1.0-a)*color(1.0, 1.0, 1.0) + a*color(0.5, 0.7, 1.0);
28842896
}
28852897
};
28862898

0 commit comments

Comments
 (0)