2012-09-25 54 views
4

我需要在Qt5項目中執行多重採樣,但我不確定如何使用QOpenGLFrameBufferObject來執行FSAA。就我搜索的文檔而言,沒有關於如何執行此操作的示例,文檔僅提及:「如果要使用啓用多重採樣的幀緩衝區對象作爲紋理,則首先需要使用QOpenGLContext將其複製到常規幀緩衝區對象。:: blitFramebuffer()」我的代碼目前看起來是這樣的:Qt5使用QOpenGLFrameBufferObject的OpenGL多重採樣類

//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

嗨,這個解決方案的工作? – SRINI794

回答

4

你並不需要使用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);