(阴影贴图)将深度缓冲区渲染到纹理并绘制它以进行检查

时间:2013-08-05 14:54:39

标签: macos opengl buffer depth fbo

我一直试图让阴影贴图起作用,但我可以;甚至超过它的深度缓冲区部分。 yadayadayada,我一直在网上看几个小时。这是我到目前为止的代码 -

制作FBO和纹理深度:

GLuint framebuffer;
    GLuint depth;
    GLuint texture;

    glGenFramebuffers(1, &framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);

    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1280, 800, 0, GL_RGBA, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glBindTexture(GL_TEXTURE_2D, 0);


    glGenTextures(1, &depth);
    glBindTexture(GL_TEXTURE_2D, depth);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 1280, 800, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glBindTexture(GL_TEXTURE_2D, 0);




    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth, 0);
    //glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
    glDrawBuffer(GL_NONE);


    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        std::cout << "There was a problem with making the framebuffer\n";
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    return framebuffer;

将“深度”渲染到四边形以供查看:

 glBindFramebuffer(GL_FRAMEBUFFER, FBO);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   for (int i = 0; i < objects.size(); i++)
   {
    objects[i].renderObjectForDepth();
   }
   glBindFrameBuffer(GL_TEXTURE_2D,0);
   glBindTexture2D(GL_TEXTURE_2D, texture)
   drawQuad();

对我而言,一切看起来都很好,但它会生成像附图所示的图像。我已经把头发撕掉好几天了,任何帮助都会受到赞赏。

编辑:这是因为我渲染了空白纹理,IE是从未写过的纹理颜色。绘制深度纹理并编写颜色纹理后,它可以正常工作。但是如何在没有颜色纹理的情况下做到这一点?

The 'depth buffer being rendered'

1 个答案:

答案 0 :(得分:2)

在渲染到仅深度FBO时,检查是否设置了以下两种状态:

glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);