Просмотр исходного кода

-improved performance for getVertexArray (still takes 40ms per frame, ToDo: Alex)
-all graphics work on camera-coordinates now (not on pixels+depth)
-commented out buffer-draw and used old draw again (better performance)

Alexander Hendrich 11 лет назад
Родитель
Сommit
fd21a84397

+ 16 - 13
bbiwarg/DataSource/IisuDataSource.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Drawing;
+using System.Diagnostics;
 using System.Runtime.InteropServices;
 using Iisu;
 using MathNet.Numerics.LinearAlgebra.Single;
@@ -37,6 +38,10 @@ namespace bbiwarg.DataSource
         private IParameterHandle<float> vfov;
 
         private ImageData currentImage;
+        private double maxU;
+        private double maxV;
+        private int width;
+        private int height;
 
         /*
          * Creates an Iisu data source.
@@ -69,6 +74,8 @@ namespace bbiwarg.DataSource
             frameRate = device.RegisterParameterHandle<float>("SOURCE.FrameRate");
             hfov = device.RegisterParameterHandle<float>("SOURCE.CAMERA.DEPTH.HFOV");
             vfov = device.RegisterParameterHandle<float>("SOURCE.CAMERA.DEPTH.VFOV");
+            maxU = Math.Tan(hfov.Value / 2f);
+            maxV = Math.Tan(vfov.Value / 2f);
 
             // events
             device.EventManager.RegisterEventListener("DEVICE.Status", new Iisu.EventDelegates.Device.Status(onDeviceStatusChanged));
@@ -225,6 +232,8 @@ namespace bbiwarg.DataSource
 
         private void createImageData() {
             currentImage = new ImageData(getDepthImage(), getConfidenceImage(), getColorImage(), getUVImage());
+            width = currentImage.getWidth();
+            height = currentImage.getHeight();
         }
 
         private void checkHandIndex(uint handIndex) 
@@ -349,13 +358,9 @@ namespace bbiwarg.DataSource
 
         public VertexArray getVertexArray()
         {
-            int width = currentImage.getWidth();
-            int height = currentImage.getHeight();
-
             Vertex[] vertices = new Vertex[width * height];
             Color[] colors = new Color[width * height];
 
-
             int index = 0;
             for (int x = 0; x < width; x++)
             {
@@ -367,28 +372,26 @@ namespace bbiwarg.DataSource
                     index++;
                 }
             }
+
+
             return new VertexArray(vertices, colors);
         }
 
         private Vertex create3DVertexFrom2D(float pixelX, float pixelY, int depth)
         {
-            int width = currentImage.getWidth();
-            int height = currentImage.getHeight();
-            float alphaX = hfov.Value;
-            float alphaY = vfov.Value;
             float convertedDepth = depth / 1000f; // mm into m
 
             float u = (pixelX / (float)width - 0.5f) * 2f;
             float v = ((1 - pixelY / (float)height) - 0.5f) * 2f;
 
-            float relX = (float)(u * Math.Tan(alphaX / 2f));
-            float relY = (float)(v * Math.Tan(alphaY / 2f));
+            double relX = (u * maxU);
+            double relY = (v * maxV);
 
             float z = (float)(convertedDepth / Math.Sqrt(1 + relX * relX + relY * relY));
-            float x = relX * z;
-            float y = relY * z;
+            float x = (float)(relX * z);
+            float y = (float)(relY * z);
 
-            return new Vertex(x, y, z );
+            return new Vertex(x, y, z);
         }
     }
 }

+ 5 - 2
bbiwarg/DataSource/ImageData.cs

@@ -53,8 +53,11 @@ namespace bbiwarg.DataSource
             if (u < 0 || v < 0)
                 return Color.Black;
 
-            int xInColorImage = (int) (u * colorImage.getWidth()) % colorImage.getWidth();
-            int yInColorImage = (int) (v * colorImage.getHeight()) % colorImage.getHeight();
+            int colorImageWidth = colorImage.getWidth();
+            int colorImageHeight = colorImage.getHeight();
+
+            int xInColorImage = (int) (u * colorImageWidth) % colorImageWidth;
+            int yInColorImage = (int) (v * colorImageHeight) % colorImageHeight;
             return colorImage.getColor(xInColorImage, yInColorImage);
         }
     }

+ 13 - 0
bbiwarg/DataSource/VertexArray.cs

@@ -18,5 +18,18 @@ namespace bbiwarg.DataSource
             this.vertices = vertices;
             this.colors = colors;
         }
+
+        public int getNumVertices() 
+        {
+            return vertices.Length;
+        }
+
+        public Vertex getVertex(int i) {
+            return vertices[i];
+        }
+
+        public Color getColor(int i) {
+            return colors[i];
+        }
     }
 }

+ 2 - 2
bbiwarg/Graphics/IGraphicElement.cs

@@ -10,8 +10,8 @@ namespace bbiwarg.Graphics
 {
     interface IGraphicElement
     {
-        void draw(int width, int height);
-        List<Vector> getVertices(int width, int height);
+        void draw();
+        List<Vector> getVertices();
         uint[] getTriangleIndices();
         Color getColor();
     }

+ 39 - 32
bbiwarg/Graphics/Output.cs

@@ -15,7 +15,7 @@ namespace bbiwarg.Graphics
     class Output : GameWindow
     {
         private IVideoDataSource source;
-        private Point[,] depthPixels = new Point[320,240];
+        private Point[] depthPixels;
         private ImageData currentImage;
 
         private List<IGraphicElement> graphicElements = new List<IGraphicElement>();
@@ -24,7 +24,7 @@ namespace bbiwarg.Graphics
         private float[] vertices = new float[0];
         private uint[] triangles = new uint[0];
 
-        private float maxDelta = 0;
+        private Point palmPoint;
 
 
         public Output(IVideoDataSource source)
@@ -40,7 +40,7 @@ namespace bbiwarg.Graphics
             GL.ClearColor(Color.Black);
 
             initializeDepthPixels();
-            initBuffers();
+            //initBuffers();
         }
 
         protected override void OnRenderFrame(FrameEventArgs e)
@@ -55,10 +55,14 @@ namespace bbiwarg.Graphics
             source.updateFrame();
             currentImage = source.getImageData();
 
-            updateDepthPixels();
-
-            updateBuffer();
-            drawBuffer();
+            updateDepthPixels();
+            
+            foreach (IGraphicElement graphicElement in graphicElements) {
+                graphicElement.draw();
+            }
+            
+            //updateBuffer();
+            //drawBuffer();
 
             SwapBuffers();
         }
@@ -67,43 +71,46 @@ namespace bbiwarg.Graphics
         {
             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);
+            Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
             GL.MatrixMode(MatrixMode.Projection);
             GL.LoadMatrix(ref projection);
         }
 
         private void initializeDepthPixels()
         {
-            int width = currentImage.getWidth();
-            int height = currentImage.getHeight();
-
-            for (int x = 0; x < width; x++)
-            {
-                for (int y = 0; y < height; 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);
-                }
+            VertexArray vertexArray = source.getVertexArray();
+            int numVertices = vertexArray.getNumVertices();
+            depthPixels = new Point[numVertices];
+
+            float size = 0.001f;
+            for (int i = 0; i < numVertices; i++) {
+                Vertex vertex = vertexArray.getVertex(i);
+                Color color = vertexArray.getColor(i);
+                Point pixel = new Point(vertex, color, size);
+                depthPixels[i] = pixel;
+                graphicElements.Add(pixel);
             }
+
+            Vector palmPosition = source.getPalmPosition3D(1);
+            Vertex palmVertex = new Vertex(palmPosition[0], palmPosition[2], palmPosition[1]);
+            palmPoint = new Point(palmVertex, Color.Yellow, 0.005f);
+            graphicElements.Add(palmPoint);
         }
 
         private void updateDepthPixels()
         {
-            for (int x = 0; x < currentImage.getWidth(); x++)
-            {
-                for (int y = 0; y < currentImage.getHeight(); y++)
-                {
-                    Point depthPixel = depthPixels[x, y];
+            VertexArray vertexArray = source.getVertexArray();
+            int numVertices = vertexArray.getNumVertices();
 
-                    depthPixel.position.z = currentImage.getDepth(x, y);
-                    depthPixel.color = currentImage.getColor(x, y);
-                }
+            for (int i = 0; i < numVertices; i++)
+            {
+                depthPixels[i].position = vertexArray.getVertex(i);
+                depthPixels[i].color = vertexArray.getColor(i);
             }
+            Vector palmPosition = source.getPalmPosition3D(1);
+            palmPoint.position.x = palmPosition[0];
+            palmPoint.position.y = palmPosition[2];
+            palmPoint.position.z = palmPosition[1];
         }
 
         private void initBuffers()
@@ -130,7 +137,7 @@ namespace bbiwarg.Graphics
 
             foreach (IGraphicElement graphicElement in graphicElements)
             {
-                List<Vector> elementVertices = graphicElement.getVertices(currentImage.getWidth(), currentImage.getHeight());
+                List<Vector> elementVertices = graphicElement.getVertices();
                 uint[] elementTriangles = graphicElement.getTriangleIndices();
                 Color elementColor = graphicElement.getColor();
 

+ 10 - 15
bbiwarg/Graphics/Point.cs

@@ -22,32 +22,27 @@ namespace bbiwarg.Graphics
             this.size = size;
         }
 
-        public void draw(int width, int height)
+        public void draw()
         {
             GL.Color4(color);
 
-            float relX = position.x - width / 2;
-            float relY = height / 2 - position.y;
-
             GL.Begin(BeginMode.Polygon);
-            GL.Vertex3(relX - size, relY + size, -position.z);
-            GL.Vertex3(relX + size, relY + size, -position.z);
-            GL.Vertex3(relX + size, relY - size, -position.z);
-            GL.Vertex3(relX - size, relY - size, -position.z);
+            GL.Vertex3(position.x - size, position.y + size, -position.z);
+            GL.Vertex3(position.x + size, position.y + size, -position.z);
+            GL.Vertex3(position.x + size, position.y - size, -position.z);
+            GL.Vertex3(position.x - size, position.y - size, -position.z);
             GL.End();
         }
 
-        public List<Vector> getVertices(int width, int height)
+        public List<Vector> getVertices()
         {
-            float relX = position.x - width / 2;
-            float relY = height / 2 - position.y;
 
             List<Vector> result = new List<Vector>();
 
-            result.Add(new DenseVector(new float[] { relX - size, relY + size, -position.z }));
-            result.Add(new DenseVector(new float[] { relX + size, relY + size, -position.z }));
-            result.Add(new DenseVector(new float[] { relX + size, relY - size, -position.z }));
-            result.Add(new DenseVector(new float[] { relX - size, relY - size, -position.z }));
+            result.Add(new DenseVector(new float[] { position.x - size, position.y + size, -position.z }));
+            result.Add(new DenseVector(new float[] { position.x + size, position.y + size, -position.z }));
+            result.Add(new DenseVector(new float[] { position.x + size, position.y - size, -position.z }));
+            result.Add(new DenseVector(new float[] { position.x - size, position.y - size, -position.z }));
 
             return result;
 

+ 1 - 1
bbiwarg/Graphics/Vertex.cs

@@ -15,7 +15,7 @@ namespace bbiwarg.Graphics
         public Vertex(float x, float y, float z) {
             this.x = x;
             this.y = y;
-            this.z = z;// 300;
+            this.z = z;
         }
     }
 }