我想使用多重采样抗锯齿并将我绘制的内容保存到 BMP。我使用 FBO 进行多重采样和普通 FBO。我参考了这篇文章:GL_framebuffer_multisample。
但是当我使用glBlitFramebuffer将数据从 FBO 复制以进行多重采样到普通 FBO 时,会发生 OpenGL 错误。现在我在这里发布部分代码:
首先,创建两个 FBO:
glGenFramebuffers(1,&m_frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER,m_frameBuffer);
glGenRenderbuffers(1,&m_renderBufferColor);
glBindRenderbuffer(GL_RENDERBUFFER,m_renderBufferColor);
//在FBO里开启多重采样
glRenderbufferStorageMultisample(GL_RENDERBUFFER,4,
GL_RGB,m_subImageWidth,m_subImageHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,
GL_RENDERBUFFER,m_renderBufferColor);
glGenRenderbuffers(1,&m_renderBufferDepth);
glBindRenderbuffer(GL_RENDERBUFFER,m_renderBufferDepth);
glRenderbufferStorageMultisample(GL_RENDERBUFFER,4,
GL_DEPTH_COMPONENT24,m_subImageWidth,m_subImageHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER,m_renderBufferDepth);
glBindFramebuffer(GL_FRAMEBUFFER,0);//解除绑定
//generate standard FBO for blitting
glGenFramebuffers(1,&m_frameBufferBlit);
glBindFramebuffer(GL_FRAMEBUFFER,m_frameBufferBlit);
glGenRenderbuffers(1,&m_renderBufferColorBlit);
glBindRenderbuffer(GL_RENDERBUFFER,m_renderBufferColorBlit);
glRenderbufferStorage(GL_RENDERBUFFER,GL_RGB,
m_subImageWidth,m_subImageHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,
GL_RENDERBUFFER,m_renderBufferColorBlit);
glGenRenderbuffers(1,&m_renderBufferDepthBlit);
glBindRenderbuffer(GL_RENDERBUFFER,m_renderBufferDepthBlit);
glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH_COMPONENT24,
m_subImageWidth,m_subImageHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER,m_renderBufferDepth);
glBindFramebuffer(GL_FRAMEBUFFER,0);//解除绑定
然后画点东西:
glBindFramebuffer(GL_FRAMEBUFFER,m_frameBuffer);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
{
cout << "The frame buffer status is not complete!" << endl;
return;
}
drawing();//draw something
glBindFramebuffer(GL_FRAMEBUFFER,0);
int temp1 = GLUtils::checkForOpenGLError(__FILE__,__LINE__);
//blitting
glBindFramebuffer(GL_DRAW_FRAMEBUFFER,m_frameBufferBlit);
glBindFramebuffer(GL_READ_FRAMEBUFFER,m_frameBuffer);
int temp2 = GLUtils::checkForOpenGLError(__FILE__,__LINE__);
glBlitFramebuffer(0,0,m_subImageWidth,m_subImageHeight,
0,0,m_subImageWidth,m_subImageHeight,
GL_COLOR_BUFFER_BIT,GL_LINEAR);
int temp3 = GLUtils::checkForOpenGLError(__FILE__,__LINE__);
glBindFramebuffer(GL_READ_FRAMEBUFFER,0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0);
//glBindFramebuffer(GL_FRAMEBUFFER,m_frameBuffer);
glBindBuffer(GL_PIXEL_PACK_BUFFER,m_subImageBuffer[i]);
glPixelStorei(GL_PACK_ALIGNMENT,1);
glBindFramebuffer(GL_FRAMEBUFFER,m_frameBufferBlit);
//注意:以BGR的顺序读取
glReadPixels(0,0,m_subImageWidth,m_subImageHeight,
GL_BGR,GL_UNSIGNED_BYTE,bufferOffset(0));
int temp4 = GLUtils::checkForOpenGLError(__FILE__,__LINE__);
m_subPixels[i] = static_cast<GLubyte*>(glMapBuffer(GL_PIXEL_PACK_BUFFER,GL_READ_ONLY));
if (m_subPixels[i] == NULL)
{
cout << "NULL pointer" << endl;
}
gltGenBMP(subImageFile[i],GLT_BGR,m_subImageWidth,m_subImageHeight,m_subPixels[i]);
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
glBindBuffer(GL_PIXEL_PACK_BUFFER,0);
glBindFramebuffer(GL_FRAMEBUFFER,0);
函数GLUtils::checkForOpenGLError用于检查是否发生 OpenGL 错误(返回 1 表示发生错误)。绘图是渲染东西。函数gltGenBMP是将数据保存为BMP。它们都工作得很好。我发现temp1和temp2是0,temp3和temp4是1。所以我得出一个结论:有问题使用函数glBlitFramebuffer。但是为什么呢?是否有任何设置错误?
更新: 当我检查正常的 FBO m_frameBufferBlit 时:
glBindFramebuffer(GL_DRAW_FRAMEBUFFER,m_frameBufferBlit);
status = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
{
cout << "The frame buffer status is not complete!" << endl;
return;
}
我发现它不完整。所以我没有正确创建 FBO?
有什么问题?