gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:45:54 25 4
gpt4 key购买 nike

我正在尝试渲染我的深度图,现在我已经将它渲染到场景中的对象上以大致了解我得到的结果。据我了解,黑色值是靠近光线的物体,而白色距离较远。如果我弄错了,请纠正我。这是我得到的结果: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);

}

最佳答案

如果你想将你的深度缓冲区渲染到一个纹理,你必须将一个具有 GL_DEPTH_COMPONENT 格式的纹理绑定(bind)到帧缓冲缓冲区的渲染缓冲区。您所做的是将纹理绑定(bind)到帧缓冲区的颜色计划 0,因为您使用 GL_COLOR_ATTACHMENT0 在代码中绑定(bind)纹理。

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

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

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

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

然后您必须使用 GL_DEPTH_ATTACHMENT 将您的深度纹理绑定(bind)到帧缓冲区:

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

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

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

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

帧缓冲区解除绑定(bind)后,您可以将其纹理用作阴影 channel 的输入。

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

将纹理绑定(bind)到纹理单元:

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);,但在函数内部绑定(bind)帧缓冲区 glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);,所以清除没有用。

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

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

关于c++ - OpenGL - 阴影贴图/深度贴图给出奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45121959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com