首页 > 解决方案 > 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);

那么结果就更奇怪了。这真让我抓狂。 在此处输入图像描述

标签: c++openglshadowframebufferdepth-buffer

解决方案


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


推荐阅读