Added ITextPrinter.Begin()/End() functions to speed up font rendering. Updated Fonts and Text examples.
This commit is contained in:
parent
e020c2f55f
commit
8db2411038
4 changed files with 125 additions and 23 deletions
|
@ -165,12 +165,7 @@ namespace Examples.Tutorial
|
||||||
{
|
{
|
||||||
GL.Clear(ClearBufferMask.ColorBufferBit);
|
GL.Clear(ClearBufferMask.ColorBufferBit);
|
||||||
|
|
||||||
GL.MatrixMode(MatrixMode.Projection);
|
printer.Begin();
|
||||||
GL.LoadIdentity();
|
|
||||||
GL.Ortho(0.0, Width, Height, 0.0, 0.0, 1.0);
|
|
||||||
|
|
||||||
GL.MatrixMode(MatrixMode.Modelview);
|
|
||||||
GL.LoadIdentity();
|
|
||||||
|
|
||||||
// Print using the first font.
|
// Print using the first font.
|
||||||
for (int i = 0; i < handles.Length / 2; i++)
|
for (int i = 0; i < handles.Length / 2; i++)
|
||||||
|
@ -190,6 +185,8 @@ namespace Examples.Tutorial
|
||||||
GL.Translate(0, fonts[i].Height, 0);
|
GL.Translate(0, fonts[i].Height, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printer.End();
|
||||||
|
|
||||||
SwapBuffers();
|
SwapBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -120,20 +120,18 @@ namespace Examples.Tutorial
|
||||||
else if (scroll_speed < 0.0f && current_position < warparound_position)
|
else if (scroll_speed < 0.0f && current_position < warparound_position)
|
||||||
current_position = initial_position;
|
current_position = initial_position;
|
||||||
|
|
||||||
// Prepare to draw text. We want pixel perfect precision, so we setup a 2D mode,
|
// TextPrinter.Begin() sets up a 2d orthographic projection, with the x axis
|
||||||
// with size equal to the window (in pixels).
|
// moving from 0 to viewport.Width (left to right) and the y axis from
|
||||||
// While we could also render text in 3D mode, it would be very hard to get
|
// 0 to viewport.Height (top to bottom). This is the typical coordinate system
|
||||||
// pixel-perfect precision.
|
// used in 2d graphics, and is necessary for achieving pixel-perfect glyph rendering.
|
||||||
GL.MatrixMode(MatrixMode.Projection);
|
// TextPrinter.End() restores your previous projection/modelview matrices.
|
||||||
GL.LoadIdentity();
|
text.Begin();
|
||||||
GL.Ortho(0.0, Width, Height, 0.0, 0.0, 1.0);
|
|
||||||
|
|
||||||
GL.MatrixMode(MatrixMode.Modelview);
|
|
||||||
GL.LoadIdentity();
|
|
||||||
|
|
||||||
GL.Translate(0.0f, current_position, 0.0f);
|
GL.Translate(0.0f, current_position, 0.0f);
|
||||||
text.Draw(poem_handle);
|
text.Draw(poem_handle);
|
||||||
|
|
||||||
|
text.End();
|
||||||
|
|
||||||
SwapBuffers();
|
SwapBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,5 +21,7 @@ namespace OpenTK.Fonts
|
||||||
void Prepare(string text, TextureFont font, out TextHandle handle, float width, bool wordWarp, StringAlignment alignment);
|
void Prepare(string text, TextureFont font, out TextHandle handle, float width, bool wordWarp, StringAlignment alignment);
|
||||||
void Prepare(string text, TextureFont font, out TextHandle handle, float width, bool wordWarp, StringAlignment alignment, bool rightToLeft);
|
void Prepare(string text, TextureFont font, out TextHandle handle, float width, bool wordWarp, StringAlignment alignment, bool rightToLeft);
|
||||||
void Draw(TextHandle handle);
|
void Draw(TextHandle handle);
|
||||||
|
void Begin();
|
||||||
|
void End();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ namespace OpenTK.Fonts
|
||||||
//static char[] split_chars = new char[] { ' ', '\n', '\t', ',', '.', '/', '?', '!', ';', '\\', '-', '+', '*', '=' };
|
//static char[] split_chars = new char[] { ' ', '\n', '\t', ',', '.', '/', '?', '!', ';', '\\', '-', '+', '*', '=' };
|
||||||
static bool functionality_checked = false;
|
static bool functionality_checked = false;
|
||||||
static ITextPrinterImplementation printer;
|
static ITextPrinterImplementation printer;
|
||||||
|
float[] viewport = new float[6];
|
||||||
|
|
||||||
#region --- Constructor ---
|
#region --- Constructor ---
|
||||||
|
|
||||||
|
@ -62,21 +63,72 @@ namespace OpenTK.Fonts
|
||||||
|
|
||||||
#region --- ITextPrinter Members ---
|
#region --- ITextPrinter Members ---
|
||||||
|
|
||||||
|
#region public void Prepare(string text, TextureFont font, out TextHandle handle)
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prepares text for drawing.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The string to draw.</param>
|
||||||
|
/// <param name="font">The font to use for drawing.</param>
|
||||||
|
/// <param name="handle">The handle to the cached text. Use this to draw the text with the Draw() function.</param>
|
||||||
|
/// <see cref="TextPrinter.Draw()"/>
|
||||||
public void Prepare(string text, TextureFont font, out TextHandle handle)
|
public void Prepare(string text, TextureFont font, out TextHandle handle)
|
||||||
{
|
{
|
||||||
this.Prepare(text, font, out handle, 0, false, StringAlignment.Near, false);
|
this.Prepare(text, font, out handle, 0, false, StringAlignment.Near, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region public void Prepare(string text, TextureFont font, out TextHandle handle, float width, bool wordWarp)
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prepares text for drawing.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The string to draw.</param>
|
||||||
|
/// <param name="font">The font to use for drawing.</param>
|
||||||
|
/// <param name="handle">The handle to the cached text. Use this to draw the text with the Draw() function.</param>
|
||||||
|
/// <param name="width">Not implemented.</param>
|
||||||
|
/// <param name="wordWarp">Not implemented.</param>
|
||||||
|
/// <see cref="TextPrinter.Draw()"/>
|
||||||
public void Prepare(string text, TextureFont font, out TextHandle handle, float width, bool wordWarp)
|
public void Prepare(string text, TextureFont font, out TextHandle handle, float width, bool wordWarp)
|
||||||
{
|
{
|
||||||
this.Prepare(text, font, out handle, width, wordWarp, StringAlignment.Near, false);
|
this.Prepare(text, font, out handle, width, wordWarp, StringAlignment.Near, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region public void Prepare(string text, TextureFont font, out TextHandle handle, float width, bool wordWarp, StringAlignment alignment)
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prepares text for drawing.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The string to draw.</param>
|
||||||
|
/// <param name="font">The font to use for drawing.</param>
|
||||||
|
/// <param name="handle">The handle to the cached text. Use this to draw the text with the Draw() function.</param>
|
||||||
|
/// <param name="width">Not implemented.</param>
|
||||||
|
/// <param name="wordWarp">Not implemented.</param>
|
||||||
|
/// <param name="alignment">Not implemented.</param>
|
||||||
|
/// <see cref="TextPrinter.Draw()"/>
|
||||||
public void Prepare(string text, TextureFont font, out TextHandle handle, float width, bool wordWarp, StringAlignment alignment)
|
public void Prepare(string text, TextureFont font, out TextHandle handle, float width, bool wordWarp, StringAlignment alignment)
|
||||||
{
|
{
|
||||||
this.Prepare(text, font, out handle, width, wordWarp, alignment, false);
|
this.Prepare(text, font, out handle, width, wordWarp, alignment, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region public void Prepare(string text, TextureFont font, out TextHandle handle, float width, bool wordWarp, StringAlignment alignment, bool rightToLeft)
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prepares text for drawing.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The string to draw.</param>
|
||||||
|
/// <param name="font">The font to use for drawing.</param>
|
||||||
|
/// <param name="handle">The handle to the cached text. Use this to draw the text with the Draw() function.</param>
|
||||||
|
/// <param name="width">Not implemented.</param>
|
||||||
|
/// <param name="wordWarp">Not implemented.</param>
|
||||||
|
/// <param name="alignment">Not implemented.</param>
|
||||||
|
/// <param name="rightToLeft">Not implemented.</param>
|
||||||
|
/// <see cref="TextPrinter.Draw()"/>
|
||||||
public void Prepare(string text, TextureFont font, out TextHandle handle, float width, bool wordWarp, StringAlignment alignment, bool rightToLeft)
|
public void Prepare(string text, TextureFont font, out TextHandle handle, float width, bool wordWarp, StringAlignment alignment, bool rightToLeft)
|
||||||
{
|
{
|
||||||
if (!functionality_checked)
|
if (!functionality_checked)
|
||||||
|
@ -88,6 +140,9 @@ namespace OpenTK.Fonts
|
||||||
if (text.Length > 8192)
|
if (text.Length > 8192)
|
||||||
throw new ArgumentOutOfRangeException("text", text.Length, "Text length must be between 1 and 8192 characters");
|
throw new ArgumentOutOfRangeException("text", text.Length, "Text length must be between 1 and 8192 characters");
|
||||||
|
|
||||||
|
if (wordWarp || rightToLeft || alignment != StringAlignment.Near)
|
||||||
|
throw new NotImplementedException();
|
||||||
|
|
||||||
Vector2[] vertices = new Vector2[8 * text.Length]; // Interleaved, vertex, texcoord, vertex, etc...
|
Vector2[] vertices = new Vector2[8 * text.Length]; // Interleaved, vertex, texcoord, vertex, etc...
|
||||||
ushort[] indices = new ushort[6 * text.Length];
|
ushort[] indices = new ushort[6 * text.Length];
|
||||||
float x_pos = 0, y_pos = 0;
|
float x_pos = 0, y_pos = 0;
|
||||||
|
@ -167,8 +222,49 @@ namespace OpenTK.Fonts
|
||||||
handle.font = font;
|
handle.font = font;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region public void Draw(TextHandle handle)
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Draws the cached text referred to by the TextHandle.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="handle">The TextHandle to the cached text.</param>
|
||||||
public void Draw(TextHandle handle)
|
public void Draw(TextHandle handle)
|
||||||
{
|
{
|
||||||
|
GL.BindTexture(TextureTarget.Texture2d, handle.font.Texture);
|
||||||
|
|
||||||
|
printer.Draw(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region public void Draw(string text)
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region public void Begin()
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets up OpenGL state for drawing text.
|
||||||
|
/// </summary>
|
||||||
|
public void Begin()
|
||||||
|
{
|
||||||
|
GL.PushMatrix();
|
||||||
|
|
||||||
|
GL.GetFloat(GetPName.Viewport, viewport);
|
||||||
|
|
||||||
|
// Prepare to draw text. We want pixel perfect precision, so we setup a 2D mode,
|
||||||
|
// with size equal to the window (in pixels).
|
||||||
|
// While we could also render text in 3D mode, it would be very hard to get
|
||||||
|
// pixel-perfect precision.
|
||||||
|
GL.MatrixMode(MatrixMode.Projection);
|
||||||
|
GL.LoadIdentity();
|
||||||
|
GL.Ortho(viewport[0], viewport[2], viewport[3], viewport[1], 0.0, 1.0);
|
||||||
|
|
||||||
|
GL.MatrixMode(MatrixMode.Modelview);
|
||||||
|
GL.LoadIdentity();
|
||||||
|
|
||||||
GL.PushAttrib(AttribMask.TextureBit);
|
GL.PushAttrib(AttribMask.TextureBit);
|
||||||
GL.PushAttrib(AttribMask.EnableBit);
|
GL.PushAttrib(AttribMask.EnableBit);
|
||||||
|
|
||||||
|
@ -177,15 +273,24 @@ namespace OpenTK.Fonts
|
||||||
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
|
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
|
||||||
|
|
||||||
GL.Disable(EnableCap.DepthTest);
|
GL.Disable(EnableCap.DepthTest);
|
||||||
|
|
||||||
GL.BindTexture(TextureTarget.Texture2d, handle.font.Texture);
|
|
||||||
|
|
||||||
printer.Draw(handle);
|
|
||||||
|
|
||||||
GL.PopAttrib();
|
|
||||||
GL.PopAttrib();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region public void End()
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Restores OpenGL state.
|
||||||
|
/// </summary>
|
||||||
|
public void End()
|
||||||
|
{
|
||||||
|
GL.PopAttrib();
|
||||||
|
GL.PopAttrib();
|
||||||
|
GL.PopMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue