Output.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 uint imageBufferId, pointBufferId;
  19. public Output(IInputProvider inputProvider, IVideoHandle videoHandle)
  20. {
  21. this.inputProvider = inputProvider;
  22. this.videoHandle = videoHandle;
  23. }
  24. protected override void OnLoad(EventArgs e)
  25. {
  26. base.OnLoad(e);
  27. Title = "OutputTest";
  28. GL.ClearColor(Color.Black);
  29. initBuffers();
  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(3.0f);
  49. GL.DrawArrays(PrimitiveType.Points, 0, videoHandle.getWidth() * videoHandle.getHeight());
  50. // draw points
  51. float[] pointData;
  52. DetectionStatus[] fingerStatus = inputProvider.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) * (1 + numFingersDetected)];
  60. Color y = Color.Yellow;
  61. Vector palmPosition = videoHandle.getPalmPosition3D(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. int index = 7;
  70. Vector[] fingerPositions = inputProvider.getFingerTipPositions3D(1);
  71. for (int i = 0; i < fingerStatus.Length; ++i)
  72. {
  73. if (fingerStatus[i] == DetectionStatus.Detected || fingerStatus[i] == DetectionStatus.Tracked)
  74. {
  75. pointData[index + 0] = fingerPositions[i][0];
  76. pointData[index + 1] = fingerPositions[i][1];
  77. pointData[index + 2] = -fingerPositions[i][2];
  78. pointData[index + 3] = y.R / 255.0f;
  79. pointData[index + 4] = y.G / 255.0f;
  80. pointData[index + 5] = y.B / 255.0f;
  81. pointData[index + 6] = y.A / 255.0f;
  82. index += 7;
  83. }
  84. }
  85. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  86. GL.BindBuffer(BufferTarget.ArrayBuffer, pointBufferId);
  87. GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(pointData.Length * sizeof(float)), pointData, BufferUsageHint.DynamicDraw);
  88. GL.VertexPointer(3, VertexPointerType.Float, (4 + 3) * sizeof(float), IntPtr.Zero);
  89. GL.ColorPointer(4, ColorPointerType.Float, (4 + 3) * sizeof(float), 3 * sizeof(float));
  90. GL.PointSize(10f);
  91. GL.DrawArrays(PrimitiveType.Points, 0, pointData.Length / (4 + 3));
  92. sw.Stop();
  93. Console.WriteLine(sw.ElapsedMilliseconds);
  94. SwapBuffers();
  95. }
  96. protected override void OnResize(EventArgs e)
  97. {
  98. base.OnResize(e);
  99. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  100. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
  101. GL.MatrixMode(MatrixMode.Projection);
  102. GL.LoadMatrix(ref projection);
  103. }
  104. private void initBuffers()
  105. {
  106. GL.GenBuffers(1, out imageBufferId);
  107. GL.GenBuffers(1, out pointBufferId);
  108. GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId);
  109. GL.BufferData(BufferTarget.ArrayBuffer,
  110. (IntPtr)((3 * sizeof(float) + 4 * sizeof(byte)) * videoHandle.getWidth() * videoHandle.getHeight()),
  111. IntPtr.Zero, BufferUsageHint.StreamDraw);
  112. }
  113. }
  114. }