Output.cs 6.6 KB

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