我正在使用 C# 和 OpenTK 为精灵/纹理图集功能编写一个支持类。到目前为止,大多数功能都运行良好(正交视图上的简单 2D 切片)。
我的问题与调用 GDI+ Bitmap.MakeTransparent() 方法设置颜色(洋红色 / 0xFFFF00FF)以用作颜色键时的意外显示结果有关。
看来我为 bitmap.LockBits() 和 GL.TexImage2D() 调用使用了不正确的像素格式参数。我的代码基于确实有效的示例,但它们的共同点是传递给 LockBits() 的矩形是针对整个图像的。
与此过程有关的调用是:
<!-- language: C# -->
Bitmap bitmap = new Bitmap(filename);
bitmap.MakeTransparent(Color.Magenta);
GL.GenTextures(1, out texture_id);
GL.BindTexture(TextureTarget.Texture2D, texture_id);
// "rect" is initialized for one of:
// - the dimensions of the entire image
// (0, 0, bitmap.width, bitmap.height)
// - the dimensions for a sub-rectangle within the image (for one tile)
// (tile_x * tile_width, tile_y * tile_height, tile_width, tile_height)
// I observe different behaviors for a sub-rectangle,
// as compared to the entire image, when in combination with
// the .MakeTransparent() call.
//
// This code is in a load_tile() method, and the plan was to make
// multiple calls per image file, one per tile to extract as a GL texture.
// Without transparency, that worked fine.
Rectangle rect = new Rectangle(xx, yy, width, height);
BitmapData data = bitmap.LockBits(rect,
ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppRgb);
// In the absence of calling bitmap.MakeTransparent(),
// images loaded and displayed as expected with Format24bppRgb.
// With MakeTransparent() and Format32bppRgb, the results seem to be OS-dependent.
// (At first I thought the "correct" combination to be found,
// but then found that the results were "right" only under Windows 7.)
GL.TexImage2D(
OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, // texture_target,
0, // level,
OpenTK.Graphics.OpenGL.PixelInternalFormat.Rgba, // internal_format
data.Width, data.Height, // width, height,
0, // border,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, // pixel_format
OpenTK.Graphics.OpenGL.PixelType.UnsignedByte, // pixel_type
data.Scan0 // pixels
);
// Certainly the internal_format and pixel_format arguments are pertinent,
// but other combinations I have tried produced various undesired display results.
// After reading various (OpenGL, OpenTK, and GDI+) docs, still I am not enlightened..
bitmap.UnlockBits(data);
我在不同的 boxen 上使用上面的代码测试了一个小演示,并观察这些结果:
- Windows 7 框:洋红色像素充当透明(所需的结果)
- Windows XP 框:洋红色像素呈现为黑色
- Ubuntu Linux box:洋红色像素呈现为洋红色
这让我感到惊讶,因为我预计(GDI+ 和 OpenGL 以及 OpenTK 绑定)在不同的盒子上会表现相同。
就我吸收 GDI+ 和 OpenGL / OpenTK API 文档而言,我认为我的困惑与这两点有关:
调用 MakeTransparent() + LockBits() + GL.TexImage2D() 的正确方法是什么,以便将指定的颜色渲染为透明?
为什么在为子矩形而不是整个图像调用 LockBits() 时,对于某些像素格式参数组合,我会看到奇怪的显示结果(好像“步幅”计算错误)?
更新:我已将我的代码缩减为 Github 上的一个小项目: https ://github.com/sglasby/OpenGL_Transparent_Sprite
此外,我偶然发现了一个有效的参数组合(LockBits() 的参数 3 是 Format32bppPArgb),虽然目前尚不清楚它为什么有效,因为文档暗示需要另一种像素格式:http: //msdn.microsoft.com/ en-us/library/8517ckds.aspx (声明调用 MakeTransparent 后位图将采用 Format32bppArgb)。