Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: Urgent: Accessing Depth Texture FBO in GLSL

  1. #1
    Intern Newbie
    Join Date
    Sep 2010
    Posts
    41

    Urgent: Accessing Depth Texture FBO in GLSL

    I have a depth texture with the format GL_DEPTH_COMPONENT24 and I was wondering how you blit it to the screen for debugging purposes. How would you go about accessing these depth formats in a glsl. Are the depth values stored in r/g/b/a. How exactly do I go about this?

  2. #2
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,261

    Re: Urgent: Accessing Depth Texture FBO in GLSL

    texture(depth,coord).x;

  3. #3
    Intern Newbie
    Join Date
    Sep 2010
    Posts
    41

    Re: Urgent: Accessing Depth Texture FBO in GLSL

    Thanks allot for the quick response. Will give it a go and get back to you.

  4. #4
    Intern Newbie
    Join Date
    Sep 2010
    Posts
    41

    Re: Urgent: Accessing Depth Texture FBO in GLSL

    Why is it that the depth texture only shows visible depth values when you very close to an object? Any way of making it more visible?


  5. #5
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,718

    Re: Urgent: Accessing Depth Texture FBO in GLSL

    Why is it that the depth texture only shows visible depth values when you very close to an object?
    Because that's how depth works. Only the nearest objects have small values; most object have values close to 1.0. That's why directly visualizing a depth buffer is generally not useful unless you do some transformations do the depth data.

  6. #6
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    2,882

    Re: Urgent: Accessing Depth Texture FBO in GLSL

    Quote Originally Posted by Alfonse Reinheart
    Because that's how depth works. Only the nearest objects have small values; most object have values close to 1.0. That's why directly visualizing a depth buffer is generally not useful unless you do some transformations do the depth data.
    Yeah, for a perspective projection, see this:
    * http://www.geeks3d.com/20091216/geex...uffer-in-glsl/

    Do what they're doing, except that the last line of their LinearizeDepth(vec2 uv) function is wrong. Instead, you want:

    Code :
    return (n * z) / ( f - z * (f - n) );

    You'll notice that, unlike their function, the above for z = 0 (near clip value) results in 0 (black) and z = 1 (far clip value) results in 1 (white). You can easily derive this from the perspective definition of eye-space z (z_eye) as a function of the depth buffer value (z):

    Code :
    eye.z = n * f / ((z * (f - n)) - f);
    To map -n..-f to 0..1 (black..white), just negate, subtract n, and divide by (f-n), simplify, and you're there.

  7. #7
    Member Regular Contributor remdul's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands
    Posts
    335

    Re: Urgent: Accessing Depth Texture FBO in GLSL

    If you don't do this yet, you may need to temporarily change the compare mode:

    Code :
    // draws textured rectangle with shadow depth texture
    void CShadow::DrawDebug() const
    {
     BindShader(debugshader);
     glActiveTextureARB(GL_TEXTURE0);
     glBindTexture(GL_TEXTURE_2D, tex);
     
      // sample like regular texture
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
     
      // draw quad
      DrawRect(10,10,256,256);
     
      // reset compare mode for shadow sampling again
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
     glBindTexture(GL_TEXTURE_2D, 0);
    }
    And in the shader, sample from it like a regular texture.

  8. #8
    Intern Newbie
    Join Date
    Sep 2010
    Posts
    41

    Re: Urgent: Accessing Depth Texture FBO in GLSL

    Buffers a rendering correctly here's a pic.

    Normals, Specular Term, Diffuse Term, Depth

    It's a partial differed renderer that uses pre-pass lighting. I also had a go with best fit normals but it seems to have an effect on performance when in has to scale normals in the fragment shader making it not a viable choice. I'm busy with the material system and I'm having a problem with the piece of code below.

    Code :
    code = "#version 120\n"
     
    	"uniform mat4 u_proj;"
    	"uniform mat4 u_view;"
    	"uniform mat4 u_world;"
     
    	"uniform mat4 u_worldinvtrans;"
    	"uniform mat4 u_viewinv;"
    	"uniform mat4 u_vpinverse;"
     
    	"attribute vec3 a_pos;"
    	"attribute vec2 a_uv;"
     
    	"varying vec2 v_uv;"
    	"varying vec4 v_pos;"
     
    	"void main() {"
     
    		"vec4 worldPos=u_world*vec4(a_pos,1);"
    		"vec4 viewPos=u_view*worldPos;"
     
    		"vec4 p = u_proj*viewPos;"
    		"gl_Position = u_proj*viewPos;"
    		"v_uv = a_uv;"
    		"v_pos = gl_Position;"
    	"}";
    vs->compileSourceCode(code);
     
    code = "#version 120\n"
     
    	"uniform sampler2D u_samp;"
    	"uniform sampler2D u_light;"
     
    	"varying vec2 v_uv;"
    	"varying vec4 v_pos;"
     
    	"void main() {"
    		"vec4 c = texture2D(u_samp,v_uv);"
    		"vec4 l = texture2D(u_light,(v_pos.xy+1)*0.5f);"
    		"gl_FragData[0] = vec4(c.rgb*l.rgb+l.a,1);"
    	"}";
    fs->compileSourceCode(code);
     
    sp->attach(vs);
    sp->attach(fs);
     
    sp->link();
    sp->validate();

    The shader is used when rendering a mesh on a second pass and adding the lighting from the light buffer.

    The problem is laying with this piece of code.
    vec4 l = texture2D(u_light,(v_pos.xy+1)*0.5f);
    The texture coordinates it is producing is wrong. The light buffer is binded correctly and the uniform u_light is set correctly so it's not the problem.

  9. #9
    Intern Newbie
    Join Date
    Sep 2010
    Posts
    41

    Re: Urgent: Accessing Depth Texture FBO in GLSL

    The problem is sorted out with this line of code.
    "vec4 l = texture2D(u_light,(v_pos.xy/v_pos.w+1)*0.5f);"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •