# QUESTION OF THE DAY:

## Think of a good algorithm and estimate its complexity.

PREVIOUS < - - - - > CS 184 HOME < - - - - > CURRENT < - - - - > NEXT

# Polygon Rasterization

## Crucial Concepts from Last Lecture:

Several possible definitions of "INSIDE":
1.) "PARITY" -- "EX-OR" -- "ON-OFF":  Switch between IN and OUT every time one steps across a single contour edge.
2.) "Non-zero Winding Number":  The polygon contour, seen as a string loop cannot be pulled away from a finger stuck at an INSIDE point.
3.) "Positive
(non-zero) Winding Number":  INSIDE is true if the loops of the oriented contour caught around probe point (finger) travel in CCW direction.

(This is the preferred paradigm in computer graphics and for rapid prototyping by layered manufacturing.)

To compute the WINDING NUMBER:
Count ONE UP or ONE DOWN depending on whether the oriented contour edge that one crosses comes from the LEFT or from the RIGHT.

It is desirable to consider the orientation of a polygon contour so we can do BACKFACE ELIMINATION (and save roughly half the work in the rendering pipeline).

ALL GEOMETRY should be clipped to the viewport frame!

Clipping of lines and polygons is straight-forward and is now done by the graphics hardware;
it preserves the visible portion inside the viewport and the topological information needed for rendering.

Linear interpolation is a key operation not just for clipping, but also for morphing, for shading, and for the efficient calculation of splines and subdivision surfaces.

The Various Coordinate Systems in the Classical Rendering Pipeline:
World to screen transforms - Overview   (This is valid in some form for all rendering environments).

## Algorithm for Polygon Rasterization

Let's asume we have done a suitable transformation to bring our polygon into a normalized viewing volume where we just have to do a simple parallel projection along the z-axis (equal to setting z equal to zero) to get the desired display of the polygon.
Now -- how can we determine in an efficient manner WHICH PIXELS should be turned on ?

refer to:  DISCUSSION OF THE QUESTION OF THE DAY.

We "turn on" all the pixels that have a sampling point that falls inside the polygon contour.

Now we want to implement a rasterization algorithm in an efficient manner:

### Sweeping Scan-line Approach

First we do this one polygon at a time.   (not explained well enough in [Shirley]).

Datastructures:
-- Scene Graph -- Display List -- Edge Table -- Active Edge List.
Preprocessing:
-- Bucket-sort (in y) all polygon edges into the Edge Table. Process the scan-line buckets from bottom to top.
-- Inserting edges into scan-line buckets (details)
Scan-line Processing:
-- For the current swath of pixels update the Active Edge List of all edges that cross the current scan line.
-- Details of scan-line processing on active edge list
-- Turn on pixesl that have their sampling points on active inside spans.
(For convenience we chose the pixel sampling point to be the lower left corner, thus avoiding to have to add/subtract 0.5 all the time.)

### Gouraud Shading (= linear interpolation between vertices)

In addition to the above, let's now assume that the polygon to be shaded is just a simple triangle. It is normally a good idea to break complicated polygons into triangles, then you know what you have got; different renderers may do different (and sometimes strange) things to a concave, non-planar polygon.
However, it is still important to understand the winding number paradigm and to use it where appropriate, e.g. in layered manufacturing. There the machine needs to calculate for each layer of material deposited the exact polygon to be "painted in."

But now we want to give this triangle a smoothly changing color; where three different colors have been specified for its three vertices.
To determine the color of any point inside the triangle, we will use barycentric coordinates. (The influence of a vertex diminishes proportional to the distance from the opposite side.) This can easily be calulated with linear interpolation. A practical way to do this is to interpolate first along the edges of the triangle and then along the scan line between the previously calculated values on the edges.

### Z-buffer Algorithm (to find the "top" visible polygon)

Now, in addition to the above, we assume that we have several triangles which partially overlap in the projection into our viewport (and perhaps even intersect in 3D).
How do we determine efficiently which triangle a particular pixel should represent?

The z-buffer is a 2D array of storage elements that stores for each pixel (R,G,B)color and a z-depth.
-- Paint all triangles (one at a time) into the z-buffer (using scan-line-based raster filling);
-- the front-most pixel and its depth and color will be retained -- and finally displayed.
-- The z-depth of each pixel can be efficiently calculated by linear interpolation,
i.e., by fixed incrementation in y and in x (since we assume all polyhedron faces to be planar).

### Alternative Scan-line Approach: Processing all polygons simultaneously

Differences to basic approach above if there is no z-buffer available: Exploit linearity and coherence as much as possible!

Extra Notes on Sweep-Line-Based Hidden Feature Elimination

### The devil is in the details !

Dealing with possible coincidences needs all the attention (and produces most of the bugs).
Here is a more detailed description of a sweeping scan-line algorithm.

Study: ( i.e., try to understand fully, so that you can answer questions on an exam):
Shirley, 2nd Ed: Ch 3.6-3.7; Ch 17.1-17.2.

## Programming Assignments 1 and 2:

Both must be done individually.
Assignment #1 is due (electronically submitted) before Thursday 2/5, 11:00pm.
Assignment #2 is due (electronically submitted) before Thursday 2/12, 11:00pm.

PREVIOUS < - - - - > CS 184 HOME < - - - - > CURRENT < - - - - > NEXT
Page Editor: Carlo H. Séquin