Pro OpenGL ES for iOS

(singke) #1

202 CHAPTER 7: Well-Rendered Miscellany^


Hedley Buffer Objects
You know the drill by this time: find the exercise from Chapter 5, with the original 2D
textured square filled. This will serve as our basic framework as usual.
You’ll have to create a separate object that encapsulates the new FBO. Call it
FBOController and populate it with Listing 7-1 and Listing 7-2.
Listing 7-1. The header for FBOController.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>

@interface FBOController : NSObject
{
GLuint m_Texture;
GLuint m_FBO1;
}

-(GLint)getFBOName;
-(GLuint)getTextureName;
-(id)initWidth:(float)width height:(float)height;

@end
Listing 7-2. The main body of FBController.m

#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
#import "FBOController.h"

@implementation FBOController

-(id)initWidth:(float)width height:(float)height
{
GLint originalFBO;
GLuint depthBuffer;

//Cache the original FBO, and restore it later.

glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, &originalFBO); //1

glGenRenderbuffersOES(1, &depthBuffer); //2
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthBuffer);

glRenderbufferStorageOES(GL_RENDERBUFFER_OES, //3
GL_DEPTH_COMPONENT16_OES, width, height);

//Make the texture to render to.

glGenTextures(1, &m_Texture); //4
glBindTexture(GL_TEXTURE_2D, m_Texture);
Free download pdf