5

I have been trying to do some stenciling in a Libgdx project. So far, I have only been able to stencil out polygons (Code after the illustration).

QUESTION: How am I supposed to stencil out non-polygonal shapes in OpenGL ES 2.0? (i.e. I don't want the transparent pixels to be drawn to the stencil buffer)

How am I supposed to achieve this without using GL_ALPHA_TEST (Because OpenGL ES 2.0 doesn't allow it)?

Any help or pointers are greatly appreciated and thanks in advance.

EDIT: It would be very helpful if you could provide me a little bit of code sample for understanding.

Please consider the illustration below:

enter image description here

This is the rendering code:

@Override
public void render(float delta) {
    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_STENCIL_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );


    stage.act();
    stage.draw();

    Gdx.gl20.glColorMask(false, false, false, true);
    Gdx.gl20.glDepthMask(false);

    Gdx.gl20.glClearStencil(0x0);

    Gdx.gl20.glEnable(GL20.GL_STENCIL_TEST);

    Gdx.gl20.glStencilFunc(GL20.GL_ALWAYS, 0x1, 0x1);
    Gdx.gl20.glStencilOp(GL20.GL_KEEP, GL20.GL_KEEP, GL20.GL_REPLACE);      

    batch.begin();
    batch.draw(heart, 0, i+50);
    batch.end();

    Gdx.gl20.glColorMask(true, true, true, true);
    Gdx.gl20.glDepthMask(true);

    Gdx.gl20.glStencilFunc(GL20.GL_NOTEQUAL, 0x1, 0x1);
    Gdx.gl20.glStencilOp(GL20.GL_KEEP, GL20.GL_KEEP, GL20.GL_KEEP);

    batch.begin();
    batch.draw(heart, 0, i);
    batch.end();

    Gdx.gl20.glDisable(GL20.GL_STENCIL_TEST);
}
Daahrien
  • 10,190
  • 6
  • 39
  • 71
Rafay
  • 6,108
  • 11
  • 51
  • 71

1 Answers1

0

I don't have experience with libgdx, but in common case you can emulate alpha test in fragment shader:

if (pixelColor.a <= ALPHA_THRESHOLD) // threshold should be 0 for good visual results
        discard;
brigadir
  • 6,874
  • 6
  • 46
  • 81