276 CHAPTER 8: Putting It All Together^
In line 1 of Listing 8-13a, we define a structure for each star. Its
position is in rectangular coordinates since that is what OpenGL
expects, followed by a magnitude, colors, and something called the
hdnum. The magnitude value in this structure is normalized and used in
t h e R G B A f i e l d s. B e c a u s e s t a r s c a n a c t u a l l y b e d i f f e r e n t c o l o r s -----red
g i a n t s a n d y e l l o w d w a r f s , f o r e x a m p l e -----these values could encode
their true shades were we going to do that.
The hdnum is the identifier for stars in the ‘‘Henry Draper Catalog,’’
which covers most stars down to about magnitude 10. As with the
colors, it is not used here except for possible testing and debugging.
Next we jump to Listing 8-13b where we read in the data from the plist
in line 2.
Lines 3ff fetch the locations of each star and, using a helper function
(Listing 8-14), convert the spherical coordinates to rectangular.
Line 4 and the following lines take in the actual magnitude value and
convert it to a normalized shade of gray, clamping the values from .2
to 1.0 and ensuring that the dimmest stars will still be visible.
Now on to the execute method where lines 5ff disable stuff that might
otherwise interfere with our renderings. The depth test is turned off to
minimize interference of the points with the constellation outlines. We
could have left z-buffering on and drawn the constellations lines a little
bit behind the stars but at a slight loss of performance.
The calls to set the color and vertex pointers in lines 6f make use of
the stride parameter. Since the colors are in a format that OpenGL
already understands (the RGBA quadruplet in floats), there is no need
to have to extract them into their own array. So, all we need to do is to
pass a pointer to the address of the first component (which happens
to be red) and a value that tells the system how large the structure is
so it knows where to pick up each successive color or vertex element
in the case of vertex data.
Lines 7ff tell the system how to render the points, both size and style.
Size is specified by glPointSize() with a value of 3.0 pixels, which
seems to work for both the standard and Retina displays. We can also
have points that are either square or rounded. Since I don’t know of
any square stars last I checked, we can use rounded points by
enabling GL_POINT_SMOOTH capabilities. And if we really want really
nicely anti-aliased points, then blending needs to be activated for
those to work. Figure 8-7 shows the difference between the three
styles.