1

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!

4

2 回答 2

2

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

这可能是一个很好的解决方案!

至少,它是完整的代码......也许能够得到所有不同的部分!

于 2012-09-04T17:52:55.427 回答
1

你可能需要一个glReadBuffer电话。如果您的上下文是双缓冲的,那将是glReadBuffer( GL_BACK ).

另外,请尝试GL_DEPTH_COMPONENT24,因为我认为 32 位深度缓冲区是不寻常的。

于 2012-08-30T21:28:10.770 回答