Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Masking image that contains alpha makes the inner mask black

Tags:

opengl

libgdx

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

like image 281
Rami Avatar asked Nov 10 '15 13:11

Rami


1 Answers

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());
like image 171
Rami Avatar answered Nov 15 '22 14:11

Rami