Transparency help [Solved, I've been stupid]

Started by
27 comments, last by horizon981 17 years, 9 months ago
I am making a 2D sprite based game, but the sprites already have a background. Like this: ++++++++++++++++++ ++++++xxxxxx++++++ +++++xxxxxxxx+++++ +++++++xxxx+++++++ ++++++++xx++++++++ ++++++++++++++++++ I hope you get the point(my ASCII art sucks!) In the diagram(?) above, '+' represents brown color and 'x' is my character. Naturally, with this, im bound to a brown blackground. Is there a way to make this brown backroung transparent so that im left only with the character and can see anything this image is placed onto.(ummmm.... like the character walking on water, flying through clouds...). Pleaseeeee help me out. Please. [Edited by - horizon981 on July 4, 2006 10:56:08 AM]
Advertisement
If you're using OpenGL then it could turn out to be quite easy depending on how you load your sprites. All you have to do is convert your images to RGBA (if they aren't already in this format), switch from brown to transparent (=> alpha A = 0 whereas in other cases A = MAX) and enable alpha testing. Alpha testing is in a way color keying (which is what you want), where texels that "fail the alpha test" are rejected and not drawn on the framebuffer. Look for glEnable( GL_ALPHA_TEST ) and glAlphaFunc().

On the other hand, there is another way which is possible in OpenGL (but not in DirectX). In the old days, transparency could be achieved by using a mask and a combination of bit operations. But you'd have to use black instead of brown though. Plus, alpha testing and blending are the way to go. Masking is more or less depreciated (if you forget about stencil masking for a second).

Quote:Original post by horizon981
I am making a 2D sprite based game, but the sprites already have a background. Like this:

++++++++++++++++++
++++++xxxxxx++++++
+++++xxxxxxxx+++++
+++++++xxxx+++++++
++++++++xx++++++++
++++++++++++++++++

I hope you get the point(my ASCII art sucks!)
In the diagram(?) above, '+' represents brown color and 'x' is my character.
Naturally, with this, im bound to a brown blackground. Is there a way to make this
brown backroung transparent so that im left only with the character and can see anything this image is placed onto.(ummmm.... like the character walking on water, flying through clouds...).

Pleaseeeee help me out. Please.

What is requried is the use of the alpha channel to "mask" the background parts of the image. You have 2 options:

a) Load the image into your art program, add an alpha channel and convert the brown pixels to alpha. (You need an image format that supports alpha, eg tga).

b) If the image loader is your own, after you are done reading pixel data, copy it to a RGBA / BGRA array, and set
alpha = (r == brown_color.r && g == brown_color.g && b == brown_color.b) ? 0 : 255;
where alpha is an unsigned byte.

Before rendering the sprite:
glAlphaFunc(GL_GREATER, 0.5);glEnable(GL_ALPHA_TEST);draw_sprite();


/edit: Believe me, I opened this page and was looking at other things, I'm not that slow! [lol]
Will it work with bmp files?
Quote:Original post by horizon981
Will it work with bmp files?


As long as they are 32bit and not 24bit.
Problem No. 2: The "paint" program in windows allows only 24 bit bitmaps. Is there another way to get 32 bit bitmaps?
How about RAW files?
Quote:Original post by horizon981
Problem No. 2: The "paint" program in windows allows only 24 bit bitmaps. Is there another way to get 32 bit bitmaps?
How about RAW files?


RAW files are just another file format. You should use .tga or .dds files. Both support 32bit. I am assuming you are using your own code to load the textures? If not you should look into a texture loading library or look into Phantoms texture loader class he is working on... nehe has a tutorial on .tga loading. HTH
I remember having read there is a way to do that with blending and glTexEnv. If I am correct, this way it doesn't matter how the image is being loaded. Do you know how to do that?
not to go off topic, too much, but if you are making a 2d sprite based game, you could use allegro and then manually set the mask color to whatever you want. allegro assumes it is pink automatically, but you can change that.
Quote:Original post by horizon981
I remember having read there is a way to do that with blending and glTexEnv. If I am correct, this way it doesn't matter how the image is being loaded. Do you know how to do that?


Your textures need an alpha channel. There are a few image loader libraries out there. I use SDL+SDL_image for OGL output and texture loading.
Then DOWNLOAD GIMP and draw your sprites. When you've done this, select the parts of your image you want to be trasparent, and delete them. Then save the image as TGA (that can keep an alpha channel).
That is how I suggest you to do.

This topic is closed to new replies.

Advertisement