我正在尝试找到一种方法来剪切背景纹理的某个区域,使得该背景不会在屏幕上渲染出某个自定义图案。例如:
这个正方形可以是任何图案。 我正在使用帧缓冲对象和模板缓冲来实现这种效果。以下是代码:
然而,这根本不起作用。我应该如何使用模板缓冲来实现这一点?我还遇到了一些困难,无法理解glStencilFunc和glStencilOp。如果有人能够阐明这两个问题,那将非常有帮助。
更新:我还尝试使用glColorMask生成类似的东西。以下是代码:
这段代码产生了这样的结果: 而不是像这样:(忽略窗口大小和颜色) 注意:我正在使用libgdx库。
这个正方形可以是任何图案。 我正在使用帧缓冲对象和模板缓冲来实现这种效果。以下是代码:
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();
然而,这根本不起作用。我应该如何使用模板缓冲来实现这一点?我还遇到了一些困难,无法理解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库。
rHeart.draw
调用是如何在模板缓冲区上绘制图案的? - rotoglup