帧缓冲对象(FBO)具有多重采样但绘制了空白图像

时间:2015-08-15 20:41:29

标签: opengl fbo multisampling

我尝试使用帧缓冲对象进行离屏渲染。在非多次采样时,一切都很好;但当我把它变成多次采样时,我只得到一张空白图像。我的代码如下:

GLuint textureId, rboId, fboId;
int err;
// 1. Create a texture.
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textureId);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGBA, w, h, GL_TRUE);
err = glGetError(); // <== err = 0, no error.

// 2. Create depth render buffer: (Optional)
glGenRenderbuffers(1, &rboId);
glBindRenderbuffer(GL_RENDERBUFFER, rboId);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT, w, h);
err = glGetError(); // <== err = 0, no error.

// 3. Create and bind the frame buffer
glGenFramebuffers(1, &fboId);
glBindFramebuffer(GL_FRAMEBUFFER, fboId);
err = glGetError(); // <== err = 0, no error.

// 4. Attach the texture to FBO color attachment point
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, textureId, 0);
err = glGetError(); // <== err = 0, no error.

// 5. Attach the depth info to FBO depth attachment point
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboId);
err = glGetError(); // <== err = 0, no error.

// check FBO status
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); // status = GL_FRAMEBUFFER_COMPLETE

然后,我绘制场景并读取像素:

draw();
cv::Mat Img(h,w,CV_8UC4);
glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, Img.data); // <== Img contains nothing.

我哪里出错了?

更新: 我尝试了以下代码来读取像素,但问题仍然存在,为什么?

...
draw(); 
// then I tried "blit" the multisampled fbo to a normal fbo for reading.
GLuint normalFboId;
glGenFramebuffers(1, &normalFboId);
glBindFramebuffer(GL_FRAMEBUFFER, normalFboId);
glBindFramebuffer(GL_READ_FRAMEBUFFER, fboId);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, normalFboId);
glDrawBuffer(GL_BACK);
glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_FRAMEBUFFER, normalFboId);

cv::Mat Img(h, w, CV_8UC4, cv::Scalar(0,0,255,255));
glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, Img.data);

0 个答案:

没有答案