C++ (OpenGL)阴影贴图使对象看起来略带红色
我试图将阴影映射实现到加载fbx文件的opengl程序中,但如果使用深度贴图,该模型最终会变成红色。结果如下 我不是故意的。 这是我的渲染循环C++ (OpenGL)阴影贴图使对象看起来略带红色,c++,opengl,shadow,framebuffer,depth-buffer,C++,Opengl,Shadow,Framebuffer,Depth Buffer,我试图将阴影映射实现到加载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
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是对的。制作深度贴图后,不要忘记激活并绑定原始纹理 看起来你正在用阴影贴图对对象进行纹理处理(当作为颜色处理时,它只包含一个红色通道)@derhass你是对的!在生成阴影贴图后,我忘记激活原始纹理并将其绑定到模型。谢谢!