OutputWindow.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. videoHandle.nextFrame();
  60. //draw textures
  61. DepthImage depthImage = videoHandle.getDepthImage();
  62. Int16 minDepth = 0;// depthImage.getMinDepth();
  63. Int16 maxDepth = 400;// depthImage.getMaxDerivative();
  64. Int64 minMaxInterval = (Int16) Math.Max(maxDepth - minDepth, 1);
  65. Int16[] depthTextureData = new Int16[3 * depthImage.getWidth() * depthImage.getHeight()];
  66. Int16[] edgeTextureData = new Int16[3 * depthImage.getWidth() * depthImage.getHeight()];
  67. int index = 0;
  68. for (int y = 0; y < depthImage.getHeight(); ++y)
  69. {
  70. for (int x = 0; x < depthImage.getWidth(); ++x)
  71. {
  72. //depthTexture
  73. Int16 depth = Math.Min(depthImage.getDepthAt(x, y), maxDepth);
  74. Int16 scaledDepth = (Int16)(Int16.MaxValue - (((int)depth - minDepth) * (int)Int16.MaxValue / minMaxInterval));
  75. depthTextureData[index] = scaledDepth;
  76. depthTextureData[index + 1] = scaledDepth;
  77. depthTextureData[index + 2] = scaledDepth;
  78. //edgeTexture
  79. Int16 edge = depthImage.getEdgeAt(x, y);
  80. if (depthImage.isFingerPoint(x, y)) edge = Int16.MaxValue / 2;
  81. edgeTextureData[index] = edge;
  82. edgeTextureData[index + 1] = edge;
  83. edgeTextureData[index + 2] = edge;
  84. index += 3;
  85. }
  86. }
  87. GL.Enable(EnableCap.Texture2D);
  88. GL.BindTexture(TextureTarget.Texture2D, depthTextureID);
  89. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0, PixelFormat.Rgb, PixelType.Short, depthTextureData);
  90. float size = 0.5f;
  91. float size_2 = (float) (size / 2.0f);
  92. GL.Begin(PrimitiveType.Quads);
  93. GL.Color3(1.0, 1.0, 1.0);
  94. GL.TexCoord2(0.0, 0.0); GL.Vertex3(0, size_2, -0.5);
  95. GL.TexCoord2(1.0, 0.0); GL.Vertex3(size, size_2, -0.5);
  96. GL.TexCoord2(1.0, 1.0); GL.Vertex3(size, -size_2, -0.5);
  97. GL.TexCoord2(0.0, 1.0); GL.Vertex3(0, -size_2, -0.5);
  98. GL.End();
  99. GL.Enable(EnableCap.Texture2D);
  100. GL.BindTexture(TextureTarget.Texture2D, edgeTextureID);
  101. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0, PixelFormat.Rgb, PixelType.Short, edgeTextureData);
  102. GL.Begin(PrimitiveType.Quads);
  103. GL.Color3(1.0, 1.0, 1.0);
  104. GL.TexCoord2(0.0, 0.0); GL.Vertex3(0, -size/2.0, -0.5);
  105. GL.TexCoord2(-1.0, 0.0); GL.Vertex3(-size, -size/2.0, -0.5);
  106. GL.TexCoord2(-1.0, -1.0); GL.Vertex3(-size, size/2.0, -0.5);
  107. GL.TexCoord2(0.0, -1.0); GL.Vertex3(0, size/2.0, -0.5);
  108. GL.End();
  109. SwapBuffers();
  110. }
  111. }
  112. }