Output2D.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using OpenTK;
  7. using OpenTK.Graphics.OpenGL;
  8. using System.Drawing;
  9. using System.Diagnostics;
  10. using MathNet.Numerics.LinearAlgebra.Single;
  11. using bbiwarg.DataSource;
  12. using bbiwarg.Graphics.GraphicElements2D;
  13. namespace bbiwarg.Graphics
  14. {
  15. class Output2D: GameWindow
  16. {
  17. private IInputProvider input;
  18. private uint textureId;
  19. public Output2D(IInputProvider input): base(3 * 320, 3 * 240)
  20. {
  21. this.input = input;
  22. }
  23. protected override void OnLoad(EventArgs e)
  24. {
  25. base.OnLoad(e);
  26. Title = "OutputTest";
  27. GL.ClearColor(Color.Black);
  28. // transparency
  29. GL.Enable(EnableCap.Blend);
  30. GL.BlendEquation(BlendEquationMode.Max);
  31. //Depth Test
  32. GL.Enable(EnableCap.DepthTest);
  33. // Textures
  34. GL.Enable(EnableCap.Texture2D);
  35. GL.GenTextures(1, out textureId);
  36. GL.BindTexture(TextureTarget.Texture2D, textureId);
  37. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  38. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  39. }
  40. protected override void OnRenderFrame(FrameEventArgs e)
  41. {
  42. base.OnRenderFrame(e);
  43. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  44. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  45. GL.MatrixMode(MatrixMode.Modelview);
  46. GL.LoadMatrix(ref modelview);
  47. input.releaseFrame();
  48. input.updateFrame();
  49. Stopwatch sw = new Stopwatch();
  50. sw.Start();
  51. DepthImage depthImage = input.getDepthImage();
  52. depthImage.filterMedian(3);
  53. Vector palmOrigin = input.getPalmPosition2D(1);
  54. int palmX = (int) palmOrigin.x();
  55. int palmY = (int) palmOrigin.y();
  56. int palmDepth = depthImage.getDepth(palmX, palmY);
  57. int maxHandSize = 150; // mm Wieviel Pixel sind das?
  58. List<Vector> points = new List<Vector>();
  59. if (palmX != 0 && palmY != 0)
  60. {
  61. depthImage.thresholdDepth(palmDepth - 10, palmDepth + 30);
  62. depthImage.thresholdPosition(palmX - maxHandSize / 2, palmX + maxHandSize / 2, palmY - maxHandSize / 2, palmY + maxHandSize / 2);
  63. points = depthImage.getRectPoints();
  64. }
  65. short[] textureData = new short[3 * depthImage.getWidth() * depthImage.getHeight()];
  66. int index = 0;
  67. for (int y = 0; y < depthImage.getHeight(); ++y) {
  68. for (int x = 0; x < depthImage.getWidth(); ++x) {
  69. // 0 --> 0 / 2000 -> Int16.MaxValue
  70. int d = depthImage.getDepth(x, y);
  71. short depth = (short) (d * Int16.MaxValue / 2000);
  72. textureData[index] = textureData[index + 1] = textureData[index + 2] = depth;
  73. index += 3;
  74. }
  75. }
  76. // draw palm origin
  77. Point2D palmPoint = new Point2D(palmOrigin, Color.Yellow);
  78. palmPoint.draw(textureData, depthImage.getWidth());
  79. // draw rect points
  80. foreach (Vector v in points)
  81. {
  82. int x = (int)v.x() % depthImage.getWidth();
  83. int y = (int)v.y() % depthImage.getHeight();
  84. for (int i = -1; i < 2; ++i)
  85. {
  86. for (int j = -1; j < 2; ++j)
  87. {
  88. int px = Math.Min(depthImage.getWidth() - 1, Math.Max(0, x + i));
  89. int py = Math.Min(depthImage.getHeight() - 1, Math.Max(0, y + j));
  90. index = 3 * (py * depthImage.getWidth() + px);
  91. textureData[index + 0] = 0;
  92. textureData[index + 1] = 0;
  93. textureData[index + 2] = Int16.MaxValue;
  94. }
  95. }
  96. }
  97. GL.BindTexture(TextureTarget.Texture2D, textureId);
  98. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, depthImage.getWidth(), depthImage.getHeight(), 0,
  99. PixelFormat.Rgb, PixelType.Short, textureData);
  100. float size_2 = 0.5f / 2.0f;
  101. GL.Begin(PrimitiveType.Quads);
  102. GL.TexCoord2(0.0, 0.0); GL.Vertex3(-size_2, size_2, -0.5);
  103. GL.TexCoord2(1.0, 0.0); GL.Vertex3( size_2, size_2, -0.5);
  104. GL.TexCoord2(1.0, 1.0); GL.Vertex3( size_2, -size_2, -0.5);
  105. GL.TexCoord2(0.0, 1.0); GL.Vertex3(-size_2, -size_2, -0.5);
  106. GL.End();
  107. sw.Stop();
  108. Console.WriteLine(sw.ElapsedMilliseconds);
  109. SwapBuffers();
  110. }
  111. protected override void OnResize(EventArgs e)
  112. {
  113. base.OnResize(e);
  114. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  115. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
  116. GL.MatrixMode(MatrixMode.Projection);
  117. GL.LoadMatrix(ref projection);
  118. }
  119. }
  120. }