4

我正在使用 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)。

4

1 回答 1

0

虽然这是您的问题的一个单独问题,但在大多数情况下,您实际上应该使用 premultiplied-alpha ( Format32bppPArgb)。如果这种格式工作正常,那么理解为什么Format32bppArgb不工作主要是一项学术练习。

我使用 Intel 2000HD 在 Win7 上运行您的示例项目并得到以下结果:

  • Format32bppPArgb正常工作
  • Format32bppRgb正常工作
  • Format32bppArgb被打乱了

在进一步调查中,这似乎与 OpenGL 无关,而是与Bitmap.LockBits工作方式有关。

检查data.Stride调试器上每种方法的值:

  • Format32bppPArgb步幅为 128(位图宽度的 4 倍,正确)
  • Format32bppRgb步幅为 128(位图宽度的 4 倍,正确)
  • Format32bppArgb步幅为 512(位图宽度的 16 倍,?)

MSDN 在这里没有找到有用的东西。在这一点上,我无法说出为什么会这样。如果我设法发现任何东西,我会更新这个答案。

编辑:你瞧,如果你在解压数据时强制使用正确的步幅,输出看起来是正确的:

GL.PixelStore(PixelStoreParameter.UnpackRowLength, data.Width * 4); // 4x for 32bpp
于 2014-03-18T16:50:24.717 回答