Source key in OpenGL ?

Started by
12 comments, last by Mercury_Linx 22 years, 11 months ago
Dear All, Thanks in advance I have a bitmap and happend to have a colour RGB(0, 0, 0) as the source key for this bitmap, and how do I set the source key value in OpenGL so that it would not blit this part, and I don''t want to have a saperate image as the mask ? Mercury_Linx
...A programmer, is the soul creator for computers.
Advertisement
Run through at the image''s load time, and test each pixel to see if it is that color or not. If it is, make that pixel have a transparent (0,0,0) alpha value, otherwise make it have a opaque alpha value (255,255,255). Store it as GL_RGBA (32bit) so that the alpha channel is still there.

Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/
Thanks for the tip.

But what I did in Dirext Draw is setting the source key onto the offscreen surface e.g: lpDDSurf->SetColorKey( ... , ... )

I was wonder if there is anything similar in OpenGL, which I don''t need to go thru pixel by pixel in the bitmap file myself ... I try to avoid that



A programmer, is the soul creator for computers.
...A programmer, is the soul creator for computers.
>>But what I did in Dirext Draw is setting the source key onto the offscreen surface e.g: lpDDSurf->SetColorKey( ... , ... )
I was wonder if there is anything similar in OpenGL, which I don''t need to go thru pixel by pixel in the bitmap file myself ... I try to avoid that<<

no, u need an alpha channel its not all bad news though with an alpha channel u can do nice blending effects

http://members.xoom.com/myBollux
Thanks guys ! I''m really appreciate it

Oh ... by the way, is there any significant or different between using a bit mask or inspect the colour bit one by one during image loading time ? I''m talking in concern of hits on execution time and memory management.



...
A programmer, is the soul creator for computers.
...A programmer, is the soul creator for computers.
as most texture loading is done during init time, having an alpha channel will be far quicker.

http://members.xoom.com/myBollux
Of course if you load a 32 bit (RGBA) image instead of a 24 bit image (RGB) the operation takes 25% more time.
But you load the texture one time at the beginning of your app or when you need it.
In hardware OpenGL implementation you get the same speed (32 bit parallelism).
In other words you pay for extra 25% memory use but not in speed.

One of the reasons for the fact that does not exist any operation like SetTransparentColor() is that in general the alpha byte is used for blend (8 bit transparency) so every texel needs its own alpha level. It''s not an OpenGL limit but another example of glSimplicity.

What you need is load your 24 bit bitmap into a 32 bit image

LPBYTE pImage = new BYTE[width*height*4];

When you load the bitmap pixel per pixel simply skip one byte (your file is a 24 bit image).

Then process your image with a loop like this

DWORD transparent_color = RGB(tred, tgreen, tblue);
// for example transparent_color = 0 (black)

for(long pix=0; pix< width*height; pix++)
{
BYTE r = pImage[(pix<<2) + 0];
BYTE g = pImage[(pix<<2) + 1];
BYTE b = pImage[(pix<<2) + 2];
BYTE a; // not still defined

DWORD rgb = RGB(r, g, b);

if(rgb == transparent_color)
a = 0;
else
a = 255;

pImage[(pix<<2) + 3] = a;
}

Remember to define glAlphaFunc(GL_GREATER, 0) and
glEnable(GL_ALPHA_TEST); Of course you need also to SetUp your image as GL_RGBA texture!

It''s not difficult!

IpSeDiXiT
Thanks alot for all the info

To Andrea:

Any reasons "pix << 2" ? Is it that you assume that r, g, b and a is all together 4 byte ? But why ? I'm a little puzzled here ...

By the way, do you have a complete sample which I can have a look and try out ?

Thanks again ...


...
A programmer, is the soul creator for computers.


Edited by - Mercury_Linx on May 2, 2001 8:28:06 PM
...A programmer, is the soul creator for computers.
Yes, I''ve assumed you are using 32 bit RGBA (it''s one of the most used format);

Every pixel is described by 4 components : red, green and blue (for 24 bit RGB) and an additional component (alpha for 32 bit RGBA). Every component is one byte lenght so you have 256 different levels for every component.

There are different method to store data images however the most typical is to store images by rows in RGBA order.
If you use 32 bit RGBA images you have 4 byte for every pixel
( infact in my example pix<<2 == pix*4 )

NOTE: Some video cards don''t support 32 bit texture however you can send a 32 bit texture to the card (OpenGL drivers should convert the image in the right format)(I use also a Vodoo3)

I''m sorry for the code but I''ve not a simple example to show you ( I''ve written my own cryptic C++ classes )...you can find some good example on NeHe site.

IpSeDiXiT

This topic is closed to new replies.

Advertisement