Output.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. //Depth Test
  31. GL.Enable(EnableCap.DepthTest);
  32. initBuffers();
  33. GL.Enable(EnableCap.Blend);
  34. GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
  35. }
  36. protected override void OnRenderFrame(FrameEventArgs e)
  37. {
  38. base.OnRenderFrame(e);
  39. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  40. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  41. GL.MatrixMode(MatrixMode.Modelview);
  42. GL.LoadMatrix(ref modelview);
  43. videoHandle.nextFrame();
  44. Stopwatch sw = new Stopwatch();
  45. sw.Start();
  46. drawDepthImage();
  47. // palm (iisu)
  48. Vector palmPosition = videoHandle.getPalmPosition3D(1);
  49. Point palmPoint = new Point(palmPosition, Color.Yellow, 0.005f);
  50. palmPoint.draw();
  51. // foreFinger
  52. Vector foreFingerPosition = videoHandle.getForeFingerPosition3D(1);
  53. Point foreFingerPoint = new Point(foreFingerPosition, Color.Red, 0.05f);
  54. foreFingerPoint.draw();
  55. //handPoints
  56. List<Vector> handPoints = videoHandle.getHandPoints();
  57. for (int i = 0; i < handPoints.Count(); i++)
  58. {
  59. new Point(videoHandle.pixel2VertexPosition(handPoints[i]), Color.Yellow, 0.001f).draw();
  60. }
  61. // foreArm (iisu)
  62. Vector foreArmPosition = videoHandle.getForearmPosition3D(1);
  63. Point foreArmPoint = new Point(foreArmPosition, Color.Yellow, 0.005f);
  64. foreArmPoint.draw();
  65. // finger (iisu)
  66. Vector[] fingerPositions = videoHandle.getFingerTipPositions3D(1);
  67. DetectionStatus[] fingerStatus = videoHandle.getFingerStatus(1);
  68. for (int i = 0; i < fingerStatus.Length; ++i)
  69. {
  70. if (fingerStatus[i] == DetectionStatus.Detected || fingerStatus[i] == DetectionStatus.Tracked)
  71. {
  72. Point fingerPoint = new Point(fingerPositions[i], Color.Yellow, 0.005f);
  73. fingerPoint.draw();
  74. }
  75. }
  76. // palm
  77. Palm palm = videoHandle.getPalm(1);
  78. Rectangle palmRect = new Rectangle(palm.getCorners(), Color.FromArgb(128, Color.Blue));
  79. palmRect.draw();
  80. sw.Stop();
  81. Console.WriteLine(sw.ElapsedMilliseconds);
  82. SwapBuffers();
  83. }
  84. protected override void OnResize(EventArgs e)
  85. {
  86. base.OnResize(e);
  87. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  88. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
  89. GL.MatrixMode(MatrixMode.Projection);
  90. GL.LoadMatrix(ref projection);
  91. }
  92. private void initBuffers()
  93. {
  94. GL.GenBuffers(1, out imageBufferId);
  95. GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId);
  96. GL.BufferData(BufferTarget.ArrayBuffer,
  97. (IntPtr)((3 * sizeof(float) + 4 * sizeof(byte)) * videoHandle.getWidth() * videoHandle.getHeight()),
  98. IntPtr.Zero, BufferUsageHint.StreamDraw);
  99. }
  100. private void drawDepthImage()
  101. {
  102. GL.EnableClientState(ArrayCap.VertexArray);
  103. GL.EnableClientState(ArrayCap.ColorArray);
  104. GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId);
  105. videoHandle.createVertexArray(GL.MapBuffer(BufferTarget.ArrayBuffer, BufferAccess.WriteOnly));
  106. GL.UnmapBuffer(BufferTarget.ArrayBuffer);
  107. GL.VertexPointer(3, VertexPointerType.Float, 3 * sizeof(float) + 4 * sizeof(byte), IntPtr.Zero);
  108. GL.ColorPointer(4, ColorPointerType.UnsignedByte, 3 * sizeof(float) + 4 * sizeof(byte), 3 * sizeof(float));
  109. GL.PointSize(2.0f);
  110. GL.DrawArrays(PrimitiveType.Points, 0, videoHandle.getWidth() * videoHandle.getHeight());
  111. }
  112. }
  113. }