Browse Source

-added VertexBufferObjects

Anton 11 years ago
parent
commit
1fd9616ab4
3 changed files with 183 additions and 56 deletions
  1. 18 13
      bbiwarg/Graphics/IGraphicElement.cs
  2. 95 6
      bbiwarg/Graphics/Output.cs
  3. 70 37
      bbiwarg/Graphics/Point.cs

+ 18 - 13
bbiwarg/Graphics/IGraphicElement.cs

@@ -1,13 +1,18 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace bbiwarg.Graphics
-{
-    interface IGraphicElement
-    {
-        void draw(int width, int height);
-    }
-}
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using MathNet.Numerics.LinearAlgebra.Single;
+
+namespace bbiwarg.Graphics
+{
+    interface IGraphicElement
+    {
+        void draw(int width, int height);
+        List<Vector> getVertices(int width, int height);
+        uint[] getTriangleIndices();
+        Color getColor();
+    }
+}

+ 95 - 6
bbiwarg/Graphics/Output.cs

@@ -18,7 +18,14 @@ namespace bbiwarg.Graphics
         private Point[,] depthPixels = new Point[320,240];
         private List<IGraphicElement> graphicElements = new List<IGraphicElement>();
         private Point palmPixel;
-        private Point[] fingerPixel = new Point[5];
+        private Point[] fingerPixel = new Point[5];
+        private int VBOid = new int();
+        private int IBOid = new int();
+        private float[] vertices = new float[0];
+        private uint[] triangles = new uint[0];
+        private int width = 320;
+        private int height = 240;
+
 
         public Output(IVideoDataSource source)
         {
@@ -57,8 +64,11 @@ namespace bbiwarg.Graphics
 
                 fingerPixel[i] = new Point(vertex, palmColor, palmSize);
                 graphicElements.Add(fingerPixel[i]);
-            }
-
+            }
+
+            initBuffers();
+            updateData(graphicElements);
+            
         }
 
         protected override void OnRenderFrame(FrameEventArgs e)
@@ -72,8 +82,8 @@ namespace bbiwarg.Graphics
             source.updateFrame();
 
             ImageData image = source.getImageData();
-            int width = image.getWidth();
-            int height = image.getHeight();
+            width = image.getWidth();
+            height = image.getHeight();
 
             for (int x = 0; x < width; x++) {
                 for (int y = 0; y < height; y++) {
@@ -106,11 +116,15 @@ namespace bbiwarg.Graphics
                     fingerPixel[i].position.z = 0;
                 }
             }
-
+            /*
                 foreach (IGraphicElement graphicElement in graphicElements)
                 {
                     graphicElement.draw(width, height);
                 }
+            */
+
+            updateData(graphicElements);
+            drawBuffer();
 
             SwapBuffers();
             source.releaseFrame();
@@ -124,6 +138,81 @@ namespace bbiwarg.Graphics
             Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 3000.0f);
             GL.MatrixMode(MatrixMode.Projection);
             GL.LoadMatrix(ref projection);
+        }
+
+        private void initBuffers()
+        {
+            GL.EnableClientState(ArrayCap.VertexArray);
+
+            GL.GenBuffers(1, out VBOid);
+            GL.GenBuffers(1, out IBOid);
+
+            GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid);
+            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StreamDraw);
+            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
+
+
+            GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBOid);
+            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(triangles.Length * sizeof(int)), triangles, BufferUsageHint.StaticDraw);
+            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
+        }
+
+        private void drawBuffer()
+        {
+            GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid);
+            GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBOid);
+
+            GL.InterleavedArrays(InterleavedArrayFormat.C3fV3f, 0, IntPtr.Zero);
+            GL.DrawElements(BeginMode.Triangles, triangles.Length, DrawElementsType.UnsignedInt, 0);
+
+            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
+            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
+        }
+
+        public void updateData(List<IGraphicElement> list)
+        {
+            List<float> verticesData = new List<float>();
+            List<uint> triangleData = new List<uint>();
+
+            foreach (IGraphicElement graphicElement in list)
+            {
+                List<Vector> elementVertices = graphicElement.getVertices(width, height);
+                uint[] elementTriangles = graphicElement.getTriangleIndices();
+                Color elementColor = graphicElement.getColor();
+
+                uint c = (uint)verticesData.Count / 6;
+                foreach (Vector elementVertex in elementVertices)
+                {
+                    verticesData.AddRange(convertColor(elementColor));
+                    verticesData.AddRange(elementVertex);
+                }
+                for (int i = 0; i < elementTriangles.Length; i++)
+                {
+                    triangleData.Add(elementTriangles[i] + c);
+                }
+            }
+
+            vertices = verticesData.ToArray();
+            triangles = triangleData.ToArray();
+
+
+            GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid);
+            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StreamDraw);
+            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
+
+            GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBOid);
+            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(triangles.Length * sizeof(int)), triangles, BufferUsageHint.StaticDraw);
+            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
+
+        }
+
+        private List<float> convertColor(Color color)
+        {
+            List<float> result = new List<float>();
+            result.Add(color.R / 255f);
+            result.Add(color.G / 255f);
+            result.Add(color.B / 255f);
+            return result;
         }
     }
 }

+ 70 - 37
bbiwarg/Graphics/Point.cs

@@ -1,37 +1,70 @@
-using System;
-using System.Collections.Generic;
-using System.Drawing;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using OpenTK.Graphics.OpenGL;
-
-namespace bbiwarg.Graphics
-{
-    class Point : IGraphicElement
-    {
-        public Vertex position;
-        public Color color;
-        public float size;
-
-        public Point(Vertex position, Color color, float size) {
-            this.position = position;
-            this.color = color;
-            this.size = size;
-        }
-
-        public void draw(int width, int height) {
-            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.End();
-        }
-    }
-}
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using OpenTK.Graphics.OpenGL;
+using MathNet.Numerics.LinearAlgebra.Single;
+
+namespace bbiwarg.Graphics
+{
+    class Point : IGraphicElement
+    {
+        public Vertex position;
+        public Color color;
+        public float size;
+
+        public Point(Vertex position, Color color, float size)
+        {
+            this.position = position;
+            this.color = color;
+            this.size = size;
+        }
+
+        public void draw(int width, int height)
+        {
+            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.End();
+        }
+
+        public List<Vector> getVertices(int width, int height)
+        {
+            float relX = position.x - width / 2;
+            float relY = height / 2 - position.y;
+
+            List<Vector> result = new List<Vector>();
+
+            Vector temp = new DenseVector(new float[] { relX - size, relY + size, -position.z });
+            result.Add(temp);
+            temp = new DenseVector(new float[] { relX + size, relY + size, -position.z });
+            result.Add(temp);
+            temp = new DenseVector(new float[] { relX + size, relY - size, -position.z });
+            result.Add(temp);
+            temp = new DenseVector(new float[] { relX - size, relY - size, -position.z });
+            result.Add(temp);
+
+            return result;
+
+        }
+
+        public uint[] getTriangleIndices()
+        {
+            return new uint[] { 0, 1, 2, 0, 2, 3 };
+        }
+
+        public Color getColor()
+        {
+            return color;
+        }
+    }
+}