Пример #1
0
        void Upload()
        {
            TextSettings settings = new TextSettings()
            {
                Size = 1,
                Weight = weight,
                Border = border,
                Smooth = smooth,
                Color = Color,
                BorderColor = borderColor,
                LineHeight = lineheight,
                GlyphSpacing = glyphSpacing,
                HorizontalAlignment = horizontalAlignment,
                VerticalAlignment = verticalAlignment,
                MaxWidth = 0
            };

            vertices = Font.TextToVertices(text, settings);

            List<int> tmp = new List<int>();

            Width = 0;
            Height = 0;

            float minw = 0f, minh = 0f;

            for (int i = 0; i < vertices.Length / 4; i++)
            {
                // 1st triangle
                tmp.Add(i * 4 + 0);
                tmp.Add(i * 4 + 1);
                tmp.Add(i * 4 + 2);
                // 2nd triangle
                tmp.Add(i * 4 + 2);
                tmp.Add(i * 4 + 1);
                tmp.Add(i * 4 + 3);

                for (int o = 0; o < 4; o++ )
                {
                    if (vertices[i * 4 + o].Vertex.X > Width)
                    {
                        Width = vertices[i * 4 + o].Vertex.X;
                    }

                    if (vertices[i * 4 + o].Vertex.Y > Height)
                    {
                        Height = vertices[i * 4 + o].Vertex.Y;
                    }

                    if (vertices[i * 4 + o].Vertex.X < minw)
                    {
                        minw = vertices[i * 4 + o].Vertex.X;
                    }

                    if (vertices[i * 4 + o].Vertex.Y < minh)
                    {
                        minh = vertices[i * 4 + o].Vertex.Y;
                    }
                }

            }

            if(horizontalAlignment == Fonts.HorizontalAlignment.Centered)
            {
                Width *= 2;
            }
            else if(horizontalAlignment == Fonts.HorizontalAlignment.Right)
            {
                Width = -minw;
            }

            if(verticalAlignment == Fonts.VerticalAlignment.Middle)
            {
                Height *= -2;
            }
            else if(verticalAlignment == Fonts.VerticalAlignment.Bottom)
            {
                Height = minh;
            }

            Width *= fontsize;
            Height *= fontsize;

            indices = tmp.ToArray();

            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(vertices.Length * GPUGlyph.SizeInBytes), vertices, BufferUsageHint.DynamicDraw);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            GL.BindBuffer(BufferTarget.ElementArrayBuffer, ebo);
            GL.BufferData(BufferTarget.ElementArrayBuffer, new IntPtr(indices.Length * sizeof(int)), indices, BufferUsageHint.DynamicDraw);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
        }
Пример #2
0
        public GPUGlyph[] TextToVertices(string text, TextSettings settings)
        {
            if(Glyphs == null)
                return null;

            var vertices = new List<GPUGlyph>();

            Vector2 vOffset = Vector2.Zero;

            //vOffset.Y += settings.LineHeight / 2;

            int index = 0;
            List<int> linestarts = new List<int>();
            linestarts.Add(0);

            foreach(char c in text)
            {
                Glyph glyph;
                if(Glyphs.TryGetValue(c, out glyph))
                {
                    var gpuglyph = glyph.GetGPUGlyph(vOffset, settings.Size, settings.Weight, settings.Border, settings.Smooth, settings.Color, settings.BorderColor);

                    vertices.AddRange(gpuglyph);
                    vOffset.X += glyph.XAdvance * settings.GlyphSpacing;

                    index += gpuglyph.Length;

                    if(settings.MaxWidth > 0 && settings.MaxWidth <= vOffset.X)
                    {
                        vOffset.Y += settings.LineHeight;
                        vOffset.X = 0;

                        linestarts.Add(index);
                    }
                }
                else
                {
                    if(c == '\n')
                    {
                        vOffset.Y += settings.LineHeight;
                        vOffset.X = 0;

                        linestarts.Add(index);
                    }
                    else
                    {
                        vOffset.X += settings.Size * 0.5f;
                    }

                }

                if (settings.MaxHeight > 0 && settings.MaxHeight <= vOffset.Y)
                    break;
            }

            if(settings.HorizontalAlignment != HorizontalAlignment.Left || settings.VerticalAlignment != VerticalAlignment.Top)
            {
                int lines = linestarts.Count;

                float voffset = 0;

                if (settings.VerticalAlignment == VerticalAlignment.Middle)
                {
                    var bottompos = vertices[vertices.Count - 1].Vertex.Y;
                    voffset = -bottompos / 2.0f;
                }
                else if (settings.VerticalAlignment == VerticalAlignment.Bottom)
                {
                    var bottompos = vertices[vertices.Count - 1].Vertex.Y;
                    voffset = -bottompos;
                }

                for(int i = 0; i < lines; i++)
                {
                    var linestart = linestarts[i];
                    int lineend = vertices.Count;

                    if(i+1 != lines)
                    {
                        lineend = linestarts[i + 1];
                    }

                    Vector2 offset = new Vector2(0, voffset);

                    if (settings.HorizontalAlignment == HorizontalAlignment.Centered)
                    {
                        var rightpos = vertices[lineend - 1].Vertex.X;
                        offset += new Vector2(-rightpos / 2, 0);
                    }
                    else if (settings.HorizontalAlignment == HorizontalAlignment.Right)
                    {
                        var rightpos = vertices[lineend - 1].Vertex.X;
                        offset += new Vector2(-rightpos, 0);
                    }

                    for (int vIndex = linestart; vIndex < lineend; vIndex++)
                    {
                        var tmp = vertices[vIndex];
                        tmp.Vertex += offset;
                        vertices[vIndex] = tmp;
                    }
                }
            }

            return vertices.ToArray();
        }