OutputWindow.cs 11 KB

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