Masking in libGDX

Hey chaps,

I cant seam to find the equivalent of this(http://slick.javaunlimited.net/viewtopic.php?p=19140) in libGDX.

I have 3 images: stars, the a circle shape and some terrain.

I am trying to just display the terrain inside the circle so that the stars texture can be moved to give a fake pan/tilt effect.

I have been trying via the following but suspect it is the wrong technique:

	batch.begin();
	batch.draw(stars,0,0,WIDTH,HEIGHT);
	batch.draw(terrain,0,0,WIDTH,HEIGHT);
	Gdx.gl.glColorMask(false, false, true, true);
	batch.draw(mask,0,0);
	Gdx.gl.glColorMask(true, true, true, true);
	batch.end();

SpriteBatch defers all rendering until flush. Flush happen if texture is swapped, end is called or some batch mode is changed.

So call batch.end(); before resetting the color mask value.

I cannot seam to figure out any combination that works :frowning: Its not a good day.

Is there a better way to subtract one image from another in libGDX?

What do you mean by substract? crop/tiling?

I had to change my approach due to LD48 time constraints and my lack of knowlage on the subject.

But what I wanted was to have an image of some stars, ontop of that an image of terrain and ontop of that an circular image to crop the terrain to a circle and allow me to see the image of the stars underneath.

You can do what you asked by using stenciling. Then you would use glStencilFunc, not glColorMask. The passes would be:

  1. draw stars normally
  2. enable writing to stencil buffer, disable writing to color buffer
  3. draw mask
  4. enable stencil test, disable stencil write
  5. draw terrain

Then the terrain is drawn only where the mask was drawn.

Another solution if the terrain is on e big texture is to put the mask in the textures alpha channel.

Thanks tom, That worked exactly as I needed it to.