OutputWindow.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 autoRepeatDelay = 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) >= autoRepeatDelay)
  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) >= autoRepeatDelay)
  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) >= autoRepeatDelay)
  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. if (changedFrame || !videoHandle.sourceIsMovie())
  115. {
  116. foreach (PalmTouchEvent ev in videoHandle.getTouchEvents())
  117. {
  118. Console.WriteLine("touch at " + ev.Position + " -> " + ev.RelativePalmPosition);
  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 = (Int16)(green / 2 + Int16.MaxValue / 2);
  153. blue = (Int16)(blue / 2 + Int16.MaxValue / 2);
  154. red = (Int16)(red / 2 + Int16.MaxValue / 2);
  155. break;
  156. }
  157. // touch
  158. switch (videoHandle.getTouchImageStateAt(x, y))
  159. {
  160. case TouchImageState.touchArea:
  161. red = (Int16)(red / 2);
  162. green = (Int16)(green / 2);
  163. blue = (Int16)(blue / 2);
  164. break;
  165. case TouchImageState.touchAreaMatched:
  166. red = (Int16)(red / 2);
  167. green = (Int16)(green / 2);
  168. blue = Int16.MaxValue;
  169. break;
  170. case TouchImageState.touchAreaStatusBar:
  171. red = (Int16)(red / 2);
  172. green = Int16.MaxValue;
  173. blue = (Int16)(blue / 2);
  174. break;
  175. case TouchImageState.touchDetected:
  176. red = (Int16)(red / 2);
  177. blue = (Int16)(blue / 2);
  178. green = (Int16)(green / 2 + Int16.MaxValue / 2);
  179. break;
  180. case TouchImageState.touchTracked:
  181. red = (Int16)(red / 2 + Int16.MaxValue / 2);
  182. green = (Int16)(green / 2);
  183. blue = (Int16)(blue / 2);
  184. break;
  185. }
  186. depthTextureData[index] = red;
  187. depthTextureData[index + 1] = green;
  188. depthTextureData[index + 2] = blue;
  189. //edgeTexture
  190. FingerImageState fis = videoHandle.getFingerImageStateAt(x, y);
  191. red = green = blue = 0;
  192. if (videoHandle.isEdgeAt(x, y)) blue = Int16.MaxValue;
  193. else if (fis == FingerImageState.fingerTracked) green = Int16.MaxValue;
  194. else if (fis == FingerImageState.fingerDetected) red = Int16.MaxValue;
  195. else if (fis == FingerImageState.fingerSlice) red = blue = Int16.MaxValue;
  196. else if (fis == FingerImageState.possibleFingerSlice) red = Int16.MaxValue;
  197. edgeTextureData[index] = red;
  198. edgeTextureData[index + 1] = green;
  199. edgeTextureData[index + 2] = blue;
  200. index += 3;
  201. }
  202. }
  203. GL.Enable(EnableCap.Texture2D);
  204. GL.BindTexture(TextureTarget.Texture2D, depthTextureID);
  205. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0, PixelFormat.Rgb, PixelType.Short, depthTextureData);
  206. float size = 0.5f;
  207. float size_2 = (float)(size / 2.0f);
  208. GL.Begin(PrimitiveType.Quads);
  209. GL.Color3(1.0, 1.0, 1.0);
  210. GL.TexCoord2(0.0, 0.0); GL.Vertex3(0, size_2, -0.5);
  211. GL.TexCoord2(1.0, 0.0); GL.Vertex3(size, size_2, -0.5);
  212. GL.TexCoord2(1.0, 1.0); GL.Vertex3(size, -size_2, -0.5);
  213. GL.TexCoord2(0.0, 1.0); GL.Vertex3(0, -size_2, -0.5);
  214. GL.End();
  215. GL.Enable(EnableCap.Texture2D);
  216. GL.BindTexture(TextureTarget.Texture2D, edgeTextureID);
  217. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0, PixelFormat.Rgb, PixelType.Short, edgeTextureData);
  218. GL.Begin(PrimitiveType.Quads);
  219. GL.Color3(1.0, 1.0, 1.0);
  220. GL.TexCoord2(0.0, 0.0); GL.Vertex3(0, -size / 2.0, -0.5);
  221. GL.TexCoord2(-1.0, 0.0); GL.Vertex3(-size, -size / 2.0, -0.5);
  222. GL.TexCoord2(-1.0, -1.0); GL.Vertex3(-size, size / 2.0, -0.5);
  223. GL.TexCoord2(0.0, -1.0); GL.Vertex3(0, size / 2.0, -0.5);
  224. GL.End();
  225. SwapBuffers();
  226. }
  227. }
  228. }