Shadow map - some shadows get cut off

0

I'm working on a voxel engine and my shadow map has some strange behavior. When I turn my directional light to a certain angle, some shadows are cut off.

here a pic how it looks like and my shadow map.

this is how I render my shadows:

glDisable(GL_CULL_FACE);
    
        glEnable(GL_DEPTH_TEST);
        glViewport(0, 0, SHADOW_WIDTH_, SHADOW_HEIGHT_);
        glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO_);
        glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
        glClear(GL_DEPTH_BUFFER_BIT);
        glm::mat4 lightProjection, lightView;
        
        float near_plane = 1.0f, far_plane = 200.0f;
        //lightProjection = glm::perspective(glm::radians(45.0f), (GLfloat)SHADOW_WIDTH_ / (GLfloat)SHADOW_HEIGHT_, near_plane, far_plane); // note that if you use a perspective projection matrix you'll have to change the light position as the current light position isn't enough to reflect the whole scene
        lightProjection = glm::ortho<float>(-100.0f, 100.0f, -100.0f, 100.0f, near_plane, far_plane);
        
        glm::vec3 lightPos;
        lightPos.x = LightControl_->GetLightPosition(LightControl_->GetDirectionalLightNames()[0]->ToString()).X;
        lightPos.y = LightControl_->GetLightPosition(LightControl_->GetDirectionalLightNames()[0]->ToString()).Y;
        lightPos.z = LightControl_->GetLightPosition(LightControl_->GetDirectionalLightNames()[0]->ToString()).Z;
        
        glm::vec3 target;
        target.x = LightControl_->GetLightDirection(LightControl_->GetDirectionalLightNames()[0]->ToString()).X;
        target.y = LightControl_->GetLightDirection(LightControl_->GetDirectionalLightNames()[0]->ToString()).Y;
        target.z = LightControl_->GetLightDirection(LightControl_->GetDirectionalLightNames()[0]->ToString()).Z;

        
        lightView = glm::lookAt(lightPos, target+lightPos, glm::vec3(0.0, 1.0, 0.0));
        *lightSpaceMatrix = lightProjection * lightView;
        // render scene from light's point of view
        shaderProgram.Use();
        glm::mat4 model = glm::mat4(1.0f);
        glUniformMatrix4fv(shaderProgram.GetUniform("lightSpaceMatrix"), 1, GL_FALSE, glm::value_ptr(*lightSpaceMatrix));
        glUniformMatrix4fv(shaderProgram.GetUniform("u_model"), 1, GL_FALSE, glm::value_ptr(model));

        glViewport(0, 0, SHADOW_WIDTH_, SHADOW_HEIGHT_);
        glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO_);
        glClear(GL_DEPTH_BUFFER_BIT);

        auto tmp = GraphicControl_->GetVoxelBuffer("test");
        tmp.ClearVoxelRenderList();
        tmp.VoxelBufferData(chunksForRendering);
        
        RenderVoxelBuffer(tmp,shaderProgram);
        glBindFramebuffer(GL_FRAMEBUFFER, GraphicControl_->GetDefaultFrameBuffer()->getFBO());

        glViewport(0, 0, 1920, 1080);
        glEnable(GL_CULL_FACE);

shadow map vert:

#version 450 core

layout(location = 0) in uint all_voxel_data;

out VsOut {
     mat4 lightSpaceMatrix;
     mat4 model;
     vec3 u_chunk_location;
} vs_out;


uniform mat4 lightSpaceMatrix;
uniform mat4 model;
uniform vec3 u_chunk_location;


const int AllVoxelDataMask[6] = {
    0xF8000000, // x-Position
    0x07C00000, // y-Position
    0x003E0000, // z-Position
    0x0001F800, // culled Faces
    0x00000700, // Render Options
    0x000000FF  // color Index for Palette
};


vec4 DecodePosition(uint encoded_position) 
{
    float x = float((encoded_position & AllVoxelDataMask[0]) >> 27) + u_chunk_location.x * 32;
    float y = float((encoded_position & AllVoxelDataMask[1]) >> 22) + u_chunk_location.y * 32;
    float z = float((encoded_position & AllVoxelDataMask[2]) >> 17) + u_chunk_location.z * 32;

    return vec4(x, y, z, 1.0);
}


void main()
{
    vs_out.lightSpaceMatrix=lightSpaceMatrix;
    vs_out.u_chunk_location=u_chunk_location;
    vs_out.model=model;

    gl_Position = DecodePosition(all_voxel_data);
}

shadow map geom:

#version 450 core

layout(points) in;
layout(triangle_strip, max_vertices = 36) out;


#define halfVoxelSize 0.5

//   2-------6
//  /|      /|
// 3-------7 |
// | 0-----|-4
// |/      |/
// 1-------5
const vec4 VoxelVertices[8] = {
    vec4(-halfVoxelSize, -halfVoxelSize, -halfVoxelSize, 0.0f), // Back-Bot-Left
    vec4(-halfVoxelSize, -halfVoxelSize, halfVoxelSize, 0.0f),  // Front-Bot-Left
    vec4(-halfVoxelSize, halfVoxelSize, -halfVoxelSize, 0.0f),  // Back-Top-Left
    vec4(-halfVoxelSize, halfVoxelSize, halfVoxelSize, 0.0f),   // Front-Top-Left
    vec4(halfVoxelSize, -halfVoxelSize, -halfVoxelSize, 0.0f),  // Back-Bot-Right
    vec4(halfVoxelSize, -halfVoxelSize, halfVoxelSize, 0.0f),   // Front-Bot-Right
    vec4(halfVoxelSize, halfVoxelSize, -halfVoxelSize, 0.0f),   // Back-Top-Right
    vec4(halfVoxelSize, halfVoxelSize, halfVoxelSize, 0.0f) // Front-Top-Right
};

const vec3 VoxelNormals[6] = {
    vec3(-1.0f,  0.0f,  0.0f),  // Left-X-Axis
    vec3( 1.0f,  0.0f,  0.0f),  // Right-X-Axis
    vec3( 0.0f, -1.0f,  0.0f),  // Bot-Y-Axis
    vec3( 0.0f,  1.0f,  0.0f),  // Top-Y-Axis
    vec3( 0.0f,  0.0f, -1.0f),  // Back-Z-Axis 
    vec3( 0.0f,  0.0f,  1.0f)   // Front-Z-Axis
};

in VsOut {

    mat4 lightSpaceMatrix;
    mat4 model;
    vec3 u_chunk_location;

} gs_in[];

out GsOut {
    mat4 lightSpaceMatrix;
    mat4 model;
    vec3 u_chunk_location;
} gs_out;

void AddTriangle(vec4 a, vec4 b, vec4 c)
{

    gl_Position = gs_in[0].lightSpaceMatrix * a;
    EmitVertex();
    gl_Position = gs_in[0].lightSpaceMatrix * b;
    EmitVertex();   
    gl_Position = gs_in[0].lightSpaceMatrix * c;
    EmitVertex();


    EndPrimitive();
}

void AddQuad(vec4 a, vec4 b, vec4 c, vec4 d, vec3 normal) 
{
    vec4 center = gl_in[0].gl_Position;     // Zugriff auf das erste Voxel, was gleichzeitig unser einzigstes Voxel ist. Weil gl_Points verwendet wird.
    gs_out.model=gs_in[0].model;
    
    if (dot(-(gs_in[0].lightSpaceMatrix * center), (gs_in[0].lightSpaceMatrix * vec4(normal, 0.0f))) <= 0.0)
        return;

    // a-------d
    // |  \    |  
    // |    \  |  
    // b-------c
    AddTriangle(center + a, center + b, center + c);
    AddTriangle(center + c, center + d, center + a);
}

void main() 
{
    // 0-------3
    // |       |
    // |       |
    // 1-------2
    AddQuad(VoxelVertices[3], VoxelVertices[1], VoxelVertices[5], VoxelVertices[7], VoxelNormals[5]); // Front-Surface
    AddQuad(VoxelVertices[2], VoxelVertices[0], VoxelVertices[1], VoxelVertices[3], VoxelNormals[0]); // Left-Surface
    AddQuad(VoxelVertices[7], VoxelVertices[5], VoxelVertices[4], VoxelVertices[6], VoxelNormals[1]); // Right-Surface
    AddQuad(VoxelVertices[2], VoxelVertices[3], VoxelVertices[7], VoxelVertices[6], VoxelNormals[3]); // Top-Surface
    AddQuad(VoxelVertices[1], VoxelVertices[0], VoxelVertices[4], VoxelVertices[5], VoxelNormals[2]); // Bot-Surface
    AddQuad(VoxelVertices[6], VoxelVertices[4], VoxelVertices[0], VoxelVertices[2], VoxelNormals[4]); // Back-Surface
    
}

shadow map frag

#version 450 core

out vec4 outColor;
void main()
{             
     //gl_FragDepth = gl_FragCoord.z;
}

does anyone know what's wrong?

opengl
rendering
shadow
voxel
asked on Stack Overflow Jul 1, 2020 by Laynel

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0