Qt5 OpenGL Multisampling using the QOpenGLFrameBufferObject class

I need to perform multisampling in a Qt5 project, but I'm not sure how to use QOpenGLFrameBufferObject to execute FSAA. There is no example on how to do this if I searched, and the documentation only mentions: โ€œIf you want to use a framebuffer object with multisampling included as a texture, you must first copy it from a regular framebuffer object using QOpenGLContext :: blitFramebuffer () " Currently my code is as follows:

//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); 
+4
source share
1 answer

You do not need to use QGLFramebufferObjectSurface to do the necessary downsampling, as you can just use two QGLFramebufferObject . QOpenGLFramebufferObject::blitFramebuffer (which calls glBlitFramebuffer ) will automatically control downsampling (or glBlitFramebuffer ) from the target of the source frame buffer to the target of the target frame buffer. blitFramebuffer also allows you to dictate how the transform is calculated using ( GL_NEAREST or GL_LINEAR ) and which attachments should be transferred ( GL_COLOR_BUFFER_BIT combination GL_COLOR_BUFFER_BIT , GL_DEPTH_BUFFER_BIT , GL_STENCIL_BUFFER_BIT ).

So, for your needs, you want to create 2 QOpenGLFramebufferObject , where one will contain a multisampled texture that will be rendered, and the other will contain a downsampled texture. You will then use QOpenGLFramebufferObject::blitFramebuffer to reduce the original texture to the final texture.

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 reduce the sampling rate (after rendering to a multi-sampled texture), just do something like this. (using preferred filtering)

 QOpenGLFramebufferObject::blitFramebuffer( &downSampledFBO, &multiSampledFBO, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST); 
+6
source

Source: https://habr.com/ru/post/1436249/


All Articles