OutputWindow.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.KeyDown += HandleKeyDown;
  46. Console.WriteLine("video controls enabled (space=pause, right=nextFrame, left=previousFrame)");
  47. }
  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 = inputProvider.ImageWidth;
  55. int imageHeight = inputProvider.ImageHeight;
  56. float imageAspectRatio = (float)imageWidth / (float)imageHeight;
  57. int numRows = 1 + (Constants.OutputNumImages - 1) / Constants.OutputNumImagesPerRow;
  58. int numCols = Math.Min(Constants.OutputNumImages, Constants.OutputNumImagesPerRow);
  59. int heightForWidth = (int)((float)screenWidth / ((float)numCols * imageAspectRatio) * (float)numRows);
  60. GL.Viewport(0, (screenHeight - heightForWidth) / 2, screenWidth, heightForWidth);
  61. // top left at (0,0) every image from (i, j) to (i + 1, j + 1)
  62. Matrix4 projection = Matrix4.CreateOrthographicOffCenter(0, numCols, numRows, 0, 0.001f, 10f);
  63. GL.MatrixMode(MatrixMode.Projection);
  64. GL.LoadMatrix(ref projection);
  65. }
  66. protected override void OnRenderFrame(FrameEventArgs e)
  67. {
  68. Timer.start("onRenderFrame");
  69. base.OnRenderFrame(e);
  70. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  71. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  72. GL.MatrixMode(MatrixMode.Modelview);
  73. GL.LoadMatrix(ref modelview);
  74. inputHandler.updateFrame();
  75. Title = Constants.OutputTitle + " (Frame: " + inputProvider.CurrentFrame + ")";
  76. Timer.start("buildTextures");
  77. GL.Enable(EnableCap.Texture2D);
  78. int imageIndex = 0;
  79. foreach (OutputImage image in inputHandler.OutputImages)
  80. {
  81. int x = imageIndex % Constants.OutputNumImagesPerRow;
  82. int y = imageIndex / Constants.OutputNumImagesPerRow;
  83. GL.BindTexture(TextureTarget.Texture2D, textureID);
  84. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, inputProvider.ImageWidth, inputProvider.ImageHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, image.Image.MIplImage.imageData);
  85. GL.Begin(PrimitiveType.Quads);
  86. GL.Color3(1.0, 1.0, 1.0);
  87. GL.TexCoord2(0, 0); GL.Vertex3(0 + x, 0 + y, -1);
  88. GL.TexCoord2(1, 0); GL.Vertex3(1 + x, 0 + y, -1);
  89. GL.TexCoord2(1, 1); GL.Vertex3(1 + x, 1 + y, -1);
  90. GL.TexCoord2(0, 1); GL.Vertex3(0 + x, 1 + y, -1);
  91. GL.End();
  92. ++imageIndex;
  93. }
  94. Timer.stop("buildTextures");
  95. Timer.start("swapBuffers");
  96. SwapBuffers();
  97. Timer.stop("swapBuffers");
  98. Timer.stop("buildTextures");
  99. Timer.stop("onRenderFrame");
  100. Timer.outputAll();
  101. }
  102. private void HandleKeyDown(object sender, KeyboardKeyEventArgs e)
  103. {
  104. VideoInputProvider vip = (VideoInputProvider)inputProvider;
  105. switch (e.Key)
  106. {
  107. case Key.Space:
  108. if (vip.IsPaused)
  109. vip.play();
  110. else
  111. vip.pause();
  112. break;
  113. case Key.Right:
  114. if (vip.IsPaused)
  115. vip.nextFrame();
  116. break;
  117. case Key.Left:
  118. if (vip.IsPaused)
  119. vip.previousFrame();
  120. break;
  121. }
  122. }
  123. }
  124. }