I am receiving an 8-bit brightness data from a detector (Leap Motion). Here is some info about the Leap Motion camera images. I want to load those images to a Bitmap and then to a texture every frame so I can create the illusion of a see-trough video. I would like to stick to OpenGL 1.0 both because I haven't used shaders before and because I am using the OpenTK port for Oculus Rift which is an early alpha and doesn't support OpenGL 2.0 and above yet. Also in general I am new to OpenGL, and in this specific case color formats, so I am a bit lost. I am using the OpenTK loading texture example and trying to combine it with the above instructions from the Leap Motion documentation.
Here is my code for creating the bitmap using the detector image data and loading it into a texture (I do this in the OnUpdateFrame method):
//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);
This is how I load the texture at 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();
What I get when I run the code is a white box where the texture should be and the code crashes with a System.AccessViolationException. I assume it has something to do with the PixelFormat
for the texture and the Bitmap but I am not sure how to resolve it. What will be the appropriate parameters to use for GL.TexImage2D()
?
Moreover, is there a better way for me to handle the processing of the data frame-by-frame?