Depth cube map's all depth components are 1.0

Hi,

I am learning about point shadow from LearnOpenGL. I’ve initialized a cube map framebuffer like this:

glGenTextures(1, &TBO[0]);
glBindTexture(GL_TEXTURE_CUBE_MAP, TBO[0]);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
for (int i = 0; i < 6; i++) {
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT, w, h, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
}
glGenFramebuffers(1, &FBO);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, TBO[0], 0);
glDrawBuffer(0);
glReadBuffer(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

And I am rendering it using following shaders:

Vertex shader

#version 330 core
layout (location = 0) in vec3 aPos;
uniform mat4 model;

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

Geometry shader

#version 330 core
layout (triangles) in;
layout (triangle_strip, max_vertices = 18) out;
uniform mat4 view[6];

out vec4 fragPos;

void main() {
    for (int face = 0; face < 6; face++) {
        gl_Layer = face;
        for (int i = 0; i < 3; i++) {
            fragPos = gl_in[i].gl_Position;
            gl_Position = view[face] * fragPos;
            EmitVertex();
        }
        EndPrimitive();
    }
}

Fragment shader

#version 330 core
in vec4 fragPos;
uniform float time;

void main() {
    vec3 lightPos = vec3(sin(time), 4.0, cos(time)); // The light's spinning
    float far = 25.0f; // Far plane because I am too lazy to use uniform
    float lightDist = length(fragPos.xyz - lightPos) / far;
    gl_FragDepth = lightDist;
}

And here’s how I am gonna render the depth cube map:

lightPos = glm::vec3(sinf(time), 4.0f, cosf(time));
lightAts[0] = glm::lookAt(lightPos, lightPos + glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f));
lightAts[1] = glm::lookAt(lightPos, lightPos + glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f));
lightAts[2] = glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f));
lightAts[3] = glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
lightAts[4] = glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f));
lightAts[5] = glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, -1.0f, 0.0f));

glEnable(GL_DEPTH_TEST);
glViewport(0, 0, 1024, 1024);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glClear(GL_DEPTH_BUFFER_BIT);
glUseProgram(lightProgram.get());
/* model */ glUniformMatrix4fv(lightProgram.mLoc, 1, GL_FALSE, glm::value_ptr(mvp.model));
/* lights' views */ glUniformMatrix4fv(lightProgram.v0, 1, GL_FALSE, glm::value_ptr(lightAts[0]));
glUniformMatrix4fv(lightProgram.v1, 1, GL_FALSE, glm::value_ptr(lightAts[1]));
glUniformMatrix4fv(lightProgram.v2, 1, GL_FALSE, glm::value_ptr(lightAts[2]));
glUniformMatrix4fv(lightProgram.v3, 1, GL_FALSE, glm::value_ptr(lightAts[3]));
glUniformMatrix4fv(lightProgram.v4, 1, GL_FALSE, glm::value_ptr(lightAts[4]));
glUniformMatrix4fv(lightProgram.v5, 1, GL_FALSE, glm::value_ptr(lightAts[5]));
/* perspective */ glUniformMatrix4fv(lightProgram.pLoc, 1, GL_FALSE, glm::value_ptr(lightPerspective));
/* time */ glUniform1f(lightProgram.tLoc, time);
        
glBindVertexArray(model.VAO);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, model.vertices);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

But then, when rendering the actual scene (the one on the right), I find out that all values sampled from the depth cube maps are 1.0 (one on the left):

void main() {
    vec3 lightPos = vec3(sin(time), 4.0, cos(time)); // Too lazy to use uniform
    vec3 fragToLight = pos - lightPos;
    float closestDepth = texture(depth, fragToLight).r;
    if (closestDepth == 1.0) {
        color = vec4(1.0, 0.0, 0.0, 1.0);
        return;
    }
    color = vec4(0.0, 0.0, 0.0, 1.0);
}

I’ve been debugging this for hours and even though it is fun, I am getting quite desperate. Could anyone help?

Huge thanks in advance,
42yeah

It appears that you tried to factor out the perspective projection, but you don’t appear to be using it. The client code references more matrices (v0-v5, mLoc, pLoc, tLoc) than exist in the shaders (model and view[6]).

1 Like

Aaaaah the perspective matrix! I feel so stupid! It works now! Thank you very much!

Also might I ask why isn’t it pitch black just like other empty textures, but white?

The texture isn’t empty; glClear will fill it, and the initial state is equivalent to glClearDepth(1.0).

1 Like

Oooh thanks a bunch! I get it now!