PDA

View Full Version : glBlendFunc() for transparent background



surfdabbler
08-01-2010, 11:28 PM
I am rendering anti-aliased lines onto a transparent background - the idea being to save as a PNG with alpha channel, and the overlay against different backgrounds.

I'm having problems getting the blending to work well. I'm using

glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);

This works well, so long as the lines are the same colour as the pre-cleared background. For example, I use

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

This gives a nice base for drawing black lines. But, if I draw white lines, the white doesn't come through. Ideally, the first time a pixel is drawn, it should completely overwrite the colour planes, and add it's alpha into the alpha channel. Then subequent writes should do the blending. But, there doesn't seem to be a way to do this.

I'm using depth buffering with non-sorted lines. There are also some polygons in my render, and drawing transparent polyons also has the same issue.

Any ideas? It seems a simple test to check the dest alpha, but OpenGL doesn't seem to have the necessary options to work correctly with dest alpha!

I'd love a solution for this. :)

ZbuffeR
08-02-2010, 03:03 AM
This should work :

A black+transparent background with glClearColor(0.0f, 0.0f, 0.0f, 0.0f); and a simple glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Then use this as premultiplied alpha when overlaying, with glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

A very good explanation about premultiplied alpha here :
http://home.comcast.net/~tom_forsyth/blog.wiki.html#[[Premultiplied%20alpha]]

(beware, this forum mangle links having square brackets)