Output.cs 3.3 KB

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