Transparent Color?

Posts: 608
Joined: 2002.04
Post: #1
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?
Post: #2
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.
Posts: 608
Joined: 2002.04
Post: #3
I generally use targa's so I could use the alpha channel. How would I read it?
Post: #4
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.

Posts: 608
Joined: 2002.04
Post: #5
OK, that seems simple enough. How do I use the included alpha mask?
Post: #6
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.

Posts: 608
Joined: 2002.04
Post: #7
Nice! Thanks a ton, swcrissman.
Thread Closed 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  ? Find color value of 'pixel' in color buffer? Elphaba 1 6,427 Jul 22, 2009 01:23 PM
Last Post: Bachus
  Transparent textures kordeul 2 4,574 Aug 27, 2007 01:38 AM
Last Post: kordeul
  OpenGL, SDL, and Transparent Bitmaps RyanA 4 6,855 Jun 26, 2006 04:26 PM
Last Post: RyanA
  About transparent colors un textures valle 2 4,260 Oct 26, 2004 09:52 AM
Last Post: valle
  "boingX" (transparent ogl) under carbon GioFX 2 4,866 Jun 29, 2004 09:08 AM
Last Post: GioFX