objarni's picture

Using OpenTK Fonts to print time

I played around a little with the Quick Start Sample. I tried to print the last read "Time" property from the RenderFrameEventArgs, but except for the first frame, the displayed triangle dissappeared!

Pasting both OnUpdateFrame() and OnRenderFrame() just for completeness:

    /// <summary>
    /// Called when it is time to setup the next frame. Add you game logic here.
    /// </summary>
    /// <param name="e">Contains timing information for framerate independent logic.</param>
    public override void OnUpdateFrame(UpdateFrameEventArgs e)
    {
      if (Keyboard[Key.Escape])
        Exit();
      time = e.Time;
    }
    double time;
    Font font = new Font(FontFamily.GenericSerif, 16);
    TextureFont texFont = null;
    ITextPrinter printer = new TextPrinter();
    /// <summary>
    /// Called when it is time to render the next frame. Add your rendering code here.
    /// </summary>
    /// <param name="e">Contains timing information.</param>
    public override void OnRenderFrame(RenderFrameEventArgs e)
    {
      GL.Clear(ClearBufferMask.ColorBufferBit |
               ClearBufferMask.DepthBufferBit);
 
      GL.MatrixMode(MatrixMode.Modelview);
      GL.LoadIdentity();
      Glu.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
 
      GL.Begin(BeginMode.Triangles);
 
      GL.Color3(Color.LightYellow); GL.Vertex3(-1.0f, -1.0f, 4.0f);
      GL.Color3(Color.LightYellow); GL.Vertex3(1.0f, -1.0f, 4.0f);
      GL.Color3(Color.LightSkyBlue); GL.Vertex3(0.0f, 1.0f, 4.0f);
 
      GL.End();
 
      if (texFont == null)
        texFont = new TextureFont(font);
      TextHandle handle = null;
      GL.LoadIdentity();
      GL.Translate(0, 0, -5);
      printer.Begin();
      printer.Prepare(time.ToString(), texFont, out handle);
      printer.Draw(handle);
      printer.End();
      handle.Dispose();
 
      SwapBuffers();
    }

I also tried using the TextPrinter.Draw(string, TextureFont)-method but got a "not implemented exception", I guess it's coming up in the next release or something?

Am I missing something obvious?

AttachmentSize
Time.png19 KB

Comments

Comment viewing options

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

Dynamic text is coming in the next version (hence the not implemented exception).

What happens if you move the Prepare call before the printer.Begin statement? (as you can see, this part is still WIP :) )

objarni's picture

I'm not by my dev.computer right now, I'll check it out as soon as I get there (probably some days though .. if you want to try yourself the above code is self-contained if replacing the appropriate sections of Quick Start Sample Game.cs code..)

the Fiddler.'s picture

Cool, I'll do that as soon as start working on text rendering again.

Inertia's picture

The method public void End() at the end of TextPrinter.cs is probably the cause of the triangle disappearing (it was in my case). Only the modelview matrix is restored when End() is called, it should look something like this:

public void End()
        {
            GL.PopAttrib();
            GL.PopAttrib();
 
            GL.MatrixMode( MatrixMode.Projection );
            GL.PopMatrix( );
 
            GL.MatrixMode( MatrixMode.Modelview );
            GL.PopMatrix( );
        }

and the Begin Method needs somewhere

            GL.MatrixMode( MatrixMode.Modelview );
            GL.PushMatrix( );
 
            GL.MatrixMode( MatrixMode.Projection );
            GL.PushMatrix( );

Maybe you should also (re)store the depth-buffer-bit, but I couldn't find the enum to do this.

the Fiddler.'s picture

Thanks, fixed in svn. I think the GL.PushAttrib(AttribMask.EnableBit); code takes care of the depth-buffer-bit, but haven't tried to verify yet.

Inertia's picture