123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- 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)(Constants.OutputScaleFactor * Math.Max(1, Math.Min(Constants.OutputNumImagesPerRow, Constants.OutputNumImages)) * inputProvider.ImageWidth),
- (int)(Constants.OutputScaleFactor * (1 + (Constants.OutputNumImages - 1) / Constants.OutputNumImagesPerRow) * inputProvider.ImageHeight))
- {
- this.inputProvider = inputProvider;
- this.inputHandler = inputHandler;
- }
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad(e);
- Title = Constants.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;
- VSync = VSyncMode.Off;
- }
- protected override void OnResize(EventArgs e)
- {
- base.OnResize(e);
- int screenWidth = ClientRectangle.Width;
- int screenHeight = ClientRectangle.Height;
- int imageWidth = inputProvider.ImageWidth;
- int imageHeight = inputProvider.ImageHeight;
- float imageAspectRatio = (float)imageWidth / (float)imageHeight;
- int numRows = 1 + (Constants.OutputNumImages - 1) / Constants.OutputNumImagesPerRow;
- int numCols = Math.Min(Constants.OutputNumImages, Constants.OutputNumImagesPerRow);
- int heightForWidth = (int)((float)screenWidth / ((float)numCols * 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 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);
- inputHandler.updateFrame();
- Title = Constants.OutputTitle + " (Frame: " + inputProvider.CurrentFrameID + ")";
- Timer.start("buildTextures");
- GL.Enable(EnableCap.Texture2D);
- int imageIndex = 0;
- foreach (OutputImage image in inputHandler.OutputImages)
- {
- int x = imageIndex % Constants.OutputNumImagesPerRow;
- int y = imageIndex / Constants.OutputNumImagesPerRow;
- GL.BindTexture(TextureTarget.Texture2D, textureID);
- GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, inputProvider.ImageWidth, inputProvider.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)
- Constants.PalmGridNumRows++;
- else
- Constants.PalmGridNumColumns++;
- break;
- case Key.Minus:
- case Key.KeypadMinus:
- case Key.Slash: //fix
- if (palmGridControlFocus == PalmGridControlFocus.Rows)
- Constants.PalmGridNumRows = Math.Max(1, Constants.PalmGridNumRows - 1);
- else
- Constants.PalmGridNumColumns = Math.Max(1, Constants.PalmGridNumColumns-1);
- break;
- }
- }
- }
- }
|