widgg
widgg

Reputation: 1428

OpenGL depth buffer to texture (for various image sizes)

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!

Upvotes: 1

Views: 2086

Answers (2)

widgg
widgg

Reputation: 1428

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!

Upvotes: 2

JWWalker
JWWalker

Reputation: 22707

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.

Upvotes: 1

Related Questions