【发布时间】:2014-03-19 00:50:24
【问题描述】:
我正在为精灵/纹理图集功能编写一个支持类,使用 C# 和 OpenTK。 到目前为止,大多数功能都运行良好(正交视图上的简单 2D 切片)。
我的问题与调用 GDI+ Bitmap.MakeTransparent() 方法设置颜色 (Magenta / 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 框:洋红色像素呈现为洋红色
这让我很吃惊,因为我预计(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)。
【问题讨论】:
-
对我来说,
Format32bppArgb在 Win7/Intel 上给出了打乱的结果。Format32bppRgb和Format32bppPArgb似乎有效。正在调查。 -
出现了一些有趣的东西:
Format32bppArpb没有MakeTransparent有data.Stride = 128;与MakeTransparent、data.Stride = 512。Format32bppPArgb总是有data.Stride = 128。不知道为什么会这样,但它可能指向解决方案。