using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using OpenTK; using OpenTK.Graphics.OpenGL; using System.Drawing; using System.Diagnostics; using MathNet.Numerics.LinearAlgebra.Single; using bbiwarg.Graphics.GraphicElements2D; using bbiwarg.InputProviders; using bbiwarg.Images; using bbiwarg.Helpers; using bbiwarg.VideoHandles; namespace bbiwarg.Graphics { class Output2D: GameWindow { private VideoHandle videoHandle; private uint textureId; public Output2D(VideoHandle videoHandle): base(3 * 320, 3 * 240) { this.videoHandle = videoHandle; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); Title = "OutputTest"; GL.ClearColor(Color.Black); // transparency GL.Enable(EnableCap.Blend); GL.BlendEquation(BlendEquationMode.Max); //Depth Test GL.Enable(EnableCap.DepthTest); // Textures 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); } protected override void OnRenderFrame(FrameEventArgs e) { 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); videoHandle.nextFrame(); Stopwatch sw = new Stopwatch(); sw.Start(); short[] textureData = new short[3 * videoHandle.getWidth() * videoHandle.getHeight()]; int index = 0; short minDepth = videoHandle.getHandImage().getMinDepth(); short maxDepth = videoHandle.getHandImage().getMaxDepth(); for (int y = 0; y < videoHandle.getHeight(); ++y) { for (int x = 0; x < videoHandle.getWidth(); ++x) { short depth = videoHandle.getHandImage().getDepth(x, y); short diff = (short) Math.Max(maxDepth - minDepth, 1); short scaledDepth = (short) (Int16.MaxValue - (((int) depth - minDepth) * (int) Int16.MaxValue / diff)); textureData[index] = scaledDepth; textureData[index + 1] = scaledDepth; textureData[index + 2] = scaledDepth; index += 3; } } // draw histogram GL.Disable(EnableCap.Texture2D); int[] histogram = videoHandle.getSmoothedHistogram(); int maxValue = 0; for (int i = 0; i < histogram.Length; ++i) { if (histogram[i] > maxValue) maxValue = histogram[i]; } Int16[] segmentationDepth = videoHandle.getSegementationDepth(); GL.Begin(PrimitiveType.LineStrip); GL.Color3(0, 0, 1.0); GL.LineWidth(5.0f); for (int i = 0; i < histogram.Length; ++i) { if (i > segmentationDepth[0]) GL.Color3(Color.Yellow); if (i > segmentationDepth[1]) GL.Color3(Color.Red); if (i > segmentationDepth[2]) GL.Color3(Color.Silver); GL.Vertex3(-0.25 + i * (0.5 / histogram.Length), -0.25 + histogram[i] * (0.5 / (maxValue - 5)), -0.5); } GL.End(); // draw texture GL.Enable(EnableCap.Texture2D); GL.BindTexture(TextureTarget.Texture2D, textureId); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0, PixelFormat.Rgb, PixelType.Short, textureData); float size_2 = 0.5f / 2.0f; GL.Begin(PrimitiveType.Quads); GL.Color3(1.0, 1.0, 1.0); GL.TexCoord2(0.0, 0.0); GL.Vertex3(-size_2, size_2, -0.5); GL.TexCoord2(1.0, 0.0); GL.Vertex3( size_2, size_2, -0.5); GL.TexCoord2(1.0, 1.0); GL.Vertex3( size_2, -size_2, -0.5); GL.TexCoord2(0.0, 1.0); GL.Vertex3(-size_2, -size_2, -0.5); GL.End(); sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); SwapBuffers(); } protected override void OnResize(EventArgs e) { base.OnResize(e); GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height); Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f); GL.MatrixMode(MatrixMode.Projection); GL.LoadMatrix(ref projection); } } }