Output.cs 4.6 KB

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