views:

1061

answers:

5

Hi,

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.

A: 

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.

Cruachan
A: 

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

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
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
A: 

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

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
A: 

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);
Nick
I totally misunderstood the question but one ~would~ need to enable blend so I wont delete my answer.
Nick
+4  A: 

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