Rasterization (Technical) ------------------------- Technical details about rasterization: Traditional rasterization is done as follows: Basically, we trace along the triangle left and right edges scanline by scanline, then fill in the pixels between these edges. We: 1. Break the triangle up into two trapezoids by sorting the Y coordinates. Let's sort from small to large and pretend that Y screen coordinates start at the top and increase downward. 2. Compute the "starting" value for each variable that we want to interpolate. Minimally, this includes the X and Z coordinates. It may also include the intrinsic R/G/B/A color, and it may include the U and V texture coordinates (see below), and perhaps other stuff. The starting value is the left side value. It may just be the value of the variable at the top vertex, but you have to adjust it slightly in order to be sub-pixel accurate. 3. Compute the "ending" value for each variable. This is the right side value of each variable, similarly adjusted. 4. Compute the Y-deltas for each starting and ending variable. This is the value that you must add to each starting or ending value to get to the correct value for the next scanline. (There's one delta for the start, another delta for the end.) These deltas are also referred to as slopes, since for the X coordinate the value _is_ just the slope. 5. Loop over the scanlines (Y values) from Y1 to Y2: a. Compute the X-delta between the start and the end values. (You don't have to do this for X, since the X delta is 1.) b. Loop over the pixels from the start X to the end X: i. rasterize the pixel using the current values (see below) ii. adjust each current value by the X-delta (end of loop) c. Adjust each starting and ending value by the appropriate Y-deltas. (end of loop) 6. You've now done the top trapezoid. For the bottom trapezoid, we need to adjust either the starting or ending values and their deltas, according to whether Y2 was on the left or right side of the triangle. 7. Repeat step 5 for the Y values from Y2 to Y3. ----- * Some triangles result in only a single trapezoid (if their top or bottom edges are perfectly horizontal). * U and V coordinates are positions in a texture map. Each vertex of the triangle is "pinned" to a given position in the texture map by specifing the 2D coordinates (U and V) in the texture map. * Perspective correction demands that you pre-multiply the raw values to be interpolated by 1/Z, then compute Z from 1/Z at each pixel, then multiply each interpolated value by Z to get the correctly interpolated (with respect to perspective) value out. This is usually only applied to U and V, but it makes sense to apply it to color as well. (Doesn't apply to X or Y.) * Rasterizing the pixel means taking all those raw values, doing the perspective correction bit described, looking up the correct texel from the U and V coordinates, taking that color value and possibly combining it with the intrinsic color, applying fog, alpha blending as appropriate, Z-buffering, and storing the new color and Z as appropriate (but probably not in that exact order). ----- I've described "traditional" rasterization. There are other ways to do it as well, both for real-time (interactive) rendering as well as high-quality (slow) rendering. ---------------------------------------------------------------------- (c) 1997 Carl Mueller