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 8 of 8

Thread: How to force unquantized (-1.0->1.0) depth in GLSL

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2009
    Posts
    4

    How to force unquantized (-1.0->1.0) depth in GLSL

    Hi!

    What are the key parameters to look at in order to have fragment depth mapped to the range (-1.0 -> 1.0) instead of the range (near plane -> far plane) ? I'm trying to get rid of the far plane divide in the following (extremely simple) shader code because I don't think it's neat

    Vertex shader:
    Code :
    void main( void ) {
     
    	gl_Position = ftransform();
    }

    Fragment shader:
    Code :
    void main (void) {
    	float result=gl_FragCoord.z/gl_FragCoord.w;
    	result/=10.0; // <--- this must go
    	gl_FragColor = vec4(result,result,result,1.0);
    }

    I'm rendering to an FBO which has a depth attachment with internal and external format GL_DEPTH_COMPONENT and have tried several texture types (GL_FLOAT, GL_HALF_FLOAT, GL_UNSIGNED_BYTE) but nothing seems to make it so the frag depth is ranged from -1.0 to 1.0, the values always range from 0 to 10.0 (far plane)

    It's something simple I'm overlooking but I can't put my finger on it. Maybe you can?

  2. #2
    Intern Contributor
    Join Date
    Nov 2006
    Location
    Peru
    Posts
    97

    Re: How to force unquantized (-1.0->1.0) depth in GLSL

    FBO + MRT ?

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

    Re: How to force unquantized (-1.0->1.0) depth in GLSL

    That division (which you should make a multiplication) is the fastest way.
    Could move it to vtxshader;

  4. #4
    Junior Member Newbie
    Join Date
    Apr 2009
    Posts
    4

    Re: How to force unquantized (-1.0->1.0) depth in GLSL

    No MRT, just single rendertarget to an fbo. Sure it can be moved to the vertex shader (actually already did that using a varying float) but I posted it as being in the fragment shader to keep the example as simplified as possible.

    You see, whatever code or book I look at, everyone seems to start off with a given -1.0,1.0 range. So somewhere in my code, which is huge, some annoying little thing is making it so that this is no longer the case; this just makes me curious as to the possible parameters which determine these 2 behaviours.

  5. #5
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,370

    Re: How to force unquantized (-1.0->1.0) depth in GLSL

    What are the key parameters to look at in order to have fragment depth mapped to the range (-1.0 -> 1.0) instead of the range (near plane -> far plane) ?
    I don't understand your problem. After perspective divide (I mean, after dividing by w), you get normalized device coordinates, which actually are in the [-1, 1]. So you don't need to divide by the far plane distance or something!
    Then the actual deprh stored in the depth buffer is in the [0 1] range which is 0.5 * [-1 1] + 0.5

  6. #6
    Junior Member Newbie Dave Driesen's Avatar
    Join Date
    Apr 2009
    Location
    Belgium
    Posts
    7

    Re: How to force unquantized (-1.0->1.0) depth in GLSL

    Hehe yes it is confusing and I've been going over and over it in my head for a while as well so my explanation is probably pretty confusing in itself.

    What I observed is that after perspective divide I'm not getting normalized coordinates. What I'm getting is (well erm, "looked to me like") the actual distance from the near plane to the frag.

    So let's say that if you have a near plane of 1 and a far plane of 21, and draw a frag smack in the middle at distance 11, then after perspective divide, what you should end up with is roughly 0.0 and what goes in the depth buffer is 0.5 right, that makes sense.

    But instead, what I'm getting after perspective divide is 10.0, so in order to fix this and get to that 0.5 that needs to go into the depth buffer (well shadow map in this case), I need to divide that 10.0 by 20(=near-far). For some reason, these coordinates seem to indeed be quantized to the integer(?) distance between the planes.

    Don't break your head over this though, all the code works but it does seem bizarre and needless so I'm just throwing it out there in case someone else might have encountered this in the past or something; and seeing as google is no help, this seems like a good place to leave a solution for others in the future

Posting Permissions

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