libGDX Framebuffer Alpha Issues -
i'm having issues libgdx framebuffer , alpha. below 2 images of expected result , actual result. can please tell me doing wrong , how can correct it. here code:
framebuffer buffer; sprite sprite; spritebatch batch; texture texture1; texture texture2; texture texture3; sprite texture2sprite; @override public void create () { batch = new spritebatch(); texture1 = new texture("1.png"); texture2 = new texture("2.png"); texture3 = new texture("3.png"); texture2sprite = new sprite(texture2); texture2sprite.setalpha(0.5f); texture2sprite.setposition(100, 100); buffer = new framebuffer(pixmap.format.rgba8888, gdx.graphics.getwidth(), gdx.graphics.getheight(), false); sprite = new sprite(buffer.getcolorbuffertexture()); sprite.flip(false, true); } public void createfbo() { buffer.begin(); gdx.gl.glclearcolor(0f, 0f, 0f, 0f); gdx.gl.glclear(gl20.gl_color_buffer_bit); batch.begin(); batch.draw(texture1, 0f, 0f); texture2sprite.draw(batch); batch.end(); buffer.end(); } @override public void render () { createfbo(); gdx.gl.glclearcolor(0f, 0f, 1f, 1f); gdx.gl.glclear(gl20.gl_color_buffer_bit); batch.begin(); batch.enableblending(); batch.draw(texture3, 200, 200); sprite.draw(batch); batch.end(); }
i had pretty same issue semi-transparent colours in framebuffer
, problem , solution can find on badlogic forum topic here.
basically, need draw framebuffer
in pre-multiplied alpha state. creating custom fragment shader (my full code on topic). set blending mode match (gl_one, gl_one_minus_src_alpha).
then draw sprite using same blend function default shader. should end this;
public void createfbo(){ buffer.begin(); batch.begin(); gdx.gl.glclearcolor(0f, 0f, 0f, 0f); gdx.gl.glclear(gl20.gl_color_buffer_bit); batch.setshader(pmashaderprogram); //pre-multiplied alpha shaderprogram batch.setblendfunction(gl20.gl_one, gl20.gl_one_minus_src_alpha); batch.draw(texture1, 0f, 0f); texture2sprite.draw(batch); batch.setshader(null); //default shaderprogram batch.setblendfunction(gl20.gl_src_alpha, gl20.gl_one_minus_src_alpha); //default blend mode batch.end(); buffer.end(); } @override public void render () { createfbo(); batch.begin(); batch.enableblending(); gdx.gl.glclearcolor(0f, 0f, 1f, 1f); gdx.gl.glclear(gl20.gl_color_buffer_bit); batch.draw(texture3, 200, 200); batch.setblendfunction(gl20.gl_one, gl20.gl_one_minus_src_alpha); sprite.draw(batch); batch.setblendfunction(gl20.gl_src_alpha, gl20.gl_one_minus_src_alpha); batch.end(); }