FBO depth texture not working properly

So, im trying to implement shadow mapping into my game and I can’t seem to render to the depth texture properly (which is attached to a FBO). I don’t know what part of the code that causes this problem so I wont post code for now. Anyway, here’s a screenshot from my game. It has nothing to do with the problem, I posted it so you could see how it looks like.

Anyway, so instead of rendering the actual textures, I rendered the depth map instead. This is the result:

Yeah, that’s not how it should look like. There appears to be a lot of tiny point everywhere and if you look closley, you can see that theres a line where every edge of the triangle is. However, look at the quad to the right. The depthmap seems to be correct for that quad.

I increased the depth texture dimensions and it now looks like this:

More and smaller points, not very surprising. I’m completly lost now. It’s probably something really simple, but I can’t just figure out what it is. Oh, and by the way, the depth texture is not rendered from the lights POV, but the cameras. I did this to make it more simple.

Any ideas on why this could be happening?

Edit: It appears to work the way it should if the size of the depth texture is less than or equal to 32x32. This doesn’t make me less confused though.

You´re having the depth texture bound while rendering to it? AH. You forgot to set the viewport! xD

The depth texture is not bound while rendering to it. This is my depth map rendering method (simplified)


public void createDepthMap(Light light) {
		
	glBindFramebuffer(GL_FRAMEBUFFER, light.getShadowMapFBO()); 
		
	glClear(GL_DEPTH_BUFFER_BIT);
		
	glPushAttrib(GL_VIEWPORT_BIT); 
	glViewport(0,0,Constants.SHADOW_MAP_WIDTH, Constants.SHADOW_MAP_HEIGHT);
	glColorMask(false, false, false, false);
	
	for (Renderable r : renderableEntities) 
			r.render();
		
	glColorMask(true, true, true, true);
	glPopAttrib(); 
	glBindFramebuffer(GL_FRAMEBUFFER, 0); 
		
	glActiveTexture(GL_TEXTURE7);
	glBindTexture(GL_TEXTURE_2D,light.getShadowMapTexture());
	glActiveTexture(GL_TEXTURE0);
	
}

Here’s my FBO generation method if it makes any difference

public void initFBO() {

		depthTexture = glGenTextures();
		glBindTexture(GL_TEXTURE_2D, depthTexture);
		
		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_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		
		
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
		glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
		
		glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, Constants.SHADOW_MAP_WIDTH, Constants.SHADOW_MAP_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, (ByteBuffer)null);
		
		glBindTexture(GL_TEXTURE_2D, 0);
		
		fbo = glGenFramebuffers();
		glBindFramebuffer(GL_FRAMEBUFFER,fbo);
		glDrawBuffer(GL_NONE);
		glReadBuffer(GL_NONE);
		
		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D, depthTexture, 0);
		
		int FBOstatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
		  if(FBOstatus != GL_FRAMEBUFFER_COMPLETE)
			  System.out.println("GL_FRAMEBUFFER_COMPLETE failed, CANNOT use FBO\n");

		  glBindFramebuffer(GL_FRAMEBUFFER, 0);

	}

Shader problem? You’re also not changing to the light’s matrix when rendering the shadow map.

Could definitly be a shader problem. Ill try to remove all unrelated code in my shaders and post them here later today.

The reason why im not changing to the lights matrix is because right now I’m rendering the depth texture from the cameras POV.

You might still need to at least update the projection matrix since you have a new aspect ratio. Frankly I´m at quite a loss here…

I finally managed to solve it! All I had to do was to disable textures when rendering the depth map.

Still not sure why. It worked when I did this:


// Works
glBindTexture(GL_TEXTURE_2D, tex1)
object1.render();
object2.render();
...

But if I bound another texture it stopped working:


// Doesn't work
glBindTexture(GL_TEXTURE_2D, tex1)
object1.render();
glBindTexture(GL_TEXTURE_2D, tex2)
object2.render();
...

Really weird. Anyway, I’m really happy I finally got it working.

Also,

Thanks for this. I actually forgot to make a new projection matrix. Perhaps not a huge deal, but it probably would’ve given me some bugs to fix later on.

Now I gotta post some smileys to express my happiness:

;D ;D :slight_smile: :smiley: ;D 8) :stuck_out_tongue: :slight_smile: ;D

… which is why I hate the fixed functionality pipeline. You know what happens if you call glColor(…)? ALL following vertices will have the same color. What do you think happens if you have a texture bound but no texture coordinates? It uses the same coordinates (the ones last specified) for every vertex and samples the texture with it! =D Go go OGL3!

How is this bad? O_o

:o I could imagine that to lead to some really weird bugs! ;D

I agree. Starting to use VBOs and writing my own shaders was the best decision ever. Not only is it easier to achieve what you want to, it also gives you a much better understanding of basicly everything related to graphics programming.

How is that good?! It´s ridiculously easy to forget one of them. glClearColor, glEnable(over 9000 different values), glColor, glTexCoord, glSecondaryColor, glBlendMode, e.t.c. If you don´t want texturing you have to disable it or unbind any texture. With OpenGL 3 it doesn´t matter since your shader won´t read the texture. OpenGL reduces the number settings to a managable number, partly by deprecating a lot and partly by forcing you to use things like VAOs, shaders, e.t.c. You can´t leak glClientEnable() stuff since you have VAOs, you can´t leak vertex attributes since everything has to be in buffers, only a very small number of glEnables exist so it´s easy to keep track of them.

Not to mention better performance with instancing and geometry shaders. Oh, and texturing that makes sense. Sampler objects should´ve been there in OGL 0.1, and the pre-shader multitexturing controls are a complete joke.

It’s a shame that a lot of computers doesn’t support OpenGL 3+ yet. That’s why im limiting myself to OpenGL 2 for now.

Anyway, here’s a off-topic question. Are loops supported in GLSL 1.20? The shader doesn’t work on my old computer (which supports glsl 1.2 and opengl 2.1). It compiles without any errors but everything is just black. If I remove the loop and just copy everything inside it a few times, it will work.