OutputWindow.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using OpenTK;
  8. using OpenTK.Input;
  9. using OpenTK.Graphics.OpenGL;
  10. using bbiwarg.InputProviders;
  11. using bbiwarg.Utility;
  12. namespace bbiwarg.Graphics
  13. {
  14. class OutputWindow : GameWindow
  15. {
  16. private InputHandler inputHandler;
  17. private InputProvider inputProvider;
  18. private uint textureID;
  19. public OutputWindow(InputProvider inputProvider, InputHandler inputHandler)
  20. : base((int)(Constants.OutputScaleFactor * Math.Max(1, Math.Min(Constants.OutputNumImagesPerRow, Constants.OutputNumImages)) * inputProvider.ImageWidth),
  21. (int)(Constants.OutputScaleFactor * (1 + (Constants.OutputNumImages - 1) / Constants.OutputNumImagesPerRow) * inputProvider.ImageHeight))
  22. {
  23. this.inputProvider = inputProvider;
  24. this.inputHandler = inputHandler;
  25. }
  26. protected override void OnLoad(EventArgs e)
  27. {
  28. base.OnLoad(e);
  29. Title = Constants.OutputTitle;
  30. GL.ClearColor(Color.Black);
  31. // transparency
  32. GL.Enable(EnableCap.Blend);
  33. GL.BlendEquation(BlendEquationMode.Max);
  34. // Depth Test
  35. GL.Enable(EnableCap.DepthTest);
  36. // Texture
  37. GL.Enable(EnableCap.Texture2D);
  38. GL.GenTextures(1, out textureID);
  39. GL.BindTexture(TextureTarget.Texture2D, textureID);
  40. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  41. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  42. // video controls
  43. if (inputProvider is VideoInputProvider)
  44. {
  45. Keyboard.KeyRepeat = true;
  46. Keyboard.KeyDown += HandleKeyDown;
  47. Logger.log("video controls enabled (space=pause, right=nextFrame, left=previousFrame)", LogSubject.VideoControls);
  48. }
  49. }
  50. protected override void OnResize(EventArgs e)
  51. {
  52. base.OnResize(e);
  53. int screenWidth = ClientRectangle.Width;
  54. int screenHeight = ClientRectangle.Height;
  55. int imageWidth = inputProvider.ImageWidth;
  56. int imageHeight = inputProvider.ImageHeight;
  57. float imageAspectRatio = (float)imageWidth / (float)imageHeight;
  58. int numRows = 1 + (Constants.OutputNumImages - 1) / Constants.OutputNumImagesPerRow;
  59. int numCols = Math.Min(Constants.OutputNumImages, 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. if(!(inputProvider is VideoInputProvider) || (inputProvider is VideoInputProvider && !((VideoInputProvider)inputProvider).IsPaused))
  76. inputHandler.updateFrame();
  77. Title = Constants.OutputTitle + " (Frame: " + inputProvider.CurrentFrame + ")";
  78. Timer.start("buildTextures");
  79. GL.Enable(EnableCap.Texture2D);
  80. int imageIndex = 0;
  81. foreach (OutputImage image in inputHandler.OutputImages)
  82. {
  83. int x = imageIndex % Constants.OutputNumImagesPerRow;
  84. int y = imageIndex / Constants.OutputNumImagesPerRow;
  85. GL.BindTexture(TextureTarget.Texture2D, textureID);
  86. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, inputProvider.ImageWidth, inputProvider.ImageHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, image.Image.MIplImage.imageData);
  87. GL.Begin(PrimitiveType.Quads);
  88. GL.Color3(1.0, 1.0, 1.0);
  89. GL.TexCoord2(0, 0); GL.Vertex3(0 + x, 0 + y, -1);
  90. GL.TexCoord2(1, 0); GL.Vertex3(1 + x, 0 + y, -1);
  91. GL.TexCoord2(1, 1); GL.Vertex3(1 + x, 1 + y, -1);
  92. GL.TexCoord2(0, 1); GL.Vertex3(0 + x, 1 + y, -1);
  93. GL.End();
  94. ++imageIndex;
  95. }
  96. Timer.stop("buildTextures");
  97. Timer.start("swapBuffers");
  98. SwapBuffers();
  99. Timer.stop("swapBuffers");
  100. Timer.stop("buildTextures");
  101. Timer.stop("onRenderFrame");
  102. Timer.outputAll();
  103. }
  104. private void HandleKeyDown(object sender, KeyboardKeyEventArgs e)
  105. {
  106. VideoInputProvider vip = (VideoInputProvider)inputProvider;
  107. switch (e.Key)
  108. {
  109. case Key.Space:
  110. if (vip.IsPaused)
  111. vip.play();
  112. else
  113. vip.pause();
  114. break;
  115. case Key.Right:
  116. if (vip.IsPaused)
  117. {
  118. vip.nextFrame();
  119. inputHandler.updateFrame();
  120. }
  121. break;
  122. case Key.Left:
  123. if (vip.IsPaused)
  124. {
  125. vip.previousFrame();
  126. inputHandler.updateFrame();
  127. }
  128. break;
  129. }
  130. }
  131. }
  132. }