Pro OpenGL ES for iOS

(singke) #1

24 CHAPTER 1: Computer Graphics: From Then to Now^


Let’s take a look at what’s going on here:
„ Line 1, the start of the update method, is actually one of the delegate
calls from the new GLKViewController object. This supports frame-rate
hints, as in, ‘‘I’d love to have my new game Dangerous Poodles update
at 100 fps, if you can do so please.’’ It will also let you know what its
real frame rate is, the number of frames since starting the session, and
it handles pause and resume functions.
„ In line 2, besides defining the objects to show, we need to define the
viewing frustum. This simply specifies how big of a swath of area you
want to see in your world. Think of it as a camera’s zoom lens, where
you can zoom in or out. This then gets converted into a projection-
matrix, similar to a transformation matrix that we saw earlier. This
encapsulates the information to project your object up against you
device’s display.
Note that the first value supplied to GLKMatrix4MakePerspective is 65,
meaning that we want our ‘‘lens’’ to have a 65 degree field-of-view.
This is generated using one of the many new math library calls that
also form a part of the GLKit. The calls include support for vectors,
matrices, and quaternions (covered later), exclusively for 3D scene
manipulation.
„ The GLKBaseEffect used to contain one of the cubes needs to be told
to use this matrix in line 3.
„ Line 4 generates a translation matrix. This describes how to move, or
translate, your object through space. In this case, the -4 value moves it
away from our eyepoint by 4 units. By default, the OpenGL coordinate
system has the X-axis, left and right, the Y-axis up and down, and the
Z-axis, forward and back. We are looking towards ---Z.
The matrix, baseModelViewMatrix, gets its name from OpenGL’s
‘‘ModelView’’ matrix, which the one invoked more frequently than any
others.
By applying it first, we are actually moving our entire world away by 4
units. Below we add separate motions to the individual cubes.
„ Now we want to rotate the cube. Line 5 shows that transformations
can be concatenated by multiplying them together. Here we reuse the
baseModelView matrix from the previous line.
„ ‘‘What?’’ you are no doubt asking, ‘‘another one of these silly matrix
things?’’ Even seemingly simple motions sometimes require a
convoluted assortment of rotations and translations. Here in line 6 the
cube is moved -1.5 units away from its own origin. That’s why neither
is actually centered in the screen but orbit around an invisible
something.
Free download pdf