# CS488 - Introduction to Computer Graphics - Lecture 15

## Review

1. Polygons
2. Back-face culling

## Hidden Surface Removal

Every polygon, in 3D, has two sides

• the normal vector differentiates the sides
• different sides may have different properties

It is very convenient to assume that surfaces are closed

• Naturally open surfaces, e.g., a piece of paper, can be closed by adding polygons

Then we can say that every polygon has only a single side with visual properties

• The normal vector points away from this surface

#### Backface Culling

Do not render any polygon with n.E < 0.

• On average this cuts the rendering effort in half.
• Remember that E is the ray from the polygon to the eye, not the view direction.

Does not handle occlusion, so we need to finish off with one of the following algorithms.

#### Painter's Algorithm

Sort polygons back to front, then render backmost first.

• device-independent
• slow if there's lots of occlusion
• sorting may not be possible, which makes things hard.
• not easy to implement
• potentially O(n^2), but rarely is that bad. n: number of polygons.

#### Warnock's Algorithm

Divide and conquer

2. Divide into four subwindows
3. Keep dividing into subwindows until
• the subwindow is a single pixel, OR
• there is a single polygon in the window

then paint the window

Warnock's algorithm is

• easy to implement, device independent
• slow, often O(p.n). p: number of pixels.

#### Depth-Buffer Algorithm (also called zed-buffer)

Use a second frame buffer with depth values stored in it

1. Initalize depth-buffer to far plane distance
2. If a pixel has z less than the current depth buffer value
• Write it, and
• Update the depth buffer

The depth buffer algorithm is

1. easy to implement
2. fast, O(n)
3. no longer memory-intensive, though it used to be

Almost all graphics cards have hardware depth buffers.

• Using the depth buffer may be hard the first time.

## Mouse Interface

#### Rotation

For assignment 3 you need to be able to rotate with the mouse

The virtual trackball

• vertical motion rotates about y
• horizontal motion rotates about x
• circular motion outside the trackball rotates about z

You need to modify the sample code.

#### Picking

For assignment 3 you need to be able to select graphical objects with the mouse

The simple principle

1. Mouse gives (x, y).
2. Render pixel at (x, y ) keeping track of the polygon it comes from.
3. Associate polygon with object

For the assignment let GL do it for you.

• Associate names (unsigned integers) with objects that are drawn
• Get back a hit stack, with primitives `near' where the mouse clicked
• It's up to you how you handle the hit stack.

See the notes.

## Hierarchical Models

Suppose you are modelling classical temples. How do you do it?

1. Open up Vitruvius. You will find that all temples are assumbled from a few basic parts, such as
• shafts, bases, capitals
• stylobates, stereobates
• architraves
• etc.
2. You don't know the words, but there are drawings with dimensions.
3. There are rules for making them bigger and smaller, so you think of scaling matrices.
4. There are rules for putting them in different orientations, so you think of rotation matrices.
5. There are rules for placing them in different locations, so you think of translation matrices.

At this point you can make a temple, but it's a lot of work. To make it easier, divide and conquer

1. Column = base + shaft + capital. Reuse this as a unit.
2. Entablature = architrave + frieze + cornice. Reuse this as a unit.

You get the idea. The result is

• a data structure called an acyclic directed graph, DAG, which is like a tree designed for computer graphics.