libgdx 贴花未绘制到模板缓冲区中
Posted
技术标签:
【中文标题】libgdx 贴花未绘制到模板缓冲区中【英文标题】:libgdx decal not drawing into stencil buffer 【发布时间】:2014-07-16 13:53:29 【问题描述】:我正在使用 libgdx 引擎开发 3d 应用程序。
我刚刚发现 decalBatch 没有绘制到模板缓冲区中。 我想为 3d 世界制作模板面具,但它根本不起作用。
这是适用于精灵批处理的代码,但不适用于贴花批处理。 请帮忙!
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();
编辑:
我发现应该清除深度缓冲区,启用和禁用 DepthMask,但我无法管理它。
【问题讨论】:
【参考方案1】:哦,我刚刚想通了。 必须启用深度缓冲区,因为贴花在 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);
【讨论】:
以上是关于libgdx 贴花未绘制到模板缓冲区中的主要内容,如果未能解决你的问题,请参考以下文章