Libgdx - Texutre masking

Hello.

I have 2 images.

The first is my base image. It will be loaded to TextureRegion. So, lets make assumption i have it loaded for variable;


TextureReguion koala = ...;

I don’t want draw entire image, but only pixel who cover black pixels in my shape.png image. But this may not be persist change. In future i want just take off the mask and draw entire picture. Format of mask is flexible. This is only example. I can use anyone.
Can You write few line of code which can do this and draw masked texture on SpriteBatch?

not sure, do ask in the libgdx forum !

http://www.badlogicgames.com/forum/

Use a shader. Sample the first texture, sample the second texture, multiply them together. Well, since your mask texture is black where you want the other texture to be seen, I suppose you’d have to invert the value of the mask texture before doing the multiply though.

Check out my tutorial series. One of the lessons includes a very similar effect.

[icode]config.useGL20 = true;[/icode] must be set when creating the activity
[icode]isGL20Available()[/icode] must return true

If you want the masking to work on GLES10, you’ll need to tinker with texture combiners and blend modes.

Uhm, this seems like a thing to do with blending, for example (find the suitable)


Unless there is some more to it.

davedes’ shader tutorial is very well written though. Should read that too

Shaders is pretty hard to newbe i think. But i found information this is dooable with stencil test. Unluckly any of my try to do this faild. Can you write simple example code using this method?

To use the stencil test for this effect (without shaders), you’d have to render the mask texture as geometry (= a 5 vertex polygon creating a pentagon), not a rectangular textured quad with a texture of a pentagon. However, if you do that there’s nothing stopping you from completely getting rid of the extra pass, since you can just directly put the koala texture on your 5 vertex polygon.

See http://nehe.gamedev.net/tutorial/masking/15006/ for a non-shader implementation.

Everything in OpenGL is hard for a newby, but learning how to use shaders in the first place saves you a ton of other hard stuff you won’t need anyway as soon as you are capable of using shaders…

Take your time and read http://wiki.labomedia.org/images/1/10/Orange_Book_-_OpenGL_Shading_Language_2nd_Edition.pdf, at least Chapters 4,5,9 and 10, but best the whole book.

Don’t whine about it being 600+ pages - the book will save you much more time than you will spend reading it. :stuck_out_tongue: