Output.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 ForeFingerDetection foreFingerDetection;
  18. private uint imageBufferId, pointBufferId;
  19. public Output(IVideoHandle videoHandle)
  20. {
  21. this.videoHandle = videoHandle;
  22. this.foreFingerDetection = new ForeFingerDetection(videoHandle);
  23. }
  24. protected override void OnLoad(EventArgs e)
  25. {
  26. base.OnLoad(e);
  27. Title = "OutputTest";
  28. GL.ClearColor(Color.Black);
  29. initBuffers();
  30. }
  31. protected override void OnRenderFrame(FrameEventArgs e)
  32. {
  33. base.OnRenderFrame(e);
  34. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  35. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  36. GL.MatrixMode(MatrixMode.Modelview);
  37. GL.LoadMatrix(ref modelview);
  38. videoHandle.nextFrame();
  39. Stopwatch sw = new Stopwatch();
  40. sw.Start();
  41. GL.EnableClientState(ArrayCap.VertexArray);
  42. GL.EnableClientState(ArrayCap.ColorArray);
  43. GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId);
  44. videoHandle.createVertexArray(GL.MapBuffer(BufferTarget.ArrayBuffer, BufferAccess.WriteOnly));
  45. GL.UnmapBuffer(BufferTarget.ArrayBuffer);
  46. GL.VertexPointer(3, VertexPointerType.Float, 3 * sizeof(float) + 4 * sizeof(byte), IntPtr.Zero);
  47. GL.ColorPointer(4, ColorPointerType.UnsignedByte, 3 * sizeof(float) + 4 * sizeof(byte), 3 * sizeof(float));
  48. GL.PointSize(3.0f);
  49. GL.DrawArrays(PrimitiveType.Points, 0, videoHandle.getWidth() * videoHandle.getHeight());
  50. // draw points
  51. float[] pointData;
  52. DetectionStatus[] fingerStatus = videoHandle.getFingerStatus(1);
  53. int numFingersDetected = 0;
  54. for (int i = 0; i < fingerStatus.Length; ++i)
  55. {
  56. if (fingerStatus[i] == DetectionStatus.Detected || fingerStatus[i] == DetectionStatus.Tracked)
  57. ++numFingersDetected;
  58. }
  59. pointData = new float[(4 + 3) * (3 + numFingersDetected)];
  60. Color y = Color.Yellow;
  61. Vector palmPosition = videoHandle.getPalmPosition3D(1);
  62. Vector foreFingerPosition = videoHandle.pixel2VertexPosition(foreFingerDetection.getForeFingerPosition3D(1));
  63. Vector foreArmPosition = videoHandle.getForearmPosition3D(1);
  64. pointData[0] = palmPosition[0];
  65. pointData[1] = palmPosition[1];
  66. pointData[2] = -palmPosition[2];
  67. pointData[3] = y.R / 255.0f;
  68. pointData[4] = y.G / 255.0f;
  69. pointData[5] = y.B / 255.0f;
  70. pointData[6] = y.A / 255.0f;
  71. pointData[7] = foreArmPosition[0];
  72. pointData[8] = foreArmPosition[1];
  73. pointData[9] = -foreArmPosition[2];
  74. pointData[10] = y.R / 255.0f;
  75. pointData[11] = y.G / 255.0f;
  76. pointData[12] = y.B / 255.0f;
  77. pointData[13] = y.A / 255.0f;
  78. int index = 14;
  79. Vector[] fingerPositions = videoHandle.getFingerTipPositions3D(1);
  80. for (int i = 0; i < fingerStatus.Length; ++i)
  81. {
  82. if (fingerStatus[i] == DetectionStatus.Detected || fingerStatus[i] == DetectionStatus.Tracked)
  83. {
  84. pointData[index + 0] = fingerPositions[i][0];
  85. pointData[index + 1] = fingerPositions[i][1];
  86. pointData[index + 2] = -fingerPositions[i][2];
  87. pointData[index + 3] = y.R / 255.0f;
  88. pointData[index + 4] = y.G / 255.0f;
  89. pointData[index + 5] = y.B / 255.0f;
  90. pointData[index + 6] = y.A / 255.0f;
  91. index += 7;
  92. }
  93. }
  94. pointData[index + 0] = foreFingerPosition[0];
  95. pointData[index + 1] = foreFingerPosition[1];
  96. pointData[index + 2] = foreFingerPosition[2];
  97. pointData[index + 3] = Color.Red.R / 255.0f;
  98. pointData[index + 4] = Color.Red.G / 255.0f;
  99. pointData[index + 5] = Color.Red.B / 255.0f;
  100. pointData[index + 6] = Color.Red.A / 255.0f;
  101. index += 7;
  102. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  103. GL.BindBuffer(BufferTarget.ArrayBuffer, pointBufferId);
  104. GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(pointData.Length * sizeof(float)), pointData, BufferUsageHint.DynamicDraw);
  105. GL.VertexPointer(3, VertexPointerType.Float, (4 + 3) * sizeof(float), IntPtr.Zero);
  106. GL.ColorPointer(4, ColorPointerType.Float, (4 + 3) * sizeof(float), 3 * sizeof(float));
  107. GL.PointSize(10f);
  108. GL.DrawArrays(PrimitiveType.Points, 0, pointData.Length / (4 + 3));
  109. sw.Stop();
  110. Console.WriteLine(sw.ElapsedMilliseconds);
  111. SwapBuffers();
  112. }
  113. protected override void OnResize(EventArgs e)
  114. {
  115. base.OnResize(e);
  116. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  117. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
  118. GL.MatrixMode(MatrixMode.Projection);
  119. GL.LoadMatrix(ref projection);
  120. }
  121. private void initBuffers()
  122. {
  123. GL.GenBuffers(1, out imageBufferId);
  124. GL.GenBuffers(1, out pointBufferId);
  125. GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId);
  126. GL.BufferData(BufferTarget.ArrayBuffer,
  127. (IntPtr)((3 * sizeof(float) + 4 * sizeof(byte)) * videoHandle.getWidth() * videoHandle.getHeight()),
  128. IntPtr.Zero, BufferUsageHint.StreamDraw);
  129. }
  130. }
  131. }