color key transparency

hi, is there any way i could archieve color-key
alpha transparent blending - where some
specified color is 100% translucent when
drawing - like half-life’s blue color?

best regards.

Yes there is. When you load your image, go through each bit, and if the r,g,b values are the same as some color key your want to be transparent, let’s say, black, then if r=g=b=0, then you would set the alpha value to 0, otherwise set it to 1.

For instance, let’s say you’ve loaded the bits of the image into some variable

unsigned char data = new unsigned char[widthheight*3];

then you would setup a second variable

unsigned char data2 = new unsigned char[widthheight*4]

you need a 4 because you’re adding the alpha component.

Anyway, so you just loop through each bit in the data array copying over the values into data2 and if those values are the same as your alpha key then set the alpha value to 0.

int index2=0;
for(int index=0; index < widthheight3; index+=3)
{
data2[index2] = data[index];
data2[index2+1] = data[index+1];
data2[index2+2] = data[index+2];

if(data[index] == 0 && data[index+1] == 0 && data[index+2] == 0)
data2[index+3]=0;
else
data2[index+3]=1;

index2+=4;
}

Once you’ve done this, you just call the following opengl functions.

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

This tells OpenGL to only accept pixels with alpha values greater than 0, otherwise trash them. So all those pixels with alpha values we set to 0 in the bitmap will not be drawn to the screen. Hope this helps.