【问题标题】:Draw text using OpenTK使用 OpenTK 绘制文本
【发布时间】:2014-11-18 18:47:26
【问题描述】:

我正在疯狂地尝试使用 OpenTK 在 OpenGL 窗口上绘制一些文本! 我遵循了一些教程,但无法使其工作,当我启用绘制文本的纹理时,我只有一个白色窗口,而我正在为测试绘制的 QUAD 就消失了。 如果有人有时间检查代码,它在下面。我还可以发送我的测试程序以更快地检查它。提前感谢您对此提供的任何帮助。

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

using OpenTK.Graphics.OpenGL;

using System.Diagnostics;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Bitmap textBmp;
        int textTexture = -1;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (!glControl1.Context.IsCurrent)
            {
                glControl1.MakeCurrent();
            }

            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();

            GL.Ortho(0, glControl1.Width, 0, glControl1.Height, -1000, 1000);

            GL.Scale(1, 1, 1);

            GL.Viewport(0, 0, glControl1.Width, glControl1.Height);

            GL.ClearColor(Color.White);

            // Better point and line drawing
            GL.Hint(HintTarget.PointSmoothHint, HintMode.Nicest);
            GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest);

            GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);

            GL.Enable(EnableCap.PointSmooth);
            GL.Enable(EnableCap.LineSmooth);

            GL.Enable(EnableCap.Blend);

            // Hide stuff behind in 3D
            GL.Enable(EnableCap.DepthTest);

            // Enable the texture
            GL.Enable(EnableCap.Texture2D);

            // Create Bitmap and OpenGL texture
            textBmp = new Bitmap((int)glControl1.Width, (int)glControl1.Height);

            textTexture = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, textTexture);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, textBmp.Width, textBmp.Height, 0,
                            OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);

            ErrorCode errorCode = GL.GetError();
            Debug.Assert(errorCode == ErrorCode.NoError, "OpenTK error!");
        }

        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            ErrorCode errorCode;

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);

            GL.PushMatrix();

            GL.Color3(Color.Black);

            GL.Begin(PrimitiveType.Quads);

            GL.Vertex3(10, 10, 10);
            GL.Vertex3(40, 10, 10);
            GL.Vertex3(40, 50, 10);
            GL.Vertex3(10, 50, 10);

            GL.End();

            if (textBmp != null)
            {
                using (Graphics gfx = Graphics.FromImage(textBmp))
                {
                    gfx.Clear(Color.Transparent);
                    gfx.DrawString("text", new Font("Arial", 10), Brushes.Black, new PointF(textBmp.Width / 2, textBmp.Height));
                }

                BitmapData data = textBmp.LockBits(new Rectangle(0, 0, textBmp.Width, textBmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, (int)glControl1.Width, (int)glControl1.Height, 0,
                    OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);

                textBmp.UnlockBits(data);

                errorCode = GL.GetError();
                Debug.Assert(errorCode == ErrorCode.NoError, "OpenTK error!");

                GL.Begin(PrimitiveType.Quads);
                GL.TexCoord2(0f, 1f); GL.Vertex2(0f, 0f);
                GL.TexCoord2(1f, 1f); GL.Vertex2(1f, 0f);
                GL.TexCoord2(1f, 0f); GL.Vertex2(1f, 1f);
                GL.TexCoord2(0f, 0f); GL.Vertex2(0f, 1f);
                GL.End();
            }

            errorCode = GL.GetError();
            Debug.Assert(errorCode == ErrorCode.NoError, "OpenTK error!");

            glControl1.SwapBuffers();
        }
    }
}

【问题讨论】:

  • 您想要常规的 2D GDI 文本,还是放置在 3D 表面上的纹理文本?
  • 只是放置在轴附近的常规 2D 文本和几个图例,没有花哨的东西。
  • 查找在 OpenTK 上添加 FPS 仪表。我认为它向您展示了如何添加文本覆盖。
  • 它开始工作了!我正在使用 OpenTK 源代码项目附带的 Examples\OpenGL\1.x 文件夹中的 TextRendering.cs 代码。一旦我弄清楚了,我会在这里发布我的结论。
  • 太好了,请为以后的用户做。如果您的代码有效,请作为答案发布,并附上屏幕截图。

标签: c# opengl opentk


【解决方案1】:

好的,我终于成功了。

在初始化时我刚刚做了:

if (!control.Context.IsCurrent)
{
    control.MakeCurrent();
}

GL.Ortho(0, controlWidth, 0, controlHeight, -1000, 1000);
GL.Scale(1, -1, 1); // I work with a top/left image and openGL is bottom/left
GL.Viewport(0, 0, controlWidth, controlHeight);
GL.ClearColor(Color.White);
GL.Hint(HintTarget.PointSmoothHint, HintMode.Nicest);
GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest);
GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
GL.PolygonMode(MaterialFace.Front, PolygonMode.Line);
GL.Enable(EnableCap.PointSmooth);
GL.Enable(EnableCap.LineSmooth);
GL.Enable(EnableCap.Blend);
GL.Enable(EnableCap.DepthTest);
GL.ShadeModel(ShadingModel.Smooth);
GL.Enable(EnableCap.AutoNormal);

bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
gfx = Graphics.FromImage(bmp);
gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

texture = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, texture);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp.Width, bmp.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);

然后在位图中写入文字:

gfx.DrawString(text, font, brush, new PointF(x, y));

然后渲染:

if (!control.Context.IsCurrent)
{
    control.MakeCurrent();
}

GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

GL.MatrixMode(MatrixMode.Modelview);

GL.Enable(EnableCap.Texture2D);
GL.BindTexture(TextureTarget.Texture2D, Texture);

GL.Begin(PrimitiveType.Quads);

GL.TexCoord3(0.0f, 0.0f, 0f); GL.Vertex3(0f, 0f, 0f);
GL.TexCoord3(1.0f, 0.0f, 0f); GL.Vertex3(realWidth, 0f, 0f);
GL.TexCoord3(1.0f, 1.0f, 0f); GL.Vertex3(realWidth, realHeight, 0f);
GL.TexCoord3(0.0f, 1.0f, 0f); GL.Vertex3(0f, realHeight, 0f);

GL.End();

GL.Disable(EnableCap.Texture2D);

control.SwapBuffers();

成功了。
非常重要(至少我认为是):
- 在使用纹理和 GL.Disable(EnableCap.Texture2D) 后缀渲染四边形之前的 GL.Enable(EnableCap.Texture2D)。
- 启用 GL.Enable(EnableCap.Texture2D) 后的 GL.BindTexture(TextureTarget.Texture2D, Texture)。

希望这对某人有所帮助。如果我有时间,我会用它制作一个 C# 类并将其发布在这里。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多