/* * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed or intended for use * in the design, construction, operation or maintenance of any nuclear * facility. * * Sun gratefully acknowledges that this software was originally authored * and developed by Kenneth Bradley Russell and Christopher John Kline. */ package com.sun.opengl.util.j2d; import com.sun.opengl.impl.*; import com.sun.opengl.impl.packrect.*; import com.sun.opengl.util.*; import com.sun.opengl.util.texture.*; import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Composite; // For debugging purposes import java.awt.EventQueue; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Point; import java.awt.RenderingHints; import java.awt.event.*; import java.awt.font.*; import java.awt.geom.*; import java.nio.*; import java.text.*; import java.util.*; import javax.media.opengl.*; import javax.media.opengl.glu.*; /** Renders bitmapped Java 2D text into an OpenGL window with high performance, full Unicode support, and a simple API. Performs appropriate caching of text rendering results in an OpenGL texture internally to avoid repeated font rasterization. The caching is completely automatic, does not require any user intervention, and has no visible controls in the public API.
Using the {@link TextRenderer TextRenderer} is simple. Add a
"TextRenderer renderer;
" field to your {@link
javax.media.opengl.GLEventListener GLEventListener}. In your {@link
javax.media.opengl.GLEventListener#init init} method, add:
renderer = new TextRenderer(new Font("SansSerif", Font.BOLD, 36));
In the {@link javax.media.opengl.GLEventListener#display display} method of your {@link javax.media.opengl.GLEventListener 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 {@link #dispose 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 {@link
com.sun.opengl.util.j2d.TextureRenderer 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 {@link #beginRendering
beginRendering} / {@link #endRendering endRendering}.
@author John Burkey
@author Kenneth Russell
*/
public class TextRenderer {
private static final boolean DEBUG = Debug.debug("TextRenderer");
// These are occasionally useful for more in-depth debugging
private static final boolean DISABLE_GLYPH_CACHE = false;
private static final boolean DRAW_BBOXES = false;
static final int kSize = 256;
// Every certain number of render cycles, flush the strings which
// haven't been used recently
private static final int CYCLES_PER_FLUSH = 100;
// The amount of vertical dead space on the backing store before we
// force a compaction
private static final float MAX_VERTICAL_FRAGMENTATION = 0.7f;
static final int kQuadsPerBuffer = 100;
static final int kCoordsPerVertVerts = 3;
static final int kCoordsPerVertTex = 2;
static final int kVertsPerQuad = 4;
static final int kTotalBufferSizeVerts = kQuadsPerBuffer * kVertsPerQuad;
static final int kTotalBufferSizeCoordsVerts = kQuadsPerBuffer * kVertsPerQuad * kCoordsPerVertVerts;
static final int kTotalBufferSizeCoordsTex = kQuadsPerBuffer * kVertsPerQuad * kCoordsPerVertTex;
static final int kTotalBufferSizeBytesVerts = kTotalBufferSizeCoordsVerts * 4;
static final int kTotalBufferSizeBytesTex = kTotalBufferSizeCoordsTex * 4;
static final int kSizeInBytes_OneVertices_VertexData = kCoordsPerVertVerts * 4;
static final int kSizeInBytes_OneVertices_TexData = kCoordsPerVertTex * 4;
private Font font;
private boolean antialiased;
private boolean useFractionalMetrics;
// Whether we're attempting to use automatic mipmap generation support
private boolean mipmap;
private RectanglePacker packer;
private boolean haveMaxSize;
private RenderDelegate renderDelegate;
private TextureRenderer cachedBackingStore;
private Graphics2D cachedGraphics;
private FontRenderContext cachedFontRenderContext;
private Map /*
Glyphs need to be able to re-upload themselves to the backing
store on demand as we go along in the render sequence.
*/
class Glyph {
// If this Glyph represents an individual unicode glyph, this
// is its unicode ID. If it represents a String, this is -1.
private int unicodeID;
// If the above field isn't -1, then these fields are used.
// The glyph code in the font
private int glyphCode;
// The GlyphProducer which created us
private GlyphProducer producer;
// The advance of this glyph
private float advance;
// The GlyphVector for this single character; this is passed
// in during construction but cleared during the upload
// process
private GlyphVector singleUnicodeGlyphVector;
// The rectangle of this glyph on the backing store, or null
// if it has been cleared due to space pressure
private Rect glyphRectForTextureMapping;
// If this Glyph represents a String, this is the sequence of
// characters
private String str;
// Whether we need a valid advance when rendering this string
// (i.e., whether it has other single glyphs coming after it)
private boolean needAdvance;
// Creates a Glyph representing an individual Unicode character
public Glyph(int unicodeID,
int glyphCode,
float advance,
GlyphVector singleUnicodeGlyphVector,
GlyphProducer producer) {
this.unicodeID = unicodeID;
this.glyphCode = glyphCode;
this.advance = advance;
this.singleUnicodeGlyphVector = singleUnicodeGlyphVector;
this.producer = producer;
}
// Creates a Glyph representing a sequence of characters, with
// an indication of whether additional single glyphs are being
// rendered after it
public Glyph(String str, boolean needAdvance) {
this.str = str;
this.needAdvance = needAdvance;
}
/** Returns this glyph's unicode ID */
public int getUnicodeID() {
return unicodeID;
}
/** Returns this glyph's (font-specific) glyph code */
public int getGlyphCode() {
return glyphCode;
}
/** Returns the advance for this glyph */
public float getAdvance() {
return advance;
}
/** Draws this glyph and returns the (x) advance for this glyph */
public float draw3D(float inX, float inY, float z, float scaleFactor) {
if (str != null) {
draw3D_ROBUST(str, inX, inY, z, scaleFactor);
if (!needAdvance) {
return 0;
}
// Compute and return the advance for this string
GlyphVector gv = font.createGlyphVector(getFontRenderContext(), str);
float totalAdvance = 0;
for (int i = 0; i < gv.getNumGlyphs(); i++) {
totalAdvance += gv.getGlyphMetrics(i).getAdvance();
}
return totalAdvance;
}
// This is the code path taken for individual glyphs
if (glyphRectForTextureMapping == null) {
upload();
}
try {
if (mPipelinedQuadRenderer == null) {
mPipelinedQuadRenderer = new Pipelined_QuadRenderer();
}
TextureRenderer renderer = getBackingStore();
// Handles case where NPOT texture is used for backing store
TextureCoords wholeImageTexCoords = renderer.getTexture().getImageTexCoords();
float xScale = wholeImageTexCoords.right();
float yScale = wholeImageTexCoords.bottom();
Rect rect = glyphRectForTextureMapping;
TextData data = (TextData) rect.getUserData();
data.markUsed();
Rectangle2D origRect = data.origRect();
float x = inX - (scaleFactor * data.origOriginX());
float y = inY - (scaleFactor * ((float) origRect.getHeight() - data.origOriginY()));
int texturex = rect.x() + (data.origin().x - data.origOriginX());
int texturey = renderer.getHeight() - rect.y() - (int) origRect.getHeight() -
(data.origin().y - data.origOriginY());
int width = (int) origRect.getWidth();
int height = (int) origRect.getHeight();
float tx1 = xScale * (float) texturex / (float) renderer.getWidth();
float ty1 = yScale * (1.0f -
((float) texturey / (float) renderer.getHeight()));
float tx2 = xScale * (float) (texturex + width) / (float) renderer.getWidth();
float ty2 = yScale * (1.0f -
((float) (texturey + height) / (float) renderer.getHeight()));
mPipelinedQuadRenderer.glTexCoord2f(tx1, ty1);
mPipelinedQuadRenderer.glVertex3f(x, y, z);
mPipelinedQuadRenderer.glTexCoord2f(tx2, ty1);
mPipelinedQuadRenderer.glVertex3f(x + (width * scaleFactor), y,
z);
mPipelinedQuadRenderer.glTexCoord2f(tx2, ty2);
mPipelinedQuadRenderer.glVertex3f(x + (width * scaleFactor),
y + (height * scaleFactor), z);
mPipelinedQuadRenderer.glTexCoord2f(tx1, ty2);
mPipelinedQuadRenderer.glVertex3f(x,
y + (height * scaleFactor), z);
} catch (Exception e) {
e.printStackTrace();
}
return advance;
}
/** Notifies this glyph that it's been cleared out of the cache */
public void clear() {
glyphRectForTextureMapping = null;
}
private void upload() {
GlyphVector gv = getGlyphVector();
Rectangle2D origBBox = preNormalize(renderDelegate.getBounds(gv, getFontRenderContext()));
Rectangle2D bbox = normalize(origBBox);
Point origin = new Point((int) -bbox.getMinX(),
(int) -bbox.getMinY());
Rect rect = new Rect(0, 0, (int) bbox.getWidth(),
(int) bbox.getHeight(),
new TextData(null, origin, origBBox, unicodeID));
packer.add(rect);
glyphRectForTextureMapping = rect;
Graphics2D g = getGraphics2D();
// OK, should now have an (x, y) for this rectangle; rasterize
// the glyph
int strx = rect.x() + origin.x;
int stry = rect.y() + origin.y;
// Clear out the area we're going to draw into
g.setComposite(AlphaComposite.Clear);
g.fillRect(rect.x(), rect.y(), rect.w(), rect.h());
g.setComposite(AlphaComposite.Src);
// Draw the string
renderDelegate.drawGlyphVector(g, gv, strx, stry);
if (DRAW_BBOXES) {
TextData data = (TextData) rect.getUserData();
// Draw a bounding box on the backing store
g.drawRect(strx - data.origOriginX(),
stry - data.origOriginY(),
(int) data.origRect().getWidth(),
(int) data.origRect().getHeight());
g.drawRect(strx - data.origin().x,
stry - data.origin().y,
rect.w(),
rect.h());
}
// Mark this region of the TextureRenderer as dirty
getBackingStore().markDirty(rect.x(), rect.y(), rect.w(),
rect.h());
// Re-register ourselves with our producer
producer.register(this);
}
private GlyphVector getGlyphVector() {
GlyphVector gv = singleUnicodeGlyphVector;
if (gv != null) {
singleUnicodeGlyphVector = null; // Don't need this anymore
return gv;
}
singleUnicode[0] = (char) unicodeID;
return font.createGlyphVector(getFontRenderContext(), singleUnicode);
}
}
class GlyphProducer {
final int undefined = -2;
FontRenderContext fontRenderContext;
List/*TextRenderer(font, false,
false)
.
@param font the font to render with
*/
public TextRenderer(Font font) {
this(font, false, false, null, false);
}
/** Creates a new TextRenderer with the given font, using no
antialiasing or fractional metrics, and the default
RenderDelegate. If mipmap
is true, attempts to use
OpenGL's automatic mipmap generation for better smoothing when
rendering the TextureRenderer's contents at a distance.
Equivalent to TextRenderer(font, false, false)
.
@param font the font to render with
@param mipmap whether to attempt use of automatic mipmap generation
*/
public TextRenderer(Font font, boolean mipmap) {
this(font, false, false, null, mipmap);
}
/** Creates a new TextRenderer with the given Font, specified font
properties, and default RenderDelegate. The
antialiased
and useFractionalMetrics
flags provide control over the same properties at the Java 2D
level. No mipmap support is requested. Equivalent to
TextRenderer(font, antialiased, useFractionalMetrics,
null)
.
@param font the font to render with
@param antialiased whether to use antialiased fonts
@param useFractionalMetrics whether to use fractional font
metrics at the Java 2D level
*/
public TextRenderer(Font font, boolean antialiased,
boolean useFractionalMetrics) {
this(font, antialiased, useFractionalMetrics, null, false);
}
/** Creates a new TextRenderer with the given Font, specified font
properties, and given RenderDelegate. The
antialiased
and useFractionalMetrics
flags provide control over the same properties at the Java 2D
level. The renderDelegate
provides more control
over the text rendered. No mipmap support is requested.
@param font the font to render with
@param antialiased whether to use antialiased fonts
@param useFractionalMetrics whether to use fractional font
metrics at the Java 2D level
@param renderDelegate the render delegate to use to draw the
text's bitmap, or null to use the default one
*/
public TextRenderer(Font font, boolean antialiased,
boolean useFractionalMetrics, RenderDelegate renderDelegate) {
this(font, antialiased, useFractionalMetrics, renderDelegate, false);
}
/** Creates a new TextRenderer with the given Font, specified font
properties, and given RenderDelegate. The
antialiased
and useFractionalMetrics
flags provide control over the same properties at the Java 2D
level. The renderDelegate
provides more control
over the text rendered. If mipmap
is true, attempts
to use OpenGL's automatic mipmap generation for better smoothing
when rendering the TextureRenderer's contents at a distance.
@param font the font to render with
@param antialiased whether to use antialiased fonts
@param useFractionalMetrics whether to use fractional font
metrics at the Java 2D level
@param renderDelegate the render delegate to use to draw the
text's bitmap, or null to use the default one
@param mipmap whether to attempt use of automatic mipmap generation
*/
public TextRenderer(Font font, boolean antialiased,
boolean useFractionalMetrics, RenderDelegate renderDelegate,
boolean mipmap) {
this.font = font;
this.antialiased = antialiased;
this.useFractionalMetrics = useFractionalMetrics;
this.mipmap = mipmap;
// FIXME: consider adjusting the size based on font size
// (it will already automatically resize if necessary)
packer = new RectanglePacker(new Manager(), kSize, kSize);
if (renderDelegate == null) {
renderDelegate = new DefaultRenderDelegate();
}
this.renderDelegate = renderDelegate;
mGlyphProducer = new GlyphProducer(font.getNumGlyphs());
}
/** Returns the bounding rectangle of the given String, assuming it
was rendered at the origin. See {@link #getBounds(CharSequence)
getBounds(CharSequence)}. */
public Rectangle2D getBounds(String str) {
return getBounds((CharSequence) str);
}
/** Returns the bounding rectangle of the given CharSequence,
assuming it was rendered at the origin. The coordinate system of
the returned rectangle is Java 2D's, with increasing Y
coordinates in the downward direction. The relative coordinate
(0, 0) in the returned rectangle corresponds to the baseline of
the leftmost character of the rendered string, in similar
fashion to the results returned by, for example, {@link
java.awt.font.GlyphVector#getVisualBounds}. Most applications
will use only the width and height of the returned Rectangle for
the purposes of centering or justifying the String. It is not
specified which Java 2D bounds ({@link
java.awt.font.GlyphVector#getVisualBounds getVisualBounds},
{@link java.awt.font.GlyphVector#getPixelBounds getPixelBounds},
etc.) the returned bounds correspond to, although every effort
is made to ensure an accurate bound. */
public Rectangle2D getBounds(CharSequence str) {
// FIXME: this should be more optimized and use the glyph cache
Rect r = null;
if ((r = (Rect) stringLocations.get(str)) != null) {
TextData data = (TextData) r.getUserData();
// Reconstitute the Java 2D results based on the cached values
return new Rectangle2D.Double(-data.origin().x, -data.origin().y,
r.w(), r.h());
}
// Must return a Rectangle compatible with the layout algorithm --
// must be idempotent
return normalize(renderDelegate.getBounds(str, font,
getFontRenderContext()));
}
/** Returns the Font this renderer is using. */
public Font getFont() {
return font;
}
/** Returns a FontRenderContext which can be used for external
text-related size computations. This object should be considered
transient and may become invalidated between {@link
#beginRendering beginRendering} / {@link #endRendering
endRendering} pairs. */
public FontRenderContext getFontRenderContext() {
if (cachedFontRenderContext == null) {
cachedFontRenderContext = getGraphics2D().getFontRenderContext();
}
return cachedFontRenderContext;
}
/** Begins rendering with this {@link TextRenderer TextRenderer}
into the current OpenGL drawable, pushing the projection and
modelview matrices and some state bits and setting up a
two-dimensional orthographic projection with (0, 0) as the
lower-left coordinate and (width, height) as the upper-right
coordinate. Binds and enables the internal OpenGL texture
object, sets the texture environment mode to GL_MODULATE, and
changes the current color to the last color set with this
TextRenderer via {@link #setColor setColor}. This method
disables the depth test and is equivalent to
beginRendering(width, height, true).
@param width the width of the current on-screen OpenGL drawable
@param height the height of the current on-screen OpenGL drawable
@throws javax.media.opengl.GLException If an OpenGL context is not current when this method is called
*/
public void beginRendering(int width, int height) throws GLException {
beginRendering(width, height, true);
}
/** Begins rendering with this {@link TextRenderer TextRenderer}
into the current OpenGL drawable, pushing the projection and
modelview matrices and some state bits and setting up a
two-dimensional orthographic projection with (0, 0) as the
lower-left coordinate and (width, height) as the upper-right
coordinate. Binds and enables the internal OpenGL texture
object, sets the texture environment mode to GL_MODULATE, and
changes the current color to the last color set with this
TextRenderer via {@link #setColor setColor}. Disables the depth
test if the disableDepthTest argument is true.
@param width the width of the current on-screen OpenGL drawable
@param height the height of the current on-screen OpenGL drawable
@param disableDepthTest whether to disable the depth test
@throws GLException If an OpenGL context is not current when this method is called
*/
public void beginRendering(int width, int height, boolean disableDepthTest)
throws GLException {
beginRendering(true, width, height, disableDepthTest);
}
/** Begins rendering of 2D text in 3D with this {@link TextRenderer
TextRenderer} into the current OpenGL drawable. Assumes the end
user is responsible for setting up the modelview and projection
matrices, and will render text using the {@link #draw3D draw3D}
method. This method pushes some OpenGL state bits, binds and
enables the internal OpenGL texture object, sets the texture
environment mode to GL_MODULATE, and changes the current color
to the last color set with this TextRenderer via {@link
#setColor setColor}.
@throws GLException If an OpenGL context is not current when this method is called
*/
public void begin3DRendering() throws GLException {
beginRendering(false, 0, 0, false);
}
/** Changes the current color of this TextRenderer to the supplied
one. The default color is opaque white.
@param color the new color to use for rendering text
@throws GLException If an OpenGL context is not current when this method is called
*/
public void setColor(Color color) throws GLException {
boolean noNeedForFlush = (haveCachedColor && (cachedColor != null) &&
color.equals(cachedColor));
if (!noNeedForFlush) {
flushGlyphPipeline();
}
getBackingStore().setColor(color);
haveCachedColor = true;
cachedColor = color;
}
/** Changes the current color of this TextRenderer to the supplied
one, where each component ranges from 0.0f - 1.0f. The alpha
component, if used, does not need to be premultiplied into the
color channels as described in the documentation for {@link
com.sun.opengl.util.texture.Texture Texture}, although
premultiplied colors are used internally. The default color is
opaque white.
@param r the red component of the new color
@param g the green component of the new color
@param b the blue component of the new color
@param a the alpha component of the new color, 0.0f = completely
transparent, 1.0f = completely opaque
@throws GLException If an OpenGL context is not current when this method is called
*/
public void setColor(float r, float g, float b, float a)
throws GLException {
boolean noNeedForFlush = (haveCachedColor && (cachedColor == null) &&
(r == cachedR) && (g == cachedG) && (b == cachedB) &&
(a == cachedA));
if (!noNeedForFlush) {
flushGlyphPipeline();
}
getBackingStore().setColor(r, g, b, a);
haveCachedColor = true;
cachedR = r;
cachedG = g;
cachedB = b;
cachedA = a;
cachedColor = null;
}
/** Draws the supplied CharSequence at the desired location using
the renderer's current color. The baseline of the leftmost
character is at position (x, y) specified in OpenGL coordinates,
where the origin is at the lower-left of the drawable and the Y
coordinate increases in the upward direction.
@param str the string to draw
@param x the x coordinate at which to draw
@param y the y coordinate at which to draw
@throws GLException If an OpenGL context is not current when this method is called
*/
public void draw(CharSequence str, int x, int y) throws GLException {
draw3D(str, x, y, 0, 1);
}
/** Draws the supplied String at the desired location using the
renderer's current color. See {@link #draw(CharSequence, int,
int) draw(CharSequence, int, int)}. */
public void draw(String str, int x, int y) throws GLException {
draw3D(str, x, y, 0, 1);
}
/** Draws the supplied CharSequence at the desired 3D location using
the renderer's current color. The baseline of the leftmost
character is placed at position (x, y, z) in the current
coordinate system.
@param str the string to draw
@param x the x coordinate at which to draw
@param y the y coordinate at which to draw
@param z the z coordinate at which to draw
@param scaleFactor a uniform scale factor applied to the width and height of the drawn rectangle
@throws GLException If an OpenGL context is not current when this method is called
*/
public void draw3D(CharSequence str, float x, float y, float z,
float scaleFactor) {
internal_draw3D(str, x, y, z, scaleFactor);
}
/** Draws the supplied String at the desired 3D location using the
renderer's current color. See {@link #draw3D(CharSequence,
float, float, float, float) draw3D(CharSequence, float, float,
float, float)}. */
public void draw3D(String str, float x, float y, float z, float scaleFactor) {
internal_draw3D(str, x, y, z, scaleFactor);
}
/** Returns the pixel width of the given character. */
public float getCharWidth(char inChar) {
return mGlyphProducer.getGlyphPixelWidth(inChar);
}
/** Causes the TextRenderer to flush any internal caches it may be
maintaining and draw its rendering results to the screen. This
should be called after each call to draw() if you are setting
OpenGL state such as the modelview matrix between calls to
draw(). */
public void flush() {
flushGlyphPipeline();
}
/** Ends a render cycle with this {@link TextRenderer TextRenderer}.
Restores the projection and modelview matrices as well as
several OpenGL state bits. Should be paired with {@link
#beginRendering beginRendering}.
@throws GLException If an OpenGL context is not current when this method is called
*/
public void endRendering() throws GLException {
endRendering(true);
}
/** Ends a 3D render cycle with this {@link TextRenderer TextRenderer}.
Restores several OpenGL state bits. Should be paired with {@link
#begin3DRendering begin3DRendering}.
@throws GLException If an OpenGL context is not current when this method is called
*/
public void end3DRendering() throws GLException {
endRendering(false);
}
/** Disposes of all resources this TextRenderer is using. It is not
valid to use the TextRenderer after this method is called.
@throws GLException If an OpenGL context is not current when this method is called
*/
public void dispose() throws GLException {
packer.dispose();
packer = null;
cachedBackingStore = null;
cachedGraphics = null;
cachedFontRenderContext = null;
if (dbgFrame != null) {
dbgFrame.dispose();
}
}
//----------------------------------------------------------------------
// Internals only below this point
//
private static Rectangle2D preNormalize(Rectangle2D src) {
// Need to round to integer coordinates
// Also give ourselves a little slop around the reported
// bounds of glyphs because it looks like neither the visual
// nor the pixel bounds works perfectly well
int minX = (int) Math.floor(src.getMinX()) - 1;
int minY = (int) Math.floor(src.getMinY()) - 1;
int maxX = (int) Math.ceil(src.getMaxX()) + 1;
int maxY = (int) Math.ceil(src.getMaxY()) + 1;
return new Rectangle2D.Double(minX, minY, maxX - minX, maxY - minY);
}
private Rectangle2D normalize(Rectangle2D src) {
// Give ourselves a boundary around each entity on the backing
// store in order to prevent bleeding of nearby Strings due to
// the fact that we use linear filtering
// NOTE that this boundary is quite heuristic and is related
// to how far away in 3D we may view the text --
// heuristically, 1.5% of the font's height
int boundary = (int) Math.max(1, 0.015 * font.getSize());
return new Rectangle2D.Double((int) Math.floor(src.getMinX() - boundary),
(int) Math.floor(src.getMinY() - boundary),
(int) Math.ceil(src.getWidth() + 2 * boundary),
(int) Math.ceil(src.getHeight()) + 2 * boundary);
}
private TextureRenderer getBackingStore() {
TextureRenderer renderer = (TextureRenderer) packer.getBackingStore();
if (renderer != cachedBackingStore) {
// Backing store changed since last time; discard any cached Graphics2D
if (cachedGraphics != null) {
cachedGraphics.dispose();
cachedGraphics = null;
cachedFontRenderContext = null;
}
cachedBackingStore = renderer;
}
return cachedBackingStore;
}
private Graphics2D getGraphics2D() {
TextureRenderer renderer = getBackingStore();
if (cachedGraphics == null) {
cachedGraphics = renderer.createGraphics();
// Set up composite, font and rendering hints
cachedGraphics.setComposite(AlphaComposite.Src);
cachedGraphics.setColor(Color.WHITE);
cachedGraphics.setFont(font);
cachedGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
(antialiased ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON
: RenderingHints.VALUE_TEXT_ANTIALIAS_OFF));
cachedGraphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
(useFractionalMetrics
? RenderingHints.VALUE_FRACTIONALMETRICS_ON
: RenderingHints.VALUE_FRACTIONALMETRICS_OFF));
}
return cachedGraphics;
}
private void beginRendering(boolean ortho, int width, int height,
boolean disableDepthTestForOrtho) {
if (DEBUG && !debugged) {
debug();
}
inBeginEndPair = true;
isOrthoMode = ortho;
beginRenderingWidth = width;
beginRenderingHeight = height;
beginRenderingDepthTestDisabled = disableDepthTestForOrtho;
if (ortho) {
getBackingStore().beginOrthoRendering(width, height,
disableDepthTestForOrtho);
} else {
getBackingStore().begin3DRendering();
}
GL gl = GLU.getCurrentGL();
// Push client attrib bits used by the pipelined quad renderer
gl.glPushClientAttrib((int) GL.GL_ALL_CLIENT_ATTRIB_BITS);
if (!haveMaxSize) {
// Query OpenGL for the maximum texture size and set it in the
// RectanglePacker to keep it from expanding too large
int[] sz = new int[1];
gl.glGetIntegerv(GL.GL_MAX_TEXTURE_SIZE, sz, 0);
packer.setMaxSize(sz[0], sz[0]);
haveMaxSize = true;
}
if (needToResetColor && haveCachedColor) {
if (cachedColor == null) {
getBackingStore().setColor(cachedR, cachedG, cachedB, cachedA);
} else {
getBackingStore().setColor(cachedColor);
}
needToResetColor = false;
}
// Disable future attempts to use mipmapping if TextureRenderer
// doesn't support it
if (mipmap && !getBackingStore().isUsingAutoMipmapGeneration()) {
if (DEBUG) {
System.err.println("Disabled mipmapping in TextRenderer");
}
mipmap = false;
}
}
/**
* 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
*
* @param ortho
* @throws GLException
*/
private void endRendering(boolean ortho) throws GLException {
flushGlyphPipeline();
inBeginEndPair = false;
GL gl = GLU.getCurrentGL();
// Pop client attrib bits used by the pipelined quad renderer
gl.glPopClientAttrib();
// The OpenGL spec is unclear about whether this changes the
// buffer bindings, so preemptively zero out the GL_ARRAY_BUFFER
// binding
if (isVBOAvailable(gl)) {
try {
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
} catch (Exception e) {
isExtensionAvailable_VBO = false;
}
}
if (ortho) {
getBackingStore().endOrthoRendering();
} else {
getBackingStore().end3DRendering();
}
if (++numRenderCycles >= CYCLES_PER_FLUSH) {
numRenderCycles = 0;
if (DEBUG) {
System.err.println("Clearing unused entries in endRendering()");
}
clearUnusedEntries();
}
}
private void clearUnusedEntries() {
final java.util.List deadRects = new ArrayList /*