SECTION 2.13 PROGRAMMING EXAMPLES AND EXERCISES 57
The vector type
Because we experienced compilation problems w ith versions of GCC lower than 3.0,
we had to explicitly qualify the
vector type in all examples. For instance, we write
e3ga::vector instead of just vector when we are in the e3ga model. This prevents old
GCC versions from confusing
e3ga::vector with std::vector,whichisanarraytype
provided by the C++ standard template library.
2.13.1 DRAWING BIVECTORS
In this first example, we draw a grid of 2-D bivectors. The code is shown in Figure 2.9 and
the output is shown in Figure 2.10. We take two vectors
v1 and v2.Vectorv1 is fixed to
e
1
, and v2 is rotated 360 degrees in 24 steps of 15 degrees.
The vectors are rendered by the default multivector drawing function
draw().Weprovide
two ways to draw the bivectors: as a parallelogram or as a disc. The discs are rendered by
draw(), but the parallelog rams we render ourselves. To switch between the two bivector
drawing modes, click anywhere and select the mode from the popup menu.
2.13.2 EXERCISE: HIDDEN SURFACE REMOVAL
In computer graphics, 3-D models are often built from convex polygons. Each polygon
is defined by an ordered list of vertices. Most of the time, triangles (3 vertices) or quads
(4 vertices) are used. When a solid model is rendered opaque, polygons that face away
from the camera are invisible. Because these back-facing polygons are invisible, no time
needs to be spent on rasterizing them if they can be singled out early on.
Back-facing polygons can be identified by computing the orientation of the projected
(2-D) vertices of the polygon. The convention is that 3-D models are constructed such that
the vertices of a polygon have a counterclockwise order when observed from the outside
of the model (see Figure 2.11). Back-facing polygons have a clockwise vertex order.
In the example, the surface is triangulated, so we need to find a way to determine the
relative orientation of a triangle formed by the endpoints of three vectors a, b, c in
2-D. It is not a ∧ b ∧ c, for that would be zero. Instead, we should consider one of the
vertices a as an anchor, and use the bivector spanned by the difference vectors (b −a) and
(c −a) relative to the standard bivector e
1
∧e
2
. After this hint, implementation should be
straightforward.
We have provided the code that renders a 3-D model from 2-D vertices (Figure 2.13).
As you can see on the left in Figure 2.12, the code renders the model without back-
face culling. T he model is rendered as a wireframe so that you can see the back-facing
polygons. The right side of Figure 2.12 is the result you should get when you have
correctly implemented back-face culling.