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. :confused:

Apparently yes.

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 [var]discard[/var] statement in the fragment shader. But note that this disables the early fragment test optimisation, so it has a performance impact.

Apparently the value of the stencil buffer is not incremented if alpha = 0. (Event if the alpha test is disabled)

                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)

"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 :

"} 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

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.

Ok I used another algorithm it’s solved the problem…