Transparent Color?

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

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

Code:
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

glEnable(GL_TEXTURE_2D);
        
        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 */);
        glEnd();
        
glDisable(GL_TEXTURE_2D);

glDisable(GL_TEXTURE_2D);
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.

Spencer
Moderator
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 4,440 Jul 22, 2009 01:23 PM
Last Post: Bachus
  Transparent textures kordeul 2 2,603 Aug 27, 2007 01:38 AM
Last Post: kordeul
  Transparent texture color LongJumper 9 4,248 Oct 20, 2003 10:04 PM
Last Post: LongJumper