textures with alpha are blended towards black

hello,
this is not a jogl problem, but rather related to the opengl generally and maybe java’s ImageIO.

i am using a lot of texture’s that contain transparent parts, so i save them as pngs. then i use this code to load the png:

TextureIO.newTexture(new TextureData(0, 0, true, ImageIO.read(new File(“pngfile.png”))));

now my blendfunc is this:
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

but the result is a darkening of the image, where the alpha decreases.

here is a screenshot:

http://www.embege.com/misc/dark-blending.jpg

now you can see a dark edge along the border of one texture, which i want to get rid of. if i am not mistaken, then this shouldn’t be there, so i was suspecting that the ImageIO functions create a buffered image with a standard-black-background.

if i change the blendfunc to:
gl.glBlendFunc(GL.GL_DST_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
then the blending is correct for this specific case. however when i want to fade in objects by setting the alpha using glColor then the blended result looks weird, cause the colors seem to be added somehow.

this is what i am expecting:

http://www.embege.com/misc/good-blending.jpg

could the problem be the ImageIO reading? or something else? thanks!

What happens if you draw the image without blending? I’ll bet that the transparent bits are actually coloured black, and so texture filtering is causing the black to bleed into the visible bits.

To remove the black you’ll have to figure out where it’s comming from. Some image programs don’t remember colour info with alpha=0, and ‘helpfully’ replace it with black, in which case you should either get a better image editor or fix the image up after loading in code - bleeding the non-transparent colours outwards works nicely.

For a quick fix you can always turn off texture filtering (set it to GL_NEAREST).

Free wisdom: Premultiplied alpha

thanks for your help!

Exactly!

[quote]To remove the black you’ll have to figure out where it’s comming from. Some image programs don’t remember colour info with alpha=0, and ‘helpfully’ replace it with black, in which case you should either get a better image editor or fix the image up after loading in code - bleeding the non-transparent colours outwards works nicely.
[/quote]
well i am using PhotoShop as the editor and the file format is png so i am afraid, that it is not possible to set a bleeding color.
if it is, how?

spasi, thanks for the link.

but is this flooding even possible with PNGs?

The point of premultiplied alpha is that you don’t need bleeding. Besides, bleeding doesn’t work correctly in all cases (anisotropic sampling, lower mipmaps).

ok, thanks.

do you have any pointers to an openGL implementation of premultiplied alpha. i dont understand directX at all.

thanks!

Simply use glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA). The difficult part is how you manage the premultiplied textures. Artists don’t like premultiplied images of course, so you’ll have to add support for doing the multiplication in your engine (as a preprocess or even when loading the texture at runtime). It can also be done dynamically using fragment shaders, register combiners, etc.

thanks.
yeah, i will probably do the premultiplication when i load the textures.

so i guess i will have to multiply the rgb channel with the alpha, right?

Exactly.