Output.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. GL.Enable(EnableCap.Blend);
  29. GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
  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(2.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 = new DenseVector(new float[] { 0, 0, 0 }); //videoHandle.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. // draw quads
  110. float[] quadData = new float[4 * (4 + 3) * 1];
  111. Palm palm = videoHandle.getPalm(1);
  112. Vector ul = palm.getUpperLeft();
  113. Vector ur = palm.getUpperRight();
  114. Vector lr = palm.getLowerRight();
  115. Vector ll = palm.getLowerLeft();
  116. quadData[0] = ul.x();
  117. quadData[1] = ul.y();
  118. quadData[2] = -ul.z();
  119. quadData[3] = y.R / 255.0f;
  120. quadData[4] = y.G / 255.0f;
  121. quadData[5] = y.B / 255.0f;
  122. quadData[6] = 0.5f;
  123. quadData[7] = ur.x();
  124. quadData[8] = ur.y();
  125. quadData[9] = -ur.z();
  126. quadData[10] = y.R / 255.0f;
  127. quadData[11] = y.G / 255.0f;
  128. quadData[12] = y.B / 255.0f;
  129. quadData[13] = 0.5f;
  130. quadData[14] = lr.x();
  131. quadData[15] = lr.y();
  132. quadData[16] = -lr.z();
  133. quadData[17] = y.R / 255.0f;
  134. quadData[18] = y.G / 255.0f;
  135. quadData[19] = y.B / 255.0f;
  136. quadData[20] = 0.5f;
  137. quadData[21] = ll.x();
  138. quadData[22] = ll.y();
  139. quadData[23] = -ll.z();
  140. quadData[24] = y.R / 255.0f;
  141. quadData[25] = y.G / 255.0f;
  142. quadData[26] = y.B / 255.0f;
  143. quadData[27] = 0.5f;
  144. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  145. GL.BindBuffer(BufferTarget.ArrayBuffer, quadBufferId);
  146. GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(quadData.Length * sizeof(float)), quadData, BufferUsageHint.DynamicDraw);
  147. GL.VertexPointer(3, VertexPointerType.Float, (4 + 3) * sizeof(float), IntPtr.Zero);
  148. GL.ColorPointer(4, ColorPointerType.Float, (4 + 3) * sizeof(float), 3 * sizeof(float));
  149. GL.DrawArrays(PrimitiveType.Quads, 0, quadData.Length / (4 + 3));
  150. sw.Stop();
  151. Console.WriteLine(sw.ElapsedMilliseconds);
  152. SwapBuffers();
  153. }
  154. protected override void OnResize(EventArgs e)
  155. {
  156. base.OnResize(e);
  157. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  158. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
  159. GL.MatrixMode(MatrixMode.Projection);
  160. GL.LoadMatrix(ref projection);
  161. }
  162. private void initBuffers()
  163. {
  164. GL.GenBuffers(1, out imageBufferId);
  165. GL.GenBuffers(1, out pointBufferId);
  166. GL.GenBuffers(1, out quadBufferId);
  167. GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId);
  168. GL.BufferData(BufferTarget.ArrayBuffer,
  169. (IntPtr)((3 * sizeof(float) + 4 * sizeof(byte)) * videoHandle.getWidth() * videoHandle.getHeight()),
  170. IntPtr.Zero, BufferUsageHint.StreamDraw);
  171. }
  172. }
  173. }