Skip to main content

Chapter 7 Polygons

Currently, our basic unit of drawing is a line. When we create a shape, we add edges to an edge matrix, and we loop through that matrix drawing lines. A 3D shape rendered using edges only has no real surface to speak of. Instead, it is a wire-frame model. Wire-frame models can be rendered quickly, but because they lack true surface, there are limits to how realistic they can look.
If we change our basic unit of drawing form a line to a polygon, we will have surfaces to work with, generating a polygon mesh. Having a surface gives us more options when rendering 3D objects. Most notably:
  • Polygons can be filled with colors, creating solid objects.
  • We can describe polygons as "facing" a particular direction, and use that information to determine sides of our 3D shapes that are facing away from the viewer and should therefore not be rendered.
  • We can also use the direction a polygon is facing to help calculate real-world lighting and shading values.
There are many possible shapes we could use as our basic polygons, we will use triangles, as they are the simplest polygon.

Section 7.1 Polygon Lists

Our current shapes are all based on _edge lists_, where every 2 points determine a distinct edge to be drawn. We should keep this framework around in order to draw 2 dimensional shapes when desired. We need to add polygon lists, where every 3 points determine a distinct triangle. Here is a chart comparing the functions in our 2D, edge based, drawing stack and their 3D, polygon based, equivalents.
Table 7.1.
Edge Framework Polygon Framework
add_point
Add a point to the edge list
add_point
Add a point to the polygon list
add_edge
Add the endpoints of a line to the edge list.
add_polygon
Add the three vertices of a triangle into the polygon list.
The vertices must be added incounter-clockwise order.
draw_lines
Go through the edge list 2 points at a time,
drawing a line between each pair of points.
draw_polygons
Go through the polygon list 3 points at a time,
connecting the points to draw a triangle.
Other polygon features can be added here later.
By adding the points in counter-clockwise order, we will be able to eventually determine what is forwards and backwards with respect to the triangle. This will help us implement more advanced features later.

Section 7.2 Polygon Based Shapes

Subsection 7.2.1 Box

A box has 6 faces, each one made up of 2 triangles. Like before, the easiest thing to do here is add your 12 triangles directly into the polygon list. The order of the triangles does not matter, but the order of the points does. Points should always be added counter-clockwise.

Subsection 7.2.2 Sphere

We generate spheres by drawing a series of rotated semi-circles. If you followed my advice, you should have a separate generate_sphere function that returns a matrix of points for the sphere. Our job now is to go through that list of points, adding triples of points representing the triangles on the surface of the sphere to our polygon list. The best thing to do is physically draw the first two semicircles generated by your code, and write out what triangles are required.
2 sphere semicircles
When drawing semi-circles, you need to generate the poles, which are created when t=0 and t=1, respectively. This is different from drawing full circles, you don’t need to include t=1 there because the point at t=1 is identical to the point at t=0.The way my loops are written, this means when I set steps to 10 I actually get 11 points per semi-circle.
It is also important to keep track of the direction your semi-circles are drawn in. If you used the example from class (semi-circles rotated about the x-axis), then each new semi-circle is drawn in front of/below the previous one. Here are some of the triangles taken from the diagram above:
          0: p0, p1, p12
          1: p1, p2, p13
          2: p1, p13, p12
        
At the poles, there is only one triangle to add per section, this is triangle 0 above. Otherwise, triangles are added in pairs, as is shown by triangles 1 and 2. If n is the number of points in a semi-circle, then we can define the triangles for the non-poles as:
           p, p + 1, (p + n) + 1
           p, (p + n) + 1, p + n
         
At the poles, we need to make sure not to add both triangles. Following the above formulae, at p=0, we would have triangles 0, 1, 12 and 0, 12, 11. But for the second triangle, 0 and 11 are the same point. This would render as a straight line, which might not seem like a big deal, but having these "degenerate" triangles at the poles will cause problems down the line, so it’s best to exclude them now.

Subsection 7.2.3 Torus

Torus creation is very similar to the sphere, with a couple of changes.
  • Tori must be generated with full circles.
  • No 2 circles of a torus share any common points, which means we don’t have poles. This actually makes it easier to code since we don’t have those special cases.
  • The tori from class are generated by rotating about the y-axis. This means that each new circle is drawn behind the previous one.
As with the sphere, it is advised that you draw out the first two slices of torus to map out the appropriate triangles.