OutputWindow.cs 13 KB

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