OpenGL)阴影贴图使物体看起来偏红
OpenGL) Shadow mapping makes the object look reddish
我试图对加载 fbx 文件的 opengl 程序实施阴影映射,但如果我使用深度贴图,模型就会变成红色。这是结果。
我不是故意画地板的。
这是我的渲染循环
void Renderer::renderLoop(){
while (!glfwWindowShouldClose(window))
{
glClearColor(0.7f, 0.7f, 0.7f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// per-frame time logic
// --------------------
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// input
processInput(window);
// render scene in the light's viewpoint.
depthMapShader->use();
depthMapShader->setMat4("lightSpaceMatrix", lightSpaceMatrix);
depthMapAnimationShader->use();
depthMapAnimationShader->setMat4("lightSpaceMatrix", lightSpaceMatrix);
glViewport(0, 0, DepthMap->SHADOW_WIDTH, DepthMap->SHADOW_HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, DepthMap->depthMapFBO);
glClear(GL_DEPTH_BUFFER_BIT);// You have to clear AFTER binding the frame buffer
renderLightSpaceScene();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
//Now render ordinary scene
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
modelShader->use();
glm::mat4 model = glm::mat4(1.0);
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 2500.0f);
glm::mat4 view = camera.GetViewMatrix();
modelShader->setMat4("model", model);
modelShader->setMat4("projection", projection);
modelShader->setMat4("view", view);
modelShader->setVec3("lightDirection", lightDir);
modelShader->setFloat("signal", animationStart);
modelShader->setVec3("viewPos", camera.Position);
if(startAnimation){
fbxAssimp->updateAnimation(frameIndex, modelShader);
frameIndex++;
if(frameIndex == frameNum)
frameIndex=0;
}
fbxAssimp->Draw(modelShader);
glfwSwapBuffers(window);
glfwPollEvents();
}
}
我知道我的模型的着色器是正确的,因为如果我注释掉这部分
//render the scene in light's viewpoint
/*
depthMapShader->use();
depthMapShader->setMat4("lightSpaceMatrix", lightSpaceMatrix);
depthMapAnimationShader->use();
depthMapAnimationShader->setMat4("lightSpaceMatrix", lightSpaceMatrix);
glViewport(0, 0, DepthMap->SHADOW_WIDTH, DepthMap->SHADOW_HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, DepthMap->depthMapFBO);
glClear(GL_DEPTH_BUFFER_BIT);// You have to clear AFTER binding the frame buffer
renderLightSpaceScene();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
*/
然后结果变为正常。
奇怪的是,如果我只评论这一行
glClear(GL_DEPTH_BUFFER_BIT);// You have to clear AFTER binding the frame buffer
然后它仍然渲染正常,但在这种情况下我无法制作阴影贴图。
这种现象有什么原因吗?如有任何帮助,我们将不胜感激!
编辑
如果我在原始渲染循环的末尾添加地板渲染代码,
floorShader->use();
floorShader->setMat4("projection", projection);
floorShader->setMat4("view", view);
floorShader->setVec3("viewPos", camera.Position);
floorShader->setMat4("lightSpaceMatrix", lightSpaceMatrix);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, DepthMap->depthMapTexture);
ourFloor->draw(floorShader);
那结果就更奇怪了。这真让我抓狂。
@derhass 是对的。制作深度图后别忘了激活和绑定你的原始纹理!
我试图对加载 fbx 文件的 opengl 程序实施阴影映射,但如果我使用深度贴图,模型就会变成红色。这是结果。
我不是故意画地板的。 这是我的渲染循环
void Renderer::renderLoop(){
while (!glfwWindowShouldClose(window))
{
glClearColor(0.7f, 0.7f, 0.7f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// per-frame time logic
// --------------------
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// input
processInput(window);
// render scene in the light's viewpoint.
depthMapShader->use();
depthMapShader->setMat4("lightSpaceMatrix", lightSpaceMatrix);
depthMapAnimationShader->use();
depthMapAnimationShader->setMat4("lightSpaceMatrix", lightSpaceMatrix);
glViewport(0, 0, DepthMap->SHADOW_WIDTH, DepthMap->SHADOW_HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, DepthMap->depthMapFBO);
glClear(GL_DEPTH_BUFFER_BIT);// You have to clear AFTER binding the frame buffer
renderLightSpaceScene();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
//Now render ordinary scene
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
modelShader->use();
glm::mat4 model = glm::mat4(1.0);
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 2500.0f);
glm::mat4 view = camera.GetViewMatrix();
modelShader->setMat4("model", model);
modelShader->setMat4("projection", projection);
modelShader->setMat4("view", view);
modelShader->setVec3("lightDirection", lightDir);
modelShader->setFloat("signal", animationStart);
modelShader->setVec3("viewPos", camera.Position);
if(startAnimation){
fbxAssimp->updateAnimation(frameIndex, modelShader);
frameIndex++;
if(frameIndex == frameNum)
frameIndex=0;
}
fbxAssimp->Draw(modelShader);
glfwSwapBuffers(window);
glfwPollEvents();
}
}
我知道我的模型的着色器是正确的,因为如果我注释掉这部分
//render the scene in light's viewpoint
/*
depthMapShader->use();
depthMapShader->setMat4("lightSpaceMatrix", lightSpaceMatrix);
depthMapAnimationShader->use();
depthMapAnimationShader->setMat4("lightSpaceMatrix", lightSpaceMatrix);
glViewport(0, 0, DepthMap->SHADOW_WIDTH, DepthMap->SHADOW_HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, DepthMap->depthMapFBO);
glClear(GL_DEPTH_BUFFER_BIT);// You have to clear AFTER binding the frame buffer
renderLightSpaceScene();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
*/
然后结果变为正常。
奇怪的是,如果我只评论这一行
glClear(GL_DEPTH_BUFFER_BIT);// You have to clear AFTER binding the frame buffer
然后它仍然渲染正常,但在这种情况下我无法制作阴影贴图。
这种现象有什么原因吗?如有任何帮助,我们将不胜感激!
编辑
如果我在原始渲染循环的末尾添加地板渲染代码,
floorShader->use();
floorShader->setMat4("projection", projection);
floorShader->setMat4("view", view);
floorShader->setVec3("viewPos", camera.Position);
floorShader->setMat4("lightSpaceMatrix", lightSpaceMatrix);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, DepthMap->depthMapTexture);
ourFloor->draw(floorShader);
那结果就更奇怪了。这真让我抓狂。
@derhass 是对的。制作深度图后别忘了激活和绑定你的原始纹理!