1
votes

I'm having a problem with the depth buffer. I want to put into a texture. But it doesn't seem to work.

So, here's the piece of code I execute after rendering the objects:

  glGenTextures(1, (GLuint*)&_depthTexture);
  glBindTexture(GL_TEXTURE_2D, _depthTexture);

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

  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

  const pair<int, int> &img_size = getImageSize();
  glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, img_size.first, img_size.second, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, img_size.first, img_size.second);

  glClear( GL_DEPTH_BUFFER_BIT );

The thing is (I'm with OpenGL 3.2+), the image for rendering has different size. Most of the time, it won't be a 2^i by 2^j for the size. So, is that a problem ?

Also, the other part of the problem might be in the fragment shader after:

#version 140

uniform sampler2D depthTexture;
uniform ivec2 screenSize;

out vec4 outColor;

void main()
{
    vec2 depthCoord = gl_FragCoord.xy / screenSize.xy;
    float d = texture2D(depthTexture, depthCoord).x;
    outColor =  vec4(d, d, d, 1.0);
}

After that, when I render a second time some shapes, I want to use the previous depth (the texture depth buffer), to do some effects.

But seriously... can anyone just show me a piece of code where you can get the depth buffer into a texture? I don't care if it's rendering to the texture or if the texture is extracted after the rendering! As long as I have a texture with the depth value to do the second pass... that's what is important!

2
"it doesn't seam to work." In what way does it not work? - Nicol Bolas
my image is black! And according to other website, the depth buffer should look white when it's far and black close... but half of my image should be white!... at least the part covered by the new triangles! - widgg
"the depth buffer should look white when it's far and black close" That rather depends on what's stored there. If you use the default depth range, then this will be true. - Nicol Bolas
But my code, is it okay or not ? to put the depth buffer in a texture ? - widgg
Try glEnable(GL_DEPTH_TEST); Why not use FBO for rending depth and avoid copy operation and texture size issue? - ChatCloud

2 Answers

2
votes

http://www.joeforte.net/projects/soft-particles/

this might be a good solution!

At least, it's the full code... might be able to get all the different parts!

1
votes

You may need a glReadBuffer call. If your context is double-buffered, that would be glReadBuffer( GL_BACK ).

Also, try GL_DEPTH_COMPONENT24, since a 32-bit depth buffer would be unusual, I think.