Output.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 IInputProvider inputProvider;
  17. private IVideoHandle videoHandle;
  18. private ForeFingerDetection foreFingerDetection;
  19. private PalmDetection palmDetection;
  20. private uint imageBufferId, pointBufferId, quadBufferId;
  21. public Output(IInputProvider inputProvider, IVideoHandle videoHandle)
  22. {
  23. this.inputProvider = inputProvider;
  24. this.videoHandle = videoHandle;
  25. this.foreFingerDetection = new ForeFingerDetection(videoHandle);
  26. this.palmDetection = new PalmDetection(inputProvider, videoHandle);
  27. }
  28. protected override void OnLoad(EventArgs e)
  29. {
  30. base.OnLoad(e);
  31. Title = "OutputTest";
  32. GL.ClearColor(Color.Black);
  33. initBuffers();
  34. }
  35. protected override void OnRenderFrame(FrameEventArgs e)
  36. {
  37. if (!inputProvider.isActive())
  38. {
  39. inputProvider.stop();
  40. return;
  41. }
  42. base.OnRenderFrame(e);
  43. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  44. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  45. GL.MatrixMode(MatrixMode.Modelview);
  46. GL.LoadMatrix(ref modelview);
  47. videoHandle.nextFrame();
  48. Stopwatch sw = new Stopwatch();
  49. sw.Start();
  50. GL.EnableClientState(ArrayCap.VertexArray);
  51. GL.EnableClientState(ArrayCap.ColorArray);
  52. GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId);
  53. videoHandle.createVertexArray(GL.MapBuffer(BufferTarget.ArrayBuffer, BufferAccess.WriteOnly));
  54. GL.UnmapBuffer(BufferTarget.ArrayBuffer);
  55. GL.VertexPointer(3, VertexPointerType.Float, 3 * sizeof(float) + 4 * sizeof(byte), IntPtr.Zero);
  56. GL.ColorPointer(4, ColorPointerType.UnsignedByte, 3 * sizeof(float) + 4 * sizeof(byte), 3 * sizeof(float));
  57. GL.PointSize(2.0f);
  58. GL.DrawArrays(PrimitiveType.Points, 0, videoHandle.getWidth() * videoHandle.getHeight());
  59. // draw points
  60. float[] pointData;
  61. DetectionStatus[] fingerStatus = inputProvider.getFingerStatus(1);
  62. int numFingersDetected = 0;
  63. for (int i = 0; i < fingerStatus.Length; ++i)
  64. {
  65. if (fingerStatus[i] == DetectionStatus.Detected || fingerStatus[i] == DetectionStatus.Tracked)
  66. ++numFingersDetected;
  67. }
  68. pointData = new float[(4 + 3) * (3 + numFingersDetected)];
  69. Color y = Color.Yellow;
  70. Vector palmPosition = videoHandle.getPalmPosition3D(1);
  71. Vector foreFingerPosition = videoHandle.pixel2VertexPosition(foreFingerDetection.getForeFingerPosition3D(1));
  72. Vector foreArmPosition = inputProvider.getForearmPosition3D(1);
  73. pointData[0] = palmPosition[0];
  74. pointData[1] = palmPosition[1];
  75. pointData[2] = -palmPosition[2];
  76. pointData[3] = y.R / 255.0f;
  77. pointData[4] = y.G / 255.0f;
  78. pointData[5] = y.B / 255.0f;
  79. pointData[6] = y.A / 255.0f;
  80. pointData[7] = foreArmPosition[0];
  81. pointData[8] = foreArmPosition[1];
  82. pointData[9] = -foreArmPosition[2];
  83. pointData[10] = y.R / 255.0f;
  84. pointData[11] = y.G / 255.0f;
  85. pointData[12] = y.B / 255.0f;
  86. pointData[13] = y.A / 255.0f;
  87. int index = 14;
  88. Vector[] fingerPositions = inputProvider.getFingerTipPositions3D(1);
  89. for (int i = 0; i < fingerStatus.Length; ++i)
  90. {
  91. if (fingerStatus[i] == DetectionStatus.Detected || fingerStatus[i] == DetectionStatus.Tracked)
  92. {
  93. pointData[index + 0] = fingerPositions[i][0];
  94. pointData[index + 1] = fingerPositions[i][1];
  95. pointData[index + 2] = -fingerPositions[i][2];
  96. pointData[index + 3] = y.R / 255.0f;
  97. pointData[index + 4] = y.G / 255.0f;
  98. pointData[index + 5] = y.B / 255.0f;
  99. pointData[index + 6] = y.A / 255.0f;
  100. index += 7;
  101. }
  102. }
  103. pointData[index + 0] = foreFingerPosition[0];
  104. pointData[index + 1] = foreFingerPosition[1];
  105. pointData[index + 2] = foreFingerPosition[2];
  106. pointData[index + 3] = Color.Red.R / 255.0f;
  107. pointData[index + 4] = Color.Red.G / 255.0f;
  108. pointData[index + 5] = Color.Red.B / 255.0f;
  109. pointData[index + 6] = Color.Red.A / 255.0f;
  110. index += 7;
  111. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  112. GL.BindBuffer(BufferTarget.ArrayBuffer, pointBufferId);
  113. GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(pointData.Length * sizeof(float)), pointData, BufferUsageHint.DynamicDraw);
  114. GL.VertexPointer(3, VertexPointerType.Float, (4 + 3) * sizeof(float), IntPtr.Zero);
  115. GL.ColorPointer(4, ColorPointerType.Float, (4 + 3) * sizeof(float), 3 * sizeof(float));
  116. GL.PointSize(10f);
  117. GL.DrawArrays(PrimitiveType.Points, 0, pointData.Length / (4 + 3));
  118. // draw quads
  119. float[] quadData = new float[4 * (4 + 3) * 1];
  120. Palm palm = palmDetection.getPalm(1);
  121. Vector ul = palm.getUpperLeft();
  122. Vector ur = palm.getUpperRight();
  123. Vector lr = palm.getLowerRight();
  124. Vector ll = palm.getLowerLeft();
  125. quadData[0] = ul.x();
  126. quadData[1] = ul.y();
  127. quadData[2] = -ul.z();
  128. quadData[3] = y.R / 255.0f;
  129. quadData[4] = y.G / 255.0f;
  130. quadData[5] = y.B / 255.0f;
  131. quadData[6] = 0.5f;
  132. quadData[7] = ur.x();
  133. quadData[8] = ur.y();
  134. quadData[9] = -ur.z();
  135. quadData[10] = y.R / 255.0f;
  136. quadData[11] = y.G / 255.0f;
  137. quadData[12] = y.B / 255.0f;
  138. quadData[13] = 0.5f;
  139. quadData[14] = lr.x();
  140. quadData[15] = lr.y();
  141. quadData[16] = -lr.z();
  142. quadData[17] = y.R / 255.0f;
  143. quadData[18] = y.G / 255.0f;
  144. quadData[19] = y.B / 255.0f;
  145. quadData[20] = 0.5f;
  146. quadData[21] = ll.x();
  147. quadData[22] = ll.y();
  148. quadData[23] = -ll.z();
  149. quadData[24] = y.R / 255.0f;
  150. quadData[25] = y.G / 255.0f;
  151. quadData[26] = y.B / 255.0f;
  152. quadData[27] = 0.5f;
  153. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  154. GL.BindBuffer(BufferTarget.ArrayBuffer, quadBufferId);
  155. GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(quadData.Length * sizeof(float)), quadData, BufferUsageHint.DynamicDraw);
  156. GL.VertexPointer(3, VertexPointerType.Float, (4 + 3) * sizeof(float), IntPtr.Zero);
  157. GL.ColorPointer(4, ColorPointerType.Float, (4 + 3) * sizeof(float), 3 * sizeof(float));
  158. GL.DrawArrays(PrimitiveType.Quads, 0, quadData.Length / (4 + 3));
  159. sw.Stop();
  160. Console.WriteLine(sw.ElapsedMilliseconds);
  161. SwapBuffers();
  162. }
  163. protected override void OnResize(EventArgs e)
  164. {
  165. base.OnResize(e);
  166. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  167. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
  168. GL.MatrixMode(MatrixMode.Projection);
  169. GL.LoadMatrix(ref projection);
  170. }
  171. private void initBuffers()
  172. {
  173. GL.GenBuffers(1, out imageBufferId);
  174. GL.GenBuffers(1, out pointBufferId);
  175. GL.GenBuffers(1, out quadBufferId);
  176. GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId);
  177. GL.BufferData(BufferTarget.ArrayBuffer,
  178. (IntPtr)((3 * sizeof(float) + 4 * sizeof(byte)) * videoHandle.getWidth() * videoHandle.getHeight()),
  179. IntPtr.Zero, BufferUsageHint.StreamDraw);
  180. }
  181. }
  182. }