objarni's picture

TextureFont v0.1

I finished a rough version of the TextureFont class. The WriteCharacter/WriteString got a little tidier aswell as more optimized. For example I moved out the "Begin/End"-calls from WriteCharacter to WriteString. WriteString is the only public API for the class, example code:

    private void DrawText()
    {
      string text = string.Format("Hello! FPS={0:000.0}", fps);
 
      // Assume orthogonal 0,0,width,height projection matrix
      GL.Color3(Color.Khaki);
      GL.Translate(w / 2, h / 2, 0); // place text at center of view
      double scale = h * 0.05; // make it 5% of the height of the view
      GL.Scale(scale, scale, scale);
      GL.Rotate(rotation, 0, 0, 1);
      TextureFont texFont = new TextureFont(texture);
      texFont.WriteString(text);
    }

The TexUtil.cs file can be downloaded here, I'm releasing it under same license as OpenTK, if you are interesting in using this functionality.


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
objarni's picture

Serinus - you should also consider moving the CreateTextureFromFile-call to some kind of setup-logic of you application (Main/constructor/OnLoad or similar..)

Right now you are re-loading the .png file *every frame* = extremely slow :)

Make the 'int texture' variable an instance variable instead of a local variable in OnUpdateFrame.

roriron's picture

Thank you. It's faster then TextPrinter.
But I have a problem when I try to draw lines and write string : the lines didn't display!

        private void glChart_Paint(object sender, PaintEventArgs e)
        {
            if (!glChartLoaded)
                return;
            glChart.MakeCurrent();
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            GL.Viewport(0, 0, chartWidth, chartHeight);
            //======================
            DrawLines();
            DrawText();
            //======================================
            glChart.SwapBuffers();  
        }
        private void DrawText()
        {
            GL.Color3(Color.Green);
            GL.Translate(chartWidth/2,chartHeight/2,0); 
            double scale =chartWidth* 0.02; 
            GL.Scale(scale, scale, scale);
            TextureFont texFont = new TextureFont(fontID);
            texFont.WriteString("Test");
        }

Could you help me find out the problem?
Thanks!!!

the Fiddler's picture

Try disabling textures before drawing the lines.

roriron's picture

It work very well, thank you so much!!!