使用深度渲染缓冲区有什么问题? OpenGL ES 2.0

时间:2010-12-07 15:12:13

标签: iphone opengl-es depth-buffer

我使用此代码:

context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    if (!context || ![EAGLContext setCurrentContext:context] || ![self loadShaders])
    {
        [self release];
        return nil;
    }

    glGenFramebuffers(1, &defaultFramebuffer);
    glGenRenderbuffers(1, &colorRenderbuffer);
    glGenRenderbuffers(1, &depthRenderbuffer);

    glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);

    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

但是当我运行应用程序时,我看到紫色的屏幕?这段代码出了什么问题?

2 个答案:

答案 0 :(得分:6)

我没有看到你将颜色渲染缓冲区绑定到它要显示的CAEAGLLayer的位置(虽然这可能会在以后发生),而且我没有看到你启用深度测试。另外,如果我没有弄错,你需要绑定颜色渲染缓冲区,为此调用glFramebufferRenderbuffer(),然后绑定深度渲染缓冲区并再次调用它。

以下是我用于在OpenGL ES 2.0上设置类似显示的代码:

glEnable(GL_DEPTH_TEST);

glGenFramebuffers(1, &viewFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);

glGenRenderbuffers(1, &viewRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);

[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];

glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, viewRenderbuffer);

glGenRenderbuffers(1, &depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, backingWidth, backingHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);

if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) 
{
    NSLog(@"Failure with framebuffer generation");
    return NO;
}

此应用程序的完整代码可用here

答案 1 :(得分:1)

我意识到这是多大年纪,但是我用相同的结果做了完全相同的事情,所以我想我会做出贡献。在我的项目中发生的事情是我在设置渲染缓冲区时在自动完成时过快地按下回车键,结果如下:

glGenBuffers(1, &_colorRenderBuffer);

而不是:

glGenRenderbuffers(1, &_colorRenderBuffer);

最终结果:紫色屏幕。