Output2D.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.Graphics.GraphicElements2D;
  12. using bbiwarg.InputProviders;
  13. using bbiwarg.Images;
  14. using bbiwarg.Helpers;
  15. using bbiwarg.VideoHandles;
  16. namespace bbiwarg.Graphics
  17. {
  18. class Output2D: GameWindow
  19. {
  20. private VideoHandle videoHandle;
  21. private uint textureId;
  22. public Output2D(VideoHandle videoHandle): base(3 * 320, 3 * 240)
  23. {
  24. this.videoHandle = videoHandle;
  25. }
  26. protected override void OnLoad(EventArgs e)
  27. {
  28. base.OnLoad(e);
  29. Title = "OutputTest";
  30. GL.ClearColor(Color.Black);
  31. // transparency
  32. GL.Enable(EnableCap.Blend);
  33. GL.BlendEquation(BlendEquationMode.Max);
  34. //Depth Test
  35. GL.Enable(EnableCap.DepthTest);
  36. // Textures
  37. GL.Enable(EnableCap.Texture2D);
  38. GL.GenTextures(1, out textureId);
  39. GL.BindTexture(TextureTarget.Texture2D, textureId);
  40. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  41. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  42. }
  43. protected override void OnRenderFrame(FrameEventArgs e)
  44. {
  45. base.OnRenderFrame(e);
  46. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  47. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  48. GL.MatrixMode(MatrixMode.Modelview);
  49. GL.LoadMatrix(ref modelview);
  50. videoHandle.nextFrame();
  51. Stopwatch sw = new Stopwatch();
  52. sw.Start();
  53. short[] textureData = new short[3 * videoHandle.getWidth() * videoHandle.getHeight()];
  54. int index = 0;
  55. short minDepth = videoHandle.getHandImage().getMinDepth();
  56. short maxDepth = videoHandle.getHandImage().getMaxDepth();
  57. for (int y = 0; y < videoHandle.getHeight(); ++y)
  58. {
  59. for (int x = 0; x < videoHandle.getWidth(); ++x)
  60. {
  61. short depth = videoHandle.getHandImage().getDepth(x, y);
  62. short diff = (short) Math.Max(maxDepth - minDepth, 1);
  63. short scaledDepth = (short) (Int16.MaxValue - (((int) depth - minDepth) * (int) Int16.MaxValue / diff));
  64. textureData[index] = scaledDepth;
  65. textureData[index + 1] = scaledDepth;
  66. textureData[index + 2] = scaledDepth;
  67. index += 3;
  68. }
  69. }
  70. // draw histogram
  71. GL.Disable(EnableCap.Texture2D);
  72. int[] histogram = videoHandle.getSmoothedHistogram();
  73. int maxValue = 0;
  74. for (int i = 0; i < histogram.Length; ++i)
  75. {
  76. if (histogram[i] > maxValue)
  77. maxValue = histogram[i];
  78. }
  79. Int16[] segmentationDepth = videoHandle.getSegementationDepth();
  80. GL.Begin(PrimitiveType.LineStrip);
  81. GL.Color3(0, 0, 1.0);
  82. GL.LineWidth(5.0f);
  83. for (int i = 0; i < histogram.Length; ++i)
  84. {
  85. if (i > segmentationDepth[0]) GL.Color3(Color.Yellow);
  86. if (i > segmentationDepth[1]) GL.Color3(Color.Red);
  87. if (i > segmentationDepth[2]) GL.Color3(Color.Silver);
  88. GL.Vertex3(-0.25 + i * (0.5 / histogram.Length), -0.25 + histogram[i] * (0.5 / (maxValue - 5)), -0.5);
  89. }
  90. GL.End();
  91. // draw texture
  92. GL.Enable(EnableCap.Texture2D);
  93. GL.BindTexture(TextureTarget.Texture2D, textureId);
  94. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0,
  95. PixelFormat.Rgb, PixelType.Short, textureData);
  96. float size_2 = 0.5f / 2.0f;
  97. GL.Begin(PrimitiveType.Quads);
  98. GL.Color3(1.0, 1.0, 1.0);
  99. GL.TexCoord2(0.0, 0.0); GL.Vertex3(-size_2, size_2, -0.5);
  100. GL.TexCoord2(1.0, 0.0); GL.Vertex3( size_2, size_2, -0.5);
  101. GL.TexCoord2(1.0, 1.0); GL.Vertex3( size_2, -size_2, -0.5);
  102. GL.TexCoord2(0.0, 1.0); GL.Vertex3(-size_2, -size_2, -0.5);
  103. GL.End();
  104. sw.Stop();
  105. Console.WriteLine(sw.ElapsedMilliseconds);
  106. SwapBuffers();
  107. }
  108. protected override void OnResize(EventArgs e)
  109. {
  110. base.OnResize(e);
  111. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  112. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
  113. GL.MatrixMode(MatrixMode.Projection);
  114. GL.LoadMatrix(ref projection);
  115. }
  116. }
  117. }