PNG Transparency

Started by
17 comments, last by Prune 13 years, 8 months ago
Ok! I've removed successfully the background! the problem was in the ILConvertImage that had IL_RGB instead of IL_RGBA.

But, The picture is not been drawn with its colors! the ball still blue! Thoughts?

[]'s, Ruy.
Advertisement
What parameters for glBlendFunc did you use to generate that picture?

Have you tried premultiplied alpha? (glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);)

As a last resort, you can increase the alpha-reference (glAlphaFunc(GL_GREATER,0.99);), but this will create a pixellated edge like you've got in that picture (but less black with higher numbers).

...

In your image, the black pixels are in a ring, which means that some of them are correctly being drawn transparently, right?

This "edge bleeding" is a common issue, and it's obvious if we look at two pixels along an edge:
|Orange RGB|Black RGB|
|100% Alpha| 0% Alpha|
When the texture is enlarged, those two pixels are going to be blended together by the bilinear texture filtering, which will produce dark orange with a greater-than-zero alpha value, giving your object a dark outline.

The easiest solution to this is to use premultiplied alpha blending.

[EDIT]
Quote:But, The picture is not been drawn with its colors! the ball still blue! Thoughts?
What blending function are you using?
Hi again! Thanks for your reply!
Yes, I used pre-multiplied alpha. See, as I said I was able to erase the background (it was just a mistype of mine...) but appeared a new problem: the figure its changing colors!

I deactivated the blending (using Gldisable, not in the conversion of the image) and the picture looked like this:

Click

As you can see, the colors are inverted!

I changed from RGBA to BGRA but I'm getting the same result!
ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE); to ilConvertImage(IL_BGRA, IL_UNSIGNED_BYTE);

Any ideas why?

Thanks a lot!
Notice how the black of the ball lines are white... Basically, the image is being rendered as its negative, it isn't a RGB <-> BGR issue. Again, what are the blending parameters you're passing? It looks like you have the opposite of what you should be using.

EDIT: ack, this is what I get for reading too quickly... again >.>' Maybe the image is being loaded with the components reversed? What are the values you get in the texture data itself from ilConvertImage? It doesn't seem like an OpenGL issue.
Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.
On a related note, can someone explain to me how to properly deal with PNG transparency in Photoshop? In all versions I've used (up to CS3), PS doesn't seem to preserve color information in transparent areas. In other formats such as TIFF, PS shows the transparency as an alpha channel--as it makes sense. But for whatever reason, it treats PNG transparency differently, and in my opinion, not a way that makes sense. I always end up saving as TIFF and using Imagemagick to convert to PNG, which just adds extra steps to the pipeline. Is there some procedure in PS to actually edit the alpha channel of a PNG without messing up the color information?
"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)
GLuint ContentLoader::loadTexture(char* TextureFile) {	SDL_Surface* Surface = IMG_Load((new string("Images\\"))-&gt;append(TextureFile).c_str());	SDL_SetAlpha(Surface,0,0);	int w = Surface-&gt;w;	int h = Surface-&gt;h;	SDL_PixelFormat *pixelFormat = Surface-&gt;format;	SDL_Surface *image = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, pixelFormat-&gt;Rmask, pixelFormat-&gt;Gmask, pixelFormat-&gt;Bmask, pixelFormat-&gt;Amask);	SDL_BlitSurface(Surface, NULL, image, NULL);	GLuint textureId;	glGenTextures(1, &textureId);	glBindTexture(GL_TEXTURE_2D, textureId);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image-&gt;w, image-&gt;h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image-&gt;pixels);	SDL_FreeSurface(image);	SDL_FreeSurface(Surface);	return textureId;}


What I use to load a png with transparancy as a opengl texture
If the blending function is working properly this will work.


[Edited by - Altourus on July 17, 2010 6:32:44 AM]
This seems inefficient--you're loading into an SDL surface only to blit onto an OpenGL one... why not load to OpenGL directly?
"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)
Quote:Original post by Prune
This seems inefficient--you're loading into an SDL surface only to blit onto an OpenGL one... why not load to OpenGL directly?


Only examples Ive ever seen use SDL_Image to load the image like that, I'd imagine because its quicker then writing your own png parser.
I never implied you should write your own parser. Surely everyone uses libpng.
"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)

This topic is closed to new replies.

Advertisement