Pro OpenGL ES for iOS

(singke) #1

CHAPTER 3: Building a 3D World (^71)
However, in reality, the order of transformations is actually applied from last to first. Now
put glTranslatef() ahead of the two rotations, and you should see something like
Figure 3-10, which is exactly what we wanted in the first place. Here is the code needed
to do that:
glTranslatef(0.0, (GLfloat)(sinf(transY)/2.0), z);
glRotatef(spinY, 0.0, 1.0, 0.0);
glRotatef(spinX, 1.0, 0.0, 0.0);
Figure 3-10. Making the cube spin
You can visualize transformation ordering in two different ways: the local coordinate or
the world coordinate approach. Seeing things in the former, you move the objects to
their final resting place and then perform the rotations. Because the coordinate system
is local, the object will rotate around its own origin, making the previous sequence make
sense when going from top to bottom. If you choose the world approach, which is in
effect what OpenGL ES is doing, you must perform the rotations first around the object’s
local axis before performing the translation. In that way, the transformations actually
happen from bottom to top. The net result is the same and so is the code, and both are
confusing and easily can get out of sequence. That is why you’ll see many a 3D guy or
gal holding something at arm’s length while moving themselves all around to help them
figure out why their great-looking catapult model is flying below the ground. This is
called the 3D shuffle. And just to make things more confusing, this is only for OpenGL
ES 1. The reason being is that the transformations are queued up until the rendering
pass is done, and then processed from the first transformation call to the last. While in
ES 2, you must perform all of the transforms yourself, in which case they are done

Free download pdf