Example #1
0
        private int AddVertex(Vertex v)
        {
            int index = 0;

            if (linechangedvertices.Count != 0)
            {
                index = linechangedvertices.Dequeue();
                _vbo.SetVertex(index, v);
            }
            if (availablevertices.Count != 0)
            {
                index = availablevertices.Dequeue();
                _vbo.SetVertex(index, v);
            }
            else
            {
                index = _vbo.AddVertex(v);
            }
            return(index);
        }
Example #2
0
        public void DrawPath(Path path, VBO vbo)
        {
            List <List <Vector2> > shapes     = new List <List <Vector2> >();
            List <Vector2>         positions  = new List <Vector2>();
            List <Vector2>         tesselated = new List <Vector2>();
            Point position = new Point();

            foreach (var op in path.Operations)
            {
                var mt = op as MoveTo;
                if (mt != null)
                {
                    shapes.Add(positions);
                    positions = new List <Vector2>();

                    positions.Add(Vec2(mt.Point));
                    position = mt.Point;
                    continue;
                }
                var lt = op as LineTo;
                if (lt != null)
                {
                    positions.Add(Vec2(lt.Point));
                    position = lt.Point;
                    continue;
                }
                var at = op as ArcTo;
                if (at != null)
                {
                    var p = nsvg.nsvg__pathArcTo((Vector2d)Vec2(position), at);
                    positions.AddRange(p);
                    position = at.Point;
                    continue;
                }
                var ct = op as CurveTo;
                if (ct != null)
                {
                    if (double.IsNaN(ct.Control1.X) && double.IsNaN(ct.Control1.Y))
                    {
                        BezierCurveQuadric b = new BezierCurveQuadric(Vec2(position), Vec2(ct.EndPoint),
                                                                      Vec2(ct.Control2));
                        Vector2 old = b.CalculatePoint(0f);
                        positions.Add(old);
                        var precision = 0.05f;
                        for (float i = precision; i < 1f + precision; i += precision)
                        {
                            Vector2 j = b.CalculatePoint(i);
                            positions.Add(j);
                            old = j;
                        }
                    }
                    else
                    {
                        BezierCurveCubic b = new BezierCurveCubic(Vec2(position), Vec2(ct.EndPoint), Vec2(ct.Control1),
                                                                  Vec2(ct.Control2));
                        Vector2 old = b.CalculatePoint(0f);
                        positions.Add(old);
                        var precision = 0.05f;
                        for (float i = precision; i < 1f + precision; i += precision)
                        {
                            Vector2 j = b.CalculatePoint(i);
                            positions.Add(j);
                        }
                    }
                    position = ct.EndPoint;
                }
                var cp = op as ClosePath;
                if (cp != null)
                {
                    if (positions.Count > 0)
                    {
                        positions.Add(positions[0]);
                        shapes.Add(positions);
                    }
                    positions = new List <Vector2>();
                    position  = new Point();
                    continue;
                }
            }
            shapes.Add(positions);

            brushcheck(path.Brush);
            LibTessDotNet.Tess t        = new LibTessDotNet.Tess();
            List <Vec3>        vertices = new List <Vec3>();

            foreach (var s in shapes)
            {
                ContourVertex[] cv = new ContourVertex[s.Count];
                for (int i = 0; i < s.Count; i++)
                {
                    var v = s[i];
                    cv[i] = new ContourVertex()
                    {
                        Position = new Vec3()
                        {
                            X = v.X, Y = v.Y
                        }
                    };
                }
                t.AddContour(cv);
            }
            var rule = LibTessDotNet.WindingRule.NonZero;

            if (path.Brush != null)
            {
                rule = path.Brush.FillMode == FillMode.EvenOdd ? LibTessDotNet.WindingRule.EvenOdd : LibTessDotNet.WindingRule.NonZero;
            }
            t.Tessellate(rule, LibTessDotNet.ElementType.Polygons, 3);
            for (var i = 0; i < t.ElementCount; i++)
            {
                for (var tri = 0; tri < 3; tri++)
                {
                    vertices.Add(t.Vertices[t.Elements[(i * 3) + tri]].Position);
                }
            }
            for (int i = 0; i < vertices.Count; i++)
            {
                tesselated.Add(new Vector2(vertices[i].X, vertices[i].Y));
            }
            var lineshapes = new List <List <Vector2> >();

            foreach (var s in shapes)
            {
                if (s.Count == 0)
                {
                    continue;
                }
                List <Vector2> add = new List <Vector2>();
                for (int i = 0; i < s.Count; i++)
                {
                    add.Add(new Vector2(s[i].X, s[i].Y));
                }
                lineshapes.Add(add);
            }

            if (path.Brush is SolidBrush || path.Pen != null)
            {
                if (path.Brush is SolidBrush)
                {
                    var sb = path.Brush as SolidBrush;
                    for (int i = 0; i < tesselated.Count; i++)
                    {
                        vbo.AddVertex(new Vertex(tesselated[i], color(sb.Color)));
                    }
                }
                if (path.Pen != null)
                {
                    foreach (var list in lineshapes)
                    {
                        foreach (var v in TesselateLines(list, path.Pen))
                        {
                            vbo.AddVertex(v);
                        }
                    }
                }
            }
        }