tuankhoa1996
tuankhoa1996

Reputation: 131

Draw text in OpenTK using QuickFont

Please help me with this problem :( This is what i'm trying to do:

enter image description here

Because QuickFont use different coordinate system with my ortho so i have to calculate coordinate for my text and it's ok.

The problem is when i resized the form, the text's coordiantes become wrong.

enter image description here

Here is my code:

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

Sorry about my english :)

Upvotes: 1

Views: 2423

Answers (1)

livin_amuk
livin_amuk

Reputation: 1353

You need to recreate the projection matrix. Based on your code above, this should do it:

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

Upvotes: 1

Related Questions