【发布时间】:2012-06-18 18:46:03
【问题描述】:
我正在尝试找出一种方法来剪切背景纹理的某个区域,以便某个自定义图案不会在该背景的屏幕上呈现。例如:
这个正方形可以是任何图案。 我正在使用 Frame Buffer Object 和 Stencil Buffer 来实现这种效果。代码如下:
fbo.begin();
//Disables ColorMask and DepthMask so that all the rendering is done on the Stencil Buffer
Gdx.gl20.glColorMask(false, false, false, false);
Gdx.gl20.glDepthMask(false);
Gdx.gl20.glEnable(GL20.GL_STENCIL_TEST);
Gdx.gl20.glStencilFunc(GL20.GL_ALWAYS, 1, 0xFFFFFFFF);
Gdx.gl20.glStencilOp(GL20.GL_REPLACE, GL20.GL_REPLACE, GL20.GL_REPLACE);
stage.getSpriteBatch().begin();
rHeart.draw(stage.getSpriteBatch(), 1); //Draws the required pattern on the stencil buffer
//Enables the ColorMask and DepthMask to resume normal rendering
Gdx.gl20.glColorMask(true, true, true, true);
Gdx.gl20.glDepthMask(true);
Gdx.gl20.glStencilFunc(GL20.GL_EQUAL, 1, 0xFFFFFFFF);
Gdx.gl20.glStencilOp(GL20.GL_KEEP, GL20.GL_KEEP, GL20.GL_KEEP);
background.draw(stage.getSpriteBatch(), 1); //Draws the background such that the background is not rendered on the required pattern, leaving that area black.
stage.getSpriteBatch().end();
Gdx.gl20.glDisable(GL20.GL_STENCIL_TEST);
fbo.end();
但是这根本不起作用。我应该如何使用 Stencil Buffers 来做到这一点?我在理解 glStencilFunc 和 glStencilOp 时也遇到了一些困难。如果有人能对这两个有所了解,那将非常有帮助。
更新:我也尝试过使用 glColorMask 制作类似的东西。代码如下:
Gdx.gl20.glClearColor(0, 0, 0, 0);
stage.draw();
FrameBuffer.clearAllFrameBuffers(Gdx.app);
fbo1.begin();
Gdx.gl20.glClearColor(0, 0, 0, 0);
batch.begin();
rubber.draw(batch, 1);
Gdx.gl20.glColorMask(false, false, false, true);
coverHeart.draw(batch, 1);
Gdx.gl20.glColorMask(true, true, true, false);
batch.end();
fbo1.end();
toDrawHeart = new Image(new TextureRegion(fbo1.getColorBufferTexture()));
batch.begin();
toDrawHeart.draw(batch, 1);
batch.end();
这段代码产生了这个: 而不是这样的:(忽略窗口大小和色调)
注意:我使用的是 libgdx 库。
【问题讨论】:
-
您能否详细说明在您的情况下“根本不工作”是什么意思?
-
@Tim 这意味着正在渲染的背景不包含任何已被模板打印出来的“图案”。这意味着我得到的是一个完全粉红色的屏幕,而不是一个带有黑框的粉红色(如上所示)。
-
另外请注意,我使用的是 libgdx 库。对于桌面,我使用 LwjglApplication,将 LwjglApplicationConfiguration 对象的模板值设置为 8。
-
您确定模板可以与
SpriteBatch一起使用吗?我想简单看看code,我不确定。 -
我不确定。那么作为替代方案,我应该尝试什么?为什么你会认为 SpriteBatch 可能不适用于 Stencil Buffers?
标签: opengl-es opengl-es-2.0 libgdx framebuffer stencil-buffer