解决自ios5以来的多重采样frambuffer错误

时间:2011-11-19 20:33:17

标签: xcode opengl-es ios5 antialiasing

我想在ios5支持的项目中遇到一些问题。我有一个应用程序的代码,包含使用opengl-es的3d渲染和使用多重采样帧缓冲的抗锯齿。该代码在ios4.3上运行良好。从ios5开始,3d模型将无法渲染。在模拟器上,我只得到一个粉红色的屏幕。

经过一些测试后我发现了那个功能

glResolveMultisampleFramebufferAPPLE()

是问题所在。此功能引发错误。 0x502

NSLog(@"0x%x", glGetError()); // "0x502"

我不知道发生了什么以及为什么这个功能不适用于ios5。可以请任何人帮助我吗?以下是一些创建帧缓冲区的代码。

//first destroy frame buffers
glDeleteFramebuffersOES(1, &viewFramebuffer);
viewFramebuffer = 0;
glDeleteRenderbuffersOES(1, &viewRenderbuffer);
viewRenderbuffer = 0;

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

// Generate IDs for a framebuffer object and a color renderbuffer
glGenFramebuffersOES(1, &viewFramebuffer);
glGenRenderbuffersOES(1, &viewRenderbuffer);

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
// This call associates the storage for the current render buffer with the EAGLDrawable (our CAEAGLLayer)
// allowing us to draw into a buffer that will later be rendered to screen whereever the layer is (which corresponds with our view).
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);

//Width and height der viewBoundingBox in Pixeln
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);

//Generate our MSAA Frame and Render buffers 
glGenFramebuffersOES(1, &msaaFramebuffer); 
glGenRenderbuffersOES(1, &msaaRenderBuffer); 

//Bind our MSAA buffers 
glBindFramebufferOES(GL_FRAMEBUFFER_OES, msaaFramebuffer); 
glBindRenderbufferOES(GL_RENDERBUFFER_OES, msaaRenderBuffer);

// Generate the msaaDepthBuffer. 
// 4 will be the number of pixels that the MSAA buffer will use in order to make one pixel on the render buffer. 
glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER_OES, 4,GL_RGB565_OES, backingWidth, backingHeight); 
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, msaaRenderBuffer); 
glGenRenderbuffersOES(1, &msaaDepthBuffer);   

//Bind the msaa depth buffer. 
glBindRenderbufferOES(GL_RENDERBUFFER_OES, msaaDepthBuffer); 
glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER_OES, 4, GL_DEPTH_COMPONENT16_OES, backingWidth , backingHeight); 
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, msaaDepthBuffer);



// Make sure that you are drawing to the current context
[EAGLContext setCurrentContext:context];

glBindFramebufferOES(GL_FRAMEBUFFER_OES, msaaFramebuffer); 
[controller drawView:self];                                 

//Bind both MSAA and View FrameBuffers
glBindFramebufferOES(GL_READ_FRAMEBUFFER_APPLE, msaaFramebuffer);
glBindFramebufferOES(GL_DRAW_FRAMEBUFFER_APPLE, viewFramebuffer);

if((err = glGetError())) NSLog(@"%x error in line %u in method %s", err, __LINE__, __FUNCTION__);

// Call a resolve to combine both buffers
glResolveMultisampleFramebufferAPPLE();

if((err = glGetError())) NSLog(@"%x error in line %u in method %s", err, __LINE__, __FUNCTION__);

// Use discard to improve fill rate and overall performance!
GLenum attachments[] = {GL_DEPTH_ATTACHMENT_OES, GL_COLOR_ATTACHMENT0_OES, GL_STENCIL_ATTACHMENT_OES}; 
glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 3, attachments);

// Present final image to screen
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

1 个答案:

答案 0 :(得分:1)

对我来说,这个问题是由传递给glRenderbufferStorageMultisampleAPPLE的格式引起的。

请尝试使用GL_RGBA8_OESGL_RGB5_A1_OES