How to remove black background from textures in OpenGL

I'm looking for a way to remove the background of a 24-bit bitmap while keeping the main image completely opaque, so far the blending has served the purpose, but now I need to keep the main bit opaque. I searched on Google but didn’t find anything useful, I think I probably searched for the wrong terms, so any help would be greatly appreciated.

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

+3
source share
5 answers

When you create the texture for OpenGL, you need to create the texture as a 32-bit RGBA texture. Create a buffer to hold the contents of this new 32-bit texture and try again through a 24-bit raster map. Each time you encounter a background color, set the alpha to zero in the 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;
}
+8
source

You will need to load the textures in GL_RGBA format when calling glTexImage2D. If you have done this, you just need to enable blend:

    /* Set up blend */
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+2
source

. 24- , RGB. " "? - , - ? , , google opengl (. NeHe # 20).

0

- . 24 , , " " , (0x000000)

0

, Buffer Stencil, .

, , 0, . , .

-1

Source: https://habr.com/ru/post/1710612/


All Articles