3D Game Programming

(C. Jardin) #1

}


shape.computeFaceNormals();
shape.computeVertexNormals();
returncenter_points;
}

The main purpose of this function is to find the center of the river so that it,
and surrounding points, can be pulled down. We pull down a shape’s vertices
by setting the z property to a negative number.

In the preceding function we worked through the entire plane, one row at a
time. The first for loop sets the row variable to zero, then figures out where the
center of the river should be for that row. From Chapter 6, Project: Moving
Hands and Feet, on page 59, we already know that Math.sin() makes a nice
winding path, so we use it again here.

The four lines that compute and then recompute the center determine how
many curves there should be, how far the bends are from the center, where
this point is in the current row, and where that point falls in the entire list
of vertices. You should experiment with 4 in the first center line and 0.1 in the
second line. If you’ve already taken trigonometry in school, you know these
numbers represent the frequency and amplitude of the sine wave. Mostly, it’s
just fun to play with them.

We dig a trench in each row and then combine them to form the river. We
start from the center point for each and work our way out to ten vertices on
either side (plus and minus the distance). Last, we store the center vertex in
case we want to use it later as a way to put stuff on the river.

Once all of the rows have had some portion of them dug out, we have to
recompute normals. 3D renderers work hard to keep track of the direction in
which faces and their corners are pointing. This direction is called a normal
and it helps with lighting, shading, and shadows. We don’t have to worry
much about how normals work, but we do need to tell the renderer that we’ve
changed them by telling the shape to computeFaceNormals() and computeVertexNor-
mals().

With that, we have a trench for the river to flow through (see Figure 9, The
River Trench, on page 194).

Next we add the actual river.


Tricking the Eye


Adding the river water and the lid to keep the raft inside the river is pretty
easy for us. We need two planes—one for the water, which will be blue, and

report erratum • discuss

Warping Shapes to Make Unique Things • 193


Prepared exclusively for Michael Powell

Free download pdf