Output.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.005f);
  52. foreFingerPoint.draw();
  53. // foreArm (iisu)
  54. Vector foreArmPosition = videoHandle.getForearmPosition3D(1);
  55. Point foreArmPoint = new Point(foreArmPosition, Color.Yellow, 0.005f);
  56. foreArmPoint.draw();
  57. // finger (iisu)
  58. Vector[] fingerPositions = videoHandle.getFingerTipPositions3D(1);
  59. DetectionStatus[] fingerStatus = videoHandle.getFingerStatus(1);
  60. for (int i = 0; i < fingerStatus.Length; ++i)
  61. {
  62. if (fingerStatus[i] == DetectionStatus.Detected || fingerStatus[i] == DetectionStatus.Tracked)
  63. {
  64. Point fingerPoint = new Point(fingerPositions[i], Color.Yellow, 0.005f);
  65. fingerPoint.draw();
  66. }
  67. }
  68. // palm
  69. Palm palm = videoHandle.getPalm(1);
  70. Rectangle palmRect = new Rectangle(palm.getCorners(), Color.FromArgb(128, Color.Blue));
  71. palmRect.draw();
  72. sw.Stop();
  73. Console.WriteLine(sw.ElapsedMilliseconds);
  74. SwapBuffers();
  75. }
  76. protected override void OnResize(EventArgs e)
  77. {
  78. base.OnResize(e);
  79. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  80. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
  81. GL.MatrixMode(MatrixMode.Projection);
  82. GL.LoadMatrix(ref projection);
  83. }
  84. private void initBuffers()
  85. {
  86. GL.GenBuffers(1, out imageBufferId);
  87. GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId);
  88. GL.BufferData(BufferTarget.ArrayBuffer,
  89. (IntPtr)((3 * sizeof(float) + 4 * sizeof(byte)) * videoHandle.getWidth() * videoHandle.getHeight()),
  90. IntPtr.Zero, BufferUsageHint.StreamDraw);
  91. }
  92. private void drawDepthImage()
  93. {
  94. GL.EnableClientState(ArrayCap.VertexArray);
  95. GL.EnableClientState(ArrayCap.ColorArray);
  96. GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId);
  97. videoHandle.createVertexArray(GL.MapBuffer(BufferTarget.ArrayBuffer, BufferAccess.WriteOnly));
  98. GL.UnmapBuffer(BufferTarget.ArrayBuffer);
  99. GL.VertexPointer(3, VertexPointerType.Float, 3 * sizeof(float) + 4 * sizeof(byte), IntPtr.Zero);
  100. GL.ColorPointer(4, ColorPointerType.UnsignedByte, 3 * sizeof(float) + 4 * sizeof(byte), 3 * sizeof(float));
  101. GL.PointSize(2.0f);
  102. GL.DrawArrays(PrimitiveType.Points, 0, videoHandle.getWidth() * videoHandle.getHeight());
  103. }
  104. }
  105. }