Question

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.

Was it helpful?

Solution

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;
}

OTHER TIPS

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);

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).

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top