Example #1
0
        private unsafe void UpdateTexCoordBuffer(string text)
        {
            GlyphServer server        = this.glyphServer;
            float       textureWidth  = server.TextureWidth;
            float       textureHeight = server.TextureHeight;
            //float maxHeight = server.MaxHeight;
            //float maxDiff = server.MaxDiff;
            Dictionary <char, GlyphInfo> dict = server.CharGlyphDict;
            //float cursorX = 0, cursorY = 0;
            var glyphTexCoord = (GlyphTexCoord *)this.texCoordBuffer.MapBuffer(MapBufferAccess.ReadWrite);
            int index         = 0;

            for (int i = 0; i < this.Capacity && i < text.Length; i++)
            {
                char      c = text[i];
                GlyphInfo glyphInfo;
                if (dict.TryGetValue(c, out glyphInfo))
                {
                    float left   = (glyphInfo.x) / textureWidth;
                    float right  = (glyphInfo.x + glyphInfo.width) / textureWidth;
                    float bottom = (glyphInfo.y) / textureHeight;
                    float top    = (glyphInfo.y + glyphInfo.height) / textureHeight;
                    glyphTexCoord[index] = new GlyphTexCoord(
                        new vec2(left, top),
                        new vec2(left, bottom),
                        new vec2(right, bottom),
                        new vec2(right, top)
                        );
                    index++;
                }
            }
            this.texCoordBuffer.UnmapBuffer();
        }
Example #2
0
        public void RenderBeforeChildren(RenderEventArgs arg)
        {
            if (!this.IsInitialized)
            {
                this.Initialize();
            }

            var           method  = this.RenderUnit.Methods[0]; // the only render unit in this node.
            ShaderProgram program = method.Program;

            SingleLineModel lineModel   = this.singleLineModel;
            GlyphServer     glyphServer = lineModel.GetGlyphServer();
            Texture         texture     = glyphServer.GlyphsTexture;

            if (texture != null)
            {
                program.SetUniform("glyphTexture", texture);
            }
            var viewport = new int[4];

            GL.Instance.GetIntegerv((uint)GetTarget.Viewport, viewport);
            ICamera camera     = arg.Camera;
            mat4    projection = camera.GetProjectionMatrix();
            mat4    view       = camera.GetViewMatrix();
            mat4    model      = this.GetModelMatrix();

            program.SetUniform("mvpMat", projection * view * model);
            program.SetUniform("screenSize", new ivec2(viewport[2], viewport[3]));
            program.SetUniform("lineSize", new vec2(lineModel.LineWidth, lineModel.LineHeight));
            program.SetUniform("textColor", this.textColor);
            program.SetUniform("backgroundColor", Color.SkyBlue.ToVec4());
            program.SetUniform("fontSize", this.fontSize);

            method.Render();
        }
Example #3
0
        private unsafe void UpdatePositionBuffer(string text)
        {
            GlyphServer server = this.glyphServer;
            float       maxHeight = server.MaxHeight;
            float       maxDiff = server.MaxDiff;
            Dictionary <char, GlyphInfo> dict = server.CharGlyphDict;
            float cursorX = 0, cursorY = 0;
            var   glyphPositions = (GlyphPosition *)this.positionBuffer.MapBuffer(MapBufferAccess.ReadWrite);

            for (int i = 0; i < this.Capacity && i < text.Length; i++)
            {
                char      c = text[i];
                GlyphInfo glyphInfo;
                if (dict.TryGetValue(c, out glyphInfo))
                {
                    float left   = cursorX + glyphInfo.xOffset;
                    float right  = left + glyphInfo.width;
                    float bottom = cursorY + maxHeight - glyphInfo.yOffset - maxDiff;
                    float top    = bottom + glyphInfo.height;
                    glyphPositions[i] = new GlyphPosition(
                        new vec2(left, top),
                        new vec2(left, bottom),
                        new vec2(right, bottom),
                        new vec2(right, top)
                        );
                    cursorX += glyphInfo.xAdvance;
                }
            }
            // move glyphs to center in model space.
            //float halfWidth = cursorX / 2, halfHeight = maxHeight / 2;
            //for (int i = 0; i < text.Length; i++)
            //{
            //    GlyphPosition position = glyphPositions[i];
            //    float left = position.leftBottom.x;
            //    float right = position.rightBottom.x;
            //    float bottom = position.leftBottom.y;
            //    float top = position.leftTop.y;
            //    left -= halfWidth; right -= halfWidth;
            //    bottom -= halfHeight; top -= halfHeight;
            //    left /= cursorX; right /= cursorX;
            //    bottom /= maxHeight; top /= maxHeight;
            //    glyphPositions[index] = new GlyphPosition(
            //        new vec2(left, top),
            //        new vec2(left, bottom),
            //        new vec2(right, bottom),
            //        new vec2(right, top)
            //        );
            //}
            this.positionBuffer.UnmapBuffer();

            this.lineWidth  = cursorX;
            this.lineHeight = maxHeight;
        }
Example #4
0
        public static SingleLineNode Create(int capacity, GlyphServer glyphServer)
        {
            var vs       = new VertexShader(vertexCode);
            var fs       = new FragmentShader(fragmentCode);
            var provider = new ShaderArray(vs, fs);
            var map      = new AttributeMap();

            map.Add("inPosition", SingleLineModel.strPosition);
            map.Add("inTexCoord", SingleLineModel.strTexCoord);
            var builder = new RenderMethodBuilder(provider, map, new BlendSwitch());
            var node    = new SingleLineNode(new SingleLineModel(capacity, glyphServer), builder);

            node.Initialize();

            return(node);
        }
Example #5
0
        //private SceneNodeBase GetRootElement()
        //{
        //    var rectangle = DistanceFieldNode.Create();
        //    rectangle.Scale *= 3;
        //    string folder = System.Windows.Forms.Application.StartupPath;
        //    rectangle.TextureSource = new TextureSource(System.IO.Path.Combine(folder, @"texture2D.png"));

        //    var group = new GroupNode(rectangle);//, blend, blend2);

        //    var axis = AxisNode.Create();
        //    group.Children.Add(axis);
        //    return group;
        //}

        private SceneNodeBase GetRootElement()
        {
            var group = new GroupNode();

            {
                string      dictFilename   = "VeraMoBI.ttf_sdf.txt";
                string      glyphsFilename = "VeraMoBI.ttf_sdf.png";
                GlyphServer server         = GlyphServer.Load(dictFilename, glyphsFilename);
                var         node           = SingleLineNode.Create(100, server);
                node.TextColor = Color.Red;
                node.Text      = "The quick brown fox jumps over a lazy dog!";
                group.Children.Add(node);
                this.singleLineNode = node;
            }
            {
                var axis = AxisNode.Create();
                group.Children.Add(axis);
            }

            return(group);
        }
Example #6
0
        public static GlyphServer Load(string dictFilename, string glyphsFilename)
        {
            Dictionary <char, GlyphInfo> dict = SDFDictParser.Parse(dictFilename);
            float maxHeight, maxDiff;

            GetHeight(dict, out maxHeight, out maxDiff);
            Texture texture = LoadTexture(glyphsFilename);
            int     textureWidth, textureHeight;

            GetTextureSize(glyphsFilename, out textureWidth, out textureHeight);

            var result = new GlyphServer();

            result.charGlyphDict = dict;
            result.maxHeight     = maxHeight;
            result.maxDiff       = maxDiff;
            result.glyphsTexture = texture;
            result.textureWidth  = textureWidth;
            result.textureHeight = textureHeight;

            return(result);
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="capacity"></param>
 /// <param name="glyphServer"></param>
 public SingleLineModel(int capacity, GlyphServer glyphServer)
 {
     this.Capacity    = capacity;
     this.glyphServer = glyphServer;
 }