Pro OpenGL ES for iOS

(singke) #1

Chapter 3


Building a 3D World


In the first two chapters, we covered the cool stuff and the math stuff (which could be
either cool or boring). We went over Xcode’s OpenGL ES 2 template just to give you a
taste of the structure and design principles of a ‘‘simple’’ OpenGL app. Here in Chapter
3, we’ll take a step back, examine a very simple 2D application at first, and then migrate
it one step at a time to 3D. (4D hypercubes are beyond the scope of this work.) And
during the process, more 3D theory about projections, rotations, and the like will be
slipped in for good measure.


A Little More Theory

Remember that OpenGL ES objects are a collection of points in 3D space; that is, their
location is defined by three values. These values are joined together to form faces,
which are flat surfaces, that are triangles. The triangles are then joined together to form
objects or pieces of objects.


To get a bunch of numbers that form vertices, other numbers that form colors, and still
other numbers that combine the vertices and colors on the screen, it is necessary to tell
the system about its graphic environment. Such things as the location of the viewpoint,
the window (or viewport) that will receive the image, aspect ratios, and other bits of
digital flotsam of sorts are needed to complete the 3D circuit. More specifically, I’ll cover
OpenGL’s coordinates, how they relate to the frustum, how objects are clipped or culled
from the scene, and drawing to your device’s display.


OpenGL Coordinates


If you’ve done any sort of graphics at all on any system, you’ll be acquainted with the
run-of-the-mill X-Y coordinate system. X is always the horizontal axis, with right being
positive, while Y is always the vertical axis, with down being positive, placing the origin
in the upper-left corner. Known as screen coordinates, they are easily confused with
math coordinates, which place the origin at the lower-left corner and where, for Y, up is
positive. Fortunately, Apple’s rendering framework, Quartz 2D, bucks tradition and uses

Free download pdf