iDevGames Forums
Transparent Color? - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Transparent Color? (/thread-7804.html)

Transparent Color? - Josh - Apr 20, 2002 01:47 PM

Is there a way to set black to be a transparent color so that I don't have to have masks for all the images?

Transparent Color? - Nimrod - Apr 21, 2002 05:50 AM

I assume you mean in OpenGL...

I'd imagine there's some blending mode that will acheive that, but I'm not familiar with blending, so I can't help you with what to call specifically.

However, you can also 'filter' out the black when you load your texture. Go through each pixel, and if it's black, give it an alpha value of 1, other wise 0. This will only work (look good) if your image has crisp edges. If your image fades out at the edges, then blending is probably better.

Or, use an image format that supports alpha channels, and you don't need a separate mask texture file. PNG is an example of that.

Transparent Color? - Josh - Apr 21, 2002 10:36 AM

I generally use targa's so I could use the alpha channel. How would I read it?

Transparent Color? - swcrissman - Apr 21, 2002 05:26 PM

If you are using targas with an alpha channel, you should just set the alpha for all the black parts of the image so that they reflect the amount of transparency you want them to have.

If you are using an image format which does not allow a transparency channel, then you can do what the previous poster suggests: read in the image without the alpha channel, and then before creating the texture, add the alpha information to the image information by running it through a function which checks to see whether a pixel is black, and is so setting its alpha appropriately.

Generally, it makes life simpler to simply add the alpha when the image is created/saved, because that is faster and easier than processing the info.


Transparent Color? - Josh - Apr 21, 2002 07:40 PM

OK, that seems simple enough. How do I use the included alpha mask?

Transparent Color? - swcrissman - Apr 21, 2002 08:59 PM

When you create the texture, at some point, you'll call glTexImage2D. One of the parameters specifies the format of the information you are passing in. You'll want to set this to be: GL_RGBA, rather than GL_RGB so as to let opengl know that the texture has an alpha component.

Then, when you draw the texture, you'll want to make sure that you've enabled blending and have set the blending function to what you want, with the appropriate calls. you'll end up doing something like this:

glEnable( GL_BLEND );

        glBindTexture( GL_TEXTURE_2D, /*your texture*/ );
        glBegin(GL_QUADS); // Start Drawing A Textured quad
            glTexCoord2f(0.0f, 1.0f); glVertex3f(     /* your location */);
            glTexCoord2f(1.0f, 1.0f); glVertex3f(     /* your location */);
            glTexCoord2f(1.0f, 0.0f); glVertex3f(     /* your location */);
            glTexCoord2f(0.0f, 0.0f); glVertex3f(     /* your location */);

glDisable( GL_BLEND );

You can get various effects by changing the blendfunc. The one i've given there will probably do what you're expecting it to.

I've never worked with targa images, so i dont have any code to load them, but i assume you are doing that already in some form, so hopefully what i've given will be enough. If you search the old idevgames forums, there are a couple threads that have cocoa code for use with PNG image files, which is what I personally use. There are also a few threads on transparency which may be helpful.


Transparent Color? - Josh - Apr 22, 2002 05:27 AM

Nice! Thanks a ton, swcrissman.