OutputWindow.cs 6.7 KB

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