Output.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. public Output(IVideoDataSource source)
  17. {
  18. this.source = source;
  19. }
  20. protected override void OnLoad(EventArgs e)
  21. {
  22. base.OnLoad(e);
  23. Title = "OutputTest";
  24. GL.ClearColor(Color.Black);
  25. }
  26. protected override void OnRenderFrame(FrameEventArgs e)
  27. {
  28. base.OnRenderFrame(e);
  29. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  30. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  31. GL.MatrixMode(MatrixMode.Modelview);
  32. GL.LoadMatrix(ref modelview);
  33. source.updateFrame();
  34. ImageData image = source.getImageData();
  35. int width = image.getWidth();
  36. int height = image.getHeight();
  37. for (int x = 0; x < width; x++) {
  38. for (int y = 0; y < height; y++) {
  39. if (image.getConfidence(x, y) > 100) {
  40. int depth = image.getDepth(x, y);
  41. Vertex vertex = new Vertex(x-width/2, height/2-y, depth);
  42. Color color = image.getColor(x, y);
  43. float size = 0.5f;
  44. Point pixel = new Point(vertex, color, size);
  45. pixel.draw();
  46. }
  47. }
  48. }
  49. Vector palmPosition2D = source.getPalmPosition2D(1);
  50. int palmX = (int) palmPosition2D[0];
  51. int palmY = (int) palmPosition2D[1];
  52. int palmDepth = image.getDepth(palmX, palmY);
  53. Vertex palmVertex = new Vertex(palmX - width / 2, height / 2 - palmY, palmDepth);
  54. Color palmColor = Color.Yellow;
  55. float palmSize = 5.0f;
  56. Point palmPixel = new Point(palmVertex, palmColor, palmSize);
  57. palmPixel.draw();
  58. SwapBuffers();
  59. source.releaseFrame();
  60. }
  61. protected override void OnResize(EventArgs e)
  62. {
  63. base.OnResize(e);
  64. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  65. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 3000.0f);
  66. GL.MatrixMode(MatrixMode.Projection);
  67. GL.LoadMatrix(ref projection);
  68. }
  69. }
  70. }