0

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?

tovida
  • 21
  • 2
  • Two tips for working with OpenGL: 1. Build up step-by-step -- that is, get an untextured quad rendering; then texture it with a static, loaded bitmap; then generate a bitmap using a for loop; then get the image from the Leap, etc. 2: use OpenGLs error reporting functions liberally (glGetError()). – Charles Ward Apr 02 '15 at 18:08
  • Also, you might not have to create a Bitmap object. If a single color texture format like GL_RED is available, you could use the image data directly, i.e: glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, image.width, image.height, 0, GL_RED, GL_UNSIGNED_BYTE, image.data); – Charles Ward Apr 02 '15 at 18:12
  • @CharlesWard Regarding the step-by-step process, I thought that's what I was doing, but I may have a mistake somewhere. Can you elaborate what you mean by generate a bitmap using a for loop? I will also try `glGetError()` to find what's going on. I was able to get it to work using `GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, image.Width, image.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Green, PixelType.UnsignedByte, image.Data);` without a bitmap, but now my 3D object that should be overlayed on top of the see-trough video is also green. – tovida Apr 02 '15 at 20:01
  • For generating a bitmap, I just set each row in the image to an increasing color value to create a gradient. – Charles Ward Apr 02 '15 at 21:32
  • For the single-color pixel format, I'm not sure, maybe if you use an internal format that is also a single component, but definitely not with the RGBA format, at least not without a shader. – Charles Ward Apr 02 '15 at 21:43
  • I was able to get a black and white picture with GL_LUMINANCE for both parameters in glTexImage2D. My 3D object does have some color to it now, but it is in a very shaded - blackish way. It's very confusing. I am not quit sure what's happening. – tovida Apr 02 '15 at 21:51
  • Maybe unbind the texture? It sounds like your 3D object is also being texture mapped by the Leap image. – Charles Ward Apr 03 '15 at 03:38
  • That was the problem. Thanks for the help! – tovida Apr 03 '15 at 03:51

0 Answers0