Results 1 to 6 of 6

Thread: Problem when rendering transparent sprite.

  1. #1
    Member Contributor
    Join Date
    Oct 2015
    Posts
    60

    Problem when rendering transparent sprite.

    Hi, is the stencil buffer value affected if the alpha test fails ?

    I want to doesn't increment the stencil value if the alpha test fails.

    The problem is that when I compute the average color, and when a transparent sprite is drawn, there is a trace of the transparent sprite so ti gives me "glitch effets".

    It seems that the value in the stencil buffer is incremented event with transparent fragments. :/

  2. #2
    Member Contributor
    Join Date
    Oct 2015
    Posts
    60
    Apparently yes.

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,103
    Quote Originally Posted by laurent7601 View Post
    Hi, is the stencil buffer value affected if the alpha test fails ?
    Are you referring to glEnable(GL_ALPHA_TEST) (which requires the compatibility profile), or just writing pixels with a zero alpha component?

    In the latter case, there's nothing special about an alpha of zero. The fragment is still "rendered"; the depth and stencil buffers will be updated. The colour buffer may also be modified, depending upon the blending settings used.

    If you want fragments to be discarded entirely, execute a discard statement in the fragment shader. But note that this disables the early fragment test optimisation, so it has a performance impact.

  4. #4
    Member Contributor
    Join Date
    Oct 2015
    Posts
    60
    Apparently the value of the stencil buffer is not incremented if alpha = 0. (Event if the alpha test is disabled)
    Code :
    glCheck(glEnable(GL_STENCIL_TEST));
                    glCheck(glStencilFunc(GL_ALWAYS, 0, 0xFFFF));
                    glCheck(glStencilOp(GL_KEEP, GL_INCR, GL_INCR));

    If alpha = 0 the value of nbLayers is 1 here : (if I draw only one sprite)
    Code :
    "if (z >= max_z && nbLayers <= 1) {"
                        "    if (front_color.a == 0) {"
                        "       gl_FragColor = vec4(color.rgb * color.a, color.a);"
                        "    } else {"
                        "       gl_FragColor = vec4(front_color.rgb * front_color.a, front_color.a);"
                        "    }"

    It's a problem because if alpha = 0 black pixels are rendered instead of transparent.
    If I write the source color it doesn't work because it blend all the colors (I use additive blending after the shader execution to blend) and if I have opaque red with opaque green I have opaque yellow this is why I do this if there are multiple layers :

    Code :
    "} else if (z < max_z) {"
                        "    gl_FragColor = vec4(color.rgb * (1 - front_color.a) + front_color.rgb * front_color.a, color.a);"
                        "}"

    How can I tell to opengl to increment the stencil value event if the alpha is 0 ?

    Otherwise the code works well for oit it's just when the last sprite is transparent that I have this problem. O_O

  5. #5
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,674
    It's kind of hard to tell what it is you're trying to do here. What `front_color` is as distinct from `color`. And so forth.

    But in any case, the fragment shader has almost no effect on how the stencil test works. The only thing an FS can do that affects the stencil test is to discard the fragment. And that just means that the stencil test/operation never happens for that fragment.

  6. #6
    Member Contributor
    Join Date
    Oct 2015
    Posts
    60
    Ok I used another algorithm it's solved the problem...

Similar Threads

  1. Replies: 4
    Last Post: 06-16-2016, 04:47 AM
  2. Sprite Skewing Problem
    By Aare1 in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 02-05-2012, 05:58 PM
  3. OpenGL ES 1.1: transparent line + sprite/texture
    By Lococo in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-12-2011, 02:25 PM
  4. Point Sprite Texture Problem
    By CGDeveloper in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 09-23-2004, 01:38 PM
  5. ARB Point Sprite Problem
    By Catharsis in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 06-12-2004, 11:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean