c++ - OpenGL) 阴影映射使对象看起来偏红
问题描述
我试图在加载 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 是对的。制作深度图后不要忘记激活并绑定您的原始纹理!
推荐阅读
- java - 使用 Gson 将 Json 字符串转换为对象以用于动态类类型
- javascript - 刷新模态窗口而不关闭它
- c++ - 如何通过使用 void 传递变量中的信息
- r - 如何连接R中每3行以上的值
- javascript - Django + Service Workers:“重定向响应用于重定向模式不遵循的请求”错误
- java - 使用 jarfile 中不可用的 JSONParser 读取 JSON 文件
- regex - 在 python 3 regex 中打印有效日志文件条目的列表
- angular - 如何实现包含大量组件的 Angular 模板驱动表单?
- python - 忽略列表中的特殊字符,不包括提到的字符
- javascript - 尝试返回 givenName 的所有键值