Jump to content
  • Advertisement
Sign in to follow this  
SvetskoANase

OpenGL Transparent protions of textures in OpenGL (simple)

This topic is 3799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've been searching for a few hours, without success, on how to easily implement textures (preferably bitmaps) that have transparent portions. I'm writing a simple 2d bomber-man clone and right now, because I have no way to eliminate the background portion of the bitmap textures I'm using, it looks very unclean. At the moment I have no image editing software on my PC so I'd have trouble preparing the images like i was used to doing for other simple games I've made. I was wondering if there was a OpenGL command similar to something like: glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_Greater, ...); ... that would allow me to "ignore" the completely black portions of my bitmaps. If this is not an option, can someone point me to, preferably, the simplest solution they know of? Thank you

Share this post


Link to post
Share on other sites
Advertisement
You can try masking which is demonstrated in this NeHe tutorial. It allows for completely transparent areas and completely opaque areas.

I prefer having an alpha channel as part of my images which allows for each pixel to have a different amount of transparency. You'd have to use an image that supports an alpha channel, like a 32-bit TGA or a PNG. When you draw it, set the blending mode to this:
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

EDIT: The GIMP is a free photoshop-like program (Google it). You will be able to add an alpha channel to your images with it.

Share this post


Link to post
Share on other sites
GL doesn't support "colour key" transparency (which is where you say "black == transparenct", etc...), so you've got to do this yourself by filling in the alpha channel.

How are you loading your bitmaps currently?

You need some image-processing code to convert your bitmaps from RGB to RGBA *before* you give the image data to OpenGL. In pseudo-code, it might look like:

RGB source[][]
load source from file
RGBA dest[][]
for each pixel(x,y) in source
dest[x][y].RGB = source[x][y].RGB
if( source[x][y].RGB == BLACK )
dest[x][y].A = 0;
else
dest[x][y].A = 255;
discard source
send dest to OpenGL

Share this post


Link to post
Share on other sites
You can also write a very simple GLSL shader:


//Fragment shader
uniform sampler2D tex0;

void main ()
{
vec4 frag = texture2D(tex0, gl_TexCoord[0].st);
gl_FragColor = frag;
if (frag.rgb == vec3(0.0)) discard; //Discard fragment if the color is black
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!