OutputWindow.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. //draw textures
  114. Int16[] depthTextureData = new Int16[3 * videoHandle.getWidth() * videoHandle.getHeight()];
  115. Int16[] edgeTextureData = new Int16[3 * videoHandle.getWidth() * videoHandle.getHeight()];
  116. int index = 0;
  117. for (int y = 0; y < videoHandle.getHeight(); ++y)
  118. {
  119. for (int x = 0; x < videoHandle.getWidth(); ++x)
  120. {
  121. Int16 red = 0;
  122. Int16 green = 0;
  123. Int16 blue = 0;
  124. //depthTexture
  125. float relDepth = videoHandle.getRelativeDepth(x,y);
  126. red = green = blue = (Int16)((1.0f - videoHandle.getRelativeDepth(x, y)) * Int16.MaxValue);
  127. // palm
  128. switch (videoHandle.getPalmImageStateAt(x, y))
  129. {
  130. case PalmImageState.palmContour:
  131. red = Int16.MaxValue;
  132. blue = green = 0;
  133. break;
  134. case PalmImageState.palmRect:
  135. blue = Int16.MaxValue;
  136. red = green = 0;
  137. break;
  138. case PalmImageState.thumbLine:
  139. case PalmImageState.wristLine:
  140. green = Int16.MaxValue;
  141. blue = red = 0;
  142. break;
  143. case PalmImageState.palmGrid:
  144. green = blue = red = Int16.MaxValue;
  145. break;
  146. }
  147. // touch
  148. switch (videoHandle.getTouchImageStateAt(x, y))
  149. {
  150. case TouchImageState.touchArea:
  151. red = (Int16)(red / 2);
  152. green = (Int16)(green / 2);
  153. blue = (Int16)(blue / 2);
  154. break;
  155. case TouchImageState.touchAreaMatched:
  156. red = (Int16)(red / 2);
  157. green = (Int16)(green / 2);
  158. blue = Int16.MaxValue;
  159. break;
  160. case TouchImageState.touchAreaStatusBar:
  161. red = (Int16)(red / 2);
  162. green = Int16.MaxValue;
  163. blue = (Int16)(blue / 2);
  164. break;
  165. case TouchImageState.touchDetected:
  166. red = blue = 0;
  167. green = Int16.MaxValue;
  168. break;
  169. case TouchImageState.touchTracked:
  170. red = Int16.MaxValue;
  171. green = blue = 0;
  172. break;
  173. }
  174. depthTextureData[index] = red;
  175. depthTextureData[index + 1] = green;
  176. depthTextureData[index + 2] = blue;
  177. //edgeTexture
  178. FingerImageState fis = videoHandle.getFingerImageStateAt(x,y);
  179. red = green = blue = 0;
  180. if (videoHandle.isEdgeAt(x, y)) blue = Int16.MaxValue;
  181. else if (fis == FingerImageState.fingerTracked) green = Int16.MaxValue;
  182. else if (fis == FingerImageState.fingerDetected) red = Int16.MaxValue;
  183. else if (fis == FingerImageState.fingerSlice) red = blue = Int16.MaxValue;
  184. else if (fis == FingerImageState.possibleFingerSlice) red = Int16.MaxValue;
  185. edgeTextureData[index] = red;
  186. edgeTextureData[index + 1] = green;
  187. edgeTextureData[index + 2] = blue;
  188. index += 3;
  189. }
  190. }
  191. GL.Enable(EnableCap.Texture2D);
  192. GL.BindTexture(TextureTarget.Texture2D, depthTextureID);
  193. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0, PixelFormat.Rgb, PixelType.Short, depthTextureData);
  194. float size = 0.5f;
  195. float size_2 = (float) (size / 2.0f);
  196. GL.Begin(PrimitiveType.Quads);
  197. GL.Color3(1.0, 1.0, 1.0);
  198. GL.TexCoord2(0.0, 0.0); GL.Vertex3(0, size_2, -0.5);
  199. GL.TexCoord2(1.0, 0.0); GL.Vertex3(size, size_2, -0.5);
  200. GL.TexCoord2(1.0, 1.0); GL.Vertex3(size, -size_2, -0.5);
  201. GL.TexCoord2(0.0, 1.0); GL.Vertex3(0, -size_2, -0.5);
  202. GL.End();
  203. GL.Enable(EnableCap.Texture2D);
  204. GL.BindTexture(TextureTarget.Texture2D, edgeTextureID);
  205. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0, PixelFormat.Rgb, PixelType.Short, edgeTextureData);
  206. GL.Begin(PrimitiveType.Quads);
  207. GL.Color3(1.0, 1.0, 1.0);
  208. GL.TexCoord2(0.0, 0.0); GL.Vertex3(0, -size/2.0, -0.5);
  209. GL.TexCoord2(-1.0, 0.0); GL.Vertex3(-size, -size/2.0, -0.5);
  210. GL.TexCoord2(-1.0, -1.0); GL.Vertex3(-size, size/2.0, -0.5);
  211. GL.TexCoord2(0.0, -1.0); GL.Vertex3(0, size/2.0, -0.5);
  212. GL.End();
  213. SwapBuffers();
  214. }
  215. }
  216. }