[英]Qt5 OpenGL Multisampling using QOpenGLFrameBufferObject class
I need to perform multisampling in a Qt5 project but I am not sure how to use QOpenGLFrameBufferObject to perform FSAA. 我需要在Qt5项目中执行多次采样,但不确定如何使用QOpenGLFrameBufferObject执行FSAA。 There is no example on how to do this as far as I searched and the documentation only mentions: “If you want to use a framebuffer object with multisampling enabled as a texture, you first need to copy from it to a regular framebuffer object using QOpenGLContext::blitFramebuffer().” My code currently looks like this: 就我搜索的情况而言,没有任何示例,文档仅提及:“如果要使用已启用多重采样的帧缓冲区对象作为纹理,则首先需要使用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);
You do not need to use a QGLFramebufferObjectSurface
to perform the necessary downsampling, as you can just use two QGLFramebufferObject
s. 您不需要使用QGLFramebufferObjectSurface
来执行必要的下采样,因为您可以只使用两个QGLFramebufferObject
。 QOpenGLFramebufferObject::blitFramebuffer
(which calls glBlitFramebuffer
) will automagically manage downsampling (or upsampling) from a source framebuffer target to a destination frame buffer target. QOpenGLFramebufferObject::blitFramebuffer
(调用glBlitFramebuffer
)将自动管理从源帧缓冲区目标到目标帧缓冲区目标的下采样(或上采样)。 blitFramebuffer
also lets you dictate how conversion is computed using ( GL_NEAREST
or GL_LINEAR
) and what attachments are to be transferred (a bitwise combination of GL_COLOR_BUFFER_BIT
, GL_DEPTH_BUFFER_BIT
, GL_STENCIL_BUFFER_BIT
). blitFramebuffer
还允许您指示如何使用( GL_NEAREST
或GL_LINEAR
)计算转换以及要传输的附件( GL_COLOR_BUFFER_BIT
, GL_DEPTH_BUFFER_BIT
, GL_STENCIL_BUFFER_BIT
的按位组合)。
So, for your needs you want to create 2 QOpenGLFramebufferObject
s where one will contains the multisampled texture being rendered to, and one will contain the downsampled result texture. 因此,根据您的需要,您想要创建2个QOpenGLFramebufferObject
,其中一个将包含要渲染到的多重采样纹理,一个将包含降采样的结果纹理。 Then you will use QOpenGLFramebufferObject::blitFramebuffer
to downsample the source texture into the result texture. 然后,您将使用QOpenGLFramebufferObject::blitFramebuffer
将源纹理下采样到结果纹理中。
Here is a quick example: 这是一个简单的示例:
// 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);
Then, every time you need to downsample (after rendering to your multisampled texture) simply do something like this. 然后,每次需要降采样(渲染为多采样纹理后)时,只需执行以下操作即可。 (using your preferred filtering) (使用您首选的过滤条件)
QOpenGLFramebufferObject::blitFramebuffer(
&downSampledFBO, &multiSampledFBO,
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.