我需要在Qt5项目中执行多重采样,但我不确定如何使用QOpenGLFrameBufferObject来执行FSAA。在我搜索时没有关于如何执行此操作的示例,文档仅提到:“如果要使用启用了多重采样的帧缓冲对象作为纹理,首先需要使用QOpenGLContext从它复制到常规帧缓冲区对象:: blitFramebuffer()。“我的代码目前看起来像这样:
//Enable FSAA for better output
int vp[4];
glGetIntegerv(GL_VIEWPORT, vp);
if(m_lpFBO == NULL)
{
//MultiSampling set to 4 now
QOpenGLFramebufferObjectFormat format;
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
format.setMipmap(true);
format.setSamples(4);
format.setTextureTarget(GL_TEXTURE_2D);
format.setInternalTextureFormat(GL_RGBA32F_ARB);
//Create the FBO
m_lpFBO = new QOpenGLFramebufferObject(vp[2], vp[3], format);
m_lpFBOSurface = new QGLFramebufferObjectSurface(m_lpFBO);
}
QRect rc(0, 0, vp[2], vp[3]);
QGLSubsurface sub(lpPainter->currentSurface(), rc);
m_lpFBO->bind();
sub.setFramebufferObject(m_lpFBO);
lpPainter->pushSurface(&sub);
//Draw as usual
.
.
.
lpPainter->popSurface();
//Now Copy
QOpenGLFramebufferObject::blitFramebuffer(lpPainter->currentSurface()->framebufferObject(), rc, m_lpFBO, rc);
答案 0 :(得分:5)
您不需要使用QGLFramebufferObjectSurface来执行必要的下采样,因为您可以使用两个QGLFramebufferObjects。 QOpenGLFramebufferObject :: blitFramebuffer(调用glBlitFramebuffer)将自动管理从源帧缓冲目标到目标帧缓冲目标的下采样(或上采样)。 blitFramebuffer还允许您指定如何使用(GL_NEAREST或GL_LINEAR)计算转换以及要传输的附件(GL_COLOR_BUFFER_BIT,GL_DEPTH_BUFFER_BIT,GL_STENCIL_BUFFER_BIT的按位组合)。
因此,根据您的需要,您需要创建2个QOpenGLFramebufferObjects,其中一个将包含要渲染到的多重采样纹理,其中一个将包含下采样结果纹理。然后,您将使用QOpenGLFramebufferObject :: blitFramebuffer将源纹理下采样到结果纹理中。
这是一个简单的例子:
//MultiSampling set to 4 now
QOpenGLFramebufferObjectFormat muliSampleFormat;
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
format.setMipmap(true);
format.setSamples(4);
format.setTextureTarget(GL_TEXTURE_2D);
format.setInternalTextureFormat(GL_RGBA32F_ARB);
QOpenGLFramebufferObject multiSampledFBO = QOpenGLFramebufferObject(width,height muliSampleFormat);
QOpenGLFramebufferObjectFormat downSampledFormat;
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
format.setMipmap(true);
format.setTextureTarget(GL_TEXTURE_2D);
format.setInternalTextureFormat(GL_RGBA32F_ARB);
QOpenGLFramebufferObject downSampledFBO = QOpenGLFramebufferObject(width,height downSampledFormat);
然后,每次需要缩减采样(渲染到多重采样纹理后),只需执行此类操作即可。 (使用您首选的过滤方式)
QOpenGLFramebufferObject::blitFramebuffer(downSampledFBO,multiSampledFBO,GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT| GL_STENCIL_BUFFER_BIT,GL_NEAREST);