Pro OpenGL ES for iOS

(singke) #1

278 CHAPTER 8: Putting It All Together^


+(id)allocWithZone:(NSZone *)zone
{
@synchronized(self)
{
if (m_Singleton == nil)
{
m_Singleton = [super allocWithZone:zone];

return m_Singleton; // Assignment and return on first alUI.
}
}
return nil; //On subsequent allocation attempts, return nil.
}

-(void)sphereToRectTheta:(float)theta phi:(float)phi radius:(float)radius
xprime:(float *)xprime yprime:(float *)yprime zprime:(float *)zprime
{
float cos_theta,sin_theta,cos_phi,sin_phi;

phi=RADIANS_PER_90_DEGREES-phi; /* phi is to be measured starting from the z-
axis. */

sin_theta=sin(theta);
cos_theta=cos(theta);

sin_phi=sin(phi);
cos_phi=cos(phi);

*xprime=(float)(radius*cos_theta*sin_phi);
*yprime=(float)(radius*cos_phi);
*zprime=(float)-1.0*(radius*sin_theta*sin_phi);
}

@end
Now we can go ahead and concentrate on drawing outlines for some of the major
constellations. As with the stars, you need to fetch the data file, outlines.plist from
Apress. First we’ll cover the rendering of the names to the screen.
Unfortunately, for all that OpenGL gives us, text support is not one of them. Thus, it is
up to us, the long-suffering engineers, to implement our own text manager. There are
three ways to do this. The first is to write text out as a collection of vectors, but that’s a
poor solution because it looks terrible along with being a CPU hog. The second is to
generate a texture for each text string, while the third is to generate a ‘‘font atlas’’ (also
known as sprite sheets). Font atlases are used to contain multiple related images on a
single bitmap, with each image plucked out as needed. For text, this would have all
possible characters jammed together along with reference data specifying the location
of each character. The second way, generating a texture, is easier to implement, but font
atlases are far more flexible, because it will let you put up arbitrary lines of text. I vote for
the easy one. With this in mind, I can introduce you to the OpenGLText manager in
Listings 8-15a and 8-15b.
Free download pdf