Output.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 IVideoDataSource source;
  17. private uint imageBufferId, pointBufferId;
  18. public Output(IVideoDataSource source)
  19. {
  20. this.source = source;
  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. source.releaseFrame();
  37. source.updateFrame();
  38. Stopwatch sw = new Stopwatch();
  39. sw.Start();
  40. GL.EnableClientState(ArrayCap.VertexArray);
  41. GL.EnableClientState(ArrayCap.ColorArray);
  42. GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId);
  43. source.setVertexBuffer(GL.MapBuffer(BufferTarget.ArrayBuffer, BufferAccess.WriteOnly));
  44. source.createVertexArray();
  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. ImageData data = source.getImageData();
  49. GL.PointSize(3.0f);
  50. GL.DrawArrays(PrimitiveType.Points, 0, data.getHeight() * data.getWidth());
  51. // draw points
  52. float[] pointData;
  53. DetectionStatus[] fingerStatus = source.getFingerStatus(1);
  54. int numFingersDetected = 0;
  55. for (int i = 0; i < fingerStatus.Length; ++i)
  56. {
  57. if (fingerStatus[i] == DetectionStatus.Detected || fingerStatus[i] == DetectionStatus.Tracked)
  58. ++numFingersDetected;
  59. }
  60. pointData = new float[(4 + 3) * (1 + numFingersDetected)];
  61. Color y = Color.Yellow;
  62. Vector palmPosition = source.getPalmPosition3D(1);
  63. pointData[0] = palmPosition[0];
  64. pointData[1] = palmPosition[2];
  65. pointData[2] = -palmPosition[1];
  66. pointData[3] = y.R / 255.0f;
  67. pointData[4] = y.G / 255.0f;
  68. pointData[5] = y.B / 255.0f;
  69. pointData[6] = y.A / 255.0f;
  70. int index = 7;
  71. Vector[] fingerPositions = source.getFingerTipPositions3D(1);
  72. for (int i = 0; i < fingerStatus.Length; ++i)
  73. {
  74. if (fingerStatus[i] == DetectionStatus.Detected || fingerStatus[i] == DetectionStatus.Tracked)
  75. {
  76. pointData[index + 0] = fingerPositions[i][0];
  77. pointData[index + 1] = fingerPositions[i][2];
  78. pointData[index + 2] = -fingerPositions[i][1];
  79. pointData[index + 3] = y.R / 255.0f;
  80. pointData[index + 4] = y.G / 255.0f;
  81. pointData[index + 5] = y.B / 255.0f;
  82. pointData[index + 6] = y.A / 255.0f;
  83. index += 7;
  84. }
  85. }
  86. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  87. GL.BindBuffer(BufferTarget.ArrayBuffer, pointBufferId);
  88. GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(pointData.Length * sizeof(float)), pointData, BufferUsageHint.DynamicDraw);
  89. GL.VertexPointer(3, VertexPointerType.Float, (4 + 3) * sizeof(float), IntPtr.Zero);
  90. GL.ColorPointer(4, ColorPointerType.Float, (4 + 3) * sizeof(float), 3 * sizeof(float));
  91. GL.PointSize(10f);
  92. GL.DrawArrays(PrimitiveType.Points, 0, pointData.Length / (4 + 3));
  93. sw.Stop();
  94. Console.WriteLine(sw.ElapsedMilliseconds);
  95. SwapBuffers();
  96. }
  97. protected override void OnResize(EventArgs e)
  98. {
  99. base.OnResize(e);
  100. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  101. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
  102. GL.MatrixMode(MatrixMode.Projection);
  103. GL.LoadMatrix(ref projection);
  104. }
  105. private void initBuffers()
  106. {
  107. GL.GenBuffers(1, out imageBufferId);
  108. GL.GenBuffers(1, out pointBufferId);
  109. ImageData data = source.getImageData();
  110. GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId);
  111. GL.BufferData(BufferTarget.ArrayBuffer,
  112. (IntPtr)((3 * sizeof(float) + 4 * sizeof(byte)) * data.getWidth() * data.getHeight()),
  113. IntPtr.Zero, BufferUsageHint.StreamDraw);
  114. }
  115. }
  116. }