Pro OpenGL ES for iOS

(singke) #1

258 CHAPTER 8: Putting It All Together^


„ First we get the cached quaternion from miniGLU, in line 1, along with
the viewpoint’s xyz location from the solar-system object in line 2.
Since that hasn’t been added yet, feel free to add that now. It is simply
a getter for the already existing m_Eyeposition object in the solar-
system controller.
„ Line 3 gets the target’s location. In this case, the target is merely earth.
With that in hand, we need to find the offset of our eye point from the
earth’s center and then calculate that distance, as in line 4.
„ Line 5 takes the screen coordinates of the previous and current
positions, so we know just how much we moved since the last time.
„ Lines 6ff create a fractional rotation for each new position of the touch.
Using the actual orientation quaternion (recovered in line 1) ensures
that the new orientation from each touch position is preserved,
representing the cumulative rotations of the eye point. The three
values of scale, multiplier, and reference are all arbitrary. Scale is fixed
and was used for some fine-tuning to ensure things moved at just the
right speed that ideally will match that of your finger. The multiplier is
handy for orientation changes because it is a scaling factor that is
based on the screen’s current width and a reference value that is also
arbitrary.
Those are then multiplied against the x and y deltas and passed into
the quaternion that is used to generate the final matrix, as in line 7.
„ Line 7 takes the new matrix and multiplies it against the offset vector
to transform it to the new position, while lines 8ff actually perform the
translation to the new position and updates the solar-system
controller.
„ And finally, earth is reentered in line 9.
Ready? Ha-ha! Not quite. In initLighting(), ensure that glShadeModel() is set to
GL_SMOOTH, check that the fill light 2 is disabled, and, most importantly, delete the line
that attenuates the sunlight.
And of course, make sure to modify the headers as needed, but you probably already
know that. We hope you will now be able to move the earth around at will. You should
see something akin to Figure 8-4. And notice that you should see the sun, albeit much
smaller now, pop in and out.
Free download pdf