Pro OpenGL ES for iOS

(singke) #1

CHAPTER 2: All That Math Jazz (^41)













0 0 0 1
sin( ) 0 cos( ) 0
0 1 0 0
cos( ) 0 sin( ) 0
( , )
a a
a a
R ya
But what about multiple transformations on top of each other? Now we’re talking ugly.
Fortunately, you won’t have to worry too much about this because you can let OpenGL
do the heavy lifting. That’s what it’s for.
Assume we want to rotate around the y-axis first, followed by x and then z. The resulting
matrix might resemble the following (using a as the rotation around x, b for y, and c
for z):












− + +

− − +


0 0 0 1
sin( )cos( ) cos()sin( )sin( ) sin()sin( ) cos()sin( )cos() cos( )cos() 0
sin()cos( ) cos()cos( ) sin( ) 0
cos()cos() sin( )sin( )sin( ) cos()sin( ) sin( )sin( )cos() sin()cos( ) 0
b c b a c c b b a c a b
c a c a a
b c b a c b c b a c b a
R
Simple, eh? No wonder why the mantra for 3D engine authors is optimize, optimize,
optimize. In fact, some of my inner loop in the original Amiga version of Distant Suns
needed to be in 68K assembly. And note that this doesn’t even include scaling or
translation.
Now let’s get to the reason for this book: all of this can be done by the following three
lines:
glRotatef(b,0.0,1.0,0.0);
glRotatef(a,1.0,0.0,0.0);
glRotatef(c,0.0,0.0,1.0);
Note There are many functions in OpenGL ES 1.1 that are not available in 2.0. The latter is
oriented toward lower-level operations, sacrificing some of the ease-of-use utility routines for
flexibility and control. The transformation functions have vanished, leaving it up to the
developer to calculate their own matrices. Fortunately, there are a number of different libraries
to mimic these operations. One such library was released by Apple’s introduction of iOS 5.
Called the ES Framework API (and described in Apple’s official OpenGL ES 2.0 Programming
Guide), it’s designed to ease the transition to OpenGL ES 2.0.
When dealing with OpenGL, this particular matrix is called the Modelview because it is
applied to anything that you draw, which are either models or lights. There are two other
types that we’ll deal with a little later: the Projection and Texture matrices.
It bears repeating that the actual order of the rotations is absolutely critical when trying
to get this stuff to work. For example, a frequent task is to model an aircraft or

Free download pdf