0
votes

Hello I am trying to implement shadows in openGL using C++. I created a FrameBuffer and a DepthTexture. Every frame Im rendering my entities to the FrameBuffer. For now Im just displaying the texture on the screen like any other GUI, but the texture is completely white and it's not changing when I move the camera around. I hope you can help me find the problem.

My Code:

 

    //Creating FrameBuffer + Texture
    glGenFramebuffers(1, &depthMapFBO);
    glGenTextures(1, &depthMap);
    glBindTexture(GL_TEXTURE_2D, depthMap);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, m_width, m_height, 0,  GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
    glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthMap, 0); 
    glDrawBuffer(GL_NONE);
    glReadBuffer(GL_NONE);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

 

In the renderer:

 

    glm::mat4 lightSpaceMatrix = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, Camera::ZNEAR, Camera::ZFAR) * glm::lookAt(m_position, m_position + m_forward, m_up);
    m_shader.Bind();
    m_shader.loadMat4("lightSpaceMatrix", lightSpaceMatrix);

    //Bind the FrameBuffer
    glViewport(0, 0, m_width, m_height);
    glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
    glClear(GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);

    //Render all Entitys
    for (auto entity : m_entitys) {
        m_shader.loadMat4("model", entity->getTransform()->getModel());
        entity->getTexturedModel()->getMesh()->Draw();
    }

    //Unbind the FrameBuffer
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glViewport(0, 0, Setting::width, Setting::height);
    m_shader.unbind();

 

My Vertex Shader:

 

    #version 430
    in layout(location=0) vec3 position;
    uniform mat4 lightSpaceMatrix;
    uniform mat4 model;

    void main()
    {
        gl_Position = lightSpaceMatrix * model * vec4(position, 1.0f);
    }

 

My Fragment Shader:

 

    #version 430
    void main()
    {
        gl_FragColor = vec4(1);
    }

 
1
What color you expect to show up with gl_FragColor = vec4(1);? - Ramil Kudashev
Because I don't have a colour attachment I thought it doesn't matter what's happening in the fragment shader, so I just set gl_FragColor to vec4(1) - Philipp98
Did you try to render scene normally with that projection matrix? Maybe nothing gets written and you just get cleared all white in depth buffer. Also, do you have correct glDepthFunc? - Ramil Kudashev

1 Answers

0
votes

After using my normal projection matrix instead of the orthographical matrix it worked fine. So I experimented a bit and the near and far plane were set to 0 when I created the orthographical matrix. I fixed it and its now working. Thank you for your help.