简体   繁体   中英

Stenciling textures in OpenGL ES 2.0

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? (ie 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:

在此输入图像描述

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);
}

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM