Using OpenTK Fonts to print time
Posted Thursday, 22 November, 2007 - 19:03 by objarni in
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();
}
/// 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?
| Attachment | Size |
|---|---|
| Time.png | 19 KB |




Comments
Nov 22
20:06:02Dynamic text is coming in
posted by the Fiddler.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 :) )
Nov 22
21:41:48I'm not by my dev.computer
posted by objarniI'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..)
Nov 22
21:45:19Cool, I'll do that as soon
posted by the Fiddler.Cool, I'll do that as soon as start working on text rendering again.
Dec 01
16:30:49The method public void End()
posted by InertiaThe 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:{
GL.PopAttrib();
GL.PopAttrib();
GL.MatrixMode( MatrixMode.Projection );
GL.PopMatrix( );
GL.MatrixMode( MatrixMode.Modelview );
GL.PopMatrix( );
}
and the Begin Method needs somewhere
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.
Dec 01
16:51:14Thanks, fixed in svn. I
posted by the Fiddler.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.Dec 01
17:32:03You're right, it does.
posted by InertiaYou're right, it does. http://wiki.delphigl.com/index.php/glPushAttrib