使用QuickFont在OpenTK中绘制文本

时间:2016-04-16 02:06:23

标签: c# opentk

请帮助我解决这个问题:( 这就是我想要做的事情:

enter image description here

因为QuickFont使用与我的ortho不同的坐标系,所以我必须为我的文本计算坐标,这没关系。

问题是当我调整表单大小时,文本的coordiantes会出错。

enter image description here

这是我的代码:

public static void DrawOxy(float lO, float rO, float tO, float bO, int controlW, int controlH)
        {
            GL.Color3(Color.Blue);
            GL.Begin(BeginMode.Lines);
            GL.Vertex2(lO, 0);
            GL.Vertex2(rO, 0);
            GL.Vertex2(0, tO);
            GL.Vertex2(0, bO);
            for (float i = lO; i < rO; i+=2)
            {
                GL.Vertex2(i, 0.5);
                GL.Vertex2(i, -0.5);
            }
            for (float j = bO; j < tO; j+=2)
            {
                GL.Vertex2(0.2, j);
                GL.Vertex2(-0.2, j);
            }
            GL.End();
            QFont font = new QFont(new Font(FontFamily.GenericSansSerif, 15));
            GL.PushAttrib(AttribMask.ColorBufferBit);
            font.Options.Colour = Color.Red;
            QFont.Begin();
            float horStep = ((float)controlW / rO);
            font.Print(horStep.ToString(), new Vector2(0, 0));
            float beginX = 0;
            for (float i = lO; i < rO; i += 2)
            {
                font.Print(i.ToString(), new Vector2(beginX, (((float)controlH / 2))));
                beginX += horStep;
            }
            QFont.End();
            GL.PopAttrib();
        }


private void SetupViewport()
        {
            int w = glControl1.Width;
            int h = glControl1.Height;
            int left = -(w / 2);
            int right = w / 2;
            int top = h / 2;
            int bottom = -(h / 2);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            GL.Ortho(leftOr, rightOr, bottomOr, topOr, -1, 1); // Bottom-left corner pixel has coordinate (0, 0)
            GL.Viewport(0, 0, w, h); // Use all of the glControl painting area
        }

        private void glControl1_Load(object sender, EventArgs e)
        {
            loaded = true;
            GL.ClearColor(Color.White);
            SetupViewport();
        }


        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            if (!loaded) // Play nice
                return;

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.LineWidth(2);
            DrawingObjects.DrawOxy(leftOr, rightOr, topOr, bottomOr, glControl1.Width, glControl1.Height);


            glControl1.SwapBuffers();
        }

        private void glControl1_Resize(object sender, EventArgs e)
        {
            if (!loaded)
            {
                return;
            }

            SetupViewport();
        }

对不起我的英文:)

1 个答案:

答案 0 :(得分:1)

您需要重新创建投影矩阵。根据您的上述代码,我们应该这样做:

protected override void OnResize(EventArgs e)
{
    base.OnResize(e);
    SetupViewport();
}