0
votes

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 .

1

1 Answers

0
votes

Can you please edit your question to show the shader you are using to sample the depth texture?

Immediately, I notice that you have texture comparison set to GL_COMPARE_R_TO_TEXTURE; this means that unless you sample the texture using sampler2DShadow, samplerCubeShadow, etc. that the results will be undefined. Textures with comparison mode != GL_NONE must be sampled using a ...Shadow sampler, likewise textures with comparison mode == GL_NONE must not be sampled with a ...Shadow sampler.

And even if you use the appropriate sampler type for your depth texture, if you have texture comparison enabled then the result of sampling the texture is going to be a binary pass/fail (1.0 or 0.0) result of performing a depth test on the stored depth, rather than the depth itself. You really cannot visualize the depth stored in a shadow map using texture comparison. But the good news is that you can change the comparison mode on-the-fly depending on how you intend to use the depth texture, it just needs to match your sampler type in the shader you are using.

You might want to take a look at this related answer that I wrote, which explains the differences between shadow and non-shadow samplers.