123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using OpenTK;
- using OpenTK.Input;
- using OpenTK.Graphics.OpenGL;
- using bbiwarg.InputProviders;
- using bbiwarg.Utility;
- namespace bbiwarg.Graphics
- {
- public enum PalmGridControlFocus {
- Rows,
- Columns
- }
- class OutputWindow : GameWindow
- {
- private InputHandler inputHandler;
- private InputProvider inputProvider;
- private PalmGridControlFocus palmGridControlFocus;
- private uint textureID;
- public OutputWindow(InputProvider inputProvider, InputHandler inputHandler)
- : base((int)(Parameters.OutputScaleFactor * Math.Max(1, Math.Min(Parameters.OutputNumImagesPerRow, Parameters.OutputNumImages)) * Parameters.ImageWidth),
- (int)(Parameters.OutputScaleFactor * (1 + (Parameters.OutputNumImages - 1) / Parameters.OutputNumImagesPerRow) * Parameters.ImageHeight))
- {
- this.inputProvider = inputProvider;
- this.inputHandler = inputHandler;
- }
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad(e);
- Title = Parameters.OutputTitle;
- GL.ClearColor(Color.Black);
- // transparency
- GL.Enable(EnableCap.Blend);
- GL.BlendEquation(BlendEquationMode.Max);
- // Depth Test
- GL.Enable(EnableCap.DepthTest);
- // Texture
- GL.Enable(EnableCap.Texture2D);
- GL.GenTextures(1, out textureID);
- GL.BindTexture(TextureTarget.Texture2D, textureID);
- GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
- GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
- palmGridControlFocus = PalmGridControlFocus.Rows;
- Keyboard.KeyDown += HandleKeyDownPalmGridControls;
- if (inputProvider is VideoInputProvider)
- Keyboard.KeyDown += HandleKeyDownVideoControls;
- }
- protected override void OnResize(EventArgs e)
- {
- base.OnResize(e);
- int screenWidth = ClientRectangle.Width;
- int screenHeight = ClientRectangle.Height;
- int numRows = 1 + (Parameters.OutputNumImages - 1) / Parameters.OutputNumImagesPerRow;
- int numCols = Math.Min(Parameters.OutputNumImages, Parameters.OutputNumImagesPerRow);
- int heightForWidth = (int)((float)screenWidth / ((float)numCols * Parameters.ImageAspectRatio) * (float)numRows);
- GL.Viewport(0, (screenHeight - heightForWidth) / 2, screenWidth, heightForWidth);
- // top left at (0,0) every image from (i, j) to (i + 1, j + 1)
- Matrix4 projection = Matrix4.CreateOrthographicOffCenter(0, numCols, numRows, 0, 0.001f, 10f);
- GL.MatrixMode(MatrixMode.Projection);
- GL.LoadMatrix(ref projection);
- }
- protected override void OnUpdateFrame(FrameEventArgs e)
- {
- base.OnUpdateFrame(e);
- if (inputHandler.CurrentFrameID - inputProvider.CurrentFrameID > 20)
- inputHandler.resetConsistentObjects();
- if (inputProvider.CurrentFrameID != inputHandler.CurrentFrameID)
- inputHandler.updateFrame();
- }
- protected override void OnRenderFrame(FrameEventArgs e)
- {
- Timer.start("onRenderFrame");
- base.OnRenderFrame(e);
- GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
- Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
- GL.MatrixMode(MatrixMode.Modelview);
- GL.LoadMatrix(ref modelview);
-
- Title = Parameters.OutputTitle + " (Frame: " + inputProvider.CurrentFrameID + ")";
- Timer.start("buildTextures");
- GL.Enable(EnableCap.Texture2D);
- int imageIndex = 0;
- foreach (OutputImage image in inputHandler.OutputImages)
- {
- int x = imageIndex % Parameters.OutputNumImagesPerRow;
- int y = imageIndex / Parameters.OutputNumImagesPerRow;
- GL.BindTexture(TextureTarget.Texture2D, textureID);
- GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, Parameters.ImageWidth, Parameters.ImageHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, image.Image.MIplImage.imageData);
- GL.Begin(PrimitiveType.Quads);
- GL.Color3(1.0, 1.0, 1.0);
- GL.TexCoord2(0, 0); GL.Vertex3(0 + x, 0 + y, -1);
- GL.TexCoord2(1, 0); GL.Vertex3(1 + x, 0 + y, -1);
- GL.TexCoord2(1, 1); GL.Vertex3(1 + x, 1 + y, -1);
- GL.TexCoord2(0, 1); GL.Vertex3(0 + x, 1 + y, -1);
- GL.End();
- ++imageIndex;
- }
- Timer.stop("buildTextures");
- Timer.start("swapBuffers");
- SwapBuffers();
- Timer.stop("swapBuffers");
- Timer.stop("buildTextures");
- Timer.stop("onRenderFrame");
- Timer.outputAll();
- }
- private void HandleKeyDownVideoControls(object sender, KeyboardKeyEventArgs e)
- {
- VideoInputProvider vip = (VideoInputProvider)inputProvider;
- switch (e.Key)
- {
- case Key.Space:
- if (vip.IsPaused)
- vip.play();
- else
- vip.pause();
- break;
- case Key.Right:
- if (vip.IsPaused)
- {
- vip.nextFrame();
- inputHandler.updateFrame();
- }
- break;
- case Key.Left:
- if (vip.IsPaused)
- {
- vip.previousFrame();
- inputHandler.updateFrame();
- }
- break;
- }
- }
- private void HandleKeyDownPalmGridControls(object sender, KeyboardKeyEventArgs e)
- {
- switch (e.Key) {
- case Key.R:
- palmGridControlFocus = PalmGridControlFocus.Rows;
- break;
- case Key.C:
- palmGridControlFocus = PalmGridControlFocus.Columns;
- break;
- case Key.Plus:
- case Key.KeypadPlus:
- case Key.BracketRight: //fix
- if (palmGridControlFocus == PalmGridControlFocus.Rows)
- Parameters.PalmGridNumRows++;
- else
- Parameters.PalmGridNumColumns++;
- break;
- case Key.Minus:
- case Key.KeypadMinus:
- case Key.Slash: //fix
- if (palmGridControlFocus == PalmGridControlFocus.Rows)
- Parameters.PalmGridNumRows = Math.Max(1, Parameters.PalmGridNumRows - 1);
- else
- Parameters.PalmGridNumColumns = Math.Max(1, Parameters.PalmGridNumColumns-1);
- break;
- }
- }
- }
- }
|