【问题标题】:Cut a translucent square in a texture在纹理中切割出一个半透明的正方形
【发布时间】:2013-02-27 16:56:43
【问题描述】:

如何移除(切掉)纹理中的透明矩形,使孔变为半透明。

在 Android 上,我会使用 Xfermodes 方法:

How to use masks in android

但在 libgdx 中我将不得不使用 opengl。到目前为止,我几乎通过使用 glBlendFunc 实现了我想要的东西 从这个很好而且很有帮助的page 我学到了

glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);

应该可以解决我的问题,但我试了一下,并没有达到预期的效果:

batch.end();
batch.begin();
//Draw the background
super.draw(batch, x, y, width, height);
batch.setBlendFunction(GL20.GL_ZERO,
        GL20.GL_ONE_MINUS_SRC_ALPHA);

//draw the mask
mask.draw(batch, x + innerButtonTable.getX(), y
        + innerButtonTable.getY(), innerButtonTable.getWidth(),
        innerButtonTable.getHeight());

batch.end();
batch.setBlendFunction(GL20.GL_SRC_ALPHA,
        GL20.GL_ONE_MINUS_SRC_ALPHA);
batch.begin();

它只是使蒙版区域纯黑色,而我期待透明,任何想法。

这是我得到的:

这是我所期望的:

【问题讨论】:

  • 你有一段代码给我们吗?会更好地理解您的问题。
  • 嗯嗯,我想了一些 sn-p,但到目前为止我还没有办法,因此我希望一个通用的问题更有用。但我会记录我到目前为止的负担。
  • 我从glblendfunc.php 站点获得了那些setBlendFunc 参数的纯黑色蒙版效果。你能附上你得到的或你想要的截图吗? (另外,请注意glblendfunc.php 网站上的“前景”与“背景”的大小相同,因此前景的全 alpha 应用于整个背景。)
  • 我也有这个效果,但我以为背景是黑色的。我添加了预期和当前状态。

标签: java android opengl-es libgdx


【解决方案1】:

我通过使用模板缓冲区解决了我的问题:

Gdx.gl.glClear(GL_STENCIL_BUFFER_BIT);
batch.end();
//disable color mask
Gdx.gl.glColorMask(false, false, false, false);
Gdx.gl.glDepthMask(false);
//enable the stencil
Gdx.gl.glEnable(GL20.GL_STENCIL_TEST);
Gdx.gl.glStencilFunc(GL20.GL_ALWAYS, 0x1, 0xffffffff);
Gdx.gl.glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);

batch.begin();
//draw the mask
mask.draw(batch, x + innerButtonTable.getX(), y
        + innerButtonTable.getY(), innerButtonTable.getWidth(),
        innerButtonTable.getHeight());

batch.end();
batch.begin();

//enable color mask 
Gdx.gl.glColorMask(true, true, true, true);
Gdx.gl.glDepthMask(true);
//just draw where outside of the mask
Gdx.gl.glStencilFunc(GL_NOTEQUAL, 0x1, 0xffffffff);
Gdx.gl.glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
//draw the destination texture
super.draw(batch, x, y, width, height);
batch.end();
//disable the stencil
Gdx.gl.glDisable(GL20.GL_STENCIL_TEST);

【讨论】:

    猜你喜欢
    • 2012-10-26
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    • 2020-07-05
    相关资源
    最近更新 更多