Using the TextRenderer is simple. Add a
"TextRenderer renderer;" field to your GLEventListener. In your init method, add:
renderer = new TextRenderer(new Font("SansSerif", Font.BOLD, 36));
In the display method of your
GLEventListener, add:
renderer.beginRendering(drawable.getWidth(), drawable.getHeight());
// optionally set the color
renderer.setColor(1.0f, 0.2f, 0.2f, 0.8f);
renderer.draw("Text to draw", xPosition, yPosition);
// ... more draw commands, color changes, etc.
renderer.endRendering();
Unless you are sharing textures and display lists between OpenGL
contexts, you do not need to call the dispose
method of the TextRenderer; the OpenGL resources it uses
internally will be cleaned up automatically when the OpenGL
context is destroyed.Note
that the TextRenderer may cause the vertex and texture coordinate array buffer bindings to change, or to be unbound. This is important to note if you are using Vertex Buffer Objects (VBOs) in your application.
Internally, the renderer uses a rectangle packing algorithm to
pack both glyphs and full Strings' rendering results (which are
variable size) onto a larger OpenGL texture. The internal backing
store is maintained using a TextureRenderer. A least
recently used (LRU) algorithm is used to discard previously
rendered strings; the specific algorithm is undefined, but is
currently implemented by flushing unused Strings' rendering
results every few hundred rendering cycles, where a rendering
cycle is defined as a pair of calls to beginRendering / endRendering.
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| private static class | |
| pack-priv class | |
| public static class | |
| pack-priv class | TextRenderer.
A Glyph represents either a single unicode glyph or a substring of characters to be drawn. |
| pack-priv class | |
| pack-priv class | |
| pack-priv class | |
| public static interface | TextRenderer.
Class supporting more full control over the process of rendering the bitmapped text. |
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| private final boolean | |
| private boolean | |
| private int | |
| private int | |
| private float | |
| private float | |
| private TextureRenderer | |
| private Color | |
| private FontRenderContext | |
| private float | |
| private Graphics2D | |
| private float | |
| private boolean | |
| private static final int | |
| private Frame | |
| private static final boolean | |
| private boolean | |
| private static final boolean | |
| private static final boolean | |
| private final Font | |
| private boolean | |
| private boolean | |
| private boolean | |
| private boolean | |
| private boolean | |
| pack-priv static final int | |
| pack-priv static final int | |
| pack-priv static final int | |
| pack-priv static final int | |
| pack-priv static final int | |
| pack-priv static final int | |
| pack-priv static final int | |
| pack-priv static final int | |
| pack-priv static final int | |
| pack-priv static final int | |
| pack-priv static final int | |
| pack-priv static final int | |
| private static final float | |
| private final TextRenderer. | |
| private boolean | |
| pack-priv TextRenderer. | |
| private boolean | |
| private int | |
| private RectanglePacker | |
| private final TextRenderer. | |
| private final char[] | |
| private boolean | |
| private final Map | |
| private final boolean | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public | TextRenderer(final Font
the font to render with font)Creates a new TextRenderer with the given font, using no antialiasing or fractional metrics, and the default RenderDelegate. |
| public | TextRenderer(final Font
the font to render with font, final boolean whether to attempt use of automatic mipmap generation mipmap)Creates a new TextRenderer with the given font, using no antialiasing or fractional metrics, and the default RenderDelegate. |
| public | TextRenderer(final Font
the font to render with font, final boolean whether to use antialiased fonts antialiased, final boolean whether to use fractional font
metrics at the Java 2D level useFractionalMetrics)Creates a new TextRenderer with the given Font, specified font properties, and default RenderDelegate. |
| public | TextRenderer(final Font
the font to render with font, final boolean whether to use antialiased fonts antialiased, final boolean whether to use fractional font
metrics at the Java 2D level useFractionalMetrics, final TextRenderer.the render delegate to use to draw the
text's bitmap, or null to use the default one renderDelegateCreates a new TextRenderer with the given Font, specified font properties, and given RenderDelegate. |
| public | TextRenderer(final Font
the font to render with font, final boolean whether to use antialiased fonts antialiased, final boolean whether to use fractional font
metrics at the Java 2D level useFractionalMetrics, TextRenderer.the render delegate to use to draw the
text's bitmap, or null to use the default one renderDelegate,whether to attempt use of automatic mipmap generation mipmap)Creates a new TextRenderer with the given Font, specified font properties, and given RenderDelegate. |
| Modifier and Type | Method and Description |
|---|---|
| public void | begin3DRendering()
Begins rendering of 2D text in 3D with this |
| public void | beginRendering(final int
the width of the current on-screen OpenGL drawable width, final int the height of the current on-screen OpenGL drawable height)Begins rendering with this |
| public void | beginRendering(final int
the width of the current on-screen OpenGL drawable width, final int the height of the current on-screen OpenGL drawable height, final boolean whether to disable the depth test disableDepthTest)Begins rendering with this |
| private void | beginRendering(final boolean ortho, final int width, final int height, final boolean disableDepthTestForOrtho)
|
| private void | |
| private void | |
| public void | |
| public void | draw(final CharSequence
the string to draw str, final int the x coordinate at which to draw x, final int the y coordinate at which to draw y)Draws the supplied CharSequence at the desired location using the renderer's current color. |
| public void | |
| public void | draw3D(final CharSequence
the string to draw str, final float the x coordinate at which to draw x, final float the y coordinate at which to draw y, final float the z coordinate at which to draw z, final float a uniform scale factor applied to the width and height of the drawn rectangle scaleFactor)Draws the supplied CharSequence at the desired 3D location using the renderer's current color. |
| public void | |
| private void | draw3D_ROBUST(final CharSequence str, final float x, final float y, final float z, final float scaleFactor)
|
| public void | |
| public void | |
| private void | endRendering(final boolean ortho)
emzic: here the call to glBindBuffer crashes on certain graphicscard/driver combinations this is why the ugly try-catch block has been added, which falls back to the old textrenderer |
| public void | flush()
Causes the TextRenderer to flush any internal caches it may be maintaining and draw its rendering results to the screen. |
| private void | |
| private TextureRenderer | |
| public Rectangle2D | |
| public Rectangle2D | getBounds(final CharSequence str)
Returns the bounding rectangle of the given CharSequence, assuming it was rendered at the origin. |
| public float | |
| public Font | |
| public FontRenderContext | getFontRenderContext()
Returns a FontRenderContext which can be used for external text-related size computations. |
| private Graphics2D | |
| public boolean | getSmoothing()
Indicates whether smoothing is enabled in the backing TextureRenderer of this TextRenderer. |
| public final boolean | getUseVertexArrays()
Indicates whether vertex arrays are being used internally for rendering, or whether text is rendered using the OpenGL immediate mode commands. |
| private void | internal_draw3D(final CharSequence str, float x, final float y, final float z, final float scaleFactor)
|
| private final boolean | |
| private Rectangle2D | |
| private static Rectangle2D | |
| public void | |
| public void | setColor(final float
the red component of the new color r, final float the green component of the new color g, final float the blue component of the new color b, final float the alpha component of the new color, 0.0f = completely
transparent, 1.0f = completely opaque a)Changes the current color of this TextRenderer to the supplied one, where each component ranges from 0.0f - 1.0f. |
| public void | setSmoothing(final boolean smoothing)
Sets whether smoothing (i.e., GL_LINEAR filtering) is enabled in the backing TextureRenderer of this TextRenderer. |
| public void | setUseVertexArrays(final boolean useVertexArrays)
Sets whether vertex arrays are being used internally for rendering, or whether text is rendered using the OpenGL immediate mode commands. |