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


Comments
Dynamic text is coming in
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 :) )
I'm not by my dev.computer
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..)
Cool, I'll do that as soon
Cool, I'll do that as soon as start working on text rendering again.
The method public void End()
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:and the Begin Method needs somewhere
Maybe you should also (re)store the depth-buffer-bit, but I couldn't find the enum to do this.
Thanks, fixed in svn. I
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.You're right, it does.
You're right, it does. http://wiki.delphigl.com/index.php/glPushAttrib