1
\$\begingroup\$

I am having trouble generating a depth map for my scene. I tried to figure out this issue a 2 weeks ago and got nowhere after days of trying to fix it, so I took a break and tried to tackle it again today. I'm still stuck and with no ideas.

Here's the main code:

#Sets up the depth FBO and the texture that will be used to render to a quad
const unsigned int d_width = 1024, d_height = 1024;

    unsigned int depthFBO;
    glGenFramebuffers(1, &depthFBO);
    glBindBuffer(GL_FRAMEBUFFER, depthFBO);

    unsigned int depthMap;
    glGenTextures(1, &depthMap);
    glBindTexture(GL_TEXTURE_2D, depthMap);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, d_width, d_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);

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
    glDrawBuffer(GL_NONE);
    glReadBuffer(GL_NONE);

    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        std::cout << "Incomplete stuff!" << std::endl;

    glBindBuffer(GL_FRAMEBUFFER, 0);

    glm::vec3 lightPos(-2.0f, 4.0f, -1.0f);

Rendering loop:

/* Render here */
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glClearColor(0.1f, 0.1f, 0.1f, 1.0f);

        #render scene from the light's perspective
        float near_plane = 1.0f, far_plane = 8.0f;
        glm::mat4 lightProj = glm::ortho(-10.0f, 10.0f, 10.0f, 10.0f, near_plane, far_plane);
        glm::mat4 lightView = glm::lookAt(lightPos, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
        glm::mat4 lightSpaceMat = lightProj * lightView;

        depthShader.use();
        depthS.setUniformMat4("lightSpaceMat", lightSpaceMat);
        glm::mat4 model = glm::mat4(1.0f);
        depthS.setUniformMat4("model", model);

        glViewport(0, 0, d_width, d_height);
        glBindFramebuffer(GL_FRAMEBUFFER, depthFBO);
        glClear(GL_DEPTH_BUFFER_BIT);
        
        #render everything else
        planeVAO.bind();
        model = glm::translate(model, glm::vec3(0.0f, -1.0f, 0.0f));
        depthS.setUniformMat4("model", model);

        glDrawArrays(GL_TRIANGLES, 0, 6);

        cubeVAO.bind();
        model = glm::translate(model, glm::vec3(1.0f, 1.0f, -2.0f));
        model = glm::scale(model, glm::vec3(0.6f));
        depthS.setUniformMat4("model", model);
        glDrawArrays(GL_TRIANGLES, 0, 36);

        model = glm::translate(model, glm::vec3(2.0f, 1.0f, -1.0f));
        depthS.setUniformMat4("model", model);
        glDrawArrays(GL_TRIANGLES, 0, 36);

        #render to the quad now which is supposed to display the depth map
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glViewport(0, 0, scr_width, scr_height);
        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
        
        quadVAO.bind();
        quadShader.use();
        glActiveTexture(GL_TEXTURE15);
        glBindTexture(GL_TEXTURE_2D, depthMap);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

Here are the shaders:

depth shaders:

#vertex
#version 330 core

layout (location = 0) in vec3 aPos;

uniform mat4 lightSpaceMat;
uniform mat4 model;

void main()
{
    gl_Position = lightSpaceMat * model * vec4(aPos, 1.0);
}

#fragment
#version 330 core

void main()
{
    gl_FragDepth = gl_FragCoord.z;
}

visual quad shaders:

#vertex
#version 330 core

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTex;

out vec2 TexCoords;

void main()
{
    TexCoords = aTex;
    gl_Position = vec4(aPos, 1.0);
}

#fragment
#version 330 core

out vec4 FragColor;

in vec2 TexCoords;

uniform sampler2D depthMap;

void main()
{
    float depthValue = texture(depthMap, TexCoords).r;
    FragColor = vec4(vec3(depthValue), 1.0);
}

Anyone have any idea what I might be doing wrong? I have tried debugging in every way I could think of and have made no progress.

edit: full source.cpp code:

#include <fstream>
#include <sstream>

#include <iostream>
#include <string>
#include <vector>
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include <gtc/type_ptr.hpp>

#include "Shader.h" 
#include "Textures.h"
#include "Camera.h"
#include "Context.h"
#include "Renderer.h"
#include "DataDefs.h"
#include "Renderer.h"
#include "StaticDataDefs.h"
#include "VertexBuffer.h"
#include "Context.h"
#include "Model.h"

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <tuple>
#include <array>

#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"
#include <stdio.h>
#include <map>

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

float planeVertices[] = {
    // positions            // normals         // texcoords
     25.0f, -0.5f,  25.0f,  0.0f, 1.0f, 0.0f,  25.0f,  0.0f,
    -25.0f, -0.5f,  25.0f,  0.0f, 1.0f, 0.0f,   0.0f,  0.0f,
    -25.0f, -0.5f, -25.0f,  0.0f, 1.0f, 0.0f,   0.0f, 25.0f,

     25.0f, -0.5f,  25.0f,  0.0f, 1.0f, 0.0f,  25.0f,  0.0f,
    -25.0f, -0.5f, -25.0f,  0.0f, 1.0f, 0.0f,   0.0f, 25.0f,
     25.0f, -0.5f, -25.0f,  0.0f, 1.0f, 0.0f,  25.0f, 10.0f
};

const unsigned int scr_width = 800;
const unsigned int scr_height = 600;

float quadVertices[] = {
    // positions        // texture Coords
    -1.0f,  1.0f, 0.0f, 0.0f, 1.0f,
    -1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
     1.0f,  1.0f, 0.0f, 1.0f, 1.0f,
     1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
};

int main(void)
{
    //Setup all openGL stuff
    setProject init;
    GLFWwindow* window = init.returnWindow();

    Camera camera; 
    camera.setInputs(window, camera);

    glEnable(GL_DEPTH_TEST);

    Shader normal;
    normal.setUpShader("Resources/Shaders/lightObject.vs", "Resources/Shaders/lightObject.fs");
    Shader depthS;
    depthS.setUpShader("Resources/Shaders/depthS.vs", "Resources/Shaders/depthS.fs");
    Shader depthR;
    depthR.setUpShader("Resources/Shaders/depthR.vs", "Resources/Shaders/depthR.fs");

    Context va;
    VertexBuffer vb(vert3d.size() * sizeof(Def3), vert3d.data(), 's', "type3a", GL_FALSE, GL_FLOAT);
    va.addBuffer(vb, "VBO");
    GLCall(va.setVBOAttribs());

    Context va2;
    VertexBuffer vb2(sizeof(quadVertices), &quadVertices[0], 's', "type2a", GL_FALSE, GL_FLOAT);
    va2.addBuffer(vb2, "VBO");
    GLCall(va2.setVBOAttribs());
    
    Context plane;
    VertexBuffer planevb(sizeof(planeVertices), &planeVertices[0], 's', "type3a", GL_FALSE, GL_FLOAT);
    plane.addBuffer(planevb, "VBO");
    GLCall(plane.setVBOAttribs());
   
    depthR.use();
    depthR.setInt("depthMap", 15);

    const unsigned int d_width = 1024, d_height = 1024;

    unsigned int depthFBO;
    glGenFramebuffers(1, &depthFBO);
    glBindBuffer(GL_FRAMEBUFFER, depthFBO);

    unsigned int depthMap;
    glGenTextures(1, &depthMap);
    glActiveTexture(GL_TEXTURE15);
    glBindTexture(GL_TEXTURE_2D, depthMap);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, d_width, d_height, 0, GL_DEPTH_ATTACHMENT, 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);

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
    glReadBuffer(GL_NONE);
    glDrawBuffer(GL_NONE);

    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        std::cout << "Incomplete stuff!" << std::endl;

    glBindBuffer(GL_FRAMEBUFFER, 0);

    glm::vec3 lightPos(2.0f, 4.0f, 1.0f);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
     
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glClearColor(0.1f, 0.1f, 0.1f, 1.0f);

        texo.textureBinder(0);

        //set up scene, draw objects, etc.

        camera.processInput(window);
        camera.calcDelta();

        float fov = camera.return_fov();

        float near_plane = 1.0f, far_plane = 8.0f;
        glm::mat4 lightProj = glm::ortho(-10.0f, 10.0f, 10.0f, 10.0f, near_plane, far_plane);
        glm::mat4 lightView = glm::lookAt(lightPos, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
        glm::mat4 lightSpaceMat = lightProj * lightView;

        depthS.use();
        depthS.setUniformMat4("lightSpaceMat", lightSpaceMat);
        glm::mat4 model = glm::mat4(1.0f);
        depthS.setUniformMat4("model", model);
      
        glViewport(0, 0, d_width, d_height);
        glBindFramebuffer(GL_FRAMEBUFFER, depthFBO);
        glClear(GL_DEPTH_BUFFER_BIT);

        depthS.use();

        plane.bind();
        model = glm::translate(model, glm::vec3(0.0f, -1.0f, 0.0f));
        depthS.setUniformMat4("model", model);

        glDrawArrays(GL_TRIANGLES, 0, 6);

        va.bind();
        model = glm::translate(model, glm::vec3(1.0f, 1.0f, -2.0f));
        model = glm::scale(model, glm::vec3(0.6f));
        depthS.setUniformMat4("model", model);
        glDrawArrays(GL_TRIANGLES, 0, 36);

        model = glm::translate(model, glm::vec3(2.0f, 1.0f, -1.0f));
        depthS.setUniformMat4("model", model);
        glDrawArrays(GL_TRIANGLES, 0, 36);

        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glViewport(0, 0, scr_width, scr_height);
        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
        
        va2.bind();
        depthR.use();
        glActiveTexture(GL_TEXTURE15);
        glBindTexture(GL_TEXTURE_2D, depthMap);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);
        
        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

(the "VA" VAO holds the data for the cubes and "VAO2" for the quad)

https://github.com/julkothegu/p_1_test

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Sorry I cant comment yet.

I havent done shadow mapping in openGL yet, however, I cannot see that you are setting your uniform sampler2D depthMap. It should be set like:
glUniform1i(glGetUniformLocation(PROGRAM_ID, "depthMap"), 15);

Its being set to 15 because of this code:
glActiveTexture(GL_TEXTURE15);
glBindTexture(GL_TEXTURE_2D, depthMap)

In above code you are saying your depthMap texture is at index 15.

Edit: Okay I gave it a try(I am also following the learOpengl.com) and this is my render. Please note I am not doing a double pass . I am just basically changing my perspective projection to ortho and I got the following result:

enter image description here

This is my render with perspective: enter image description here

Therefore, I would strongly recommend you to first do a normal render and then try the shadows.

\$\endgroup\$
8
  • \$\begingroup\$ Hi. I set the uniform to that location earlier in the program, I just skipped over much of the setting up code. Everything to do with settings uniforms, VAOs, VBOs, and all other basic stuff isn't the problem here (unfortunately). \$\endgroup\$
    – Julk
    Jul 1, 2020 at 10:33
  • \$\begingroup\$ Okay, So now I have to ask what is the problem? I.e what are you seeing? Are you seeing anything on the screen? Does your scene render properly and just not the shadows? \$\endgroup\$ Jul 1, 2020 at 10:55
  • \$\begingroup\$ Nothing seems to be rendering to the screen. It's just black. I'm pretty sure it's not an issue with the framebuffer since it doesn't trigger an incomplete error, and I can't see anything wrong with the shaders. I haven't had anything to go on from for weeks so I was hoping someone could see something wrong with my methodology/usage of openGL/literally anything else. \$\endgroup\$
    – Julk
    Jul 1, 2020 at 11:44
  • \$\begingroup\$ I haven't added any shadows yet btw. The only thing I'm trying to do atm is to display the depth of the scene's objects for visualization purposes (something like this: learnopengl.com/img/advanced-lighting/…) Seems like even that is too much for me though. \$\endgroup\$
    – Julk
    Jul 1, 2020 at 11:46
  • \$\begingroup\$ I would suggest that you should first render your normal scene. All we are trying to do with shadows is - put the camera where the light is and look towards the scene you are trying to render. I can help you with debugging this if you provide the full source code. \$\endgroup\$ Jul 2, 2020 at 7:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .