126 CHAPTER 4: Turning On the Lights^
Here’s what’s going on:
Line 1 creates a lighter shade of yellow. This just colors the sun a
slightly more accurate hue.
We need a black color to ‘‘turn off’’ some of the material
characteristics if needed, as in line 2.
In line 3, the orbital increment is needed to get the earth to orbit the
sun.
glPushMatrix() in line 4 is a new API call. When combined with
glPopMatrix(), it helps isolate the transformations for one part of the
world from another part. In this case, the first glPushMatrix() actually
prevents the following call to glTranslate() from adding new
translations upon itself. You could dump the glPush/PopMatrix pair
and put the glTranslate() out of execute(), into the initialization
code, just as long as it is called only once.
The translation in line 5 ensures that the objects are ‘‘moved away’’
from our eyepoint. Remember that everything in an OpenGL ES world
effectively revolves around the eyepoint. I prefer to have a common
origin that doesn’t rely on viewer’s location, and in this case, it is the
position of the sun as expressed in offsets from the eyepoint.
Line 6 merely enforces the sun’s location as being at the origin.
Ooh! Another glPushMatrix() in line 7. This ensures that any
transformations on the earth don’t affect the sun.
Lines 8 and 9 get the earth to orbit the sun. How? In line 10 a little
utility function is called. That performs any transitions and moves an
object away from the origin if need be. As you recall, the
transformations can be thought of being last called/first used. So, the
translation in executePlanets() is actually performed first, followed by
the glRotation(). Note that this method will have the earth orbiting in
a perfect circle, whereas in reality, no planets will have a perfectly
circular orbit, so glTranlsation() will be used.
glPopMatrix() in line 11 dumps any of the transformations unique to
the earth.
Line 12 sets the sun’s material to be emissive. Note that the calls to
glMaterialfv() are not bound to any specific object. They set the
current material used by all following objects only until the next calls
are made. Line 13 turns off any specular settings used for the earth.
Line 14 calls our utility again, this time with the sun.