Output.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. 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(3.0f);
  51. GL.DrawArrays(PrimitiveType.Points, 0, videoHandle.getWidth() * videoHandle.getHeight());
  52. // draw points
  53. float[] pointData;
  54. DetectionStatus[] fingerStatus = inputProvider.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) * (2 + numFingersDetected)];
  62. Color y = Color.Yellow;
  63. Vector palmPosition = videoHandle.getPalmPosition3D(1);
  64. Vector foreFingerPosition = videoHandle.pixel2VertexPosition(foreFingerDetection.getForeFingerPosition3D(1));
  65. pointData[0] = palmPosition[0];
  66. pointData[1] = palmPosition[1];
  67. pointData[2] = -palmPosition[2];
  68. pointData[3] = y.R / 255.0f;
  69. pointData[4] = y.G / 255.0f;
  70. pointData[5] = y.B / 255.0f;
  71. pointData[6] = y.A / 255.0f;
  72. int index = 7;
  73. Vector[] fingerPositions = inputProvider.getFingerTipPositions3D(1);
  74. for (int i = 0; i < fingerStatus.Length; ++i)
  75. {
  76. if (fingerStatus[i] == DetectionStatus.Detected || fingerStatus[i] == DetectionStatus.Tracked)
  77. {
  78. pointData[index + 0] = fingerPositions[i][0];
  79. pointData[index + 1] = fingerPositions[i][1];
  80. pointData[index + 2] = -fingerPositions[i][2];
  81. pointData[index + 3] = y.R / 255.0f;
  82. pointData[index + 4] = y.G / 255.0f;
  83. pointData[index + 5] = y.B / 255.0f;
  84. pointData[index + 6] = y.A / 255.0f;
  85. index += 7;
  86. }
  87. }
  88. pointData[index + 0] = foreFingerPosition[0];
  89. pointData[index + 1] = foreFingerPosition[1];
  90. pointData[index + 2] = foreFingerPosition[2];
  91. pointData[index + 3] = Color.Red.R / 255.0f;
  92. pointData[index + 4] = Color.Red.G / 255.0f;
  93. pointData[index + 5] = Color.Red.B / 255.0f;
  94. pointData[index + 6] = Color.Red.A / 255.0f;
  95. index += 7;
  96. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  97. GL.BindBuffer(BufferTarget.ArrayBuffer, pointBufferId);
  98. GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(pointData.Length * sizeof(float)), pointData, BufferUsageHint.DynamicDraw);
  99. GL.VertexPointer(3, VertexPointerType.Float, (4 + 3) * sizeof(float), IntPtr.Zero);
  100. GL.ColorPointer(4, ColorPointerType.Float, (4 + 3) * sizeof(float), 3 * sizeof(float));
  101. GL.PointSize(10f);
  102. GL.DrawArrays(PrimitiveType.Points, 0, pointData.Length / (4 + 3));
  103. sw.Stop();
  104. Console.WriteLine(sw.ElapsedMilliseconds);
  105. SwapBuffers();
  106. }
  107. protected override void OnResize(EventArgs e)
  108. {
  109. base.OnResize(e);
  110. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  111. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
  112. GL.MatrixMode(MatrixMode.Projection);
  113. GL.LoadMatrix(ref projection);
  114. }
  115. private void initBuffers()
  116. {
  117. GL.GenBuffers(1, out imageBufferId);
  118. GL.GenBuffers(1, out pointBufferId);
  119. GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId);
  120. GL.BufferData(BufferTarget.ArrayBuffer,
  121. (IntPtr)((3 * sizeof(float) + 4 * sizeof(byte)) * videoHandle.getWidth() * videoHandle.getHeight()),
  122. IntPtr.Zero, BufferUsageHint.StreamDraw);
  123. }
  124. }
  125. }