OutputWindow.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 textureId;
  21. private bool paused = false;
  22. private long timeSpacePressed, timeLeftPressed, timeRightPressed;
  23. private Stopwatch watch;
  24. public OutputWindow(VideoHandle videoHandle)
  25. : base((int) (1.5 * videoHandle.OutputImages.Length * videoHandle.Width), (int) (1.5 * videoHandle.Height))
  26. {
  27. this.videoHandle = videoHandle;
  28. watch = new Stopwatch();
  29. watch.Start();
  30. }
  31. protected override void OnLoad(EventArgs e)
  32. {
  33. base.OnLoad(e);
  34. Title = "BBIWARG - Output";
  35. GL.ClearColor(Color.Black);
  36. // transparency
  37. GL.Enable(EnableCap.Blend);
  38. GL.BlendEquation(BlendEquationMode.Max);
  39. //Depth Test
  40. GL.Enable(EnableCap.DepthTest);
  41. // Texture
  42. GL.Enable(EnableCap.Texture2D);
  43. GL.GenTextures(1, out textureId);
  44. GL.BindTexture(TextureTarget.Texture2D, textureId);
  45. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  46. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  47. }
  48. protected override void OnResize(EventArgs e)
  49. {
  50. base.OnResize(e);
  51. int screenWidth = ClientRectangle.Width;
  52. int screenHeight = ClientRectangle.Height;
  53. int imageWidth = videoHandle.Width;
  54. int imageHeight = videoHandle.Height;
  55. float imageAspectRatio = (float)imageWidth / (float)imageHeight;
  56. int heightForWidth = (int) ((float) screenWidth / ((float) videoHandle.OutputImages.Length * imageAspectRatio));
  57. GL.Viewport(0, (screenHeight - heightForWidth) / 2, screenWidth, heightForWidth);
  58. // top left at (0,0) every image from (i, j) to (i + 1, j + 1)
  59. Matrix4 projection = Matrix4.CreateOrthographicOffCenter(0, videoHandle.OutputImages.Length, 1, 0, 0.001f, 10f);
  60. GL.MatrixMode(MatrixMode.Projection);
  61. GL.LoadMatrix(ref projection);
  62. }
  63. protected override void OnRenderFrame(FrameEventArgs e)
  64. {
  65. Timer.start("onRenderFrame");
  66. base.OnRenderFrame(e);
  67. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  68. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  69. GL.MatrixMode(MatrixMode.Modelview);
  70. GL.LoadMatrix(ref modelview);
  71. const int autoRepeatDelay = 100; // ms
  72. if (videoHandle.sourceIsMovie())
  73. {
  74. // pause and unpause with space
  75. long elapsed = watch.ElapsedMilliseconds;
  76. if (OpenTK.Input.Keyboard.GetState().IsKeyDown(OpenTK.Input.Key.Space) && (elapsed - timeSpacePressed) >= autoRepeatDelay)
  77. {
  78. timeSpacePressed = elapsed;
  79. if (paused)
  80. videoHandle.unpauseMovie();
  81. else
  82. videoHandle.pauseMovie();
  83. paused = !paused;
  84. }
  85. // when paused go to next / previous frame with right / left keys
  86. if (paused)
  87. {
  88. if (OpenTK.Input.Keyboard.GetState().IsKeyDown(OpenTK.Input.Key.Right) && (elapsed - timeRightPressed) >= autoRepeatDelay)
  89. {
  90. timeRightPressed = elapsed;
  91. videoHandle.unpauseMovie();
  92. videoHandle.nextFrame();
  93. videoHandle.pauseMovie();
  94. }
  95. else if (OpenTK.Input.Keyboard.GetState().IsKeyDown(OpenTK.Input.Key.Left) && (elapsed - timeLeftPressed) >= autoRepeatDelay)
  96. {
  97. timeLeftPressed = elapsed;
  98. videoHandle.unpauseMovie();
  99. videoHandle.reversePlay();
  100. videoHandle.nextFrame();
  101. videoHandle.reversePlay();
  102. videoHandle.pauseMovie();
  103. }
  104. }
  105. else
  106. {
  107. videoHandle.nextFrame();
  108. }
  109. }
  110. else
  111. {
  112. videoHandle.nextFrame();
  113. }
  114. if (videoHandle.sourceIsMovie())
  115. Title = "BBIWARG - Output (Frame " + videoHandle.getCurrentMovieFrame() + ")";
  116. Timer.start("outputTextures");
  117. GL.Enable(EnableCap.Texture2D);
  118. int imageIndex = 0;
  119. foreach (OutputImage image in videoHandle.OutputImages)
  120. {
  121. GL.BindTexture(TextureTarget.Texture2D, textureId);
  122. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.Width, videoHandle.Height, 0, PixelFormat.Rgb, PixelType.UnsignedByte, image.Image.MIplImage.imageData);
  123. GL.Begin(PrimitiveType.Quads);
  124. GL.Color3(1.0, 1.0, 1.0);
  125. GL.TexCoord2(0, 0); GL.Vertex3(0 + imageIndex, 0, -1);
  126. GL.TexCoord2(1, 0); GL.Vertex3(1 + imageIndex, 0, -1);
  127. GL.TexCoord2(1, 1); GL.Vertex3(1 + imageIndex, 1, -1);
  128. GL.TexCoord2(0, 1); GL.Vertex3(0 + imageIndex, 1, -1);
  129. GL.End();
  130. ++imageIndex;
  131. }
  132. Timer.start("swapBuffers");
  133. SwapBuffers();
  134. Timer.stop("swapBuffers");
  135. Timer.stop("outputTextures");
  136. Timer.stop("onRenderFrame");
  137. Timer.outputAll();
  138. }
  139. }
  140. }