libgdx decal没有绘制到模板缓冲区

时间:2014-07-16 13:53:30

标签: libgdx masking stencil-buffer decal

我正在使用libgdx引擎中的3d应用程序。

我只是发现decalBatch并没有吸引到模板缓冲区。 我想为3D世界制作模板面具,它根本不起作用。

这是用于sprite批处理的代码,但是它不能用于decal批处理。 请帮助!

    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_STENCIL_BUFFER_BIT);
    // batch.setProjectionMatrix(camera.combined);

    // setup drawing to stencil buffer
    Gdx.gl20.glEnable(GL20.GL_STENCIL_TEST);
    Gdx.gl20.glStencilFunc(GL20.GL_ALWAYS, 0x1, 0xffffffff);
    Gdx.gl20.glStencilOp(GL20.GL_REPLACE, GL20.GL_REPLACE, GL20.GL_REPLACE);
    Gdx.gl20.glColorMask(false, false, false, false);

    // draw base pattern
    shapeRenderer.begin(ShapeType.Filled);
    shapeRenderer.identity();
    shapeRenderer.setColor(1f, 0f, 0f, 0.5f);

    shapeRenderer.rect(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), 100, 100);

    shapeRenderer.end();

    spriteBatch.begin();
    // fix stencil buffer, enable color buffer
    Gdx.gl20.glColorMask(true, true, true, true);
    Gdx.gl20.glStencilOp(GL20.GL_KEEP, GL20.GL_KEEP, GL20.GL_KEEP);

    // draw where pattern has NOT been drawn
    Gdx.gl20.glStencilFunc(GL20.GL_NOTEQUAL, 0x1, 0xff);

    // decalBatch.add(decal);
    // decalBatch.flush();

    spriteBatch.draw(Assets.instance.background, 0, 0, Gdx.graphics.getWidth(),         Gdx.graphics.getHeight());

    // draw where pattern HAS been drawn.
    // Gdx.gl20.glStencilFunc(GL20.GL_EQUAL, 0x1, 0xff);

    // spriteBatch.draw(Assets.instance.actor1, -Gdx.graphics.getWidth() /
    // 2, -Gdx.graphics.getHeight() / 2, Gdx.graphics.getWidth(),
    // Gdx.graphics.getHeight());

    spriteBatch.end();

编辑:

我发现应该清除Depth缓冲区,启用和禁用DepthMask,但我无法管理它。

1 个答案:

答案 0 :(得分:3)

哦,我只是想通了。 必须启用深度缓冲,否则贴花在3d世界中。

如果有人感兴趣,这是解决方案:

    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_STENCIL_BUFFER_BIT |     GL20.GL_DEPTH_BUFFER_BIT);

    // setup drawing to stencil buffer
    Gdx.gl20.glEnable(GL20.GL_STENCIL_TEST);
    Gdx.gl20.glStencilFunc(GL20.GL_ALWAYS, 0x1, 0xffffffff);
    Gdx.gl20.glStencilOp(GL20.GL_REPLACE, GL20.GL_REPLACE, GL20.GL_REPLACE);
    Gdx.gl20.glColorMask(false, false, false, false);
    Gdx.gl20.glDepthMask(false);

    // draw base pattern
    shapeRenderer.begin(ShapeType.Filled);
    shapeRenderer.identity();
    shapeRenderer.setColor(1f, 0f, 0f, 0.5f);

    shapeRenderer.rect(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, 100, 100);

    shapeRenderer.end();

    // fix stencil buffer, enable color buffer
    Gdx.gl20.glColorMask(true, true, true, true);
    Gdx.gl20.glDepthMask(true);
    Gdx.gl20.glStencilOp(GL20.GL_KEEP, GL20.GL_KEEP, GL20.GL_KEEP);

    // draw where pattern has NOT been drawn
    Gdx.gl20.glStencilFunc(GL20.GL_NOTEQUAL, 0x1, 0xff);

    decalBatch.add(decal);
    decalBatch.flush();

    Gdx.gl20.glDisable(GL20.GL_STENCIL_TEST);