簡體   English   中英

使用 QuickFont 在 OpenTK 中繪制文本

[英]Draw text in OpenTK using QuickFont

請幫我解決這個問題:(這就是我想要做的:

在此處輸入圖片說明

因為 QuickFont 與我的正射使用不同的坐標系,所以我必須為我的文本計算坐標,這沒關系。

問題是當我調整表單大小時,文本的坐標變得錯誤。

在此處輸入圖片說明

這是我的代碼:

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

對不起我的英語:)

您需要重新創建投影矩陣。 根據您上面的代碼,應該這樣做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM