Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Depth Clamped Decals

  1. #1
    Junior Member Regular Contributor
    Join Date
    Mar 2012
    Posts
    114

    Depth Clamped Decals

    Hello,

    I am trying to clamp decals to walls such that they do not overhang edges by testing if decal fragment depths are within a certain tolerance of the existing depth buffer value.

    Is there a way to do this without a shader?

    I tried using a shader, but it doesn't seem to read the depth properly. The decals just disappear if they are a certain distance away.

    Here is the pertinent line in the shader:

    Code :
    if(gl_FragCoord.z < texture2D(gDepthSampler, gl_FragCoord.xy / vec2(windowWidth, windowHeight)).r - 0.01)
            discard;

    Thank you for any help you can offer.

  2. #2
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,421
    Precision will always be an issue. Since you are dealing with floating point values, you need to do something like this
    Code :
    float depth = texture2D(gDepthSampler, gl_FragCoord.xy / vec2(windowWidth, windowHeight)).r;
    if((abs(gl_FragCoord.z - depth) < 0.01)
      discard;

    but perhaps it isn't a good idea to use a fixed value like 0.01 since depth precision changes from back to front (when you are using a perspective projection).
    There is less precision at the back than there is at the front.
    Perhaps instead of a fixed value, use a 1D texture (floating point) with various values :

    Code :
    float depth = texture2D(gDepthSampler, gl_FragCoord.xy / vec2(windowWidth, windowHeight)).r;
    float precision = texture1D(Sampler, gl_FragCoord.z).r;
    if((abs(gl_FragCoord.z - depth) < precision)
      discard;
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  3. #3
    Junior Member Regular Contributor
    Join Date
    Mar 2012
    Posts
    114
    Thank you for your reply!

    Precision will probably become an issue later on, but right now it seems like it isn't reading the depth texture properly.

    I set GL_TEXTURE_COMPARE_MODE to GL_NONE, and disabled depth buffer writes (glDepthMask). However, I seem to always get the same value from the texture, since the decals disappear all at once at a certain distance.

    What are the proper settings for reading the depth texture in a shader?

  4. #4
    Junior Member Regular Contributor
    Join Date
    Mar 2012
    Posts
    114
    Bumping...

  5. #5
    Junior Member Regular Contributor
    Join Date
    Mar 2012
    Posts
    114
    Bumping...

  6. #6
    Junior Member Regular Contributor
    Join Date
    Mar 2012
    Posts
    114
    Bumping...

  7. #7
    Junior Member Regular Contributor malexander's Avatar
    Join Date
    Aug 2009
    Location
    Ontario
    Posts
    246
    Values in a depth texture will be in the range [0,1], and non-linear at that. To convert them back to a world space, you need to run them through:

    Code :
    Wz = a /  (depth + b)
     
    where:
     
    a = (far * near) / (near - far);
    b = -0.5 * (far+near) / (far-near) - 0.5;

    and far and near are the values of the near and far clip planes. You can precompute a and b in your CPU code and send them as a vec2 uniform to the shader. These values are taken from the inverse projection matrix entries that affect Z.

    Doing this on both gl_FragCoord.z and the depth texture value will allow you to use a fixed Z offset based on your scene.

    Also, if you're using an AMD card, it doesn't appear to like reading from a depth texture that's attached to the current draw framebuffer, even if depth writes are disabled.
    Last edited by malexander; 09-05-2012 at 05:37 PM.

  8. #8
    Member Regular Contributor
    Join Date
    Jan 2005
    Location
    USA
    Posts
    433
    Is GlDepthFunc still around? I don't suppose the tolerance for GL_EQUAL is acceptable if so. I always thought depth tests should have a tolerance state.
    God have mercy on the soul that wanted hard decimal points and pure ctor conversion in GLSL.

  9. #9
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,712
    Is GlDepthFunc still around?
    A better question would be, "How could it be gone?" If you don't have glDepthFunc, you don't have depth testing, since the function is what defines the test. It's not like the only possibility other than GL_LESS was GL_EQUAL; even if they ditched GL_EQUAL (and there's absolutely no reason to do so), they still would have to handle GL_GREATER and so forth.

    I don't suppose the tolerance for GL_EQUAL is acceptable if so.
    The tolerance for GL_EQUAL is and has always been acceptable: equals. Not "kinda equals". Or "nearly equals." It's "equals." That is the tolerance for it, and sometimes that is exactly what you need.

  10. #10
    Member Regular Contributor
    Join Date
    Jan 2005
    Location
    USA
    Posts
    433
    ^It was an expression; but then again you never know. I was just curious because that seemed like what the OP is looking for. But I realize z-fighting and all. I am way behind the times OpenGL wise. ES is just like I remember it. But I don't know how the main branch has evolved in the last 5 to 10 years. I intend to find out over the course of the next few years.

    Was just offering maybe another way to think about the problem because a depth buffer seems pretty drastic unless its possible to sample the bound depth buffer or it is already handy or the application is solely about decals! GL_EQUAL seems pretty useless because PolygonOffset can't really address it, but I may be wrong. Don't take my word for anything.
    God have mercy on the soul that wanted hard decimal points and pure ctor conversion in GLSL.

Posting Permissions

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