Output.cs 8.4 KB

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