2

我一直在尝试让阴影贴图工作,但我什至无法通过它的深度缓冲区部分。我已经在互联网上找了几个小时了,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();

对我来说,一切看起来都很好,但它会生成像所附图片一样的图像。我已经扯掉我的头发好几天了,任何帮助将不胜感激。

编辑:这是因为我正在渲染一个空白纹理,即从未被写入的纹理颜色。绘制深度纹理并写入颜色纹理后,它工作正常。但是没有颜色纹理我该怎么做呢?

“正在渲染的深度缓冲区”

4

1 回答 1

2

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

glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
于 2013-08-05T15:31:24.560 回答