openGL应用程序中的destroyFramebuffer方法

时间:2010-10-14 03:47:27

标签: iphone opengl-es eaglview

在我的OpenGL类型的iphone应用程序中,我在GLView实现中使用了以下方法。

我注意到每次我在开发期间启动应用程序时,最后一次关闭应用程序时出现的最后一帧,将在应用程序开始动画之前先绘制...

但是当我注释掉这个方法时,应用程序会在没有上一次旧结束帧的情况下启动。

所以我在这里有点不对劲。可能直接在这种方法......任何想法?

(现在我在动画之前得到一个空白的白色屏幕(作为第一帧),所以这里有些不对劲)

//------------------------------------------------------------------------------------
- (void)destroyFramebuffer 
{
NSLog(@"destroyFramebuffer");
    glDeleteFramebuffersOES(1, &viewFramebuffer);
    viewFramebuffer = 0;
    glDeleteRenderbuffersOES(1, &viewRenderbuffer);
    viewRenderbuffer = 0;

    if(depthRenderbuffer) 
    {
        glDeleteRenderbuffersOES(1, &depthRenderbuffer);
        depthRenderbuffer = 0;
    }
}

下面是“createFrameBuffer”方法的外观 (GLView实施的启动方式是这3行):

    [EAGLContext setCurrentContext:context];
//    [self destroyFramebuffer];
    [self createFramebuffer];



//------------------------------------------------------------------------------------
- (BOOL)createFramebuffer 
{
NSLog(@"createFramebuffer");
    glGenFramebuffersOES(1, &viewFramebuffer);
    glGenRenderbuffersOES(1, &viewRenderbuffer);

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);

    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);

    if (USE_DEPTH_BUFFER) 
    {
        glGenRenderbuffersOES(1, &depthRenderbuffer);
        glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
        glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
        glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
    }

    if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) 
    {
        NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
        return NO;
    }

    return YES;
}

1 个答案:

答案 0 :(得分:0)

当您销毁渲染缓冲区时,内存将被回收。下次再次使用相同内存的可能性非常高,但并不总能得到保证。您只需要在创建时清除缓冲区:(或至少渲染填充缓冲区的内容)

glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);

另外值得更改你的destroy方法来检查你的帧缓冲区和渲染缓冲区是否存在,否则你可能会请求GL删除你没有做过的东西。例如:

if (viewFramebuffer) {
    glDeleteFramebuffers(1, &viewFramebuffer), viewFramebuffer = 0;
}
if (viewRenderbuffer) {
    glDeleteRenderbuffers(1, &viewRenderbuffer), viewRenderbuffer = 0;
}