270 CHAPTER 8: Putting It All Together^
Then we come down to where the flare is actually rendered starting
with lines 8ff. Most of the code here largely handles one basic effect:
what happens when the sun goes behind the earth. Naturally, the lens
flare will vanish, but it won’t pop in or out instantly because the sun
has a finite diameter. So, values such as the grazeDist and vanishDist
tell us when the sun first intersects the earth, starting the dimming
process, and when it is finally covered completely, blanking out the
flare images. Using the earth’s screen x and y values as well as those
for the sun, it becomes an easy matter to specify a fade function.
Lines 9ff actually determine the brightness of the flares.
percentVisible is ‘‘full’’ brightness at 1.0 but can actually be a little
more, because it multiplies the colors of the flare by this number.
Since not all colors would be maxed out at 1.0, I can actually go a little
higher. But why? When the sun goes behind the earth, one might
expect the final beams of light to be refracted by the atmosphere just a
tad, magnifying the brightness for one short and, in this case, barely
visible flash. (You get extra credit if you want to make it one of the
fabled green flashes.)
The flare’s execute method is called in line 10. The sun’s screen x and
screen y values serve as the sourceLocation parameter that causes
the lens flare.
We also need an updated executePlanet() to return the new values
used to place the sun and the flare images, as in line 11.
One of the enhancements is in line 12, where we can block the actual
rendering of the body if all we’re interested in are their screen
parameters.
And finally (it’s about time, eh?), we have the call to the new
gluGetScreenLocation helper function, line 13, in miniGLU.mm, covered
earlier.
To the planet object, add a getter routine for the radius that, in this case, is identical to
the m_Scale variable:
-(float)getRadius
{
return m_Scale;
}
That should do it. I am sure you’ll be able to compile with no errors or warnings,
because you’re just that good. And because you are just that good, you will likely be
rewarded with the images in Figure 8-6. And feel free to play with ambient light and
specular lighting as I have done. The effect might not be very realistic, but it looks
very nice.