【问题标题】:C# Font display OpenGLC#字体显示OpenGL
【发布时间】:2013-04-18 00:13:42
【问题描述】:

我正在使用 Windows 中的 Tao 框架开发 2D CAD 应用程序。我想使用 Windows 库中的字体来显示绘图信息。除此之外,我想旋转缩放我的文本。使用位图字体我无法做到这一点。

我完成了 OpenGL 字体调查 [http://www.opengl.org/archives/resources/features/fontsurvey/],但其中大部分是基于 C++ 的 API。

您能指导我在 C# 中有哪些可用的解决方案吗?

【问题讨论】:

  • 无论 API 是什么,这些技术都很强大。实现诸如位图字体之类的东西应该是一项相当简单的任务。
  • @JustinMeiners 我创建了一种位图字体。但我想旋转,缩放字体。使用位图我不能做这种事情。我想将 windows 字体带到我的应用程序中。
  • 我明白了——寻找图书馆的好理由。

标签: c# opengl


【解决方案1】:

对于 3D,我遵循了与 OpenTK 相关的示例,该示例与 Tao.Framework 兼容。

    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;
    }

【讨论】:

  • 太棒了。我想在 2D 中实现它并回复你。谢谢你快速的回复。我会检查。如果有其他选择,请告诉我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多