OutputWindow.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using OpenTK;
  9. using OpenTK.Graphics.OpenGL;
  10. namespace bbiwarg.Graphics
  11. {
  12. class OutputWindow : GameWindow
  13. {
  14. private VideoHandle videoHandle;
  15. private uint depthTextureID;
  16. private uint edgeTextureID;
  17. public OutputWindow(VideoHandle videoHandle): base(3 * videoHandle.getWidth(), 2 * videoHandle.getHeight())
  18. {
  19. this.videoHandle = videoHandle;
  20. }
  21. protected override void OnLoad(EventArgs e)
  22. {
  23. base.OnLoad(e);
  24. Title = "BBIWARG - Output";
  25. GL.ClearColor(Color.Black);
  26. // transparency
  27. GL.Enable(EnableCap.Blend);
  28. GL.BlendEquation(BlendEquationMode.Max);
  29. //Depth Test
  30. GL.Enable(EnableCap.DepthTest);
  31. // Textures
  32. GL.Enable(EnableCap.Texture2D);
  33. //depthTexture
  34. GL.GenTextures(1, out depthTextureID);
  35. GL.BindTexture(TextureTarget.Texture2D, depthTextureID);
  36. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  37. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  38. //edgetexture
  39. GL.GenTextures(1, out edgeTextureID);
  40. GL.BindTexture(TextureTarget.Texture2D, edgeTextureID);
  41. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  42. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  43. }
  44. protected override void OnResize(EventArgs e)
  45. {
  46. base.OnResize(e);
  47. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  48. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
  49. GL.MatrixMode(MatrixMode.Projection);
  50. GL.LoadMatrix(ref projection);
  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. Stopwatch sw = new Stopwatch();
  60. sw.Start();
  61. videoHandle.nextFrame();
  62. sw.Stop();
  63. Console.WriteLine(sw.ElapsedMilliseconds);
  64. //draw textures
  65. DepthImage depthImage = videoHandle.getDepthImage();
  66. Int16 minDepth = 0;// depthImage.getMinDepth();
  67. Int16 maxDepth = 400;// depthImage.getMaxDerivative();
  68. Int64 minMaxInterval = (Int16) Math.Max(maxDepth - minDepth, 1);
  69. Int16[] depthTextureData = new Int16[3 * depthImage.getWidth() * depthImage.getHeight()];
  70. Int16[] edgeTextureData = new Int16[3 * depthImage.getWidth() * depthImage.getHeight()];
  71. int index = 0;
  72. for (int y = 0; y < depthImage.getHeight(); ++y)
  73. {
  74. for (int x = 0; x < depthImage.getWidth(); ++x)
  75. {
  76. //depthTexture
  77. Int16 depth = Math.Min(depthImage.getDepthAt(x, y), maxDepth);
  78. Int16 scaledDepth = (Int16)(Int16.MaxValue - (((int)depth - minDepth) * (int)Int16.MaxValue / minMaxInterval));
  79. depthTextureData[index] = scaledDepth;
  80. depthTextureData[index + 1] = scaledDepth;
  81. depthTextureData[index + 2] = scaledDepth;
  82. //edgeTexture
  83. Int16 edge = depthImage.getEdgeAt(x, y);
  84. Int16 fingerPoint;
  85. if (depthImage.isFingerPoint(x, y)) fingerPoint = Int16.MaxValue;
  86. else fingerPoint = 0;
  87. edgeTextureData[index] = fingerPoint;
  88. edgeTextureData[index + 1] = fingerPoint;
  89. edgeTextureData[index + 2] = edge;
  90. index += 3;
  91. }
  92. }
  93. GL.Enable(EnableCap.Texture2D);
  94. GL.BindTexture(TextureTarget.Texture2D, depthTextureID);
  95. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0, PixelFormat.Rgb, PixelType.Short, depthTextureData);
  96. float size = 0.5f;
  97. float size_2 = (float) (size / 2.0f);
  98. GL.Begin(PrimitiveType.Quads);
  99. GL.Color3(1.0, 1.0, 1.0);
  100. GL.TexCoord2(0.0, 0.0); GL.Vertex3(0, size_2, -0.5);
  101. GL.TexCoord2(1.0, 0.0); GL.Vertex3(size, size_2, -0.5);
  102. GL.TexCoord2(1.0, 1.0); GL.Vertex3(size, -size_2, -0.5);
  103. GL.TexCoord2(0.0, 1.0); GL.Vertex3(0, -size_2, -0.5);
  104. GL.End();
  105. GL.Enable(EnableCap.Texture2D);
  106. GL.BindTexture(TextureTarget.Texture2D, edgeTextureID);
  107. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0, PixelFormat.Rgb, PixelType.Short, edgeTextureData);
  108. GL.Begin(PrimitiveType.Quads);
  109. GL.Color3(1.0, 1.0, 1.0);
  110. GL.TexCoord2(0.0, 0.0); GL.Vertex3(0, -size/2.0, -0.5);
  111. GL.TexCoord2(-1.0, 0.0); GL.Vertex3(-size, -size/2.0, -0.5);
  112. GL.TexCoord2(-1.0, -1.0); GL.Vertex3(-size, size/2.0, -0.5);
  113. GL.TexCoord2(0.0, -1.0); GL.Vertex3(0, size/2.0, -0.5);
  114. GL.End();
  115. SwapBuffers();
  116. }
  117. }
  118. }