iPhone Dev: Render Depth Buffer to Texture results in black texture
NickName:benitosub Ask DateTime:2011-09-12T20:48:50

iPhone Dev: Render Depth Buffer to Texture results in black texture

EDIT: I took FBOs out of the equation to make sure the problem wasn't coming from there, so now I am just doing the following:

setup texture:

GLuint FB0Texture;
glGenTextures(1, &FB0Texture);
glBindTexture(GL_TEXTURE_2D, FB0Texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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);

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, size.width, size.height, 
                 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);

and then to render:

[self drawScene];

[(EAGLView *)self.view bindFB0];

glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 320, 480);

[self drawFullScreenQuad]; (it's actually not full screen so I can see both the 3d scene and the resulting texture)

and I get the same results! Black texture if I setup FB0Texture as a "DEPTH_COMPONENT" texture, but normal scene if I setup FB0Texture as a regular GL_RGBA texture...

So I guess I am either setting up the depth texture wrong, or is it possible I am doing something wrong in the rendering which would result in the RGBA texture looking normal but the depth texture being screwed? GL_DEPTH_TEST is enabled and working fine (models look correct) so I think there is nothing wrong with the actual rendering, but at this point I am desperate for clues ...

Please help!

// END OF EDIT

I am trying to implement some basic shadow mapping on iphone, but I am stuck at rendering the depth values to a texture ... This is driving me crazy, I am obviously doing something wrong but cannot figure out what it is ...

So what I am doing :

  • setup default framebuffer, attach color render buffer and depth render buffer
  • setup a new framebuffer (call it FB0), attach a texture to the depth component

Then when I render:

  • draw scene to FB0
  • switch to default framebuffer
  • draw full screen quad using FB0 as a texture

And I get ... nothing! (well I get a full white texture)

If I attach the texture to the color component of FB0 instead of the depth component, everything works fine, as in I draw the scene to FB0, then use FB0 as a texture to draw a full screen quad, and I see exactly what I would see if I was drawing directly to the default framebuffer.

However when I attach the texture to the depth component, all I get is this white screen / texture.

Some code:

setup buffers:

// Create default framebuffer object.
glGenFramebuffers(1, &defaultFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);

// Create color render buffer and allocate backing store.
glGenRenderbuffers(1, &colorRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);

// Create depth render buffer
glGenRenderbuffers(1, &depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 320, 480); 
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);


// Create Depth Map Frame Buffer

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

// Attach texture to depth component        

glGenTextures(1, &FB0Texture);
glBindTexture(GL_TEXTURE_2D, FB0Texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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);

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, size.width, size.height, 
                 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, 0);

glFramebufferTexture2D(GL_FRAMEBUFFER, 
                       GL_DEPTH_ATTACHMENT, 
                       GL_TEXTURE_2D, 
                       FB0Texture, 
                       0);

If I check the status of the framebuffers after this I get no error messages, so I am assuming the buffers are created correctly.

Then in the rendering:

[(EAGLView *)self.view setFB0]; (just binds the framebuffer)

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

[self drawScene];

[(EAGLView *)self.view setFramebuffer]; (binds back the default frame buffer)

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

[(EAGLView *)self.view bindFB0Texture];

[self drawFullScreenQuad];

[(EAGLView *)self.view presentFramebuffer];

At this point I would expect to see a black and white texture with levels of gray indicating the depth, but instead I just get a solid white quad / texture.

Again, if I attach FB0Texture to GL_COLOR_ATTACHMENT0 instead of GL_DEPTH_ATTACHMENT everything works as expected, my scene gets rendered to the texture correctly. So I guess the problem is coming from how I am attaching the texture, or maybe the order in which I am doing stuff ?

Copyright Notice:Content Author:「benitosub」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7388128/iphone-dev-render-depth-buffer-to-texture-results-in-black-texture

More about “iPhone Dev: Render Depth Buffer to Texture results in black texture” related questions

iPhone Dev: Render Depth Buffer to Texture results in black texture

EDIT: I took FBOs out of the equation to make sure the problem wasn't coming from there, so now I am just doing the following: setup texture: GLuint FB0Texture; glGenTextures(1, &FB0Texture);

Show Detail

Render the depth buffer into a texture using a frame buffer

I am using JOGL, but this question applies to OpenGL in general. There seem to be similar questions lying around, but they are either directed at GLSL code, have to do with copying the contents of a

Show Detail

Unable to render depth texture

I'm having trouble with rendering depth texture using frame buffer in opengl and I can not find the problem by myself. Here are the setup: //initialize color texture glGenTextures(1, &color_...

Show Detail

Render depth buffer to texture

Quite new at shaders, so please bear with me if I am doing something silly here. :) I am trying to render the depth buffer of a scene to a texture using opengl ES 2.0 on iOS, but I do not seem to ...

Show Detail

Switching from render buffer to depth texture

Here is my code for creating a new FBO. Right now, I am using a render buffer. /** * Creates a new FBO. * @param width The width of the FBO to create. * @param height The height of the FBO to c...

Show Detail

OpenGL ES 2.0 render to framebuffer/texture results in black texture

I'm using libgdx but this is pretty much vanilla opengl es 2.0 stuff. Just try and ignore the Gdx.gl prefix everywhere ^^ I'm testing it on my desktop as well as android device and it's the same st...

Show Detail

openGL render to texture in iPhone fails when MSAA is enabled

My render to texture iPhone code only works if I disable MSAA, otherwise all I get is a black texture. What could be the cause of the problem? Here is my code: glViewport(0,0, target->_Width, ...

Show Detail

OpenGL - Frame Buffer Depth Texture differs from Color Depth Texture

I'm doing shadow mapping in OpenGL - as such I've created a frame buffer object where I render the depth of the scene from the view of a light. glBindRenderbuffer(GL_RENDERBUFFER, color_buffer);

Show Detail

render depth buffer to texture using glCopyTexSubImage2D

I want to render the depth buffer to a texture, and draw a quad with the DepthTexture. But it doesn't work. First i make DepthTexture: glGenTextures(1, &_iTexDepth); glBindTexture(GL_TEXTURE...

Show Detail

Create depth buffer histogram texture with GLSL

I'm using the depth buffer of the current context to influence a texture I am displaying. The texture is 1 dimensional and in grayscale. From left to right represents from near to far. The more pix...

Show Detail