libgdx, Android crash when using Stencil?

On desktop it works fine, but when in android: “Unfortunately gamename-android has stopped” as soon as the app launches.
I’ve done some line commenting, and reached the conclusion that these lines are making the game crash (create() method).

		Gdx.gl11.glEnable(Gdx.gl11.GL_STENCIL_TEST);
		Gdx.gl11.glClearStencil(0);

If I comment them, the rest of the code won’t work because it needs them.
I don’t think posting the rest of the code is relevant since these 2 lines are making the game crash as soon as they’re reached…

Does this mean most android devices have no support for this or something like that?
Why does libgdx’s ScissorStack works fine? Isn’t it using something like that?
I would use libgdx’s ScissorStack, but it works only with rectangles.

Any light is appreciated :slight_smile:

What does LogCat say?

There are two likely scenarios:

  1. The gl11 object is null and you are getting a null pointer exception. If useGL20 is enabled, then gl10 and gl11 will be null. So the proper code is to do this:
//enable stencil testing
Gdx.gl.glEnable(GL10.GL_STENCIL_TEST);

//set the stencil clear color -- it is zero by default
//so this line is just to be safe
Gdx.gl.glClearStencil(0);

//clear the stencil buffer
Gdx.gl.glClear(GL10.GL_STENCIL_BUFFER_BIT);

If you need something specific to GL20 or GL10 you should use [icode]Gdx.graphics.isGL20Available()[/icode]

  1. You didn’t enable the stencil buffer during your application initialization:

Android:


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

Desktop:

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

Regarding your problem. Scissor stack does not use the stencil buffer. What kind of masking do you need? Arbitrary shapes? You can always use images and mask via a shader; this allows for anti-aliasing (unlike stencil testing).

Here is an example of using a shader to mask arbitrary shapes, with a mask texture:

If you’re new to shaders, you should start from the beginning:

And here is an example of computing a mask in the shader, which means it is resolution-independent:
http://www.badlogicgames.com/forum/viewtopic.php?f=11&t=8540&p=38862&hilit=rotated+rectangle+mask#p38862

Thanks for your helpful answer, I changed from gl11 to gl and it worked like a charm, such a simple thing, I’d never guess it, thanks again.