Pro OpenGL ES for iOS

(singke) #1

82 CHAPTER 3: Building a 3D World^


„ Lines 3 and 4 allocate the memory for both the vertices and the colors.
Later other arrays will be needed to hold the texture coordinates and
face normals needed for lighting, but for now, let’s keep things simple.
Notice that we’re doing 32-bit colors, as with the cube. Three bytes
are for the RGB triplet, while the fourth is for alpha (translucency) but is
not needed in this example.
„ Line 5 starts the outer loop, going from the bottom-most stack (or the
southern polar regions of our planet or altitude of -90 degrees) and up
to the northern pole, at +90 degrees.

Some Greek identifiers are used here for spherical coordinates. Phi is
commonly used for the latitude-like points, while theta is used for
longitude.
„ Lines 6 and 7 generate the latitude for the boundaries of a specific
strip. For starters, when phiIdx is 0, we want phi0 to be -90 degrees,
or -1.57. The -.5 shoves everything down by 90 degrees; otherwise,
our values would go from 0 to 180 degrees.
„ In lines 8ff, some values are precalculated to minimize the CPU load.
„ Line 9 is the inner loop, going from 0 to 360 degrees, and defines the
slices. The math is similar, so no need to go into extreme detail,
except that we are calculating the points on a circle, via line 10. Both
m_Scale and m_Squash come into play here. But for now, just assume
that they are both 1.0, so the sphere is normalized.

Notice that vertex 0 and vertex 2 are addressed here. Vertex 0 is x,
w h i l e v e r t e x 2 i s z -----which are parallel to the ground, the X-Z plane.
Since vertex 1 is the same as y, it remains constant for each loop and
of course represents the latitude. Since we’re doing the loops in pairs,
vertices 3, 4, and 5 cover up the
next loop.

In effect, we are generating pairs of points, namely, each point and its
mate immediately above it. And this is the format that GL expects for
the triangle strips, as shown in Figure 3-17.
Free download pdf