Jump to content
  • Advertisement
Sign in to follow this  
bisthebis

What's wrong with my Depth Texture ?

This topic is 914 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

At the moment I'm playing with FBO, which I don't really master. I have no problems with color atachments, but I can't figure out how to use depth textures in my render.

At the moment, I render my scene on a FBO (color+depth), then I draw a textured quad using the depth texture... But it doesn't work, my screen stays black (everyhting is 0 : even if i multiply by 1000 it stays at 0). (Depth test works : if I render the color texture instead of the depth, depth test works correctly)

 

Rendering loop : 

///Render to FBO
glEnable(GL_DEPTH_TEST);

    FBO.Bind(); //Do I really need to explain what it does ?
	glUseProgram(ShaderProgram->getProgramID());
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        ///Setting uniforms etc

    mainScene.drawAll(); 


//draw the quad

glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    glDisable(GL_DEPTH_TEST);
    glViewport(0, 0, m_window.getSize().x, m_window.getSize().y);

    glUseProgram(2DShader.getProgramID());

    quadModel.bindVAO();
    glBindTexture(GL_TEXTURE_2D, FBO.DepthTexureID);
    glDrawArrays(GL_TRIANGLES, 0, 6);

And FBO creation :

glGenFramebuffers(1, &FBO_ID);
	glBindFramebuffer(GL_FRAMEBUFFER, FBO_ID);

		glGenTextures(1, &ColorBufferID);
		glBindTexture(GL_TEXTURE_2D, ColorBufferID);

			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ColorBufferID, 0);

	glBindTexture(GL_TEXTURE_2D, 0);








glGenTextures(1, &DepthTexture);
		glBindTexture(GL_TEXTURE_2D, DepthTexture);
                glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
                glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, DepthTexture, 0);
        glBindTexture(GL_TEXTURE_2D, 0);

Share this post


Link to post
Share on other sites
Advertisement

to use depth buffer in FBO you need to attach renderbuffer to it (part of code is for es 2.0 so i use depth componenet 16)

 

code creates fbo texture, then fbo object then rbo object (its a part of shadowmapping algo i use)


glGenTextures(1, &SHADOW_DEPTH_TEX);
glBindTexture(GL_TEXTURE_2D, SHADOW_DEPTH_TEX);
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_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, RENDER_BUFF_W, RENDER_BUFF_H, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);



glGenFramebuffers(1, &SHADOW_FBO1);
glGenRenderbuffers(1, &SHADOW_RBO1);
glBindRenderbuffer(GL_RENDERBUFFER, SHADOW_RBO1);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, RENDER_BUFF_W, RENDER_BUFF_H);
glBindFramebuffer(GL_FRAMEBUFFER, SHADOW_FBO1);
glBindTexture(GL_TEXTURE_2D, SHADOW_DEPTH_TEX);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                          GL_DEPTH_ATTACHMENT,
                          GL_RENDERBUFFER,
						  SHADOW_RBO1);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, SHADOW_DEPTH_TEX, 0);

Share this post


Link to post
Share on other sites

I don't understand why you use RGBA instead of DEPTH_COMPONENT for the texture. Is it a GLES restriction ?

(BTW, I thought RenderBuffers and depth textures were antinomic... Was I wrong ?)

 

But my problem got solved : I didn't set texture parameters so sampling failed. So simple ^^

Share this post


Link to post
Share on other sites

either way depth test doesnt work on fbo without renderbuffer attached.

(and yes its es 2.0 restriction - i store float in 4 bytes so its rgba for each pixel.

Edited by WiredCat

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!