Search code examples
c++openglshadowframebufferdepth-buffer

OpenGL) Shadow mapping makes the object look reddish


I was trying to implement shadow mapping to my opengl program that loads an fbx file, but the model ends of being reddish if I use a depth map. Here is the result.

enter image description here

I didn't draw the floor on purpose. This is my render loop

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

I know the shader for my model is correct, because if I comment out this part

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

Then the result changes to normal. enter image description here

The weird thing is, if I just comment this line

glClear(GL_DEPTH_BUFFER_BIT);// You have to clear AFTER binding the frame buffer

Then it still renders normal, but in this case I wouldn't be able to make a shadow map.

Is there a reason for this phenomenon? Any help would be appreciated!


EDIT

If I add the floor-rendering code at the end of the original render loop,

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

then the result is even weirder. This is driving me crazy. enter image description here


Solution

  • @derhass was right. Don't forget to activate & bind your original texture after making the depth map everyone!