How to remove black background from textures in OpenGL

Go To StackoverFlow.com

3

I'm looking for a way to remove the background of a 24bit bitmap, while keeping the main image totally opaque, up until now blending has served the purpose but now I need to keep the main bit opaque. I've searched on Google but found nothing helpful, I think I'm probably searching for the wrong terms though, so any help would be greatly appreciated.

Edit: Yes sorry, I'm using black right now as the background.

2009-06-16 19:25
by Auraomega
I'm assuming by background you are talking about a designated color, such as black: R0G0B0 - Andrew Garrison 2009-06-16 19:35


8

When you are creating the texture for OpenGL, you'll want to create the texture as a 32-bit RGBA texture. Create a buffer to hold the contents of this new 32-bit texture and iterate through your 24-bit bitmap. Every time you come across your background color, set the alpha to zero in your new 32-bit bitmap.

struct Color32;
struct Color24;

void Load24BitTexture(Color24* ipTex24, int width, int height)
{
   Color32* lpTex32 = new Color32[width*height];

   for(x = 0; x < width; x++)
   {
      for(y = 0; y < height; y++)
      {
         int index = y*width+x;
         lpTex32[index].r = ipTex24[index].r;
         lpTex32[index].g = ipTex24[index].g;
         lpTex32[index].b = ipTex24[index].b;

         if( ipTex24[index] == BackgroundColor)
            lpTex32[index].a = 0;
         else
            lpTex32[index].a = 255;
      }
   }

   glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, lpTex32);

   delete [] lpTex32;
}
2009-06-16 19:47
by Andrew Garrison
Fantastic, exactly what I was looking for. I wasn't quite sure how the alpha channel was read in OGL, after Googling it and reading your code I think I'm sorted and ready to go. Thank you : - Auraomega 2009-06-16 19:57


2

You will need to load your textures with GL_RGBA format in your call to glTexImage2D. If you have done this then you just need to enable blend:

    /* Set up blend */
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2009-06-16 19:47
by Nick Van Brunt
I totally misunderstood the question but one ~would~ need to enable blend so I wont delete my answer - Nick Van Brunt 2009-06-16 20:22


0

This sounds a bit confusing. 24bit image has no transparency, it’s simply an array of RGB values. What do you mean by “keeping the main bit opaque”? Do you want to have all pixels with certain color – for example all black pixels – transparent? If this is what you want, you can google opengl transparency mask (see the NeHe #20 for example).

2009-06-16 19:44
by zoul
I said I originally used the blend functions to remove the black, but it left me with a translucent central image, instead of opaque.

I have used a transparency mask in the past, but I had lots of issues with it and didn't like requiring another file per image - Auraomega 2009-06-16 19:49

Aha, I think I understand now. If you have enough memory, simply go with an alpha channel in the image, as others have pointed out - zoul 2009-06-16 19:54


0

What you really need is an alpha channel in your image. Since it's 24 bit, you don't have one, but you could create one on the fly as you load the image by simply setting the alpha to zero for every black (0x000000) pixel

2009-06-16 19:45
by Eric Petroelje
This would be probably the easiest solution if he has enough memory to burn. If not, it would be a waste to spend a whole 8bit alpha channel on indexed transparency - zoul 2009-06-16 19:50


-1

Sounds to me like you're looking for the Stencil Buffer, which is used for just this sort of task of restricting the drawing area.

The buffer stores a value associated to each pixels, where it's 1 subsequent drawing will be rendered and where it's 0 it will be masked. Usually stencil buffers are used with multipass rendering techniques to create things like reflections and heads up displays.

2009-06-16 19:42
by Cruachan
Ads