Pro OpenGL ES for iOS

(singke) #1

204 CHAPTER 7: Well-Rendered Miscellany^


You should recognize the pattern here, because creating FBOs is a lot like many of the
other OpenGL objects. So, let’s break it down:
„ FBOs, as with many of the OpenGL objects, use ‘‘names’’ as handles
to uniquely identify them. In line 1, we’re getting the original frame
buffer object that serves as the main screen. The idea is to be a good
neighbor and restore it at the very end. Otherwise, the wrong one
could be used.
„ Line 2 has us generating a name for our depth buffer. Then it is bound
to the system as our current render buffer. If this is the first time that
object has been bound, OpenGL will allocate it minus the image
memory and then use that allocated memory in all subsequent
bindings.
„ In line 3 we actually allocated the memory for the buffer’s image data.
Since images require large blocks of memory, they should never be
allocated until needed. That is why the bind and the allocation
operations are usually kept separate.
„ At this point in lines 4ff, we need to allocate a texture image and have
it linked up to our frame buffer. This is the interface required to
camouflage our FBO so it looks just like any other texture to OpenGL.
Here we can also set up some of the normal texture settings for edge
conditions and use bilinear filtering.
„ Up until now we’ve merely created the depth buffer and image
interface. In line 5, we actually create the frame buffer object and
attach the previous bits to it.
„ Line 6 attaches the texture first. Notice the use of
GL_COLOR_ATTACHMENT0_OES.The texture bit actually holds the color
information, so it is called the color attachment.
„ In line 7, we do the same for the depth buffer, using
GL_DEPTH_ATTACHMENT_OES. And remember that in OpenGL ES we have
only three types of buffer attachments: depth, color, and stencil. The
latter does things such as blocking rendering in a certain part of the
screen. The adult version of OpenGL adds a fourth kind,
GL_DEPTH_STENCIL_ATTACHMENT.
„ Line 8 does a quick error check.
„ As referenced earlier, we need to restore the original FBO for our main
screen, in line 9.
„ And finally, lines 10 and 11 provide some getters so we can use the
new FBO.
So, that’s merely creating an FBO. You’ll see that it is a fairly no-frills piece of code,
using the built-in functions available in both OpenGL ES 1 and 2. And yes, it does seem
a little overly complicated, but it’s easily wrapped with a helper function.
Free download pdf