OutputWindow.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. using bbiwarg.Detectors.Touch;
  12. namespace bbiwarg.Graphics
  13. {
  14. class OutputWindow : GameWindow
  15. {
  16. private VideoHandle videoHandle;
  17. private uint depthTextureID;
  18. private uint edgeTextureID;
  19. private bool paused = false;
  20. private long timeSpacePressed, timeLeftPressed, timeRightPressed;
  21. private Stopwatch watch;
  22. public OutputWindow(VideoHandle videoHandle)
  23. : base(3 * videoHandle.getWidth(), 2 * videoHandle.getHeight())
  24. {
  25. this.videoHandle = videoHandle;
  26. watch = new Stopwatch();
  27. watch.Start();
  28. }
  29. protected override void OnLoad(EventArgs e)
  30. {
  31. base.OnLoad(e);
  32. Title = "BBIWARG - Output";
  33. GL.ClearColor(Color.Black);
  34. // transparency
  35. GL.Enable(EnableCap.Blend);
  36. GL.BlendEquation(BlendEquationMode.Max);
  37. //Depth Test
  38. GL.Enable(EnableCap.DepthTest);
  39. // Textures
  40. GL.Enable(EnableCap.Texture2D);
  41. //depthTexture
  42. GL.GenTextures(1, out depthTextureID);
  43. GL.BindTexture(TextureTarget.Texture2D, depthTextureID);
  44. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  45. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  46. //edgetexture
  47. GL.GenTextures(1, out edgeTextureID);
  48. GL.BindTexture(TextureTarget.Texture2D, edgeTextureID);
  49. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  50. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  51. }
  52. protected override void OnResize(EventArgs e)
  53. {
  54. base.OnResize(e);
  55. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  56. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
  57. GL.MatrixMode(MatrixMode.Projection);
  58. GL.LoadMatrix(ref projection);
  59. }
  60. protected override void OnRenderFrame(FrameEventArgs e)
  61. {
  62. base.OnRenderFrame(e);
  63. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  64. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  65. GL.MatrixMode(MatrixMode.Modelview);
  66. GL.LoadMatrix(ref modelview);
  67. bool changedFrame = false;
  68. const int auotRepeatDelay = 100; // ms
  69. if (videoHandle.sourceIsMovie())
  70. {
  71. // pause and unpause with space
  72. long elapsed = watch.ElapsedMilliseconds;
  73. if (OpenTK.Input.Keyboard.GetState().IsKeyDown(OpenTK.Input.Key.Space) && (elapsed - timeSpacePressed) >= auotRepeatDelay)
  74. {
  75. timeSpacePressed = elapsed;
  76. if (paused)
  77. videoHandle.unpauseMovie();
  78. else
  79. videoHandle.pauseMovie();
  80. paused = !paused;
  81. }
  82. // when paused go to next / previous frame with right / left keys
  83. if (paused)
  84. {
  85. if (OpenTK.Input.Keyboard.GetState().IsKeyDown(OpenTK.Input.Key.Right) && (elapsed - timeRightPressed) >= auotRepeatDelay)
  86. {
  87. timeRightPressed = elapsed;
  88. videoHandle.unpauseMovie();
  89. videoHandle.nextFrame();
  90. videoHandle.pauseMovie();
  91. changedFrame = true;
  92. }
  93. else if (OpenTK.Input.Keyboard.GetState().IsKeyDown(OpenTK.Input.Key.Left) && (elapsed - timeLeftPressed) >= auotRepeatDelay)
  94. {
  95. timeLeftPressed = elapsed;
  96. videoHandle.unpauseMovie();
  97. videoHandle.reversePlay();
  98. videoHandle.nextFrame();
  99. videoHandle.reversePlay();
  100. videoHandle.pauseMovie();
  101. changedFrame = true;
  102. }
  103. }
  104. else
  105. {
  106. videoHandle.nextFrame();
  107. changedFrame = true;
  108. }
  109. }
  110. else
  111. {
  112. videoHandle.nextFrame();
  113. }
  114. //draw textures
  115. Int16[] depthTextureData = new Int16[3 * videoHandle.getWidth() * videoHandle.getHeight()];
  116. Int16[] edgeTextureData = new Int16[3 * videoHandle.getWidth() * videoHandle.getHeight()];
  117. int index = 0;
  118. for (int y = 0; y < videoHandle.getHeight(); ++y)
  119. {
  120. for (int x = 0; x < videoHandle.getWidth(); ++x)
  121. {
  122. Int16 red = 0;
  123. Int16 green = 0;
  124. Int16 blue = 0;
  125. //depthTexture
  126. float relDepth = videoHandle.getRelativeDepth(x, y);
  127. red = green = blue = (Int16)((1.0f - videoHandle.getRelativeDepth(x, y)) * Int16.MaxValue);
  128. // palm
  129. switch (videoHandle.getPalmImageStateAt(x, y))
  130. {
  131. case PalmImageState.palmContour:
  132. red = Int16.MaxValue;
  133. blue = green = 0;
  134. break;
  135. case PalmImageState.palmRect:
  136. blue = Int16.MaxValue;
  137. red = green = 0;
  138. break;
  139. case PalmImageState.thumbLine:
  140. case PalmImageState.wristLine:
  141. green = Int16.MaxValue;
  142. blue = red = 0;
  143. break;
  144. case PalmImageState.palmGrid:
  145. green = (Int16)(green / 2 + Int16.MaxValue / 2);
  146. blue = (Int16)(blue / 2 + Int16.MaxValue / 2);
  147. red = (Int16)(red / 2 + Int16.MaxValue / 2);
  148. break;
  149. }
  150. // touch
  151. switch (videoHandle.getTouchImageStateAt(x, y))
  152. {
  153. case TouchImageState.touchArea:
  154. red = (Int16)(red / 2);
  155. green = (Int16)(green / 2);
  156. blue = (Int16)(blue / 2);
  157. break;
  158. case TouchImageState.touchAreaMatched:
  159. red = (Int16)(red / 2);
  160. green = (Int16)(green / 2);
  161. blue = Int16.MaxValue;
  162. break;
  163. case TouchImageState.touchAreaStatusBar:
  164. red = (Int16)(red / 2);
  165. green = Int16.MaxValue;
  166. blue = (Int16)(blue / 2);
  167. break;
  168. case TouchImageState.touchDetected:
  169. red = (Int16)(red / 2);
  170. blue = (Int16)(blue / 2);
  171. green = (Int16)(green / 2 + Int16.MaxValue / 2);
  172. break;
  173. case TouchImageState.touchTracked:
  174. red = (Int16)(red / 2 + Int16.MaxValue / 2);
  175. green = (Int16)(green / 2);
  176. blue = (Int16)(blue / 2);
  177. break;
  178. }
  179. depthTextureData[index] = red;
  180. depthTextureData[index + 1] = green;
  181. depthTextureData[index + 2] = blue;
  182. //edgeTexture
  183. FingerImageState fis = videoHandle.getFingerImageStateAt(x, y);
  184. red = green = blue = 0;
  185. if (videoHandle.isEdgeAt(x, y)) blue = Int16.MaxValue;
  186. else if (fis == FingerImageState.fingerTracked) green = Int16.MaxValue;
  187. else if (fis == FingerImageState.fingerDetected) red = Int16.MaxValue;
  188. else if (fis == FingerImageState.fingerSlice) red = blue = Int16.MaxValue;
  189. else if (fis == FingerImageState.possibleFingerSlice) red = Int16.MaxValue;
  190. edgeTextureData[index] = red;
  191. edgeTextureData[index + 1] = green;
  192. edgeTextureData[index + 2] = blue;
  193. index += 3;
  194. }
  195. }
  196. GL.Enable(EnableCap.Texture2D);
  197. GL.BindTexture(TextureTarget.Texture2D, depthTextureID);
  198. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0, PixelFormat.Rgb, PixelType.Short, depthTextureData);
  199. float size = 0.5f;
  200. float size_2 = (float)(size / 2.0f);
  201. GL.Begin(PrimitiveType.Quads);
  202. GL.Color3(1.0, 1.0, 1.0);
  203. GL.TexCoord2(0.0, 0.0); GL.Vertex3(0, size_2, -0.5);
  204. GL.TexCoord2(1.0, 0.0); GL.Vertex3(size, size_2, -0.5);
  205. GL.TexCoord2(1.0, 1.0); GL.Vertex3(size, -size_2, -0.5);
  206. GL.TexCoord2(0.0, 1.0); GL.Vertex3(0, -size_2, -0.5);
  207. GL.End();
  208. GL.Enable(EnableCap.Texture2D);
  209. GL.BindTexture(TextureTarget.Texture2D, edgeTextureID);
  210. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0, PixelFormat.Rgb, PixelType.Short, edgeTextureData);
  211. GL.Begin(PrimitiveType.Quads);
  212. GL.Color3(1.0, 1.0, 1.0);
  213. GL.TexCoord2(0.0, 0.0); GL.Vertex3(0, -size / 2.0, -0.5);
  214. GL.TexCoord2(-1.0, 0.0); GL.Vertex3(-size, -size / 2.0, -0.5);
  215. GL.TexCoord2(-1.0, -1.0); GL.Vertex3(-size, size / 2.0, -0.5);
  216. GL.TexCoord2(0.0, -1.0); GL.Vertex3(0, size / 2.0, -0.5);
  217. GL.End();
  218. SwapBuffers();
  219. }
  220. }
  221. }