我正在接收来自检测器(Leap Motion)的 8 位亮度数据。这是有关Leap Motion 相机图像的一些信息。我想将这些图像加载到位图,然后每帧加载到纹理,这样我就可以创建透视视频的错觉。我想坚持使用 OpenGL 1.0,既因为我之前没有使用过着色器,也因为我正在为 Oculus Rift 使用 OpenTK 端口,这是一个早期的 alpha 版本,还不支持 OpenGL 2.0 及更高版本。另外,总的来说,我是 OpenGL 的新手,在这种特殊情况下,颜色格式也是如此,所以我有点迷茫。我正在使用OpenTK 加载纹理示例,并尝试将其与 Leap Motion 文档中的上述说明结合使用。
这是我使用检测器图像数据创建位图并将其加载到纹理中的代码(我在 OnUpdateFrame 方法中执行此操作):
//Get brightness data from Leap Motion
Frame frame = CustomController.Instance.Frame();
Leap.Image image = frame.Images [1];
//Load image data to a bitmap
Bitmap bitmap = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
//Convert to greyscake
ColorPalette grayscale = bitmap.Palette;
for (int i = 0; i < 256; i++)
{
grayscale.Entries[i] = Color.FromArgb((int)255, i, i, i);
}
bitmap.Palette = grayscale;
//Load bitmap to texture
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
GL.GenTextures(1, out texture);
GL.BindTexture(TextureTarget.Texture2D, texture);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
bitmap.UnlockBits(data);
这就是我在 OnRenderFrame 加载纹理的方式:
GL.BindTexture(TextureTarget.Texture2D, texture);
GL.Begin(PrimitiveType.Quads);
GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(-3f, -2f);
GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(3f, -2f);
GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(3f, 2f);
GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(-3f, 2f);
GL.End();
当我运行代码时,我得到的是一个白框,应该是纹理,并且代码因 System.AccessViolationException 而崩溃。我认为它与PixelFormat
纹理和位图有关,但我不确定如何解决它。什么是合适的参数GL.TexImage2D()
?
此外,我是否有更好的方法来逐帧处理数据?