Allan's Blog


本文摘自 –


  1. Setup Layer
  2. Setup Context
  3. Setup RenderBuffer
  4. Setup FrameBuffer
  5. Render
- (id)initWithFrame:(CGRect)frame
    self = [super initWithFrame:frame];
    if (self) {        
        [self setupLayer];        
        [self setupContext];                
        [self setupRenderBuffer];        
        [self setupFrameBuffer];                
        [self render];        
    return self;


- (void)setupLayer {
    _eaglLayer = (CAEAGLLayer*) self.layer;
    _eaglLayer.opaque = YES;


To do anything with OpenGL, you need to create an EAGLContext, and set the current context to the newly created context.

An EAGLContext manages all of the information iOS needs to draw with OpenGL. It’s similar to how you need a Core Graphics context to do anything with Core Graphics.

When you create a context, you specify what version of the API you want to use. Here, you specify that you want to use OpenGL ES 2.0. If it is not available (such as if the program was run on an iPhone 3G), the app would terminate.

- (void)setupContext {   
    EAGLRenderingAPI api = kEAGLRenderingAPIOpenGLES2;
    _context = [[EAGLContext alloc] initWithAPI:api];
    if (!_context) {
        NSLog(@"Failed to initialize OpenGLES 2.0 context");
    if (![EAGLContext setCurrentContext:_context]) {
        NSLog(@"Failed to set current OpenGL context");


The next step to use OpenGL is to create a render buffer, which is an OpenGL object that stores the rendered image to present to the screen.

Sometimes you’ll see a render buffer also referred to as a color buffer, because in essence it’s storing colors to display!

There are three steps to create a render buffer:(创建缓冲区的步骤)

  • Call glGenRenderbuffers to create a new render buffer object. This returns a unique integer for the the render buffer (we store it here in _colorRenderBuffer). Sometimes you’ll see this unique integer referred to as an “OpenGL name.”

  • Call glBindRenderbuffer to tell OpenGL “whenever I refer to GL_RENDERBUFFER, I really mean _colorRenderBuffer.”

  • Finally, allocate some storage for the render buffer. The EAGLContext you created earlier has a method you can use for this called renderbufferStorage.

GLuint m_renderbuffer;//创建一个渲染缓冲区对象

- (void)setupRenderBuffer {
    glGenRenderbuffers(1, &_colorRenderBuffer);//创建一个渲染缓冲区对象
    glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderBuffer);//将该渲染缓冲区对象绑定到管线上
    [_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer];    


A frame buffer is an OpenGL object that contains a render buffer, and some other buffers you’ll learn about later such as a depth buffer, stencil buffer, and accumulation buffer.

The first two steps for creating a frame buffer is very similar to creating a render buffer – it uses the glGen and glBind like you’ve seen before, just ending with “Framebuffer/s” instead of “Renderbuffer/s”.

The last function call (glFramebufferRenderbuffer) is new however. It lets you attach the render buffer you created earlier to the frame buffer’s GL_COLOR_ATTACHMENT0 slot.

- (void)setupFrameBuffer {    
    GLuint framebuffer;
    glGenFramebuffers(1, &framebuffer);//创建一个帧染缓冲区对象
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);//将该帧染缓冲区对象绑定到管线上
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 
        GL_RENDERBUFFER, _colorRenderBuffer);//将创建的渲染缓冲区绑定到帧缓冲区上,并使用颜色填充


- (void)render {
    glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
    [_context presentRenderbuffer:GL_RENDERBUFFER];
Allan Chan