8

I'm trying to mask a background image that is smaller than the mask. and the space between the background and mask appears black.

mask and background

enter image description here

This is the code I'm using:

     batch.end();
     batch.begin();     
     Gdx.gl20.glColorMask(false, false, false, true);
     batch.setBlendFunction(GL20.GL_ONE, GL20.GL_ZERO);
     batch.draw(mask, getX(), getY());
     batch.flush();
     Gdx.gl20.glColorMask(true, true, true, true);      
     batch.setBlendFunction(GL20.GL_DST_ALPHA, GL20.GL_ONE_MINUS_DST_ALPHA);        
     batch.draw(texture, getX(), getY());       
     batch.flush();
     batch.setBlendFunction(GL20.GL_SRC_ALPHA,GL20.GL_ONE_MINUS_SRC_ALPHA);
     batch.end();
     batch.begin();

I tried all kind function combinations without any success. probably i'm missing something.

Update

Attaching chart that I build of all possible (relevant) results of src and dst blending function. Fortunately none of the below is working, and as I guessed there is something more need to be done in order to achieve the result.

     Gdx.gl20.glColorMask(true, true, true, true);      
     batch.setBlendFunction(src_func, dst_func);        
     batch.draw(texture, getX(), getY());       

enter image description here

Rami
  • 2,098
  • 5
  • 25
  • 37

2 Answers2

3

Solved it using FrameBuffer.

    batch.end();
    spriteBatch.begin();
        buffer.begin();
            Gdx.gl20.glColorMask(false, false, false, true);
            spriteBatch.setBlendFunction(GL20.GL_ONE, GL20.GL_ZERO);
            spriteBatch.draw(mask, 0,0);
            Gdx.gl20.glColorMask(true, true, true, true);
            spriteBatch.setBlendFunction(GL20.GL_DST_ALPHA, GL20.GL_ZERO);
            spriteBatch.draw(texture, 0,0);
            spriteBatch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        buffer.end();
    spriteBatch.end();      
    batch.begin();
    batch.draw(buffer.getColorBufferTexture(), getX(), getY());
Rami
  • 2,098
  • 5
  • 25
  • 37
0

Just taking a look at your images, it looks like:

  • The alpha of the mask is in complete control of the output alpha
  • You want the alpha of the background image to also be taken into account

In other words, you only want alpha where both images have alpha.


I haven't yet been able to look into more of the blending functions and how they work, and may update with further details on how to achieve your goals as I figure it out, but maybe knowing what you are trying to accomplish will help you figure it out before I get to it.

How to do blending in LibGDX - this question seems interesting, combining the source and destination images in a grid to show the effects.

Community
  • 1
  • 1
DoubleDouble
  • 1,493
  • 10
  • 25
  • Hi, Thank you for you answer. I know what i'm trying to accomplish i don't know how to do it. i think i tried all possible blending functions. so i guess there is something more than that need to be done. in the link you posted even it was answered it is a different scenario. as you specified. – Rami Nov 11 '15 at 06:48