C# Font display OpenGL

浪尽此生 提交于 2021-01-27 17:13:36

问题


I am developing a 2D CAD application by using Tao framework in windows. I would like to use the fonts from windows libraries to display the drawing information. In addition to that I would like to rotate scale my text. With Bitmap fonts I could not do this.

I went through OpenGL font survey [http://www.opengl.org/archives/resources/features/fontsurvey/] but most of them are C++ based APIs.

Could you guide me what are the available solutions in C#?


回答1:


For 3D I have followed the examples I found with relation to OpenTK which is compatible with the Tao.Framework.

Screen

    public void AddTexture(Bitmap texture, bool mipmaped)
    {
        this.tex_id = World.LoadTexture(texture, mipmaped);
    }
    public void AddText(string text, Color color, float x, float y, float scale)
    {
        const int side = 256;
        Bitmap texture = new Bitmap(side, side, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        Graphics g = Graphics.FromImage(texture);
        using (Brush brush = new SolidBrush(Color))
        {
            g.FillRectangle(brush, new Rectangle(Point.Empty, texture.Size));
        }
        using (Font font = new Font(SystemFonts.DialogFont.FontFamily,  12f))
        {
            SizeF sz = g.MeasureString(text, font);
            float f = 256 / Math.Max(sz.Width, sz.Height) * scale;
            g.TranslateTransform(256 / 2 + f * sz.Width / 2, 256 / 2 - f * sz.Height / 2);
            g.ScaleTransform(-f, f);
            using (Brush brush = new SolidBrush(color))
            {
                g.DrawString(text, font, brush, 0, 0);
            }
        }
        AddTexture(texture, true);
    }

    public static int LoadTexture(Bitmap texture, bool mipmaped)
    {
        int id = gl.GenTexture();
        gl.BindTexture(TextureTarget.Texture2D, id);
        int wt = texture.Width;
        int ht = texture.Height;

        gl.TexParameter(TextureTarget.Texture2D,
            TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
        gl.TexParameter(TextureTarget.Texture2D,
            TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);

        System.Drawing.Imaging.BitmapData data = texture.LockBits(
            new Rectangle(0, 0, wt, ht),
            System.Drawing.Imaging.ImageLockMode.ReadOnly,
            System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        gl.TexImage2D(TextureTarget.Texture2D, 0,
            PixelInternalFormat.Rgba, wt, ht, 0,
            PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);

        texture.UnlockBits(data);

        if (mipmaped)
        {

            gl.GenerateMipmap(GenerateMipmapTarget.Texture2D);
            gl.TexParameter(TextureTarget.Texture2D,
                TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
        }
        else
        {
            gl.TexParameter(TextureTarget.Texture2D,
                TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
        }
        gl.TexParameter(TextureTarget.Texture2D,
            TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);

        return id;
    }


来源:https://stackoverflow.com/questions/16072433/c-sharp-font-display-opengl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!