我需要在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);
您不需要使用QGLFramebufferObjectSurface
来执行必要的下采样,因为您只需使用两个QGLFramebufferObject
。 QOpenGLFramebufferObject::blitFramebuffer
(调用 glBlitFramebuffer
)将自动管理从源帧缓冲目标到目标帧缓冲目标的下采样(或上采样)。 blitFramebuffer
还允许您指定如何使用(GL_NEAREST
或GL_LINEAR
)计算转换以及要传输哪些附件(GL_COLOR_BUFFER_BIT
、GL_DEPTH_BUFFER_BIT
、GL_STENCIL_BUFFER_BIT
的按位组合)。
因此,根据您的需要,您需要创建 2 个 QOpenGLFramebufferObject
秒,其中一个将包含要渲染到的多重采样纹理,另一个将包含下采样结果纹理。然后,您将使用 QOpenGLFramebufferObject::blitFramebuffer
将源纹理缩减采样为结果纹理。
下面是一个快速示例:
// MultiSampling set to 4 now
QOpenGLFramebufferObjectFormat muliSampleFormat;
muliSampleFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
muliSampleFormat.setMipmap(true);
muliSampleFormat.setSamples(4);
muliSampleFormat.setTextureTarget(GL_TEXTURE_2D);
muliSampleFormat.setInternalTextureFormat(GL_RGBA32F_ARB);
QOpenGLFramebufferObject multiSampledFBO(width, height, format);
QOpenGLFramebufferObjectFormat downSampledFormat;
downSampledFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
downSampledFormat.setMipmap(true);
downSampledFormat.setTextureTarget(GL_TEXTURE_2D);
downSampledFormat.setInternalTextureFormat(GL_RGBA32F_ARB);
QOpenGLFramebufferObject downSampledFBO(width, height, downSampledFormat);
然后,每次需要缩减采样(渲染到多重采样纹理后)时,只需执行类似操作即可。(使用您喜欢的过滤)
QOpenGLFramebufferObject::blitFramebuffer(
&downSampledFBO, &multiSampledFBO,
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);