using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using OpenTK; using OpenTK.Graphics.OpenGL; using MathNet.Numerics.LinearAlgebra.Single; using bbiwarg.DataSource; namespace bbiwarg.Graphics { class Output : GameWindow { private IVideoHandle videoHandle; private uint imageBufferId; public Output(IVideoHandle videoHandle) { this.videoHandle = videoHandle; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); Title = "OutputTest"; GL.ClearColor(Color.Black); // transparency GL.Enable(EnableCap.Blend); GL.BlendEquation(BlendEquationMode.Max); initBuffers(); } protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY); GL.MatrixMode(MatrixMode.Modelview); GL.LoadMatrix(ref modelview); videoHandle.nextFrame(); Stopwatch sw = new Stopwatch(); sw.Start(); drawDepthImage(); // palm (iisu) Vector palmPosition = videoHandle.getPalmPosition3D(1); Point palmPoint = new Point(palmPosition, Color.Yellow, 0.005f); palmPoint.draw(); // foreFinger Vector foreFingerPosition = videoHandle.getForeFingerPosition3D(1); Point foreFingerPoint = new Point(foreFingerPosition, Color.Red, 0.005f); foreFingerPoint.draw(); // foreArm (iisu) Vector foreArmPosition = videoHandle.getForearmPosition3D(1); Point foreArmPoint = new Point(foreArmPosition, Color.Yellow, 0.005f); foreArmPoint.draw(); // finger (iisu) Vector[] fingerPositions = videoHandle.getFingerTipPositions3D(1); DetectionStatus[] fingerStatus = videoHandle.getFingerStatus(1); for (int i = 0; i < fingerStatus.Length; ++i) { if (fingerStatus[i] == DetectionStatus.Detected || fingerStatus[i] == DetectionStatus.Tracked) { Point fingerPoint = new Point(fingerPositions[i], Color.Yellow, 0.005f); fingerPoint.draw(); } } // palm Palm palm = videoHandle.getPalm(1); Rectangle palmRect = new Rectangle(palm.getCorners(), Color.FromArgb(128, Color.Blue)); palmRect.draw(); sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); SwapBuffers(); } protected override void OnResize(EventArgs e) { base.OnResize(e); GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height); Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f); GL.MatrixMode(MatrixMode.Projection); GL.LoadMatrix(ref projection); } private void initBuffers() { GL.GenBuffers(1, out imageBufferId); GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)((3 * sizeof(float) + 4 * sizeof(byte)) * videoHandle.getWidth() * videoHandle.getHeight()), IntPtr.Zero, BufferUsageHint.StreamDraw); } private void drawDepthImage() { GL.EnableClientState(ArrayCap.VertexArray); GL.EnableClientState(ArrayCap.ColorArray); GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId); videoHandle.createVertexArray(GL.MapBuffer(BufferTarget.ArrayBuffer, BufferAccess.WriteOnly)); GL.UnmapBuffer(BufferTarget.ArrayBuffer); GL.VertexPointer(3, VertexPointerType.Float, 3 * sizeof(float) + 4 * sizeof(byte), IntPtr.Zero); GL.ColorPointer(4, ColorPointerType.UnsignedByte, 3 * sizeof(float) + 4 * sizeof(byte), 3 * sizeof(float)); GL.PointSize(2.0f); GL.DrawArrays(PrimitiveType.Points, 0, videoHandle.getWidth() * videoHandle.getHeight()); } } }