LibGDX多边形面膜

时间:2017-01-07 01:10:44

标签: libgdx

LibGDX中有没有办法创建多边形蒙版?我知道如何使用正方形和圆形而不是多边形来制作蒙版。以下代码适用于使用矩形正确渲染蒙版。

        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        Gdx.gl.glClearDepthf(1f);
        Gdx.gl.glClear(GL20.GL_DEPTH_BUFFER_BIT);
        Gdx.gl.glDepthFunc(GL20.GL_LESS);
        Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
        Gdx.gl.glDepthMask(true);
        Gdx.gl.glColorMask(false, false, false, false);

        // Renders the rectangle
        shapes.begin(ShapeRenderer.ShapeType.Filled);
        shapes.setColor(1, 1, 1, 1);
        shapes.rect(Gdx.graphics.getWidth() / 2, gdx.graphics.getHeight() / 2, 200f, 200);
        shapes.end();

        batch.begin();
        Gdx.gl.glColorMask(true, true, true, true);
        Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
        Gdx.gl.glDepthFunc(GL20.GL_EQUAL);
        bg.draw(batch);
        batch.end();
        Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);

但下面的代码不起作用,只是呈现黑屏。

        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        Gdx.gl.glClearDepthf(1f);
        Gdx.gl.glClear(GL20.GL_DEPTH_BUFFER_BIT);
        Gdx.gl.glDepthFunc(GL20.GL_LESS);
        Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
        Gdx.gl.glDepthMask(true);
        Gdx.gl.glColorMask(false, false, false, false);

        // This should be the polygon being rendered as a mask here
        polyBatch.begin();
        polygonSprite.draw(polyBatch);
        polyBatch.end();

        batch.begin();
        Gdx.gl.glColorMask(true, true, true, true);
        Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
        Gdx.gl.glDepthFunc(GL20.GL_EQUAL);
        bg.draw(batch);
        batch.end();
        Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);

然而,下面的代码渲染多边形并使用与上一个示例相同的坐标。

        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // This polygon uses the same coordinates as the last example
        // however it now displays the polygon.
        polyBatch.begin();
        polygonSprite.draw(polyBatch);
        polyBatch.end();

1 个答案:

答案 0 :(得分:0)

尝试向下移动第三段中的batch.begin();,使其不包含Gdx.gl函数,如下所示:

    //batch.begin();
    //from here

    Gdx.gl.glColorMask(true, true, true, true);
    Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
    Gdx.gl.glDepthFunc(GL20.GL_EQUAL);

    //to here
    batch.begin();

    bg.draw(batch);
    batch.end();
    Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);