OutputWindow.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. public enum PalmGridControlFocus {
  15. Rows,
  16. Columns
  17. }
  18. class OutputWindow : GameWindow
  19. {
  20. private InputHandler inputHandler;
  21. private InputProvider inputProvider;
  22. private PalmGridControlFocus palmGridControlFocus;
  23. private uint textureID;
  24. public OutputWindow(InputProvider inputProvider, InputHandler inputHandler)
  25. : base((int)(Constants.OutputScaleFactor * Math.Max(1, Math.Min(Constants.OutputNumImagesPerRow, Constants.OutputNumImages)) * inputProvider.ImageWidth),
  26. (int)(Constants.OutputScaleFactor * (1 + (Constants.OutputNumImages - 1) / Constants.OutputNumImagesPerRow) * inputProvider.ImageHeight))
  27. {
  28. this.inputProvider = inputProvider;
  29. this.inputHandler = inputHandler;
  30. }
  31. protected override void OnLoad(EventArgs e)
  32. {
  33. base.OnLoad(e);
  34. Title = Constants.OutputTitle;
  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. palmGridControlFocus = PalmGridControlFocus.Rows;
  48. Keyboard.KeyDown += HandleKeyDownPalmGridControls;
  49. if (inputProvider is VideoInputProvider)
  50. Keyboard.KeyDown += HandleKeyDownVideoControls;
  51. VSync = VSyncMode.Off;
  52. }
  53. protected override void OnResize(EventArgs e)
  54. {
  55. base.OnResize(e);
  56. int screenWidth = ClientRectangle.Width;
  57. int screenHeight = ClientRectangle.Height;
  58. int imageWidth = inputProvider.ImageWidth;
  59. int imageHeight = inputProvider.ImageHeight;
  60. float imageAspectRatio = (float)imageWidth / (float)imageHeight;
  61. int numRows = 1 + (Constants.OutputNumImages - 1) / Constants.OutputNumImagesPerRow;
  62. int numCols = Math.Min(Constants.OutputNumImages, Constants.OutputNumImagesPerRow);
  63. int heightForWidth = (int)((float)screenWidth / ((float)numCols * imageAspectRatio) * (float)numRows);
  64. GL.Viewport(0, (screenHeight - heightForWidth) / 2, screenWidth, heightForWidth);
  65. // top left at (0,0) every image from (i, j) to (i + 1, j + 1)
  66. Matrix4 projection = Matrix4.CreateOrthographicOffCenter(0, numCols, numRows, 0, 0.001f, 10f);
  67. GL.MatrixMode(MatrixMode.Projection);
  68. GL.LoadMatrix(ref projection);
  69. }
  70. protected override void OnRenderFrame(FrameEventArgs e)
  71. {
  72. Timer.start("onRenderFrame");
  73. base.OnRenderFrame(e);
  74. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  75. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  76. GL.MatrixMode(MatrixMode.Modelview);
  77. GL.LoadMatrix(ref modelview);
  78. inputHandler.updateFrame();
  79. Title = Constants.OutputTitle + " (Frame: " + inputProvider.CurrentFrameID + ")";
  80. Timer.start("buildTextures");
  81. GL.Enable(EnableCap.Texture2D);
  82. int imageIndex = 0;
  83. foreach (OutputImage image in inputHandler.OutputImages)
  84. {
  85. int x = imageIndex % Constants.OutputNumImagesPerRow;
  86. int y = imageIndex / Constants.OutputNumImagesPerRow;
  87. GL.BindTexture(TextureTarget.Texture2D, textureID);
  88. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, inputProvider.ImageWidth, inputProvider.ImageHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, image.Image.MIplImage.imageData);
  89. GL.Begin(PrimitiveType.Quads);
  90. GL.Color3(1.0, 1.0, 1.0);
  91. GL.TexCoord2(0, 0); GL.Vertex3(0 + x, 0 + y, -1);
  92. GL.TexCoord2(1, 0); GL.Vertex3(1 + x, 0 + y, -1);
  93. GL.TexCoord2(1, 1); GL.Vertex3(1 + x, 1 + y, -1);
  94. GL.TexCoord2(0, 1); GL.Vertex3(0 + x, 1 + y, -1);
  95. GL.End();
  96. ++imageIndex;
  97. }
  98. Timer.stop("buildTextures");
  99. Timer.start("swapBuffers");
  100. SwapBuffers();
  101. Timer.stop("swapBuffers");
  102. Timer.stop("buildTextures");
  103. Timer.stop("onRenderFrame");
  104. Timer.outputAll();
  105. }
  106. private void HandleKeyDownVideoControls(object sender, KeyboardKeyEventArgs e)
  107. {
  108. VideoInputProvider vip = (VideoInputProvider)inputProvider;
  109. switch (e.Key)
  110. {
  111. case Key.Space:
  112. if (vip.IsPaused)
  113. vip.play();
  114. else
  115. vip.pause();
  116. break;
  117. case Key.Right:
  118. if (vip.IsPaused)
  119. {
  120. vip.nextFrame();
  121. inputHandler.updateFrame();
  122. }
  123. break;
  124. case Key.Left:
  125. if (vip.IsPaused)
  126. {
  127. vip.previousFrame();
  128. inputHandler.updateFrame();
  129. }
  130. break;
  131. }
  132. }
  133. private void HandleKeyDownPalmGridControls(object sender, KeyboardKeyEventArgs e)
  134. {
  135. switch (e.Key) {
  136. case Key.R:
  137. palmGridControlFocus = PalmGridControlFocus.Rows;
  138. break;
  139. case Key.C:
  140. palmGridControlFocus = PalmGridControlFocus.Columns;
  141. break;
  142. case Key.Plus:
  143. case Key.KeypadPlus:
  144. case Key.BracketRight: //fix
  145. if (palmGridControlFocus == PalmGridControlFocus.Rows)
  146. Constants.PalmGridNumRows++;
  147. else
  148. Constants.PalmGridNumColumns++;
  149. break;
  150. case Key.Minus:
  151. case Key.KeypadMinus:
  152. case Key.Slash: //fix
  153. if (palmGridControlFocus == PalmGridControlFocus.Rows)
  154. Constants.PalmGridNumRows = Math.Max(1, Constants.PalmGridNumRows - 1);
  155. else
  156. Constants.PalmGridNumColumns = Math.Max(1, Constants.PalmGridNumColumns-1);
  157. break;
  158. }
  159. }
  160. }
  161. }