2009-04-19 266 views
7

我有一個FBO對象,其顏色和深度附件呈現給我,然後使用glReadPixels()進行讀取,我試圖向它添加多重採樣支持。
而不是glRenderbufferStorage()我打電話glRenderbufferStorageMultisampleEXT()爲顏色附件和深度附件。幀緩衝區對象似乎已成功創建並報告爲完整。
渲染後,我試圖從glReadPixels()中讀取它。當樣本數量爲0時,即多重採樣禁用它可以很好地工作,並且獲得我想要的圖像。當我將樣本數量設置爲其他值時,比如說4,幀緩衝區仍然構造成OK,但glReadPixels()失敗,出現INVALID_OPERATIONFBO中的glReadPixels失敗,出現多重採樣

任何人都有一個想法這裏有什麼可能是錯誤的?

編輯:glReadPixels的代碼:

glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, ptr); 

其中ptr指向寬×高的uint的陣列。

+0

你可以發佈你的glReadPixels()調用的全系列(格式,類型等)? – 2009-04-29 07:03:27

回答

23

我不認爲你可以從一個多采樣的FBO讀取glReadPixels()。您需要從多重採樣的FBO向正常的FBO閃爍,綁定正常的FBO,然後從正常的FBO讀取像素。

事情是這樣的:

// Bind the multisampled FBO for reading 
glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, my_multisample_fbo); 
// Bind the normal FBO for drawing 
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, my_fbo); 
// Blit the multisampled FBO to the normal FBO 
glBlitFramebufferEXT(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST); 
//Bind the normal FBO for reading 
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, my_fbo); 
// Read the pixels! 
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 
+0

「我不認爲你可以從多采樣讀取」 - 有任何參考? – shoosh 2009-04-29 18:46:14

1

你不能用glReadPixels直接讀取多重採樣緩衝區,因爲它會引發GL_INVALID_OPERATION錯誤。您需要對另一個表面進行blit處理,以便GPU可以進行縮減採樣。你可以向後緩衝區溢出,但存在「像素所有者測試」的問題。最好再做一個FBO。假設你做了另一個FBO,現在你想要blit。這需要GL_EXT_framebuffer_blit。通常,當您的驅動程序支持GL_EXT_framebuffer_multisample時,它也支持GL_EXT_framebuffer_blit,例如nVidia Geforce 8系列。

//Bind the MS FBO 
glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, multisample_fboID); 
//Bind the standard FBO 
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, fboID); 
//Let's say I want to copy the entire surface 
//Let's say I only want to copy the color buffer only 
//Let's say I don't need the GPU to do filtering since both surfaces have the same dimension 
glBlitFramebufferEXT(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST); 
//-------------------- 
//Bind the standard FBO for reading 
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboID); 
glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, pixels); 

來源:GL EXT framebuffer multisample