Pro OpenGL ES for iOS

(singke) #1

282 CHAPTER 8: Putting It All Together^


„ Setting the fill color in line 5 ensures that the filled text characters are visible.
„ Lines 6ff push the new context on the stack, write the text to that context,
and then pop it back off.
„ Now the OpenGL texture is created in the standard way with lines 7ff
preparing the way and line 8 actually generating texture itself. Note that to
be a good neighbor, the new texture’s name is only temporarily bound, and
the previously bound texture is restored in line 9.
Jumping down to renderAtPoint(), the only other method in this object, we use a
slightly different and more simple form of drawing a texture to the screen with
glDrawTexfOES(). While Apple supports this, remember that the OES suffix says that
there is no guarantee that this call will be available on other OpenGL ES implantations,
so use at your own risk. Moreover, you can’t do any transformations on it either.
„ glDrawTexfOES() doesn’t recognize the contentScaleFactor used to
get OpenGL ES to recognize a high-resolution Retina display. So, we
must scale things up manually in line 10.
„ glDrawTexfOES() brings in a new texture parameter in line 11,
GL_TEXTURE_CROP_RECT_OES using the supplied boxRect. This lets you
clip only part of the texture to use for display. Here we’re saying that
we want the entire content.
„ And finally the label is drawn to the buffer. Notice how the scale factor
is used in both x and y in line 12.
Now for the loader/renderer for the outlines themselves. Because of the length of the
rest of the code and because a lot of it resembles the star module, only excerpts will be
used here to highlight the noteworthy parts.
When read from the plist, the data for each constellation’s outline is converted to an
array of floats that OpenGL will understand and then store back in the original
dictionaries as an NSData object. That way, the original data is kept around if need be
while being easily linked with the OpenGL representation of the same data, as shown in
Listing 8-16, also in OpenGLOutlines.
Listing 8-16. Allocating the vertex buffers

coordArray=[dict objectForKey:@"coordinates"];
numpoints=[coordArray count];
numbytes=numpoints*3*sizeof(GLfloat);

data=(GLfloat *)malloc(numbytes);

for(j=0;j<numpoints;j++)
{

coords=[coordArray objectAtIndex:j];

ra=(NSNumber *)[coords objectForKey:@"ra"];
dec=(NSNumber *)[coords objectForKey:@"dec"];
Free download pdf