Output.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using OpenTK;
  8. using OpenTK.Graphics.OpenGL;
  9. using MathNet.Numerics.LinearAlgebra.Single;
  10. using bbiwarg.DataSource;
  11. namespace bbiwarg.Graphics
  12. {
  13. class Output : GameWindow
  14. {
  15. private IVideoDataSource source;
  16. private Point[,] depthPixels = new Point[320,240];
  17. private List<IGraphicElement> graphicElements = new List<IGraphicElement>();
  18. private Point palmPixel;
  19. private Point[] fingerPixel = new Point[5];
  20. public Output(IVideoDataSource source)
  21. {
  22. this.source = source;
  23. }
  24. protected override void OnLoad(EventArgs e)
  25. {
  26. base.OnLoad(e);
  27. Title = "OutputTest";
  28. GL.ClearColor(Color.Black);
  29. for (int x = 0; x < 320; x++)
  30. {
  31. for (int y = 0; y < 240; y++)
  32. {
  33. Vertex vertex = new Vertex(x,y, 0.0f);
  34. Color color = Color.White;
  35. float size = 0.5f;
  36. Point pixel = new Point(vertex, color, size);
  37. depthPixels[x, y] = pixel;
  38. graphicElements.Add(pixel);
  39. }
  40. }
  41. Vertex palmVertex = new Vertex(0.0f, 0.0f, 0.0f);
  42. Color palmColor = Color.Yellow;
  43. float palmSize = 5.0f;
  44. palmPixel = new Point(palmVertex, palmColor, palmSize);
  45. graphicElements.Add(palmPixel);
  46. for (int i = 0; i < 5; i++) {
  47. Vertex vertex = new Vertex(0.0f, 0.0f, 0.0f);
  48. fingerPixel[i] = new Point(vertex, palmColor, palmSize);
  49. graphicElements.Add(fingerPixel[i]);
  50. }
  51. }
  52. protected override void OnRenderFrame(FrameEventArgs e)
  53. {
  54. base.OnRenderFrame(e);
  55. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  56. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  57. GL.MatrixMode(MatrixMode.Modelview);
  58. GL.LoadMatrix(ref modelview);
  59. source.updateFrame();
  60. ImageData image = source.getImageData();
  61. int width = image.getWidth();
  62. int height = image.getHeight();
  63. for (int x = 0; x < width; x++) {
  64. for (int y = 0; y < height; y++) {
  65. Point depthPixel = depthPixels[x, y];
  66. depthPixel.position.z = image.getDepth(x, y);
  67. depthPixel.color = image.getColor(x, y);
  68. }
  69. }
  70. Vector palmPosition2D = source.getPalmPosition2D(1);
  71. palmPixel.position.x = palmPosition2D[0];
  72. palmPixel.position.y = palmPosition2D[1];
  73. palmPixel.position.z = image.getDepth((int)palmPosition2D[0], (int)palmPosition2D[1]);
  74. for (int i = 0; i < 5; i++) {
  75. DetectionStatus[] fingerStatus = source.getFingerStatus(1);
  76. Vector[] fingerPositions = source.getFingerTipPositions2D(1);
  77. if (fingerStatus[i] == DetectionStatus.Tracked)
  78. {
  79. fingerPixel[i].position.x = fingerPositions[i][0];
  80. fingerPixel[i].position.y = fingerPositions[i][1];
  81. fingerPixel[i].position.z = image.getDepth((int)fingerPositions[i][0], (int)fingerPositions[i][1]);
  82. }
  83. else
  84. {
  85. fingerPixel[i].position.x = 0;
  86. fingerPixel[i].position.y = 0;
  87. fingerPixel[i].position.z = 0;
  88. }
  89. }
  90. foreach (IGraphicElement graphicElement in graphicElements)
  91. {
  92. graphicElement.draw(width, height);
  93. }
  94. SwapBuffers();
  95. source.releaseFrame();
  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 / 4, Width / (float)Height, 1.0f, 3000.0f);
  102. GL.MatrixMode(MatrixMode.Projection);
  103. GL.LoadMatrix(ref projection);
  104. }
  105. }
  106. }