Output.cs 5.1 KB

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