示例#1
0
        public static TextMesh Build(TextNode node, double scale)
        {
            Font font = node.Font;

            List<Vector2> verticies = new List<Vector2>();
            List<Vector2> textureCoords = new List<Vector2>();

            double x = 0, y = 0;
            double maxX = 0, maxY = 0;
            double texX = 0, texY = 0;
            double texXMax = 0, texYMax = 0;
            int xOffset = 0;
            Glyph glyph = null;

            foreach (TextLine line in node.Lines())
            {
                // We append them to to verticies.
                foreach (char character in line.Text)
                {
                    // Build each character.
                    glyph = font.GetGlyph(character);

                    if (glyph == null)
                        continue; // Skip -> We should render a square instead.

                    x = node.Position.X + (glyph.Offset.X * scale) + xOffset;
                    y = node.Position.Y + (glyph.Offset.Y * scale);
                    maxX = x + (glyph.Size.X * scale);
                    maxY = y + (glyph.Size.Y * scale);

                    verticies.Add(new Vector2((float)x, (float)y));
                    verticies.Add(new Vector2((float)x, (float)maxY));
                    verticies.Add(new Vector2((float)maxX, (float)y));
                    verticies.Add(new Vector2((float)maxX, (float)y));
                    verticies.Add(new Vector2((float)x, (float)maxY));
                    verticies.Add(new Vector2((float)maxX, (float)maxY));

                    texX = glyph.Position.X / font.ScaleW;
                    texY = glyph.Position.Y / font.ScaleH;
                    texXMax = (glyph.Position.X + glyph.Size.X) / font.ScaleW;
                    texYMax = (glyph.Position.Y + glyph.Size.Y) / font.ScaleH;

                    textureCoords.Add(new Vector2((float)texX, (float)texY));
                    textureCoords.Add(new Vector2((float)texX, (float)texYMax));
                    textureCoords.Add(new Vector2((float)texXMax, (float)texY));
                    textureCoords.Add(new Vector2((float)texXMax, (float)texY));
                    textureCoords.Add(new Vector2((float)texX, (float)texYMax));
                    textureCoords.Add(new Vector2((float)texXMax, (float)texYMax));

                    xOffset += (int)Math.Round(glyph.XAdvance * scale);
                }
            }

            return new TextMesh(Utils.VectorConvertor.Vector2ToFloatArray(verticies.ToArray()), verticies.Count, Utils.VectorConvertor.Vector2ToFloatArray(textureCoords.ToArray()));
        }
示例#2
0
        public void Render(string value, double pointScale, Vector2 position, Vector3 color)
        {
            TextNode node = new TextNode(this, value, position, NormalizedScale * pointScale);

            manager.Shader.Bind();

            manager.Shader.SetUniform("color", color);

            node.PreRender();
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            GL.Disable(EnableCap.DepthTest);

            // Bind texture.
            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2D, TextureId);

            node.Render();

            GL.Enable(EnableCap.DepthTest);

            GL.Disable(EnableCap.Blend);

            node.PostRender();

            manager.Shader.Unbind();

            GL.BindTexture(TextureTarget.Texture2D, 0);
        }