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. using Emgu.CV.Structure;
  15. namespace bbiwarg.Graphics
  16. {
  17. class OutputWindow : GameWindow
  18. {
  19. private VideoHandle videoHandle;
  20. private uint depthTextureID;
  21. private uint edgeTextureID;
  22. private uint touchEventTextureID;
  23. private bool paused = false;
  24. private long timeSpacePressed, timeLeftPressed, timeRightPressed;
  25. private Stopwatch watch;
  26. private const int numImages = 3;
  27. public OutputWindow(VideoHandle videoHandle)
  28. : base((int) (1.5 * numImages * videoHandle.Width), (int) (1.5 * videoHandle.Height))
  29. {
  30. this.videoHandle = videoHandle;
  31. watch = new Stopwatch();
  32. watch.Start();
  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. //touch event texture
  57. GL.GenTextures(1, out touchEventTextureID);
  58. GL.BindTexture(TextureTarget.Texture2D, touchEventTextureID);
  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. int screenWidth = ClientRectangle.Width;
  66. int screenHeight = ClientRectangle.Height;
  67. int imageWidth = videoHandle.Width;
  68. int imageHeight = videoHandle.Height;
  69. float imageAspectRatio = (float)imageWidth / (float)imageHeight;
  70. int heightForWidth = (int) ((float) screenWidth / ((float) numImages * imageAspectRatio));
  71. GL.Viewport(0, (screenHeight - heightForWidth) / 2, screenWidth, heightForWidth);
  72. // top left at (0,0) every image from (i, j) to (i + 1, j + 1)
  73. Matrix4 projection = Matrix4.CreateOrthographicOffCenter(0, 3, 1, 0, 0.001f, 10f);
  74. GL.MatrixMode(MatrixMode.Projection);
  75. GL.LoadMatrix(ref projection);
  76. }
  77. protected override void OnRenderFrame(FrameEventArgs e)
  78. {
  79. Timer.start("onRenderFrame");
  80. base.OnRenderFrame(e);
  81. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  82. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  83. GL.MatrixMode(MatrixMode.Modelview);
  84. GL.LoadMatrix(ref modelview);
  85. bool changedFrame = false;
  86. const int autoRepeatDelay = 100; // ms
  87. if (videoHandle.sourceIsMovie())
  88. {
  89. // pause and unpause with space
  90. long elapsed = watch.ElapsedMilliseconds;
  91. if (OpenTK.Input.Keyboard.GetState().IsKeyDown(OpenTK.Input.Key.Space) && (elapsed - timeSpacePressed) >= autoRepeatDelay)
  92. {
  93. timeSpacePressed = elapsed;
  94. if (paused)
  95. videoHandle.unpauseMovie();
  96. else
  97. videoHandle.pauseMovie();
  98. paused = !paused;
  99. }
  100. // when paused go to next / previous frame with right / left keys
  101. if (paused)
  102. {
  103. if (OpenTK.Input.Keyboard.GetState().IsKeyDown(OpenTK.Input.Key.Right) && (elapsed - timeRightPressed) >= autoRepeatDelay)
  104. {
  105. timeRightPressed = elapsed;
  106. videoHandle.unpauseMovie();
  107. videoHandle.nextFrame();
  108. videoHandle.pauseMovie();
  109. changedFrame = true;
  110. }
  111. else if (OpenTK.Input.Keyboard.GetState().IsKeyDown(OpenTK.Input.Key.Left) && (elapsed - timeLeftPressed) >= autoRepeatDelay)
  112. {
  113. timeLeftPressed = elapsed;
  114. videoHandle.unpauseMovie();
  115. videoHandle.reversePlay();
  116. videoHandle.nextFrame();
  117. videoHandle.reversePlay();
  118. videoHandle.pauseMovie();
  119. changedFrame = true;
  120. }
  121. }
  122. else
  123. {
  124. videoHandle.nextFrame();
  125. changedFrame = true;
  126. }
  127. }
  128. else
  129. {
  130. videoHandle.nextFrame();
  131. }
  132. if (changedFrame || !videoHandle.sourceIsMovie())
  133. {
  134. /*foreach (PalmTouchEvent ev in videoHandle.getTouchEvents())
  135. {
  136. //Console.WriteLine("touch at " + ev.Position + " -> " + ev.RelativePalmPosition);
  137. //touchVisualizer.addTouchEvent(ev);
  138. }*/
  139. }
  140. if (videoHandle.sourceIsMovie())
  141. Title = "BBIWARG - Output (Frame " + videoHandle.getCurrentMovieFrame() + ")";
  142. Timer.start("outputTextures");
  143. //draw textures
  144. Int16[] depthTextureData = new Int16[3 * videoHandle.Width * videoHandle.Height];
  145. Int16[] edgeTextureData = new Int16[3 * videoHandle.Width * videoHandle.Height];
  146. int index = 0;
  147. for (int y = 0; y < videoHandle.Height; ++y)
  148. {
  149. for (int x = 0; x < videoHandle.Width; ++x)
  150. {
  151. Int16 red = 0;
  152. Int16 green = 0;
  153. Int16 blue = 0;
  154. //depthTexture
  155. float relDepth = videoHandle.getRelativeDepth(x, y);
  156. red = green = blue = (Int16)((1.0f - videoHandle.getRelativeDepth(x, y)) * Int16.MaxValue);
  157. // palm
  158. switch (videoHandle.getPalmImageStateAt(x, y))
  159. {
  160. case PalmImageState.palmContour:
  161. red = Int16.MaxValue;
  162. blue = green = 0;
  163. break;
  164. case PalmImageState.palmRect:
  165. blue = Int16.MaxValue;
  166. red = green = 0;
  167. break;
  168. case PalmImageState.thumbLine:
  169. case PalmImageState.wristLine:
  170. green = Int16.MaxValue;
  171. blue = red = 0;
  172. break;
  173. case PalmImageState.palmGrid:
  174. green = (Int16)(green / 2 + Int16.MaxValue / 2);
  175. blue = (Int16)(blue / 2 + Int16.MaxValue / 2);
  176. red = (Int16)(red / 2 + Int16.MaxValue / 2);
  177. break;
  178. }
  179. // touch
  180. switch (videoHandle.getTouchImageStateAt(x, y))
  181. {
  182. case TouchImageState.touchArea:
  183. red = (Int16)(red / 2);
  184. green = (Int16)(green / 2);
  185. blue = (Int16)(blue / 2);
  186. break;
  187. case TouchImageState.touchAreaMatched:
  188. red = (Int16)(red / 2);
  189. green = (Int16)(green / 2);
  190. blue = Int16.MaxValue;
  191. break;
  192. case TouchImageState.touchAreaStatusBar:
  193. red = (Int16)(red / 2);
  194. green = Int16.MaxValue;
  195. blue = (Int16)(blue / 2);
  196. break;
  197. case TouchImageState.touchDetected:
  198. red = (Int16)(red / 2);
  199. blue = (Int16)(blue / 2);
  200. green = (Int16)(green / 2 + Int16.MaxValue / 2);
  201. break;
  202. case TouchImageState.touchTracked:
  203. red = (Int16)(red / 2 + Int16.MaxValue / 2);
  204. green = (Int16)(green / 2);
  205. blue = (Int16)(blue / 2);
  206. break;
  207. }
  208. depthTextureData[index] = red;
  209. depthTextureData[index + 1] = green;
  210. depthTextureData[index + 2] = blue;
  211. //edgeTexture
  212. FingerImageState fis = videoHandle.getFingerImageStateAt(x, y);
  213. red = green = blue = 0;
  214. if (videoHandle.isEdgeAt(x, y)) blue = Int16.MaxValue;
  215. else if (fis == FingerImageState.fingerTracked) green = Int16.MaxValue;
  216. else if (fis == FingerImageState.fingerDetected) red = Int16.MaxValue;
  217. else if (fis == FingerImageState.fingerSlice) red = blue = Int16.MaxValue;
  218. else if (fis == FingerImageState.possibleFingerSlice) red = Int16.MaxValue;
  219. edgeTextureData[index] = red;
  220. edgeTextureData[index + 1] = green;
  221. edgeTextureData[index + 2] = blue;
  222. index += 3;
  223. }
  224. }
  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); GL.Vertex3(0, 0, -1);
  231. GL.TexCoord2(1, 0); GL.Vertex3(1, 0, -1);
  232. GL.TexCoord2(1, 1); GL.Vertex3(1, 1, -1);
  233. GL.TexCoord2(0, 1); GL.Vertex3(0, 1, -1);
  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); GL.Vertex3(1, 0, -1);
  241. GL.TexCoord2(1, 0); GL.Vertex3(2, 0, -1);
  242. GL.TexCoord2(1, 1); GL.Vertex3(2, 1, -1);
  243. GL.TexCoord2(0, 1); GL.Vertex3(1, 1, -1);
  244. GL.End();
  245. /*GL.Enable(EnableCap.Texture2D);
  246. GL.BindTexture(TextureTarget.Texture2D, touchEventTextureID);
  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); GL.Vertex3(2, 0, -1);
  252. GL.TexCoord2(1, 0); GL.Vertex3(3, 0, -1);
  253. GL.TexCoord2(1, 1); GL.Vertex3(3, 1, -1);
  254. GL.TexCoord2(0, 1); GL.Vertex3(2, 1, -1);
  255. GL.End();*/
  256. SwapBuffers();
  257. Timer.stop("outputTextures");
  258. Timer.stop("onRenderFrame");
  259. Timer.outputAll();
  260. }
  261. }
  262. }