Libgdx 确实将 SpriteBatch 绘制到 StencilBuffer



我正在使用libgdx和OpenGL ES 1.0,我想知道SpriteBatch是否可以写入/绘制到模板缓冲区。我一直在尝试写入它,但我根本没有得到任何结果,我没有使用模板缓冲区的经验,但我一直在阅读很多,所以如果我在接下来的任何事情中错了,请纠正我。基本上我想做的是使用 SpriteBatch 将纹理绘制到模板缓冲区,因此当我绘制其他内容(禁用模板缓冲区)时,它只会在模板缓冲区等于 1 的区域上绘制。

这是我想要的结果:如果我将具有星形的纹理绘制到模具缓冲区,然后将红色纹理绘制到颜色缓冲区,我希望红色纹理省略星形在模具缓冲区中的像素。

这是我到目前为止的代码:

   batch.begin();
   Gdx.gl10.glEnable(GL10.GL_STENCIL_TEST);
   Gdx.gl10.glEnable(GL10.GL_ALPHA_TEST);
   Gdx.gl10.glStencilFunc(GL10.GL_ALWAYS, 0x1, 0xffffffff);
   Gdx.gl10.glStencilOp(GL10.GL_REPLACE, GL10.GL_REPLACE, GL10.GL_REPLACE);
   Gdx.gl10.glColorMask(false, false, false, false);
   batch.draw(myShape, 100, 100); //draw to the stencil buffer a shape (texture region)
   batch.end();
   batch.begin();
   Gdx.gl10.glColorMask(true, true, true, true);
   Gdx.gl10.glStencilOp(GL10.GL_KEEP, GL10.GL_KEEP, GL10.GL_KEEP);
   // draw where the shape has NOT been drawn
   Gdx.gl10.glStencilFunc(GL10.GL_NOTEQUAL, 0x1, 0xff);
   batch.draw(BackGroundLayer, 0, 0);// draw background
   Gdx.gl10.glDisable(GL10.GL_STENCIL_TEST);

是的,spriteBatch 确实写入了模板缓冲区,问题是我必须配置模板缓冲区。完成此操作的方法是创建一个应用程序配置对象,并在初始化应用程序时将其作为参数传递,如下所示:

对于安卓启动器,你需要这样做:

AndroidApplicationConfiguration Configuration = new  AndroidApplicationConfiguration();
Configuration.stencil = 8;  //stencil buffer size
initialize(new Game(), Configuration);   //pass it as parameter  

对于桌面是这样的

LwjglApplicationConfiguration Configuration = new  LwjglApplicationConfiguration();
Configuration.stencil = 8;
new LwjglApplication(new Game(), Configuration);

最新更新