123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- 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.DataSource;
- using bbiwarg.Graphics.GraphicElements2D;
- namespace bbiwarg.Graphics
- {
- class Output2D: GameWindow
- {
- private IInputProvider input;
- private uint textureId;
- public Output2D(IInputProvider input): base(3 * 320, 3 * 240)
- {
- this.input = input;
- }
- 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);
- input.releaseFrame();
- input.updateFrame();
- Stopwatch sw = new Stopwatch();
- sw.Start();
- DepthImage depthImage = input.getDepthImage();
- depthImage.filterMedian(3);
- Vector palmOrigin = input.getPalmPosition2D(1);
- int palmX = (int) palmOrigin.x();
- int palmY = (int) palmOrigin.y();
- int palmDepth = depthImage.getDepth(palmX, palmY);
- int maxHandSize = 150; // mm Wieviel Pixel sind das?
- List<Vector> points = new List<Vector>();
- if (palmX != 0 && palmY != 0)
- {
- depthImage.thresholdDepth(palmDepth - 10, palmDepth + 30);
- depthImage.thresholdPosition(palmX - maxHandSize / 2, palmX + maxHandSize / 2, palmY - maxHandSize / 2, palmY + maxHandSize / 2);
- points = depthImage.getRectPoints();
- }
- short[] textureData = new short[3 * depthImage.getWidth() * depthImage.getHeight()];
- int index = 0;
- for (int y = 0; y < depthImage.getHeight(); ++y) {
- for (int x = 0; x < depthImage.getWidth(); ++x) {
- // 0 --> 0 / 2000 -> Int16.MaxValue
- int d = depthImage.getDepth(x, y);
- short depth = (short) (d * Int16.MaxValue / 2000);
- textureData[index] = textureData[index + 1] = textureData[index + 2] = depth;
- index += 3;
- }
- }
- // draw palm origin
- Point2D palmPoint = new Point2D(palmOrigin, Color.Yellow);
- palmPoint.draw(textureData, depthImage.getWidth());
- // draw rect points
- foreach (Vector v in points)
- {
- int x = (int)v.x() % depthImage.getWidth();
- int y = (int)v.y() % depthImage.getHeight();
- for (int i = -1; i < 2; ++i)
- {
- for (int j = -1; j < 2; ++j)
- {
- int px = Math.Min(depthImage.getWidth() - 1, Math.Max(0, x + i));
- int py = Math.Min(depthImage.getHeight() - 1, Math.Max(0, y + j));
- index = 3 * (py * depthImage.getWidth() + px);
- textureData[index + 0] = 0;
- textureData[index + 1] = 0;
- textureData[index + 2] = Int16.MaxValue;
-
- }
- }
- }
- GL.BindTexture(TextureTarget.Texture2D, textureId);
- GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, depthImage.getWidth(), depthImage.getHeight(), 0,
- PixelFormat.Rgb, PixelType.Short, textureData);
- float size_2 = 0.5f / 2.0f;
- GL.Begin(PrimitiveType.Quads);
- 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);
- }
- }
- }
|