Pro OpenGL ES for iOS

(singke) #1

CHAPTER 7: Well-Rendered Miscellany (^219)
Figure 7-7. Reflections in Distant Suns. (Yes, it is a gratuitous plug.)
Of course, Apple’s examples were implemented in Core Graphics, but the principles are
the same: it’s a big fat hack! But so is most everything in graphics as we try to simulate
the real world through whatever means necessary, and making a reflective surface of
this type is no different.
This will introduce the next topic, which is about both stencils and reflections. Besides
the ‘‘color’’ buffer (that is, the image buffer) and the depth buffer, OpenGL also has
something called a stencil buffer.
In the pre-iOS5 days, creating a stencil buffer required another dozen-or-so lines of
code. That has since been collapsed to only a single line, a mere trifle of code. In your
viewDidLoad(), when the context is created, add:
view.drawableStencilFormat=GLKViewDrawableStencilFormat8;
The stencil format can be either 8 bits or none.
Next let’s create the routines that will generate the actual stencil. Add Listing 7-8 to your
view controller, and call renderToStencil() from your main draw loop.

Free download pdf