Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libgdx Does SpriteBatch draws to StencilBuffer?

I am using libgdx with OpenGL ES 1.0 and I am wondering if the SpriteBatch can write/draw to the stencil buffer. I've been trying to write to it and I don't get any results at all, I don't have experience using the stencil buffer but I've been reading a lot so correct me if I am wrong in anything that follows. Basically what I want to do is to draw a texture with a SpriteBatch to the stencil buffer so when I draw something else (with stencil buffer disabled) It only get drawn on the regions where the stencil buffer is equal to 1.

This it my desired result: If I draw a texture with a star shape to the stencil buffer and then I draw a red texture to the color buffer I want the red texture to omit the pixels where the star is in the stencil buffer.

This is my code so far:

   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);
like image 855
Leonso Medina Lopez Avatar asked Oct 13 '12 04:10

Leonso Medina Lopez


1 Answers

yes spriteBatch does write to the stencil buffer the problem was that I had to configure the stencil buffer. The way it is done is by creating an application configuratioin object and passing it as a parameter when initializing the application like this:

For the android launcher you need do it like this:

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

For the desktop is like this

LwjglApplicationConfiguration Configuration = new  LwjglApplicationConfiguration();
Configuration.stencil = 8;
new LwjglApplication(new Game(), Configuration);
like image 196
Leonso Medina Lopez Avatar answered Oct 11 '22 17:10

Leonso Medina Lopez