OutputWindow.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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)(Parameters.OutputScaleFactor * Math.Max(1, Math.Min(Parameters.OutputNumImagesPerRow, Parameters.OutputNumImages)) * Parameters.ImageWidth),
  26. (int)(Parameters.OutputScaleFactor * (1 + (Parameters.OutputNumImages - 1) / Parameters.OutputNumImagesPerRow) * Parameters.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 = Parameters.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. }
  52. protected override void OnResize(EventArgs e)
  53. {
  54. base.OnResize(e);
  55. int screenWidth = ClientRectangle.Width;
  56. int screenHeight = ClientRectangle.Height;
  57. int numRows = 1 + (Parameters.OutputNumImages - 1) / Parameters.OutputNumImagesPerRow;
  58. int numCols = Math.Min(Parameters.OutputNumImages, Parameters.OutputNumImagesPerRow);
  59. int heightForWidth = (int)((float)screenWidth / ((float)numCols * Parameters.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 OnUpdateFrame(FrameEventArgs e)
  67. {
  68. base.OnUpdateFrame(e);
  69. if (inputHandler.CurrentFrameID - inputProvider.CurrentFrameID > 20)
  70. inputHandler.resetConsistentObjects();
  71. if (inputProvider.CurrentFrameID != inputHandler.CurrentFrameID)
  72. inputHandler.updateFrame();
  73. }
  74. protected override void OnRenderFrame(FrameEventArgs e)
  75. {
  76. Timer.start("onRenderFrame");
  77. base.OnRenderFrame(e);
  78. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  79. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  80. GL.MatrixMode(MatrixMode.Modelview);
  81. GL.LoadMatrix(ref modelview);
  82. Title = Parameters.OutputTitle + " (Frame: " + inputProvider.CurrentFrameID + ")";
  83. Timer.start("buildTextures");
  84. GL.Enable(EnableCap.Texture2D);
  85. int imageIndex = 0;
  86. foreach (OutputImage image in inputHandler.OutputImages)
  87. {
  88. int x = imageIndex % Parameters.OutputNumImagesPerRow;
  89. int y = imageIndex / Parameters.OutputNumImagesPerRow;
  90. GL.BindTexture(TextureTarget.Texture2D, textureID);
  91. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, Parameters.ImageWidth, Parameters.ImageHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, image.Image.MIplImage.imageData);
  92. GL.Begin(PrimitiveType.Quads);
  93. GL.Color3(1.0, 1.0, 1.0);
  94. GL.TexCoord2(0, 0); GL.Vertex3(0 + x, 0 + y, -1);
  95. GL.TexCoord2(1, 0); GL.Vertex3(1 + x, 0 + y, -1);
  96. GL.TexCoord2(1, 1); GL.Vertex3(1 + x, 1 + y, -1);
  97. GL.TexCoord2(0, 1); GL.Vertex3(0 + x, 1 + y, -1);
  98. GL.End();
  99. ++imageIndex;
  100. }
  101. Timer.stop("buildTextures");
  102. Timer.start("swapBuffers");
  103. SwapBuffers();
  104. Timer.stop("swapBuffers");
  105. Timer.stop("buildTextures");
  106. Timer.stop("onRenderFrame");
  107. Timer.outputAll();
  108. }
  109. private void HandleKeyDownVideoControls(object sender, KeyboardKeyEventArgs e)
  110. {
  111. VideoInputProvider vip = (VideoInputProvider)inputProvider;
  112. switch (e.Key)
  113. {
  114. case Key.Space:
  115. if (vip.IsPaused)
  116. vip.play();
  117. else
  118. vip.pause();
  119. break;
  120. case Key.Right:
  121. if (vip.IsPaused)
  122. {
  123. vip.nextFrame();
  124. inputHandler.updateFrame();
  125. }
  126. break;
  127. case Key.Left:
  128. if (vip.IsPaused)
  129. {
  130. vip.previousFrame();
  131. inputHandler.updateFrame();
  132. }
  133. break;
  134. }
  135. }
  136. private void HandleKeyDownPalmGridControls(object sender, KeyboardKeyEventArgs e)
  137. {
  138. switch (e.Key) {
  139. case Key.R:
  140. palmGridControlFocus = PalmGridControlFocus.Rows;
  141. break;
  142. case Key.C:
  143. palmGridControlFocus = PalmGridControlFocus.Columns;
  144. break;
  145. case Key.Plus:
  146. case Key.KeypadPlus:
  147. case Key.BracketRight: //fix
  148. if (palmGridControlFocus == PalmGridControlFocus.Rows)
  149. Parameters.PalmGridNumRows++;
  150. else
  151. Parameters.PalmGridNumColumns++;
  152. break;
  153. case Key.Minus:
  154. case Key.KeypadMinus:
  155. case Key.Slash: //fix
  156. if (palmGridControlFocus == PalmGridControlFocus.Rows)
  157. Parameters.PalmGridNumRows = Math.Max(1, Parameters.PalmGridNumRows - 1);
  158. else
  159. Parameters.PalmGridNumColumns = Math.Max(1, Parameters.PalmGridNumColumns-1);
  160. break;
  161. }
  162. }
  163. }
  164. }