OutputWindow.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. using bbiwarg.Images;
  11. namespace bbiwarg.Graphics
  12. {
  13. class OutputWindow : GameWindow
  14. {
  15. private VideoHandle videoHandle;
  16. private uint depthTextureID;
  17. private uint edgeTextureID;
  18. public OutputWindow(VideoHandle videoHandle): base(3 * videoHandle.getWidth(), 2 * videoHandle.getHeight())
  19. {
  20. this.videoHandle = videoHandle;
  21. }
  22. protected override void OnLoad(EventArgs e)
  23. {
  24. base.OnLoad(e);
  25. Title = "BBIWARG - Output";
  26. GL.ClearColor(Color.Black);
  27. // transparency
  28. GL.Enable(EnableCap.Blend);
  29. GL.BlendEquation(BlendEquationMode.Max);
  30. //Depth Test
  31. GL.Enable(EnableCap.DepthTest);
  32. // Textures
  33. GL.Enable(EnableCap.Texture2D);
  34. //depthTexture
  35. GL.GenTextures(1, out depthTextureID);
  36. GL.BindTexture(TextureTarget.Texture2D, depthTextureID);
  37. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  38. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  39. //edgetexture
  40. GL.GenTextures(1, out edgeTextureID);
  41. GL.BindTexture(TextureTarget.Texture2D, edgeTextureID);
  42. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  43. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  44. }
  45. protected override void OnResize(EventArgs e)
  46. {
  47. base.OnResize(e);
  48. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  49. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
  50. GL.MatrixMode(MatrixMode.Projection);
  51. GL.LoadMatrix(ref projection);
  52. }
  53. protected override void OnRenderFrame(FrameEventArgs e)
  54. {
  55. base.OnRenderFrame(e);
  56. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  57. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  58. GL.MatrixMode(MatrixMode.Modelview);
  59. GL.LoadMatrix(ref modelview);
  60. videoHandle.nextFrame();
  61. //draw textures
  62. Int16[] depthTextureData = new Int16[3 * videoHandle.getWidth() * videoHandle.getHeight()];
  63. Int16[] edgeTextureData = new Int16[3 * videoHandle.getWidth() * videoHandle.getHeight()];
  64. int index = 0;
  65. for (int y = 0; y < videoHandle.getHeight(); ++y)
  66. {
  67. for (int x = 0; x < videoHandle.getWidth(); ++x)
  68. {
  69. Int16 red = 0;
  70. Int16 green = 0;
  71. Int16 blue = 0;
  72. //depthTexture
  73. float relDepth = videoHandle.getRelativeDepth(x,y);
  74. red = green = blue = (Int16)((1.0f - videoHandle.getRelativeDepth(x, y)) * Int16.MaxValue);
  75. TouchImageState tis = videoHandle.getTouchImageStateAt(x,y);
  76. if (tis == TouchImageState.touchArea)
  77. {
  78. red = (Int16) (red/2);
  79. green = (Int16) (green / 2);
  80. blue = (Int16) (blue / 2);
  81. }
  82. else if (tis == TouchImageState.touchAreaMatched)
  83. {
  84. red = (Int16) (red / 2);
  85. green = (Int16)(green / 2);
  86. blue = Int16.MaxValue;
  87. }
  88. else if (tis == TouchImageState.touchAreaStatusBar)
  89. {
  90. red = (Int16)(red / 2);
  91. green = Int16.MaxValue;
  92. blue = (Int16)(blue / 2); ;
  93. }
  94. else if (tis == TouchImageState.touchDetected)
  95. {
  96. red = blue = 0;
  97. green = Int16.MaxValue;
  98. }
  99. else if (tis == TouchImageState.touchTracked)
  100. {
  101. red = Int16.MaxValue;
  102. green = blue = 0;
  103. }
  104. // show palm contour
  105. if (videoHandle.isPalmPointAt(x, y))
  106. {
  107. red = Int16.MaxValue;
  108. green = blue = 0;
  109. }
  110. depthTextureData[index] = red;
  111. depthTextureData[index + 1] = green;
  112. depthTextureData[index + 2] = blue;
  113. //edgeTexture
  114. FingerImageState fis = videoHandle.getFingerImageStateAt(x,y);
  115. red = green = blue = 0;
  116. if (videoHandle.isEdgeAt(x, y)) blue = Int16.MaxValue;
  117. else if (fis == FingerImageState.fingerTracked) green = Int16.MaxValue;
  118. else if (fis == FingerImageState.fingerDetected) red = Int16.MaxValue;
  119. else if (fis == FingerImageState.fingerSlice) red = blue = Int16.MaxValue;
  120. else if (fis == FingerImageState.possibleFingerSlice) red = Int16.MaxValue;
  121. edgeTextureData[index] = red;
  122. edgeTextureData[index + 1] = green;
  123. edgeTextureData[index + 2] = blue;
  124. index += 3;
  125. }
  126. }
  127. GL.Enable(EnableCap.Texture2D);
  128. GL.BindTexture(TextureTarget.Texture2D, depthTextureID);
  129. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0, PixelFormat.Rgb, PixelType.Short, depthTextureData);
  130. float size = 0.5f;
  131. float size_2 = (float) (size / 2.0f);
  132. GL.Begin(PrimitiveType.Quads);
  133. GL.Color3(1.0, 1.0, 1.0);
  134. GL.TexCoord2(0.0, 0.0); GL.Vertex3(0, size_2, -0.5);
  135. GL.TexCoord2(1.0, 0.0); GL.Vertex3(size, size_2, -0.5);
  136. GL.TexCoord2(1.0, 1.0); GL.Vertex3(size, -size_2, -0.5);
  137. GL.TexCoord2(0.0, 1.0); GL.Vertex3(0, -size_2, -0.5);
  138. GL.End();
  139. GL.Enable(EnableCap.Texture2D);
  140. GL.BindTexture(TextureTarget.Texture2D, edgeTextureID);
  141. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0, PixelFormat.Rgb, PixelType.Short, edgeTextureData);
  142. GL.Begin(PrimitiveType.Quads);
  143. GL.Color3(1.0, 1.0, 1.0);
  144. GL.TexCoord2(0.0, 0.0); GL.Vertex3(0, -size/2.0, -0.5);
  145. GL.TexCoord2(-1.0, 0.0); GL.Vertex3(-size, -size/2.0, -0.5);
  146. GL.TexCoord2(-1.0, -1.0); GL.Vertex3(-size, size/2.0, -0.5);
  147. GL.TexCoord2(0.0, -1.0); GL.Vertex3(0, size/2.0, -0.5);
  148. GL.End();
  149. SwapBuffers();
  150. }
  151. }
  152. }