I've been trying to get shadow mapping to work , but the the depth isn't even writing to the texture . Here's my code .
//this is outside the main loop to generate the depth texture and the framebuffer
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
GLuint depthTexture;
glGenTextures(1, &depthTexture);
glBindTexture(GL_TEXTURE_2D, depthTexture);
glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT, 1024,1024,0,GL_DEPTH_COMPONENT,GL_FLOAT,0); 0,GL_DEPTH_COMPONENT,GL_FLOAT,0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glBindTexture(GL_TEXTURE_2D, 0);
GLuint FramebufferName ;
glGenFramebuffers(1, &FramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER , FramebufferName);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthTexture, 0);
glDrawBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER , 0);
/////////////////this is inside the loop
glBindFramebuffer(GL_FRAMEBUFFER , FramebufferName);
glClearDepth(1.0f);
glClear(GL_DEPTH_BUFFER_BIT);
glViewport(0,0,1024,1024);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
cout<<" WARNING !! ";
Matrix DephtViewMatrix = IDENTITY_MATRIX;
DephtViewMatrix = MultiplyMatrices(DephtViewMatrix , TRANSLATION_MATRIX(0,-25,0)); //my own math header
DephtViewMatrix = MultiplyMatrices(DephtViewMatrix , XROTATION_MATRIX(-1.6));
shadow.UseProgram();
shadow.CreateProjection(60,1024,1024,0.1 ,70);//also tried to change to orthographic projection , but it didn't help
shadow.setMVP(DephtViewMatrix , IDENTITY_MATRIX);
sphere.draws();
terrain.draws();
glBindFramebuffer(GL_FRAMEBUFFER ,0);
//vetrex shader
#version 330
layout(location=0) in vec3 in_Position;
uniform mat4 ViewMatrix;
uniform mat4 ProjectionMatrix;
void main(void)
{
gl_Position = ProjectionMatrix * ViewMatrix * vec4(in_Position.xyz ,1) ;
}
//fragment shader
#version 330
layout(location = 0) out float depth;
void main()
{
depth = gl_FragCoord.z;
}
I'm drawing the texture on a cube , the scene draws correctly when drawing it with colors .