我正在寻找一种方法来删除 24 位位图的背景,同时保持主图像完全不透明,到目前为止,混合已经达到了目的,但现在我需要保持主要位不透明。我在谷歌上搜索过,但没有发现任何帮助,我想我可能正在搜索错误的术语,所以任何帮助将不胜感激。
编辑:是的,对不起,我现在使用黑色作为背景。
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;
}
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);
这听起来有点令人困惑。24 位图像没有透明度,它只是一个 RGB 值数组。“保持主要部分不透明”是什么意思?您是否想让所有具有某种颜色的像素(例如所有黑色像素)透明?如果这是您想要的,您可以使用 google opengl 透明蒙版(例如,参见NeHe #20)。
您真正需要的是图像中的 Alpha 通道。由于它是 24 位的,因此您没有,但您可以在加载图像时动态创建一个,只需将每个黑色 (0x000000) 像素的 alpha 设置为零即可
在我看来,您正在寻找模板缓冲区,它仅用于这种限制绘图区域的任务。
缓冲区存储与每个像素关联的值,其中 1 个后续绘图将被渲染,而它为 0 将被屏蔽。通常,模板缓冲区与多通道渲染技术一起使用,以创建反射和平视显示器之类的东西。