为什么多重采样不起作用?

时间:2012-02-29 00:41:13

标签: ios opengl-es multisampling

我正试图在我的OpenGL ES应用程序中进行多重采样。

帧缓冲设置代码:

glGenFramebuffersOES(1, &framebuffer);
glGenRenderbuffersOES(1, &colorRenderbuffer);

glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);  
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer);

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

glGenFramebuffersOES(1, &sampleFramebuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, sampleFramebuffer);

glGenRenderbuffersOES(1, &sampleColorRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, sampleColorRenderbuffer);
glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER_OES, 4, GL_RGBA8_OES, backingWidth, backingWidth);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, sampleColorRenderbuffer);

glGenRenderbuffersOES(1, &sampleDepthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, sampleDepthRenderbuffer);
glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER_OES, 4, GL_DEPTH_COMPONENT16_OES, backingWidth, backingWidth);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, sampleDepthRenderbuffer);

渲染代码:

glBindFramebufferOES(GL_RENDERBUFFER_OES, sampleFramebuffer);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

GLfloat vertices[] = {
    1.0f, 1.0f, 0.0f,
    2.0f, 4.0f, 0.0f,
    1.0f, 3.0f, 0.0f
};
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);

glBindFramebufferOES(GL_READ_FRAMEBUFFER_APPLE, sampleFramebuffer);
glBindFramebufferOES(GL_DRAW_FRAMEBUFFER_APPLE, framebuffer);
glResolveMultisampleFramebufferAPPLE();

glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

结果:

Rendering result

正如你所看到的,三角形仍然有别名的边。

2 个答案:

答案 0 :(得分:2)

sampleFramebuffer是framebuffer,而不是renderbuffer。但是你在渲染代码中将它绑定为renderbuffer。那是错的。您应该通过调用glGetError检查OpenGL错误 - 这将告诉您哪个GL调用无效。

您的渲染代码应从以下行开始:

glBindFramebufferOES(GL_FRAMEBUFFER_OES, sampleFramebuffer);

其余的都可以。但是为了提高性能,您可以在Resolve调用后对多重采样帧缓冲区使用glDiscardFramebufferEXT调用:

glResolveMultisampleFramebufferAPPLE();
const GLenum discards[]  = {GL_COLOR_ATTACHMENT0,GL_DEPTH_ATTACHMENT};
glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE,2,discards);

答案 1 :(得分:0)

Apple的多次采样并没有完全消除混叠边缘,特别是在0°,90°,180°和0°附近的角度。但它应该有助于软化它们。

在我的一些测试中,我发现它的开启和关闭都有轻微区别。而且你必须决定它是否值得你的应用程序的成本。例如,在iPhone上,像素密度非常小,以至于它对我的眼睛没有任何影响。然而,在tvOS上,混叠非常明显,就像45&#34;屏幕。