OpenGL - 阴影贴图/深度贴图给出了奇怪的结果

时间:2017-07-15 19:23:09

标签: c++ opengl glsl depth-buffer shadow-mapping

我正在尝试渲染我的深度贴图,现在我将它渲染到场景中的对象上,以便了解我得到的结果是什么。据我所知,黑色值是靠近光线的物体,而白色则更远。如果我弄错了,请纠正我。这是我得到的结果:Shadow map rendered onto a cube

有了这个结果,我推测我以错误的方式创建了帧缓冲区。这就是我生成它的方式:

void createFrameBuffer()
{
    glGenFramebuffers(1, &frameBuffer);


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

    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
        SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,                                 
        GL_TEXTURE_2D, DepthMap, 0);

    glDrawBuffer(GL_NONE);
    glReadBuffer(GL_NONE);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

我们已经按照Learn OpenGL - Shadow Mapping教程进行了操作,我们的着色器几乎与他的相同。所以那里不应该有任何问题。我想我已经误解了阴影贴图的CPU部分,而不是GPU。这就是我绘制所有内容,设置矩阵和使用程序的方法。

float orthoValue = 20.0f;

glm::mat4 lightProjection = glm::ortho(-orthoValue,
    orthoValue, -orthoValue, orthoValue, NEARPLANE, FARPLANE);
glm::mat4 lightView = glm::lookAt(lightPos, glm::vec3(0.0f),
    glm::vec3(0.0, 1.0f, 0.0));
glm::mat4 lightSpaceMatrix = lightProjection * lightView;
//lightSpaceMatrix[3] = glm::vec4(lightPos, 1.0f);

if (shadows == true) {

    glUseProgram(sShaderProgram);
    glBindVertexArray(mesh.VAO);
    glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
    glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
    glClear(GL_DEPTH_BUFFER_BIT);
    glActiveTexture(GL_TEXTURE0);


    glUniformMatrix4fv(glGetUniformLocation(sShaderProgram, "lightSpaceMatrix"),
        1, GL_FALSE, glm::value_ptr(lightSpaceMatrix));
    glUniformMatrix4fv(glGetUniformLocation(sShaderProgram, "modelMatrix"),
        1, GL_FALSE, glm::value_ptr(mesh.modelMatrix));

    glDrawArrays(GL_TRIANGLES, 0, mesh.vertices.size());
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glBindVertexArray(0);

    setViewPort();
}

我非常感谢所有的提示和提前帮助。如果我遗漏了重要信息,我会添加遗漏的内容。

渲染部分编辑的更多代码,也按我们绘制的方式编辑:

这是主循环

                glClearColor(0.0, 0.0, 0.5, 1);
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                glDisable(GL_CULL_FACE);
                glEnable(GL_LIGHTING);

                //Keyboard input
                keyPressed();
                //Mouse movement
                MousePointer();

                lightProjection = glm::ortho(-orthoValue,
                    orthoValue, -orthoValue, orthoValue, NEARPLANE, FARPLANE);
                glm::lookAt(lightPos, glm::vec3(0.0f),
                    glm::vec3(0.0, 1.0, 0.0));
                lightSpaceMatrix = lightProjection * lightView;

                glUseProgram(sShaderProgram);
                glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
                glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
                glClear(GL_DEPTH_BUFFER_BIT);

                glActiveTexture(GL_TEXTURE0);
                glBindTexture(GL_TEXTURE_2D, DepthMap);

                GLint samplerLoc = glGetUniformLocation(sShaderProgram, "DepthMap");
                glUniform1i(samplerLoc, 0);

                glUniformMatrix4fv(glGetUniformLocation(sShaderProgram, "lightSpaceMatrix"),
                    1, GL_FALSE, glm::value_ptr(lightSpaceMatrix));

                for (int i = 0; i < objmesh.size(); i++) {
                    RenderShadows(*objmesh[i]);
                }
                glBindFramebuffer(GL_FRAMEBUFFER, 0);
                setViewPort();
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                //glDisable(GL_DEPTH_TEST);

                for (int i = 0; i < objmesh.size(); i++){
                    RenderVertices(gShaderProgram, *objmesh[i], true);
                }

            }

这里我们有使用shadow glsl程序的RenderShadows函数:

void RenderShadows(OBJMeshes mesh){
glUniformMatrix4fv(glGetUniformLocation(sShaderProgram, "modelMatrix"),
    1, GL_FALSE, glm::value_ptr(mesh.modelMatrix));

glBindVertexArray(mesh.VAO);
glDrawArrays(GL_TRIANGLES, 0, mesh.vertices.size());
glBindVertexArray(0);

}

最后渲染几何体的常规渲染函数:

void RenderVertices(GLuint shaderProgram, OBJMeshes mesh, bool shadows) {

GLuint CPUValuesID = glGetUniformLocation(gUbo, "MVP");

glBindBuffer(GL_UNIFORM_BUFFER, gUbo);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(CPUMatricies), &globalCPU);

glBindBuffer(GL_UNIFORM_BUFFER, mtlBuff);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(mtlValues), &mtl);

glUniformMatrix4fv(CPUValuesID, 1, GL_FALSE, &globalCPU.MVP[0][0]);

glUseProgram(shaderProgram);
glBindVertexArray(mesh.VAO);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mesh.texture);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, DepthMap);

GLint samplerLoc = glGetUniformLocation(shaderProgram, "DepthMap");
glUniform1i(samplerLoc, 1);

glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "modelMatrix"),
    1, GL_FALSE, glm::value_ptr(mesh.modelMatrix));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "lightSpaceMatrix"),
    1, GL_FALSE, glm::value_ptr(lightSpaceMatrix));

glUniform3f(glGetUniformLocation(shaderProgram, "lightPos"), lightPos.x, lightPos.y, lightPos.z);

glDrawArrays(GL_TRIANGLES, 0, mesh.vertices.size());

glBindVertexArray(0);

}

1 个答案:

答案 0 :(得分:2)

如果要将深度缓冲区渲染到纹理,则必须将具有GL_DEPTH_COMPONENT格式的纹理绑定到 渲染帧缓冲区的缓冲区。你所做的是将纹理绑定到帧缓冲区的颜色计划0,因为你使用GL_COLOR_ATTACHMENT0来绑定代码中的纹理。

glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ColorMap0, 0 );

您需要做的是创建一个渲染缓冲区并将其绑定到帧缓冲区。

GLuint frameBuffer;
glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);

GLuint renderbuffer;
glGenRenderbuffers( 1, &renderbuffer );
glBindRenderbuffer( GL_RENDERBUFFER, renderbuffer );

然后你必须使用GL_DEPTH_ATTACHMENT

将深度纹理绑定到帧缓冲区
glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, DepthMap, 0 ); 

请注意,您可以将深度缓冲区和所有颜色平面的单独纹理绑定到帧缓冲区。

正确完成后,您必须绘制场景并填充缓冲区和/或纹理。

// enable the depth test
glEnable( GL_DEPTH_TEST );

// bind frame buffer and clear the frame buffer  and the depth buffer
glBindFramebuffer( GL_FRAMEBUFFER, frameBuffer );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)`

// draw the scene
// ...

// unbind frame buffer
glBindFramebuffer( GL_FRAMEBUFFER, 0 );

// clear the drawing buffer and the depth buffer
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

在帧缓冲区取消绑定之后,您可以使用其纹理作为阴影传递的输入。

要将纹理绑定到着色器,您必须将纹理绑定到纹理单元,并将纹理单元的索引指定给着色器的统一采样器。

将纹理绑定到纹理单元:

GLuint DepthMap;
int textureUnit = 1; 
glActiveTexture( GL_TEXTURE0 + textureUnit );
glBindTexture( GL_TEXTURE_2D, DepthMap );

使用程序并将纹理单元的索引分配给统一采样器:

GLuint depthProg = ...;
glUseProgram( depthProg );

GLint depthSamplerLocation = glGetUniformLocation( u_depthAttachment );
glUniform1i( depthSamplerLocation, textureUnit );

答案的延伸

您的代码中还有一些问题:

在主循环中,在调用glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);之前清除帧缓冲区RenderShadows,但在函数内部绑定帧缓冲区glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);,因此清除是无用的。

RenderShadows中,您尝试将DepthMap纹理绑定到纹理采样器, 但DepthMap纹理是当前绑定帧缓冲区的目标纹理。纹理不能同时是源和目标,这会导致未定义的行为。

RenderVertices中,您必须将纹理单元的索引指定给 纹理采样器不是纹理对象:

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, DepthMap);

GLint samplerLoc = glGetUniformLocation(shaderProgram, "DepthMap");
// glUniform1i(samplerLoc, DepthMap); <- this is wrong
glUniform1i(samplerLoc, 1); // 1 because the texture is bound to GL_TEXTURE1