123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using OpenTK;
- using OpenTK.Graphics.OpenGL;
- using MathNet.Numerics.LinearAlgebra.Single;
- using bbiwarg.DataSource;
- namespace bbiwarg.Graphics
- {
- class Output : GameWindow
- {
- private IVideoDataSource source;
- private Point[,] depthPixels = new Point[320,240];
- private List<IGraphicElement> graphicElements = new List<IGraphicElement>();
- private Point palmPixel;
- private Point[] fingerPixel = new Point[5];
- public Output(IVideoDataSource source)
- {
- this.source = source;
- }
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad(e);
- Title = "OutputTest";
- GL.ClearColor(Color.Black);
- for (int x = 0; x < 320; x++)
- {
- for (int y = 0; y < 240; y++)
- {
- Vertex vertex = new Vertex(x,y, 0.0f);
- Color color = Color.White;
- float size = 0.5f;
- Point pixel = new Point(vertex, color, size);
- depthPixels[x, y] = pixel;
- graphicElements.Add(pixel);
- }
- }
- Vertex palmVertex = new Vertex(0.0f, 0.0f, 0.0f);
- Color palmColor = Color.Yellow;
- float palmSize = 5.0f;
- palmPixel = new Point(palmVertex, palmColor, palmSize);
- graphicElements.Add(palmPixel);
- for (int i = 0; i < 5; i++) {
- Vertex vertex = new Vertex(0.0f, 0.0f, 0.0f);
- fingerPixel[i] = new Point(vertex, palmColor, palmSize);
- graphicElements.Add(fingerPixel[i]);
- }
- }
- 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);
- source.updateFrame();
- ImageData image = source.getImageData();
- int width = image.getWidth();
- int height = image.getHeight();
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- Point depthPixel = depthPixels[x, y];
- depthPixel.position.z = image.getDepth(x, y);
- depthPixel.color = image.getColor(x, y);
- }
- }
- Vector palmPosition2D = source.getPalmPosition2D(1);
- palmPixel.position.x = palmPosition2D[0];
- palmPixel.position.y = palmPosition2D[1];
- palmPixel.position.z = image.getDepth((int)palmPosition2D[0], (int)palmPosition2D[1]);
-
- for (int i = 0; i < 5; i++) {
- DetectionStatus[] fingerStatus = source.getFingerStatus(1);
- Vector[] fingerPositions = source.getFingerTipPositions2D(1);
- if (fingerStatus[i] == DetectionStatus.Tracked)
- {
- fingerPixel[i].position.x = fingerPositions[i][0];
- fingerPixel[i].position.y = fingerPositions[i][1];
- fingerPixel[i].position.z = image.getDepth((int)fingerPositions[i][0], (int)fingerPositions[i][1]);
- }
- else
- {
- fingerPixel[i].position.x = 0;
- fingerPixel[i].position.y = 0;
- fingerPixel[i].position.z = 0;
- }
- }
- foreach (IGraphicElement graphicElement in graphicElements)
- {
- graphicElement.draw(width, height);
- }
- SwapBuffers();
- source.releaseFrame();
- }
- 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 / 4, Width / (float)Height, 1.0f, 3000.0f);
- GL.MatrixMode(MatrixMode.Projection);
- GL.LoadMatrix(ref projection);
- }
- }
- }
|