238 14.2DMagic
The mesh architecture consists of three important items: vertex, mesh, and
modifiers. First, we use a vertex class to hold each vertex’s position, color, opaci-
ty, and texture coordinates. Next, we store all our vertices inside a mesh class and
use the mesh class as a way to coordinate all the vertices, acting as a type of brain
for the overall image. Last, our modifiers do the dirty work and alter the mesh in
such a way that we conjure magic on the screen.
Mesh
The mesh is a class that contains a 2D array of vertex objects. Every place where
there is a line intersection in Figure 14.5, there exists a vertex. Using Cartesian
coordinates, a vertex can be quickly retrieved and modified. Our mesh design has
a base
Mesh class and a derived MeshUV class. In the base implementation, we do
not support texture coordinates. It’s useful for times when we want to use colored
lighting without a texture, as demonstrated in our double rainbow example later
in this chapter.
Space
There is a good argument to be made about whether a mesh should exist in
screen (or local) space or in world space. On the side of screen space, if a mesh
changes size or position, then we don’t need to recompute any of our vertices. On
the side of world space, everything with the mesh requires just a bit more work,
calculation, and CPU time on a frame-by-frame basis. That is to say, if we’re not
moving or scaling our mesh, then we’re wasting CPU time by going to and from
screen space. Our implementation assumes screen space; that is, the mesh con-
siders its top left corner to be
0,0
, and vertices see their
,
y
positions as per-
centage offsets into the mesh. Therefore, only if we change the number of mesh
points per row or column do we need to rebuild our mesh from scratch.
State
Another contention point in mesh design is whether to build vertices that contain
state. Having state means that a vertex has two sets of properties, original and
working. The original properties could be considered the normal, “resting” prop-
erties that would make the texture appear as if it had no mesh. The working prop-
erties are copies of the originals that have been modified by an effect and, thus,
have altered the state of the vertex. The vertex then fights its way back towards
its original properties by using mesh-defined physics and other parameters to al-
ter its working set.