gpt4 book ai didi

c++ - OpenGL)阴影贴图使物体看起来偏红

转载 作者:行者123 更新时间:2023-11-30 04:44:53 25 4
gpt4 key购买 nike

我试图在加载 fbx 文件的 opengl 程序中实现阴影映射,但如果我使用深度贴图,模型就会变成红色。这是结果。

enter image description here

我不是故意画地板的。这是我的渲染循环

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

然后结果变为正常。 enter image description here

奇怪的是,如果我只是评论这一行

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

那么结果就更奇怪了。这真让我抓狂。 enter image description here

最佳答案

@derhass 是对的。制作深度图后,不要忘记激活并绑定(bind)您的原始纹理!

关于c++ - OpenGL)阴影贴图使物体看起来偏红,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57507719/

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