Transparent color

49921baa7439fb1e91e02caf0a925cfe
0
DarkLight 101 Mar 25, 2003 at 15:33

Dear all,
I’m writting a computer game using Delphi/OpenGL. Almost all is done, now I should make more levels and fix some bugs. And here comes the question.
The stage is viewed from above and I have one object like a three. It uses texture with some green pixels and all other pixels should be transparent. This mean that if there is an object under the “three”, all pixels that aren’t green should be in the color of coresponding object pixel, otherwise thay should be in the background color. I was wondering if it is possible to write this type of blending - it will be quite usefull if in OpenGL is possible to define transparent color. Or maybe I should use glBitmap() to draw the green pixels after all another objects are drawn?

13 Replies

Please log in or register to post a reply.

203e4e77a4880ecbd91054865fc5d0b7
0
Phaetos 101 Mar 25, 2003 at 15:48

Hi!

You can create a tree, where each branch is a triangle or a quad plane and the
texture is of the type GL_RGBA.
Then, if you turn on GL_BLEND and provide the proper blending function
at glGlendFunc() the pixels with alpha > 0 will be more or less
transparent.
But this means, you have to provide an alpha channel in your texture image.
Most image manipulation programs can handle transparency. I suggest to
use The Gimp, its free and very powerful.

I hope this helps a bit

Greetings
Stefan

49921baa7439fb1e91e02caf0a925cfe
0
DarkLight 101 Mar 25, 2003 at 16:07

This is not exactly three, it is object which should particulary hide another objects. It is small and textures are created via RGB arrays. It is possible to use RGBA array and A-value 1 for all pixels that should be transparent? I think this should work (you say texture should be of the type GL_RGBA).
Thank you for the help

590e8bdac8129bd87b188df15e62d0e5
0
CyraX 101 Mar 25, 2003 at 16:22

Ever tried TGA??
They will have all that stuff embedded directly into the image.
It will greately ease your development time.

49921baa7439fb1e91e02caf0a925cfe
0
DarkLight 101 Mar 25, 2003 at 16:41

@CyraX

Ever tried TGA??
They will have all that stuff embedded directly into the image.
It will greately ease your development time.

Can you explain? As I know OpenGL doesn’t support image formats
Can you tell me how to use TGA in OpenGL and what exactly it will give me?

203e4e77a4880ecbd91054865fc5d0b7
0
Phaetos 101 Mar 25, 2003 at 16:44

Have a look at this page: http://nehe.gamedev.net/data/lessons/lesso…n.asp?lesson=08

It adresses directly the transparency and blending concept, and as I just read, 1 means opaque and 0 means transparent, not vice versa as I said earlier…

Cyrax: TGA is cool, yes. I use it quite often, too. Just for Info: Nearly any file format is described here: http://www.wotsit.org/

Greetings
Stefan

49921baa7439fb1e91e02caf0a925cfe
0
DarkLight 101 Mar 25, 2003 at 17:07

Problem is that I don’t want to make texture transparent, only one color from it. I know the basic of alpha blending. Of cource, 0 for transparent and 1 for solid -exuse me. But this have no matter - I’m not seeking for exact code, only for solution.

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Mar 25, 2003 at 18:06

well, the trick is.. you actually need an alpha value to determine transparency (and 1 or 0 == transparent is up to your set up of the blending equation..)

you just want one color, so what you need to do is to set there alpha to transparent..

for every pixel
 if pixel.rgb == colorkey.rgb
 pixel.alpha = transparent
 else
 pixel.alpha = opaque

you can do that eighter before uploading the texture (and upload as GL_RGBA!! :D)

or, depending on hw, you can do it in the pixelshaders..

base idea:

distance = fragment.rgb - colorkey.rgb
out.alpha = dot_product(distance,distance)

and then using for example alpha test to check if alpha is greater than some threeshold, say 0.01.. that means, distance to colorkey is far enough => color != colorkey => passes the alpha test

those are the two ways possible..

0684f9d33f52fa189aad7ac9e8c87510
0
baldurk 101 Mar 25, 2003 at 18:13

what you can do with the TGA is simple. The TGA format allows for 4 bytes of data per pixel. RGBA, red, green, blue and alpha. The alpha ‘channel’ can be different for each pixel. All you do is set it to 0 for all the pixels you want to be transparent, and 1 for all the ones you want to be opaque. You can also have values in between, but that produces semi-transparent pixels. You might want to look at the TGA format, as it actually stores the pixels in BGRA format. You need to switch the R and B bytes.

49921baa7439fb1e91e02caf0a925cfe
0
DarkLight 101 Mar 26, 2003 at 11:17

Thank to all of you for the replyis. This is exactly what I’ve searched for. As I said I’m using RGB arrays for textures and there is no matter if I’ll use RGBA or I’ll convert TGA - this is the same. I’m just didn’t know that I can specify Alpha for each pixel in texture and then blend only transparent pixels :)
Again, thank you

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Mar 26, 2003 at 11:25

good luck. happy to helped out

btw, this is what is done in the bumpmapping demo from nehe, to show the logos. he adds the alpha values manually in code to make them transparent..

took me ages those days to find that out, hehe…:D but its so simple:D

anyways, good luck

0684f9d33f52fa189aad7ac9e8c87510
0
baldurk 101 Mar 26, 2003 at 18:22

although it’s possible, surely pixel shaders would be a VERY stupid way to do it. It won’t provide much if any speed increase and it reduces your target audience wildly, because only top end Radeons support pixel shaders in hw.

not a flame, just an opinion :)

590e8bdac8129bd87b188df15e62d0e5
0
CyraX 101 Mar 27, 2003 at 08:46

Advantage with TGA-
Setting an ALPHa value in an editor (I suggest GimP) like Photoshop would be far more easier and would also be less headache.Then you also have the runtime Alpha value. If today you think htat you are on Earth and Everything that is green is transparent in the world, suppose you get a state where you have to render jungle - lo behold you are in deep trouble - you would only be showing off Space or Desert. Not exactly the kinda things you expect.
However TGA textures could be nasty big.

203e4e77a4880ecbd91054865fc5d0b7
0
Phaetos 101 Mar 27, 2003 at 08:56

But the compression algorithm for TGAs (RLE encoded) is quite simple to implement, too. So you
can reduce the size on Harddrive a bit.