Archived

This topic is now archived and is closed to further replies.

Mr_Ridd

Alternative to masking

Recommended Posts

Howzit If I want to display an image but I don''t want to show the background I would draw a mask of the image and then the actual image. How can I do it so I can take a certain color away? I''m trying to get rid of he mask. I know I can also do something with an alpha mask but I don''t really know how to do that either. Perhaps some code examples? Shot

Share this post


Link to post
Share on other sites
You can do that by using the OpenGL alpha test.

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER,0);

You should render in RGBA mode (32 bpp), and use RGBA textures. The alpha field will be used to do the alpha test. In the example above, if the alpha value is greater than 0, the test will pass, otherwise the test will fail and the pixel won''t be drawn.

NOTE: To load images in RGBA, you may use TGA files or two BMP''s, one with the color data and the other with the mask (but you must compose one single image).

Share this post


Link to post
Share on other sites
How do I set the alpha on the image that I don''t want to be drawn to 0.

I tried to import a transperent TGA file and it bombed out and gave me Wrong TGA format. I would rather use TGA''s than BMP''s though.

Share this post


Link to post
Share on other sites
Let's assume you have this texture:

OOOOOO..
OO....OO..
OO....OO..
OO....OO..
OOOOOO..

where the "O" are the RGBA color (1,1,1,1) absolutely white with alpha 1. And the "." are the color (0,0,0,0) absolutely black with alpha 0. If you do this:

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER,0);

and then draw the image, the "." wont be drawn because won't pass the alpha test.

I think there's no glu function to handle TGA bitmaps, so you must load them by yourself (TGA's are very easy to load).

[edited by - dslprog on April 5, 2004 6:01:15 AM]

[edited by - dslprog on April 5, 2004 6:02:13 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by ghosted
I use a bitmap with the transparent sections coloured a nasty pinkish purple colour (255, 0, 255).

You can then load the bitmap into an RGBA texture and set all all the aforementioned pixels to transparent using the tutorial here...


http://www.codesampler.com/source/ogl_color_key.zip


This will give you pink edges due to sampling of the texture. Better create a good TGA right from the start. Here''s how:

In Paintshop Pro and Photoshop (and the gimp too I think) there are two kinds of transparency. The most used is regular transparency (when you are setting a transparent color or background). Don''t use that. What you need is an alpha-mask. Draw your picture, then select the stuff that you want to draw in openGL. Click tools->save to alpha channel. then export the image as a 32 bit TGA.

Also, most paint programs let your image have multiple alpha channels. Make sure your image has only one before you export it.




Lone Wolves Game Development
Sander Maréchal
[Lone Wolves][Game Developers Emporium][E-mail]
[Hosting $7,95/mo][Forum FAQ][Google]

Share this post


Link to post
Share on other sites
On the contrary, that technique will give a crisp clean edge although you will be limited to only one colour transparency as a opposed to a gradient. I was under the impression that was all the OP wanted.

Share this post


Link to post
Share on other sites
quote:
Original post by ghosted
On the contrary, that technique will give a crisp clean edge although you will be limited to only one colour transparency as a opposed to a gradient. I was under the impression that was all the OP wanted.

Only if you render it with an alphatest of (GL_EQUAL, 1.0f) which can make your sprites look a little ''thin'' as an edge around it will be dropped due to the alpha test. (ofcourse you can correct this by rendering from the center of a texel instead of the edge, as is the default behaviour of OpenGL).




Lone Wolves Game Development
Sander Maréchal
[Lone Wolves][Game Developers Emporium][E-mail]
[Hosting $7,95/mo][Forum FAQ][Google]

Share this post


Link to post
Share on other sites