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



我正在尝试渲染我的深度图,现在我已经将其渲染到场景中的对象上,以大致了解我得到的结果是什么。据我所知,黑色值是靠近光的物体,而白色值是离光更远的物体。如果我弄错了,请纠正我。这是我得到的结果:渲染到立方体上的阴影贴图

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

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

我们遵循了学习OpenGL - 阴影映射教程,我们的着色器看起来几乎与他的相同。所以那里应该没有任何问题。我想我误解了影子映射的 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);
}

如果要将深度缓冲区渲染为纹理,则必须将具有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 );

答案的扩展

代码中还有一些问题:

在主循环中,你在调用RenderShadows之前清除了帧缓冲区glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);,但在函数内部你绑定了一个帧缓冲区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

相关内容

  • 没有找到相关文章

最新更新