Hello I am trying to implement shadows in openGL using C++.
I created a FrameBuffer and a DepthTexture. Every frame Im rendering my entities to the FrameBuffer. For now Im just displaying the texture on the screen like any other GUI, but the texture is completely white and it's not changing when I move the camera around. I hope you can help me find the problem.
My Code:
//Creating FrameBuffer + Texture
glGenFramebuffers(1, &depthMapFBO);
glGenTextures(1, &depthMap);
glBindTexture(GL_TEXTURE_2D, depthMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, m_width, m_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);
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthMap, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
In the renderer:
glm::mat4 lightSpaceMatrix = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, Camera::ZNEAR, Camera::ZFAR) * glm::lookAt(m_position, m_position + m_forward, m_up);
m_shader.Bind();
m_shader.loadMat4("lightSpaceMatrix", lightSpaceMatrix);
//Bind the FrameBuffer
glViewport(0, 0, m_width, m_height);
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
//Render all Entitys
for (auto entity : m_entitys) {
m_shader.loadMat4("model", entity->getTransform()->getModel());
entity->getTexturedModel()->getMesh()->Draw();
}
//Unbind the FrameBuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, Setting::width, Setting::height);
m_shader.unbind();
My Vertex Shader:
#version 430
in layout(location=0) vec3 position;
uniform mat4 lightSpaceMatrix;
uniform mat4 model;
void main()
{
gl_Position = lightSpaceMatrix * model * vec4(position, 1.0f);
}
My Fragment Shader:
#version 430
void main()
{
gl_FragColor = vec4(1);
}
gl_FragColor = vec4(1);? - Ramil Kudashev