title: OpenCV webcam frames to OpenGL texture
body: <p>I am working on C# and using <code>OpenTK</code>(OpenGL wrapper) and <code>EmguCV</code>(OpenCV wrapper).</p>
  
  <p>What I want to do is easy to understand: Grab the webcam video stream and put it on a <code>GLControl</code>.</p>
  
  <ol>
  <li><p><strong>I have a static <code>class</code> called <code>Capturer</code> which has a method that captures a frame and returns it as a <code>cv::Mat</code> wrapped object</strong>:</p>
  
  <pre><code>internal static void Initialize()
  {
      cap = new VideoCapture(1);
      cap.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps, 25);
      cap.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameWidth, 1920);
      cap.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameHeight, 1080);
  }
  
  internal static Mat GetCurrentFrame()
  {
      mat = cap.QueryFrame();
      if (!mat.IsEmpty)
      {
          return mat;
      }
      return null;
  }
  </code></pre></li>
  <li><p><strong>Now in my <code>GLControl</code> <code>Load event</code> I initialize the capturer and OpenGL</strong>:</p>
  
  <pre><code>    Capturer.Initialize();
  
      GL.ClearColor(Color.Blue);
      GL.Enable(EnableCap.Texture2D);
  
      GL.Viewport(-glControl1.Width, -glControl1.Height, glControl1.Width * 2, glControl1.Height * 2);
  </code></pre></li>
  <li><p><strong>And finally, in the <code>GLControl</code> <code>Paint event</code></strong>:</p>
  
  <pre><code>    GL.Clear(ClearBufferMask.ColorBufferBit);
  
      GL.MatrixMode(MatrixMode.Projection);
      GL.LoadIdentity();
  
      Mat m = Capturer.GetCurrentFrame();
      if (m != null)
      {
          GL.GenTextures(1, out textureId);
          GL.BindTexture(TextureTarget.Texture2D, this.textureId);
  
          GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest);
          GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Linear);
  
          GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureWrapMode.Clamp);
          GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)TextureWrapMode.Clamp);
  
          GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, 1920, 1080, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgr, PixelType.UnsignedByte, m.DataPointer);
      }
      m.Dispose();
  
      glControl1.SwapBuffers();
      glControl1.Invalidate();
  </code></pre></li>
  </ol>
  
  <p>This is showing a full Blue screen. I think the error is on <code>m.DataPointer</code>.</p>
  
  <p>(I have tried rendering the frames with <code>Bitmap</code> using the property <code>m.Bitmap</code>and it works but the performance is so bad.)</p>