View Full Version : (Open GL, blit) Making a bitmap colour transparent

02-19-2011, 09:40 AM
I'm creating a side-scroller and I need make a colour transparent within one of my layers. I'm guessing there should be function for this since it must be common.

what are the Bit blit functions?

Alfonse Reinheart
02-19-2011, 10:47 AM
There aren't any "bit blit functions" (at least, not in the way you mean). That's not how transparency works in OpenGL, and that's not how you should use OpenGL to render 2D graphics.

To use OpenGL to render 2D graphics, what you want to do is play to OpenGL's strengths. Namely, rendering textured polygons. A 2D image is just a texture. And if you position a quad (polygon with 4 vertices) correctly on the screen, with a proper texture, then you are effectively drawing that texture to the screen.

So you need to upload all of your images to OpenGL as textures. Now, you should try to group images that get used together into the same texture. For example, if you have a sprite that has multiple animation frames, then they should all go in the same texture. You select which image to use based on your texture coordinates.

Then you just draw quads to the proper location in order to make everything work.

As for transparency, OpenGL doesn't really have a way to do precisely what you're talking about. You're talking about "color keying": having a specific color be considered transparent. OpenGL uses a much more flexible blending mechanism. Instead of color keying, colors are 4-dimensional components. They have red, green, blue, and alpha, which can be used to denote transparency. An alpha of 255 can be considered opaque, and an alpha of 0 can be considered transparent.

If you want color keying, then you need to do it manually. When you load your image from wherever you get it, you need to convert it from an RGB image to an RGBA image. Every RGB value that doesn't match the color key gets an alpha of 255 (opaque) and every value that does gets an alpha of 0 (transparent).

Then you just need to set up the OpenGL blend modes correctly, and everything should work.

02-19-2011, 10:49 AM
In fact no, there is no function for this.
Bit blit and transparent color index do not exist in OpenGL.

These techniques are maybe 30 years old, and as explained in the introduction of the wikipedia page, _as been replaced by alpha compositing_ :
Search about blending, alpha channel, etc.

02-24-2011, 02:48 PM

So I will have to manually read each pixel and change its bit value. However, I did some research by talking to people and have been told that "png" are the best file format to use since they support RGBA unlike bitmaps.

Do any of you know a function I can use to load "png" files? Or loading in any image file format that supports rgba?

02-24-2011, 06:00 PM
SDL_image can do it very easily: