OutputWindow.cs 13 KB

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