glBlitFramebuffer导致OpenGL错误

时间:2012-06-25 01:43:15

标签: opengl graphics

我想使用多重采样消除锯齿并将我绘制的内容保存到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表示发生错误)。绘图是渲染内容。函数< strong> 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?

有什么问题?

1 个答案:

答案 0 :(得分:1)

回忆起帧缓冲完整性的条件可能很有用:

  • 必须至少有一个图像附加到帧缓冲区
  • 所有附件必须是附件完整
  • 所有活动的绘制缓冲区必须具有关联的图像
  • 读取缓冲区(如果处于活动状态)必须具有关联的图像
  • 所有附加图像必须具有相同数量的样本

以及附加图像完整性的条件:

  • 图像必须具有非零宽度和高度
  • 图像内部格式必须与附着点匹配 (例如:深度图像必须附加到深度附件

请注意,这些规则是real rules的简化,稍微复杂一些。