Making black background transparent in RGB texture

Hi,
This may be a very old question, but i haven’t found a solution to it yet.
I have many RGB textures with black backgrounds, loaded from files, that are updated rapidly. I need to make the black backgrounds transparent as the image portions in each file align neatly with one another. I cannot add an alpha component to each texture because of resource limitations.

Is there a way to make the black bacground transparent without Alpha? I can’t use stenciling as there’s no fixed mask.

I just need to have the black portions transparent. Any way/workaround?

Any help would be much appreciated.

What GraphicCard have you got ?

the glTexImage2D function accepts many types of texture.

There is GL_COLOR_INDEX for the 256 color mode.
It’s very old !!! it’s like the GIF format.

Otherwise there are GL_RED, GL_GREEN, … ,GL_RGBA.

I believe OpenGL converts them into GL_RGBA.

Are you sure don’nt have ALPHA ? Because the transparence MUST be in ALPHA bits. May be glBlendFunc is not correct ? Have you did glEnable(GL_BLEND) ?

Idea :

RgbColor myImage(W,H, file);

RgbaColor result[W][H];

for(int x=0…W-1; ++x)
for(int y=0…H-1; ++y)
{
if(myRgbImage[x][y]==BlackRgbColor)
{
result[x][y].r = 0;
result[x][y].g = 0;
result[x][y].b = 0;
result[x][y].a = 0;
}
else
{
result[x][y].r = myImage[x][y].r;
result[x][y].g = myImage[x][y].r;
result[x][y].b = myImage[x][y].r;
result[x][y].a = 0xFF;
}
}

glTexImage2D(GL_TEXTURE_2D,0,4,W,H,0,GL_RGBA,GL_BYTE,result);

Hi, Thanks for the response & code frag.
I don’t really want to add the alpha component to my texture as I have a large number of textures that need to be updated rapidly & the memory is limited. And there is no need for transparency per-se in my app. I just need the black pixels out.
So, i’m looking for way to keep my textures as RGB, yet make the black pixels ‘transparent’/ignore drawing the back portion.
Thanks :slight_smile:

Maybe this would work for you, if the non-transparent portions of each image aren’t overlapping.
Enable blending, and use glBlendFunc(GL_ONE, GL_ONE) for additive blending. The black portions shouldn’t change what is already drawn. For the first image you draw you might need to either clear to black or disable blending.

Originally posted by alan e:
I have many RGB textures with black backgrounds, loaded from files, that are updated rapidly. <…>
I cannot add an alpha component to each texture because of resource limitations.

What kind of excuse is that? What resource limitation are you talking about?

Of course you can add alpha. You should add alpha, because it’s the easiest and most portable solution. Especially if you load from files, as you say, the software preprocessing overhead is insignificant (how fast is your hdd? 50MB/s tops?).

True per-fragment color keying is expensive and requires some sort of “shader” hardware. You don’t want to go there if you don’t absolutely have to …