Output.cs 8.1 KB

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