Transparency in Texture without using Alpha

Started by
9 comments, last by FlyingSolo 14 years, 5 months ago
Hi, this is my first posting on this forum so please bear with me! With 20 years of bitmap-based graphics programming behind me, the move to OpenGL whilst long overdue is proving to have quite a steep learning curve! I'm sure this topic must have come up before but I've found so many articles via Google that contradict each other that after three days - I'm asking for some help. My first OGL app is purely 2D - let's keep it simple. I am happily drawing Quads and textures with translation/rotation etc and it all looks pretty good. The problem comes when trying to import some of the BMP's from a previous incarnation of this program that I need to use. These bmp's use 'magic pink' (255,0,255) to denote transparent areas. What is the accepted way to use these in OGL? I really don't want to shift to png or tga right now - I'd like to find a solution to the bmp problem. I'm sure I recall seeing a method using glBlend that would simply discard source pixels based on their colour but of course, I can't find that now :-/ Any pointers (sic) appreciated and my apologies to the more experienced GL'ers here who are probably sighing and muttering "not another bl**dy newbie...". If it matters, I'm using visual C++ Express 2008 and doing my best to avoid the use of ++. Many thanks.
Advertisement
There are essentially 3 ways you can do it in OpenGL
1) Alpha channel in picture (which you don't want, according to your post). Simple, allows partial transpareny with GL_BLEND enabled

2) Shader which intercepts pixels which match the color key and set the alpha to transparent (I'd suggest to not bother with it as you want to keep it simple and I find it to be overkill

3) Load the image into memory, iterate over the pixels. If the pixel matches your color key, set the alpha channel to transparent. Upload to texture via OpenGL. This is fairly simple, and I'd suggest doing that.

You can also use some image library like Devil and Soil (with weird uppercase in the names) and not bother implementing the loading routines yourself. You'll end up with smaller images then, too, because they'll be compressed. BMP is a pretty big format, even though it has no alpha.
Many thanks for the quick and comprehensive reply - appreciate that.

You've neatly confirmed what all my googling found. I must have seen a reference to texture filters but as you rightly point out, it's a bit of overkill.

I think it's probably best while I'm still in the early stages to adopt something like libpng so that the problem goes away in a sensible fashion.

Using OpenGL for a 2D app has it's own set of painful hurdles to overcome but I'm enjoying every minute of it so far, and after 30 years of programming - it takes quite a bit to get me excited!

Again, my very sincerest thanks :)
Quote:Original post by FlyingSolo
I think it's probably best while I'm still in the early stages to adopt something like libpng so that the problem goes away in a sensible fashion.


You definitely want some higher-level library like DevIL or SDL_image or SOIL, all of which abstracts loading image files to one function call. Libpng is fairly low-level and unless you want to ponder into the image format internals it won't provide any advantage.
libpng is not that difficult to learn. You can write complete PNG loader using just a dozen libpng function calls. Granted, there is a bit of material to read, but I think it's worth it. Write it once, use it a hundred times. I'm willing to give you a hand if you are seriously considering giving it a crack. :)
Latest project: Sideways Racing on the iPad
>>These bmp's use 'magic pink' (255,0,255)

aint that more purple? :)

anyways png,bmp,tga etc arent important as opengl ignores them + treats them all the same, theyre just a method of delivering the pixel values to opengl.
Personally Ild choose dds
http://en.wikipedia.org/wiki/DirectDraw_Surface
as youll most likely want to use compressed textures at one stage
Quote:Original post by zedz
>>These bmp's use 'magic pink' (255,0,255)

aint that more purple? :)


Ah, there's always one isn't there...

Windoze calls it "Fuschia" - guess they couldn't decide either!
youre right I just had a look

based on common definition

purple = 7F007F
pink = FFC0CB
magenta = FF00FF

http://en.wikipedia.org/wiki/List_of_colors
Heh !! I wish I had as much free time on my hands as you seem to do !!

I generally define colours as either "cool" or "icky". It's a simple system that requires little storage space (0 or 1) and is compatible with my very small brain.

Looking at the pre-defined colours in Borland Delphi (my usual weapon of choice) there are some strange colour names in there.
It's worth noting that if you're going to use hot pink then even though you set the texel to transparent, if you have texture filtering/smoothing on then the pink will leak into the edge of your sprite.

To fix this you can either turn off filtering (use GL_NEAREST for magnification filter) or while you're loading and turning pink pixels transparent you can find the nearest non-pink pixel and replace the pink with that (effecitvely smearing the colours around the edge of your sprite outwards).

This topic is closed to new replies.

Advertisement