Pro OpenGL ES for iOS

(singke) #1

CHAPTER 4: Turning On the Lights (^129)
And while you’re at it, let’s turn down the gray in the background. It’s supposed to be
space, and space isn’t gray. Go to OpenGLSolarSystemViewController and the main
drawInRect() routine and change the call to glClearColor to read as follows:
glClearColor(0.0,0.0, 0.0, 1.0);
Now compile and run. You should see something like Figure 4-17.
Figure 4-17. What’s happening in the middle?
There’s something odd here. When running, you should see the earth come out from
behind the sun on the left side, orbit toward us to cross in front of the sun, and then
move away to repeat the orbit again. But what is happening when it should be in front of
the sun in Figure 4-17 (center)?
In all graphics, computer or otherwise, the order of drawing plays a big role. If you’re
painting a portrait, you draw the background first. If you are generating a little solar
system, the sun should be drawn first (er, maybe not...or not always).
Rendering order, or depth sorting, and how to determine what objects occlude other
objects has always been a big part of computer graphics. Before the sun was added,
render order was irrelevant, because there was only a single object. But as the world
gets a lot more complicated, you’ll find that there are two general ways this problem is
solved.

Free download pdf