Output.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using OpenTK;
  9. using OpenTK.Graphics.OpenGL;
  10. using MathNet.Numerics.LinearAlgebra.Single;
  11. using bbiwarg.DataSource;
  12. namespace bbiwarg.Graphics
  13. {
  14. class Output : GameWindow
  15. {
  16. private IVideoHandle videoHandle;
  17. private uint imageBufferId;
  18. public Output(IVideoHandle videoHandle)
  19. {
  20. this.videoHandle = videoHandle;
  21. }
  22. protected override void OnLoad(EventArgs e)
  23. {
  24. base.OnLoad(e);
  25. Title = "OutputTest";
  26. GL.ClearColor(Color.Black);
  27. // transparency
  28. GL.Enable(EnableCap.Blend);
  29. GL.BlendEquation(BlendEquationMode.Max);
  30. initBuffers();
  31. GL.Enable(EnableCap.Blend);
  32. GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
  33. }
  34. protected override void OnRenderFrame(FrameEventArgs e)
  35. {
  36. base.OnRenderFrame(e);
  37. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  38. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  39. GL.MatrixMode(MatrixMode.Modelview);
  40. GL.LoadMatrix(ref modelview);
  41. videoHandle.nextFrame();
  42. Stopwatch sw = new Stopwatch();
  43. sw.Start();
  44. drawDepthImage();
  45. // palm (iisu)
  46. Vector palmPosition = videoHandle.getPalmPosition3D(1);
  47. Point palmPoint = new Point(palmPosition, Color.Yellow, 0.005f);
  48. palmPoint.draw();
  49. // foreFinger
  50. Vector foreFingerPosition = videoHandle.getForeFingerPosition3D(1);
  51. Point foreFingerPoint = new Point(foreFingerPosition, Color.Red, 0.05f);
  52. foreFingerPoint.draw();
  53. //handPoints
  54. List<Vector> handPoints = videoHandle.getHandPoints();
  55. for (int i = 0; i < handPoints.Count(); i++)
  56. {
  57. new Point(videoHandle.pixel2VertexPosition(handPoints[i]), Color.Yellow, 0.001f).draw();
  58. }
  59. // foreArm (iisu)
  60. Vector foreArmPosition = videoHandle.getForearmPosition3D(1);
  61. Point foreArmPoint = new Point(foreArmPosition, Color.Yellow, 0.005f);
  62. foreArmPoint.draw();
  63. // finger (iisu)
  64. Vector[] fingerPositions = videoHandle.getFingerTipPositions3D(1);
  65. DetectionStatus[] fingerStatus = videoHandle.getFingerStatus(1);
  66. for (int i = 0; i < fingerStatus.Length; ++i)
  67. {
  68. if (fingerStatus[i] == DetectionStatus.Detected || fingerStatus[i] == DetectionStatus.Tracked)
  69. {
  70. Point fingerPoint = new Point(fingerPositions[i], Color.Yellow, 0.005f);
  71. fingerPoint.draw();
  72. }
  73. }
  74. // palm
  75. Palm palm = videoHandle.getPalm(1);
  76. Rectangle palmRect = new Rectangle(palm.getCorners(), Color.FromArgb(128, Color.Blue));
  77. palmRect.draw();
  78. sw.Stop();
  79. Console.WriteLine(sw.ElapsedMilliseconds);
  80. SwapBuffers();
  81. }
  82. protected override void OnResize(EventArgs e)
  83. {
  84. base.OnResize(e);
  85. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  86. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
  87. GL.MatrixMode(MatrixMode.Projection);
  88. GL.LoadMatrix(ref projection);
  89. }
  90. private void initBuffers()
  91. {
  92. GL.GenBuffers(1, out imageBufferId);
  93. GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId);
  94. GL.BufferData(BufferTarget.ArrayBuffer,
  95. (IntPtr)((3 * sizeof(float) + 4 * sizeof(byte)) * videoHandle.getWidth() * videoHandle.getHeight()),
  96. IntPtr.Zero, BufferUsageHint.StreamDraw);
  97. }
  98. private void drawDepthImage()
  99. {
  100. GL.EnableClientState(ArrayCap.VertexArray);
  101. GL.EnableClientState(ArrayCap.ColorArray);
  102. GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId);
  103. videoHandle.createVertexArray(GL.MapBuffer(BufferTarget.ArrayBuffer, BufferAccess.WriteOnly));
  104. GL.UnmapBuffer(BufferTarget.ArrayBuffer);
  105. GL.VertexPointer(3, VertexPointerType.Float, 3 * sizeof(float) + 4 * sizeof(byte), IntPtr.Zero);
  106. GL.ColorPointer(4, ColorPointerType.UnsignedByte, 3 * sizeof(float) + 4 * sizeof(byte), 3 * sizeof(float));
  107. GL.PointSize(2.0f);
  108. GL.DrawArrays(PrimitiveType.Points, 0, videoHandle.getWidth() * videoHandle.getHeight());
  109. }
  110. }
  111. }