Graphics Reference
In-Depth Information
Unless there is an explicit reason to do otherwise, images are stored in row-major
order, where the element corresponding to 2D position ( x , y ) is stored at index
(x + y * width) in the array.
Listing 15.3: Rasterization pseudocode; O denotes the origin, or eyepoint.
( x , y ) :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for each pixel position
closest [ x , y ]=
for each triangle T :
for each pixel position ( x , y ) :
let R be the ray through
) from the eye
let P be the intersection of R and T
if P exists:
sum =0
for each direction:
sum += ...
if the distance to P is less than closest [ x , y ] :
pixel [ x , y ]= sum
closest [ x , y ]= | P O |
(
x , y
Rasterization allows us to process each triangle to completion independently. 2
This has several implications. It means that we can render much larger scenes than
we can hold in memory, because we only need space for one triangle at a time.
It suggests triangles as the level of parallelism. The properties of a triangle can
be maintained in registers or cache to avoid memory traffic, and only one trian-
gle needs to be memory-resident at a time. Because we consider adjacent pixels
consecutively for a given triangle, we can approximate derivatives of arbitrary
expressions across the surface of a triangle by finite differences between pixels.
This is particularly useful when we later become more sophisticated about sam-
pling strategies because it allows us to adapt our sampling rate to the rate at which
an underlying function is changing in screen space.
Note that the conditional on line 12 in Listing 15.3 refers to the closest previ-
ous intersection at a pixel. Because that intersection was from a different triangle,
that value must be stored in a 2D array that is parallel to the image. This array
did not appear in our original pseudocode or the ray-casting design. Because we
now touch each pixel many times, we must maintain a data structure for each
pixel that helps us resolve visibility between visits. Only two distances are needed
for comparison: the distance to the current point and to the previously closest
point. We don't care about points that have been previously considered but are
farther away than the closest, because they are hidden behind the closest point
and can't affect the image. The closest array stores the distance to the previously
closest point at each pixel. It is called a depth buffer or a z - buffer. Because com-
puting the distance to a point is potentially expensive, depth buffers are often
implemented to encode some other value that has the same comparison proper-
ties as distance along a ray. Common choices are
z P ,the z -coordinate of the
point P , and
z P . Recall that the camera is facing along the negative z -axis,
so these are related to distance from the z = 0 plane in which the camera sits.
1
/
2. If you're worried that to process one triangle we have to loop through all the pixels in
the image, even though the triangle does not cover most of them, then your worries are
well founded. See Section 15.6.2 for a better strategy. We're starting this way to keep
the code as nearly parallel to the ray-casting structure as possible.
 
 
Search WWH ::




Custom Search