【问题标题】:Depth Testing not working when using QOpenGLFramebufferObject使用 QOpenGLFramebufferObject 时深度测试不起作用
【发布时间】:2019-08-30 13:50:25
【问题描述】:

当我使用QOpenGLFramebufferObject 时,我遇到了深度测试不起作用的问题。

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

代码如下:

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 中的深度信息。

有什么想法可以解决吗?

【问题讨论】:

    标签: c++ qt opengl depth-testing


    【解决方案1】:

    在绑定帧缓冲区之后和绘制到它之前,您必须清除帧缓冲区的深度缓冲区和颜色缓冲区。
    如果深度缓冲区没有被清除,那么Depth Test 将会失败并且什么都不会被绘制。

    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();
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-01
      • 1970-01-01
      • 2022-10-22
      • 2016-05-18
      • 2012-03-09
      相关资源
      最近更新 更多