PDA

View Full Version : Shadow Mapping for Directional Light issues



selony24
09-24-2015, 12:07 PM
Hello,

I'm pretty new here on this forum so please don't judge me for asking "probably" a similar question (even that, from my searches I found nothing relevant).

I have some "major" issue with shadow mapping for directional light (spotlight). It actually renders fine, but I have followed a tutorial and the code seems to be pretty similar and couldn't find any explicit difference or to understand what exactly is wrong because I don't get my shadow from an object on another object which is behind of the first object. The shadows are rendered only on my plane and, still...my shadow is propagated only on floor and not on other objects as I expect.

Picture with actual behavior:

2112

I followed a tutorial which says:

1) Render from the point of view of light (using a depth texture)
2) Render normally (using that depth texture)

Code:



void ShadowMapPass(GLuint shaderProgram, GLuint shadowShaderProgram, Mesh& mesh1, Mesh& mesh2, Mesh& mesh3, GLint uniShadowView, GLint uniShadowProj, GLint uniShadowTrans, SpotLight sl[4])
{

glUseProgram(shadowShaderProgram);
shadowMapFBO.BindForWriting();
glClear(GL_DEPTH_BUFFER_BIT);

glm::mat4 lightViewMatrix = glm::lookAt(
sl[0].Position,
sl[0].Direction,
glm::vec3(0.0f, 1.0f, 0.0f)
);

glm::mat4 lightProjMatrix = glm::perspective(
45.0f,
static_cast<float>(WINDOW_WIDTH) / static_cast<float>(WINDOW_HEIGHT),
0.1f,
1000.0f
);

glUniformMatrix4fv(uniShadowProj, 1, GL_FALSE, value_ptr(lightProjMatrix));
glUniformMatrix4fv(uniShadowView, 1, GL_FALSE, value_ptr(lightViewMatrix));

glUniformMatrix4fv(uniShadowTrans, 1, GL_FALSE, value_ptr(
glm::translate(glm::vec3(0.0f, 10.0f, 0.0f)) *
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::scale(glm::vec3(10.0f, 10.0f, 10.0f)
)));

mesh1.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);

glUniformMatrix4fv(uniShadowTrans, 1, GL_FALSE, value_ptr(
glm::rotate(glm::radians(m_scale), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::translate(glm::vec3(100.0f, 20.0f, 0.0f)) *
glm::scale(glm::vec3(10.0f, 10.0f, 10.0f))
));

mesh2.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);

glUniformMatrix4fv(uniShadowTrans, 1, GL_FALSE, value_ptr(
glm::rotate(glm::radians(m_scale), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::translate(glm::vec3(0.0f, 15.0f, 100.0f)) *
glm::scale(glm::vec3(15.0f, 15.0f, 15.0f))
));

mesh3.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);

glm::mat4 trans;
glUniformMatrix4fv(uniShadowTrans, 1, GL_FALSE, value_ptr(trans));
DrawPlane();

glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void RenderPass(GLuint shaderProgram, GLuint shadowShaderProgram, Mesh& mesh1, Mesh& mesh2, Mesh& mesh3, GLint uniView, GLint uniProj, GLint uniTrans, glm::mat4& ProjectionMatrix, glm::mat4& ViewMatrix, GLint lightSpaceMatrix, SpotLight sl[4], Skybox *skybox, GLint cubeSampler, GLint cameraPos)
{
glUseProgram(shaderProgram);
shadowMapFBO.BindForReading(GL_TEXTURE1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox->m_pCubemapTex->m_textureObj);

glUniformMatrix4fv(uniProj, 1, GL_FALSE, value_ptr(ProjectionMatrix));
glUniformMatrix4fv(uniView, 1, GL_FALSE, value_ptr(ViewMatrix));

/*glUniform3f(cameraPos, view->getPosition().x, view->getPosition().y, view->getPosition().z);*/
glUniform1i(cubeSampler, 0);

glm::mat4 lightViewMatrix = glm::lookAt(
sl[0].Position,
sl[0].Direction,
glm::vec3(0.0f, 1.0f, 0.0f)
);

glm::mat4 lightProjMatrix = glm::perspective(
45.0f,
static_cast<float>(WINDOW_WIDTH) / static_cast<float>(WINDOW_HEIGHT),
0.1f,
1000.0f
);

glm::mat4 lightTranslateMatrix = glm::translate(glm::vec3(1.0f, 1.0f, 1.0f));

glUniformMatrix4fv(lightSpaceMatrix, 1, GL_FALSE, value_ptr(lightProjMatrix * lightViewMatrix * lightTranslateMatrix));

glUniformMatrix4fv(uniTrans, 1, GL_FALSE, value_ptr(
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
));
planeTexture->Bind(GL_TEXTURE0);
DrawPlane();

glUniformMatrix4fv(uniTrans, 1, GL_FALSE, value_ptr(
glm::translate(glm::vec3(0.0f, 10.0f, 0.0f)) *
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::scale(glm::vec3(10.0f, 10.0f, 10.0f)
)));

mesh1.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);

glUniformMatrix4fv(uniTrans, 1, GL_FALSE, value_ptr(
glm::rotate(glm::radians(m_scale), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::translate(glm::vec3(100.0f, 20.0f, 0.0f)) *
glm::scale(glm::vec3(10.0f, 10.0f, 10.0f))
));

mesh2.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);

glUniformMatrix4fv(uniTrans, 1, GL_FALSE, value_ptr(
glm::rotate(glm::radians(m_scale), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::translate(glm::vec3(0.0f, 15.0f, 100.0f)) *
glm::scale(glm::vec3(15.0f, 15.0f, 15.0f))
));

mesh3.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);

}


If you see some obvious mistakes/ issues I would be very grateful to know.Thanks

selony24
09-26-2015, 02:18 AM
Hello,

I'm pretty new here on this forum so please don't judge me for asking "probably" a similar question (even that, from my searches I found nothing relevant).

I have some "major" issue with shadow mapping for directional light (spotlight). It actually renders fine, but I have followed a tutorial and the code seems to be pretty similar and couldn't find any explicit difference or to understand what exactly is wrong because I don't get my shadow from an object on another object which is behind of the first object. The shadows are rendered only on my plane and, still...my shadow is propagated only on floor and not on other objects as I expect.

Picture with actual behavior:

2112

I followed a tutorial which says:

1) Render from the point of view of light (using a depth texture)
2) Render normally (using that depth texture)

Code:



void ShadowMapPass(GLuint shaderProgram, GLuint shadowShaderProgram, Mesh& mesh1, Mesh& mesh2, Mesh& mesh3, GLint uniShadowView, GLint uniShadowProj, GLint uniShadowTrans, SpotLight sl[4])
{

glUseProgram(shadowShaderProgram);
shadowMapFBO.BindForWriting();
glClear(GL_DEPTH_BUFFER_BIT);

glm::mat4 lightViewMatrix = glm::lookAt(
sl[0].Position,
sl[0].Direction,
glm::vec3(0.0f, 1.0f, 0.0f)
);

glm::mat4 lightProjMatrix = glm::perspective(
45.0f,
static_cast<float>(WINDOW_WIDTH) / static_cast<float>(WINDOW_HEIGHT),
0.1f,
1000.0f
);

glUniformMatrix4fv(uniShadowProj, 1, GL_FALSE, value_ptr(lightProjMatrix));
glUniformMatrix4fv(uniShadowView, 1, GL_FALSE, value_ptr(lightViewMatrix));

glUniformMatrix4fv(uniShadowTrans, 1, GL_FALSE, value_ptr(
glm::translate(glm::vec3(0.0f, 10.0f, 0.0f)) *
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::scale(glm::vec3(10.0f, 10.0f, 10.0f)
)));

mesh1.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);

glUniformMatrix4fv(uniShadowTrans, 1, GL_FALSE, value_ptr(
glm::rotate(glm::radians(m_scale), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::translate(glm::vec3(100.0f, 20.0f, 0.0f)) *
glm::scale(glm::vec3(10.0f, 10.0f, 10.0f))
));

mesh2.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);

glUniformMatrix4fv(uniShadowTrans, 1, GL_FALSE, value_ptr(
glm::rotate(glm::radians(m_scale), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::translate(glm::vec3(0.0f, 15.0f, 100.0f)) *
glm::scale(glm::vec3(15.0f, 15.0f, 15.0f))
));

mesh3.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);

glm::mat4 trans;
glUniformMatrix4fv(uniShadowTrans, 1, GL_FALSE, value_ptr(trans));
DrawPlane();

glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void RenderPass(GLuint shaderProgram, GLuint shadowShaderProgram, Mesh& mesh1, Mesh& mesh2, Mesh& mesh3, GLint uniView, GLint uniProj, GLint uniTrans, glm::mat4& ProjectionMatrix, glm::mat4& ViewMatrix, GLint lightSpaceMatrix, SpotLight sl[4], Skybox *skybox, GLint cubeSampler, GLint cameraPos)
{
glUseProgram(shaderProgram);
shadowMapFBO.BindForReading(GL_TEXTURE1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox->m_pCubemapTex->m_textureObj);

glUniformMatrix4fv(uniProj, 1, GL_FALSE, value_ptr(ProjectionMatrix));
glUniformMatrix4fv(uniView, 1, GL_FALSE, value_ptr(ViewMatrix));

/*glUniform3f(cameraPos, view->getPosition().x, view->getPosition().y, view->getPosition().z);*/
glUniform1i(cubeSampler, 0);

glm::mat4 lightViewMatrix = glm::lookAt(
sl[0].Position,
sl[0].Direction,
glm::vec3(0.0f, 1.0f, 0.0f)
);

glm::mat4 lightProjMatrix = glm::perspective(
45.0f,
static_cast<float>(WINDOW_WIDTH) / static_cast<float>(WINDOW_HEIGHT),
0.1f,
1000.0f
);

glm::mat4 lightTranslateMatrix = glm::translate(glm::vec3(1.0f, 1.0f, 1.0f));

glUniformMatrix4fv(lightSpaceMatrix, 1, GL_FALSE, value_ptr(lightProjMatrix * lightViewMatrix * lightTranslateMatrix));

glUniformMatrix4fv(uniTrans, 1, GL_FALSE, value_ptr(
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
));
planeTexture->Bind(GL_TEXTURE0);
DrawPlane();

glUniformMatrix4fv(uniTrans, 1, GL_FALSE, value_ptr(
glm::translate(glm::vec3(0.0f, 10.0f, 0.0f)) *
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::scale(glm::vec3(10.0f, 10.0f, 10.0f)
)));

mesh1.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);

glUniformMatrix4fv(uniTrans, 1, GL_FALSE, value_ptr(
glm::rotate(glm::radians(m_scale), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::translate(glm::vec3(100.0f, 20.0f, 0.0f)) *
glm::scale(glm::vec3(10.0f, 10.0f, 10.0f))
));

mesh2.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);

glUniformMatrix4fv(uniTrans, 1, GL_FALSE, value_ptr(
glm::rotate(glm::radians(m_scale), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::translate(glm::vec3(0.0f, 15.0f, 100.0f)) *
glm::scale(glm::vec3(15.0f, 15.0f, 15.0f))
));

mesh3.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);

}


If you see some obvious mistakes/ issues I would be very grateful to know.Thanks

Okay, I manage to find what is the problem here: actually, all objects gets on GL_TEXTURE1 the shadow map which means that, all objects contains that shadow map which is a mistake (because depth map should be rendered over all scene not on all individual objects, you can see the example in my image posted below). Is this maybe a more clear request?

Actual behavior: 2116

Modified code:



void ShadowMapPass(GLuint shaderProgram, GLuint shadowShaderProgram, Mesh& mesh1, Mesh& mesh2, Mesh& mesh3, GLint uniShadowView, GLint uniShadowProj, GLint uniShadowTrans, SpotLight sl[4])
{
glViewport(0, 0, 1024, 1024);
glUseProgram(shadowShaderProgram);
shadowMapFBO.BindForWriting();
glClear(GL_DEPTH_BUFFER_BIT);

glm::mat4 lightViewMatrix = glm::lookAt(
sl[0].Position,
glm::vec3(0.0f, -1.0f, 0.0f),
glm::vec3(1.0f)
);

glm::mat4 lightProjMatrix = glm::perspective(
45.0f,
1024.0f / 1024.0f,
1.0f,
1000000.0f
);

glUniformMatrix4fv(uniShadowProj, 1, GL_FALSE, value_ptr(lightProjMatrix));
glUniformMatrix4fv(uniShadowView, 1, GL_FALSE, value_ptr(lightViewMatrix));

glUniformMatrix4fv(uniShadowTrans, 1, GL_FALSE, value_ptr(
glm::translate(glm::vec3(0.0f, 20.0f, 0.0f)) *
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::scale(glm::vec3(10.0f, 10.0f, 10.0f)
)));

/*mesh1.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);*/
planeTexture->Bind(GL_TEXTURE0);
RenderCube();

glUniformMatrix4fv(uniShadowTrans, 1, GL_FALSE, value_ptr(
glm::rotate(glm::radians(m_scale), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::translate(glm::vec3(80.0f, 20.0f, 0.0f)) *
glm::scale(glm::vec3(10.0f, 10.0f, 10.0f))
));

mesh2.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);

glUniformMatrix4fv(uniShadowTrans, 1, GL_FALSE, value_ptr(
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::translate(glm::vec3(0.0f, 70.0f, cos(glfwGetTime()) * 40.0f)) *
glm::scale(glm::vec3(15.0f, 15.0f, 15.0f))
));

mesh3.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);

glm::mat4 trans;
glUniformMatrix4fv(uniShadowTrans, 1, GL_FALSE, value_ptr(trans));
DrawPlane();

glUniformMatrix4fv(uniShadowTrans, 1, GL_FALSE, value_ptr(
glm::scale(glm::vec3(0.05f, 0.05f, 0.05f)) *
glm::translate(glm::vec3(1.0f, 150.0f, 1.0f))
));
planeTexture->Bind(GL_TEXTURE0);
DrawPlane();

glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void RenderPass(GLuint shaderProgram, GLuint shadowShaderProgram, Mesh& mesh1, Mesh& mesh2, Mesh& mesh3, GLint uniView, GLint uniProj, GLint uniTrans, glm::mat4& ProjectionMatrix, glm::mat4& ViewMatrix, GLint lightSpaceMatrix, SpotLight sl[4], Skybox *skybox, GLint cubeSampler, GLint cameraPos)
{
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glUseProgram(shaderProgram);
shadowMapFBO.BindForReading(GL_TEXTURE1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox->m_pCubemapTex->m_textureObj);

glUniformMatrix4fv(uniProj, 1, GL_FALSE, value_ptr(ProjectionMatrix));
glUniformMatrix4fv(uniView, 1, GL_FALSE, value_ptr(ViewMatrix));

/*glUniform3f(cameraPos, view->getPosition().x, view->getPosition().y, view->getPosition().z);*/
glUniform1i(cubeSampler, 0);

glm::mat4 lightViewMatrix = glm::lookAt(
sl[0].Position,
glm::vec3(0.0f, -1.0f, 0.0f),
glm::vec3(1.0f)
);

glm::mat4 lightProjMatrix = glm::perspective(
45.0f,
1024.0f / 1024.0f,
1.0f,
1000000.0f
);

glm::mat4 lightTranslateMatrix = glm::translate(glm::vec3(1.0f, 1.0f, 1.0f));

glUniformMatrix4fv(lightSpaceMatrix, 1, GL_FALSE, value_ptr(lightProjMatrix * lightViewMatrix * lightTranslateMatrix));

glUniformMatrix4fv(uniTrans, 1, GL_FALSE, value_ptr(
glm::translate(glm::vec3(0.0f, 20.0f, 0.0f)) *
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::scale(glm::vec3(10.0f, 10.0f, 10.0f)
)));

/*mesh1.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);*/
planeTexture->Bind(GL_TEXTURE0);
RenderCube();

glUniformMatrix4fv(uniTrans, 1, GL_FALSE, value_ptr(
glm::rotate(glm::radians(m_scale), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::translate(glm::vec3(80.0f, 20.0f, 0.0f)) *
glm::scale(glm::vec3(10.0f, 10.0f, 10.0f))
));

mesh2.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);

glUniformMatrix4fv(uniTrans, 1, GL_FALSE, value_ptr(
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::translate(glm::vec3(0.0f, 70.0f, cos(glfwGetTime()) * 40.0f)) *
glm::scale(glm::vec3(15.0f, 15.0f, 15.0f))
));

mesh3.Render(shaderProgram, GL_TEXTURE0,
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
);

glUniformMatrix4fv(uniTrans, 1, GL_FALSE, value_ptr(
glm::rotate(glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f))
));
planeTexture->Bind(GL_TEXTURE0);
DrawPlane();

glUniformMatrix4fv(uniTrans, 1, GL_FALSE, value_ptr(
glm::scale(glm::vec3(0.05f, 0.05f, 0.05f)) *
glm::translate(glm::vec3(1.0f, 150.0f, 1.0f))
));
planeTexture->Bind(GL_TEXTURE0);
DrawPlane();

}