2

我遇到了使用QOpenGLFramebufferObject.

但是,如果我将默认帧缓冲区复制到QOpenGLFramebufferObjectusing中,它会起作用glBlitFramebuffer。我不知道原因。

这是代码:

void SceneView3D::paintGL()
{
    QOpenGLContext *ctx = QOpenGLContext::currentContext();

    QOpenGLFramebufferObjectFormat fboFormat;
    fboFormat.setSamples(0);
    fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
    m_framebuffer = new QOpenGLFramebufferObject(this->width(), this->height(), fboFormat);

    m_framebuffer->bind();
// --------------------------------------
// draw scene here
//-------------------------------------
    QImage image = m_framebuffer->toImage();
    image.save(R"(image.jpg)");
    m_framebuffer->release();
}

我创建了QOpenGLFramebufferObject并且已经设置了深度附件,但在输出图像中没有绘制任何内容。似乎颜色附件丢失了。

但是如果我在绘图之前添加这些代码,它就起作用了。

glBindFramebuffer(GL_READ_FRAMEBUFFER, defaultFramebufferObject());
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_framebuffer->handle());
ctx->extraFunctions()->glBlitFramebuffer(0, 0, width(), height(), 0, 0, m_framebuffer->width(), m_framebuffer->height(), GL_DEPTH_BUFFER_BIT, GL_NEAREST);

所以,我不知道为什么 defaultFramebufferObject 中的深度信息而不是我创建的 m_framebuffer 中的深度信息。

有什么想法可以解决吗?

4

1 回答 1

1

在绑定帧缓冲区之后和绘制到它之前,您必须清除帧缓冲区的深度缓冲区和颜色缓冲区。
如果深度缓冲区没有被清除,那么深度测试将失败并且根本没有绘制任何内容。

m_framebuffer->bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

// draw scene here
// [...]

QImage image = m_framebuffer->toImage();
image.save(R"(image.jpg)");
m_framebuffer->release();

注意,默认帧缓冲区的深度缓冲区被清除(可能)。将“清除”的深度缓冲区复制到帧缓冲区的深度缓冲区,也会“清除”它。

于 2019-04-09T17:58:34.370 回答