Output3D.cs 5.0 KB

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