GlassesWindow.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Drawing;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using OpenTK;
  8. using OpenTK.Input;
  9. using OpenTK.Graphics.OpenGL;
  10. using OpenTK.Graphics;
  11. using bbiwarg.Input.InputHandling;
  12. using bbiwarg.Input.InputProviding;
  13. using bbiwarg.Utility;
  14. namespace bbiwarg.Output.GlassesOutput
  15. {
  16. class GlassesWindow : GameWindow
  17. {
  18. private InputHandler inputHandler;
  19. private InputProvider inputProvider;
  20. private uint textureID;
  21. private int currentFrameID;
  22. private OutputImage image;
  23. public GlassesWindow(InputProvider inputProvider, InputHandler inputHandler)
  24. : base(Parameters.GlassesWindowWidth, Parameters.GlassesWindowHeight)
  25. {
  26. this.inputProvider = inputProvider;
  27. this.inputHandler = inputHandler;
  28. }
  29. protected override void OnLoad(EventArgs e)
  30. {
  31. base.OnLoad(e);
  32. Title = Parameters.GlassesWindowTitle;
  33. GL.ClearColor(Color.Black);
  34. // Texture
  35. GL.Enable(EnableCap.Texture2D);
  36. GL.GenTextures(1, out textureID);
  37. GL.BindTexture(TextureTarget.Texture2D, textureID);
  38. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  39. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  40. Keyboard.KeyDown += handleKeyDown;
  41. }
  42. protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
  43. {
  44. base.OnClosing(e);
  45. inputProvider.stop();
  46. }
  47. protected override void OnResize(EventArgs e)
  48. {
  49. base.OnResize(e);
  50. int screenWidth = ClientRectangle.Width;
  51. int screenHeight = ClientRectangle.Height;
  52. GL.Viewport(0, 0, screenWidth, screenHeight);
  53. //Matrix4 projection = Matrix4.CreateOrthographicOffCenter(0, numCols, numRows, 0, 0.001f, 10f);
  54. Matrix4 projection = Matrix4.CreateOrthographicOffCenter(0, 1, 1, 0, 0.001f, 10f);
  55. GL.MatrixMode(MatrixMode.Projection);
  56. GL.LoadMatrix(ref projection);
  57. }
  58. protected override void OnUpdateFrame(FrameEventArgs e)
  59. {
  60. Timer.start("GlassesWindow.OnUpdateFrame");
  61. base.OnUpdateFrame(e);
  62. if (!inputProvider.IsActive)
  63. Exit();
  64. FrameData frameData = inputHandler.FrameData;
  65. if (frameData != null)
  66. {
  67. lock (frameData)
  68. {
  69. if (currentFrameID != frameData.FrameID)
  70. {
  71. currentFrameID = frameData.FrameID;
  72. Timer.start("GlassesWindow.OnUpdateFrame::updateImage");
  73. //TODO update image
  74. if (image != null)
  75. image.Dispose();
  76. image = new OutputImage(Parameters.GlassesWindowWidth, Parameters.GlassesWindowHeight);
  77. Timer.stop("GlassesWindow.OnUpdateFrame::updateImage");
  78. }
  79. }
  80. }
  81. Timer.stop("GlassesWindow.OnUpdateFrame");
  82. }
  83. protected override void OnRenderFrame(FrameEventArgs e)
  84. {
  85. Timer.start("GlassesWindow.OnRenderFrame");
  86. base.OnRenderFrame(e);
  87. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  88. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  89. GL.MatrixMode(MatrixMode.Modelview);
  90. GL.LoadMatrix(ref modelview);
  91. Title = Parameters.DebugWindowTitle + " (Frame: " + currentFrameID + ")";
  92. GL.Enable(EnableCap.Texture2D);
  93. Timer.start("GlassesWindow.OnRenderFrame::drawImage");
  94. GL.BindTexture(TextureTarget.Texture2D, textureID);
  95. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, Parameters.GlassesWindowWidth, Parameters.GlassesWindowHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, image.Image.MIplImage.imageData);
  96. GL.Begin(PrimitiveType.Quads);
  97. GL.Color3(1.0, 1.0, 1.0);
  98. GL.TexCoord2(0, 0); GL.Vertex3(0, 0, -1);
  99. GL.TexCoord2(1, 0); GL.Vertex3(1, 0, -1);
  100. GL.TexCoord2(1, 1); GL.Vertex3(1, 1, -1);
  101. GL.TexCoord2(0, 1); GL.Vertex3(0, 1, -1);
  102. GL.End();
  103. Timer.stop("GlassesWindow.OnRenderFrame::drawImage");
  104. Timer.start("GlassesWindow.OnRenderFrame::SwapBuffers");
  105. SwapBuffers();
  106. Timer.stop("GlassesWindow.OnRenderFrame::SwapBuffers");
  107. Timer.stop("GlassesWindow.OnRenderFrame");
  108. }
  109. private void handleKeyDown(object sender, KeyboardKeyEventArgs e)
  110. {
  111. }
  112. }
  113. }