Failed to generat the shadow map

I’ve been struggling with the shadow mapping for two days (with JOGL), yet still couldn’t make it work. Now I just want to render a very simple shadow map (grass), where closer looks brighter and further looks darker, from the view of the light point.

Here is my code:


//setting up buffers
gl.glGenFramebuffers(1, framebuff);
gl.glBindFramebuffer(GL4.GL_FRAMEBUFFER, framebuff.get(0));

gl.glGenTextures(2, textureBuff);
gl.glBindTexture(GL4.GL_TEXTURE_2D, textureBuff.get(0));
gl.glTexStorage2D(GL4.GL_TEXTURE_2D, 1, GL4.GL_R32F, displayWidth, displayHeight);
gl.glTexParameteri(GL4.GL_TEXTURE_2D, GL4.GL_TEXTURE_MAG_FILTER, GL4.GL_LINEAR);
gl.glTexParameteri(GL4.GL_TEXTURE_2D, GL4.GL_TEXTURE_MIN_FILTER, GL4.GL_LINEAR_MIPMAP_LINEAR);
gl.glFramebufferTexture(GL4.GL_FRAMEBUFFER, GL4.GL_COLOR_ATTACHMENT0, textureBuff.get(0), 0);

gl.glBindTexture(GL4.GL_TEXTURE_2D, textureBuff.get(1));
gl.glTexStorage2D(GL4.GL_TEXTURE_2D, 1, GL4.GL_DEPTH_COMPONENT32F, displayWidth, displayHeight);
gl.glTexParameteri(GL4.GL_TEXTURE_2D, GL4.GL_TEXTURE_MAG_FILTER, GL4.GL_LINEAR);
gl.glTexParameteri(GL4.GL_TEXTURE_2D, GL4.GL_TEXTURE_MIN_FILTER, GL4.GL_LINEAR_MIPMAP_LINEAR);
gl.glTexParameteri(GL4.GL_TEXTURE_2D, GL4.GL_TEXTURE_COMPARE_MODE, GL4.GL_COMPARE_REF_TO_TEXTURE);
gl.glTexParameteri(GL4.GL_TEXTURE_2D, GL4.GL_TEXTURE_COMPARE_FUNC, GL4.GL_LEQUAL);
gl.glFramebufferTexture(GL4.GL_FRAMEBUFFER, GL4.GL_DEPTH_ATTACHMENT, textureBuff.get(1), 0);
gl.glDrawBuffer(GL4.GL_NONE);

if(gl.glCheckFramebufferStatus(GL4.GL_FRAMEBUFFER) != GL4.GL_FRAMEBUFFER_COMPLETE)
			System.out.println(gl.glCheckFramebufferStatus(GL4.GL_FRAMEBUFFER));

drawing command (not sure if it’s correct):


//gl.glBindFramebuffer(GL4.GL_FRAMEBUFFER, framebuff.get(0));
gl.glViewport(0, 0, displayWidth, displayWidth);
gl.glEnable(GL4.GL_POLYGON_OFFSET_FILL);
gl.glPolygonOffset(2.0f, 4.0f);

/*
gl.glClearBufferfv(GL4.GL_COLOR, 0, new float[] {0, 0, 0}, 0);
gl.glClearDepth(1.0f);
gl.glClear(GL4.GL_DEPTH_BUFFER_BIT);*/

setupMVPMatrix();
gl.glBindVertexArray(vaoBuff.get(0));

gl.glUseProgram(shaderProgram);
gl.glDrawArraysInstanced(GL4.GL_TRIANGLE_STRIP, 0, 5, 512 * 512);

gl.glDisable(GL4.GL_POLYGON_OFFSET_FILL);
//gl.glBindFramebuffer(GL4.GL_FRAMEBUFFER, 0);

when I comment the glBindFramebuffer(), the grass appears correctly with the white color (from the light point of view, which shows the matrix should be correct).
But if I call glBindFramebuffer() with depth test enabled, everything just disappeared while I expect the closer grass to be brighter and further grass to be darker. I have also checked the framebuffer status, yet it seems there is no error. Any help?

Fixed, I didn’t understand the meaning of off-screen rendering at the beginning. Just render the depth value into the default framebuffer for display with the texture storing the depth value, and everything work just fine.

what actually is rendered? Can you show the shadow texture itself? And what does your shader code look like?

I just followed the tutorial here http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/
And I was trying to achieve that gray texture showed before using the shadow map on that tutorial. The shader code is exactly the same. I just want to know the drawing commands that is suppose to achieve that gray texture (not for that geometry shape, but to render something on the screen).

If there aren’t any errors then you probably are drawing to the frame buffer correctly, everything should disappear (no longer drawing to the screen, but to a texture).

Now what I don’t see is a place where you render the textures drew to the framebuffer.

Yeah, I just realized that. But how, I checked the blue book yet I did not find the code of that part.

You might be overthinking this, have you ever drawn a texture on an object?

set an active texture, bind the texture you want to use, textureBuff.get(0) should have the color info, draw a plane with proper uv coords (and no lighting applied), and done.

Fixed finally. God, it took me 5 days. Thank you very much.

No problem! ;D