using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using OpenTK; using OpenTK.Graphics.OpenGL; using bbiwarg.Images; namespace bbiwarg.Graphics { class OutputWindow : GameWindow { private VideoHandle videoHandle; private uint depthTextureID; private uint edgeTextureID; public OutputWindow(VideoHandle videoHandle): base(3 * videoHandle.getWidth(), 2 * videoHandle.getHeight()) { this.videoHandle = videoHandle; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); Title = "BBIWARG - Output"; GL.ClearColor(Color.Black); // transparency GL.Enable(EnableCap.Blend); GL.BlendEquation(BlendEquationMode.Max); //Depth Test GL.Enable(EnableCap.DepthTest); // Textures GL.Enable(EnableCap.Texture2D); //depthTexture GL.GenTextures(1, out depthTextureID); GL.BindTexture(TextureTarget.Texture2D, depthTextureID); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); //edgetexture GL.GenTextures(1, out edgeTextureID); GL.BindTexture(TextureTarget.Texture2D, edgeTextureID); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); } 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); } 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(); //draw textures Int16[] depthTextureData = new Int16[3 * videoHandle.getWidth() * videoHandle.getHeight()]; Int16[] edgeTextureData = new Int16[3 * videoHandle.getWidth() * videoHandle.getHeight()]; int index = 0; for (int y = 0; y < videoHandle.getHeight(); ++y) { for (int x = 0; x < videoHandle.getWidth(); ++x) { Int16 red = 0; Int16 green = 0; Int16 blue = 0; //depthTexture float relDepth = videoHandle.getRelativeDepth(x,y); red = green = blue = (Int16)((1.0f - videoHandle.getRelativeDepth(x, y)) * Int16.MaxValue); TouchImageState tis = videoHandle.getTouchImageStateAt(x,y); if (tis == TouchImageState.touchArea) { red = (Int16) (red/2); green = (Int16) (green / 2); blue = (Int16) (blue / 2); } else if (tis == TouchImageState.touchAreaMatched) { red = (Int16) (red / 2); green = (Int16)(green / 2); blue = Int16.MaxValue; } else if (tis == TouchImageState.touchAreaStatusBar) { red = (Int16)(red / 2); green = Int16.MaxValue; blue = (Int16)(blue / 2); ; } else if (tis == TouchImageState.touchDetected) { red = blue = 0; green = Int16.MaxValue; } else if (tis == TouchImageState.touchTracked) { red = Int16.MaxValue; green = blue = 0; } // show palm contour if (videoHandle.isPalmPointAt(x, y)) { red = Int16.MaxValue; green = blue = 0; } depthTextureData[index] = red; depthTextureData[index + 1] = green; depthTextureData[index + 2] = blue; //edgeTexture FingerImageState fis = videoHandle.getFingerImageStateAt(x,y); red = green = blue = 0; if (videoHandle.isEdgeAt(x, y)) blue = Int16.MaxValue; else if (fis == FingerImageState.fingerTracked) green = Int16.MaxValue; else if (fis == FingerImageState.fingerDetected) red = Int16.MaxValue; else if (fis == FingerImageState.fingerSlice) red = blue = Int16.MaxValue; else if (fis == FingerImageState.possibleFingerSlice) red = Int16.MaxValue; edgeTextureData[index] = red; edgeTextureData[index + 1] = green; edgeTextureData[index + 2] = blue; index += 3; } } GL.Enable(EnableCap.Texture2D); GL.BindTexture(TextureTarget.Texture2D, depthTextureID); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0, PixelFormat.Rgb, PixelType.Short, depthTextureData); float size = 0.5f; float size_2 = (float) (size / 2.0f); GL.Begin(PrimitiveType.Quads); GL.Color3(1.0, 1.0, 1.0); GL.TexCoord2(0.0, 0.0); GL.Vertex3(0, size_2, -0.5); GL.TexCoord2(1.0, 0.0); GL.Vertex3(size, size_2, -0.5); GL.TexCoord2(1.0, 1.0); GL.Vertex3(size, -size_2, -0.5); GL.TexCoord2(0.0, 1.0); GL.Vertex3(0, -size_2, -0.5); GL.End(); GL.Enable(EnableCap.Texture2D); GL.BindTexture(TextureTarget.Texture2D, edgeTextureID); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0, PixelFormat.Rgb, PixelType.Short, edgeTextureData); GL.Begin(PrimitiveType.Quads); GL.Color3(1.0, 1.0, 1.0); GL.TexCoord2(0.0, 0.0); GL.Vertex3(0, -size/2.0, -0.5); GL.TexCoord2(-1.0, 0.0); GL.Vertex3(-size, -size/2.0, -0.5); GL.TexCoord2(-1.0, -1.0); GL.Vertex3(-size, size/2.0, -0.5); GL.TexCoord2(0.0, -1.0); GL.Vertex3(0, size/2.0, -0.5); GL.End(); SwapBuffers(); } } }