Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odd OpenGL shadow mapping behaviour

I am working on a 3D game in C++ and OpenGL 3.2 with SFML. I have been struggling to implement point light shadow mapping. What I have done so far seems to conform to what I have learnt and examples I have seen, but still, no shadows.

What I have done is write a simplistic list of all the code I use in the exact order I use it, but not as full source code, only code that is relevant (because my project is split up in several classes):

Omnidirectional shadow mapping


C++
- Initialization
-- Use shadow pass shader program
-- Generate + bind the shadow frame buffer
glGenFramebuffers(1, &shadowFrameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, shadowFrameBuffer);


-- Generate a texture
glGenTextures(1, &shadowMap);

-- Bind texture as cubemap
glBindTexture(GL_TEXTURE_CUBE_MAP);

-- Set texture parameters
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);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

-- Generate empty 1024 x 1024 for every face of the cube
for (int face = 0; face < 6; face++)
        glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_DEPTH_COMPONENT32F , 1024, 1024, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);

-- Attach the cubemap to the framebuffer
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, shadowMap, 0);

-- Only draw depth to framebuffer
glDrawBuffer(GL_NONE);

- Every frame
-- Clear screen
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

-- Render shadow map
--- Bind shadow frame buffer
glBindFramebuffer(GL_FRAMEBUFFER, shadowFrameBuffer);

--- Set the viewport to the size of the shadow map
glViewport(0, 0, 1024, 1024);

-- Cull front faces
glCullFace(GL_FRONT);

-- Use shadow mapping program
--- Define projection matrix for rendering each face
glm::mat4 depthProjectionMatrix = glm::perspective(90.0f, 1.0f, 1.0f, 10.0f);

--- Define view matrices for all six faces
std::vector<glm::mat4> depthViewMatrices;

depthViewMatrices.push_back(glm::lookAt(lightInvDir, glm::vec3(1,0,0),  glm::vec3(0,-1,0) )); // +X
depthViewMatrices.push_back(glm::lookAt(lightInvDir, glm::vec3(-1,0,0), glm::vec3(0,1,0) )); // -X
depthViewMatrices.push_back(glm::lookAt(lightInvDir, glm::vec3(0,1,0),  glm::vec3(0,0,1)  )); // +Y
depthViewMatrices.push_back(glm::lookAt(lightInvDir, glm::vec3(0,-1,0), glm::vec3(0,0,-1) )); // -Y
depthViewMatrices.push_back(glm::lookAt(lightInvDir, glm::vec3(0,0,1),  glm::vec3(0,-1,0) )); // +Z
depthViewMatrices.push_back(glm::lookAt(lightInvDir, glm::vec3(0,0,-1), glm::vec3(0,1,0)  )); // -Z

--- For every object in the scene
---- Bind the VBO of the object
---- Define the model matrix for the object based on its position and orientation
---- For all six sides of the cube
----- Set the correct side to render to
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, shadowMap, 0);

----- Clear depth buffer
glClear(GL_DEPTH_BUFFER_BIT);

----- Send model, view and projection matrices to shadow mapping shader
glUniformMatrix4fv(glGetUniformLocation(shadowMapper, "lightModelMatrix"), 1, GL_FALSE, glm::value_ptr(depthModelMatrix));
glUniformMatrix4fv(glGetUniformLocation(shadowMapper, "lightViewMatrix"), 1, GL_FALSE, glm::value_ptr(depthViewMatrices[i]));
glUniformMatrix4fv(glGetUniformLocation(shadowMapper, "lightProjectionMatrix"), 1, GL_FALSE, glm::value_ptr(depthProjectionMatrix));

----- Draw the object
glDrawElements(....);
- END SHADOW MAP DRAW
-- Cull back faces
glCullFace(GL_BACK);

-- Use standard shader program
-- Bind default framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);

-- Activate cubemap texture
glActiveTexture(GL_TEXTURE1);

-- Bind cubemap texture
glBindTexture(GL_TEXTURE_CUBE_MAP, shadowMap);

-- Tell shader to use first texture
glUniform1i(glGetUniformLocation(currentProgram->id, "shadowmap"), 1);

-- Send standard MVPs and draw objects
glDrawElements(...);

- END C++

=================================
GLSL

shadowpass vertex shader source

#version 150

in vec3 position;
out vec3 worldPosition;

uniform mat4 lightModelMatrix;
uniform mat4 lightViewMatrix;
uniform mat4 lightProjectionMatrix;

void main()
{
    gl_Position = lightProjectionMatrix * lightViewMatrix * lightModelMatrix * vec4(position, 1.0);
    worldPosition = (lightModelMatrix * vec4(position, 1.0)).xyz; // Send world position of vertex to fragment shader
}


shadowpass fragment shader source

#version 150

in vec3 worldPosition; // Vertex position in world space
out float distance; // Distance from vertex position to light position
vec3 lightWorldPosition = vec3(0.0, 0.0, 0.0); // Light position in world space

void main()
{
    distance = length(worldPosition - lightWorldPosition); // Distance from point to light
    // Distance will be written to the cubemap
}

standard vertex shader source

#version 150

in vec3 position;
in vec3 normal;
in vec2 texcoord;

uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;

out vec3 fragnormal;
out vec3 fragnormaldirection;
out vec2 fragtexcoord;
out vec4 fragposition;
out vec4 fragshadowcoord;

void main()
{
    fragposition = vec4(position, 1.0); // Position of vertex in object space
    fragtexcoord = texcoord;
    fragnormaldirection = normalize(modelInverseTranspose * normal);
    fragnormal = normalize(normal);


    gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
}

standard fragment shader source

#version 150

out vec4 outColour;

in vec3 fragnormaldirection;
in vec2 fragtexcoord;
in vec3 fragnormal;
in vec4 fragposition;

uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrixInversed;

uniform sampler2D tex;
uniform samplerCube shadowmap;

void main()
{   
    vec3 lightpos = vec3(0.0, 0.0, 0.0);

    vec3 pointToLight = (fragposition * modelMatrix).xyz - lightpos; // Get vector between this point and the light

    float dist = texture(shadowmap, pointToLight).x; // Get distance written in texture

    float shadowfactor = 1.0;

    if (length(pointToLight) > dist) // Is it occluded?
        shadowfactor = 0.5;

    outColour = texture(tex, fragtexcoord) * shadowfactor;
}

Here is a picture of what my code does now:

Broken shadow mapping

This is a strange effect but seems to be close to what I meant. It seems that any surface exposed to the light at 0, 0, 0 has an unshadowed circle at the center of it while everything else is unshadowed.

like image 463
toficofi Avatar asked Jan 11 '13 22:01

toficofi


1 Answers

One very useful way of debugging shadow maps ins indeed to have a way to display the content of the shadow maps as quads on the screen. 6 quads in case of cube shadow maps. that could be implemented as a debug easter egg where you can display the full texture on the whole screen and 'go to next face' so you can skid the 6 faces with another key combo.

Then, one of the most important things in cubic shadow maps is the depth range. A point light doesn't have an infinite range, so generally you want to scale your depth storage to match the light range.

You can use floating point 16 bits luminance (or red channel) texture to store a world depth (spherical, meaning the true length(ray-to-intersection) using a little calculation in the pixel shader) Or you can use linear depth (the same kind that is stored in a classic ZBuffer, which is the depth of the normalized device coordinates. That is the depth after the projection matrix. In which case, to reconstruct the world position once in the lighting shader (next pass), the issue is to be sure to divide by w after you multiplied by the camera-cube-face inverse view*projection.

The key to debugging shadow maps is all in shader twiddling. Start by using colors to vizualize the depth stored in your shadow maps as perceived by the pixels of your world. It was the only way that helped be fix point shadow maps in my company's engine. You can make a color code using a combination of mix and clamp like blue from 0 to 0.3, red from 0.3 to 0.6, green from 0.6 to 1. If you have world distance storage it is easier, but is still interesting to vizu it through color codes. just use the same function but dividing the distance by your expected world range.

Using that vizu scheme you'll be able to see the shadowed zone right away because they all bear the same color (since it the 'ray' was intercepted by a closer surface). Once you get to that point; the rest will all go smoothly.

good luck :)

like image 190
v.oddou Avatar answered Sep 22 '22 23:09

v.oddou