OpenGL无效操作:将深度渲染缓冲区复制到默认深度缓冲区

时间:2013-06-06 10:20:16

标签: opengl framebuffer depth-buffer

我试图关注Copying depth render buffer to the depth buffer,但它对我不起作用。这是我的完整示例:

#include <GL/glew.h>
#include <GL/glfw.h>
#include <GL/glu.h>
#include <iostream>

typedef unsigned int uint;

int main()
{
    if (glfwInit() != 1)
    {
        return 1;
    }

    const uint width = 256;
    const uint height = 256;

    if (glfwOpenWindow(width, height, 8, 8, 8, 0, 24, 0, GLFW_WINDOW) != 1)
    {
        return 1;
    }

    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        std::cerr << glewGetErrorString(err) << std::endl;
        return 1;
    }

    GLuint r;
    glGenRenderbuffers(1, &r);
    glBindRenderbuffer(GL_RENDERBUFFER, r);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
    glBindRenderbuffer(GL_RENDERBUFFER, 0);

    GLuint r2;
    glGenRenderbuffers(1, &r2);
    glBindRenderbuffer(GL_RENDERBUFFER, r2);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA4, width, height);
    glBindRenderbuffer(GL_RENDERBUFFER, 0);

    GLuint f;
    glGenFramebuffers(1, &f);
    glBindFramebuffer(GL_FRAMEBUFFER, f);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, r);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, r2);
    std::cout << (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) << std::endl;
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    std::cout << gluErrorString(glGetError()) << std::endl;

    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    glBindFramebuffer(GL_READ_FRAMEBUFFER, f);
    glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_DEPTH_BUFFER_BIT, GL_NEAREST);

    std::cout << gluErrorString(glGetError()) << std::endl;

    return 0;
}

输出我得到:

1
no error
invalid operation

24中的glfwOpenWindow是深度位数。我做错了什么?

2 个答案:

答案 0 :(得分:2)

glfwOpenWindow的参数指定了所需的值。但是,您的驱动程序完全有可能不支持您要求的确切格式(24位颜色,24位深度,无alpha,无模板)。 GLFW将在满足您的最低要求的同时尝试找到最佳匹配。

在这种情况下,它可能会为您提供最标准格式的RGBA颜色(32位)和24/8位深度/模板缓冲区,或者它可能会创建一个32位深度缓冲区。您实际上需要调用glfwGetWindowParam来查询默认帧缓冲区的深度和模板位,然后构建渲染缓冲区以匹配它。

答案 1 :(得分:0)

正如ThomasD在评论GL_DEPTH24_STENCIL8中所建议的那样。