Reputation:
I am trying to implement a post processing in opengl, my problem is there transparent images on non-transparent images. And while rendering the scene, It first renders non transparent image and then render transparent image on top of it. However libgdx somehow replace destination alpha on my frame buffer. So when I re-render it to back buffer, it remains transparent. Here is my code
@Override
public void draw(Batch batch, float alpha){
if(enabled){
batch.end();
batch.enableBlending();
batch.begin();
frameBuffer.begin();
Gdx.gl.glClear(Gdx.gl10.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(0, 0, 0, 0);
super.draw(batch, alpha);
frameBuffer.end();
batch.end();
frameBuffer.getColorBufferTexture().bind();
shader.begin();
setUniforms();
Gdx.gl.glEnable(Gdx.gl20.GL_BLEND);
Gdx.gl.glBlendFunc(Gdx.gl10.GL_SRC_ALPHA, Gdx.gl10.GL_ONE_MINUS_SRC_ALPHA);
mesh.render(shader, GL10.GL_TRIANGLES);
shader.end();
batch.begin();
}else{
super.draw(batch, alpha);
}
}
This is a custom Group. First I render children to frameBuffer then I bind it and render it to real back buffer but my result is incorrect.
And that is how it supposed to look
Green background image is rendered first to back buffer. Then my post process actor tries to render rest of scene to top of it.
Locked badges (premium league etc) has a semi-transparent image on them. With my shader, that transparent image replaces the result alpha and thus this problem occurs. (Backbuffer has no alpha channel so result alpha is always 1 but alpha blending is still on so it can blend images.)
What am I missing? I guess my blend function while rendering to frameBuffer should be like this.
result_color = src_color*src_alpha + dst_color*(1-src_alpha)
result_alpha = max(src_alpha, result_alpha)
Upvotes: 1
Views: 5694
Reputation:
I guess I found a solution, glBlendFuncSeperate does what I want. It can give different blend modes for different pictures. I am using this
Gdx.gl20.glBlendFuncSeparate(
Gdx.gl10.GL_SRC_ALPHA, Gdx.gl10.GL_ONE_MINUS_SRC_ALPHA,
Gdx.gl10.GL_ONE, Gdx.gl10.GL_ONE);
Upvotes: 2