diff options
author | Kenneth Russel <[email protected]> | 2007-01-04 07:20:30 +0000 |
---|---|---|
committer | Kenneth Russel <[email protected]> | 2007-01-04 07:20:30 +0000 |
commit | e4daa5d745f3d084e0c7180a88597b2582131b63 (patch) | |
tree | 3e5b35a4b3ec92a74abaa11228fabca32e11eae0 /src/classes/com/sun/opengl/util/j2d/TextureRenderer.java | |
parent | 10c76681d1507fa527b59cb28bcde5d1c6a4fe18 (diff) |
With extensive help from Phil Race and Chris Campbell from the Java 2D
team, added a TextRenderer class which enables high-performance
rendering of bitmapped Java 2D fonts, with full Unicode support, into
arbitrary OpenGL drawables using a simple API. Builds on top of the
TextureRenderer class and its associated functionality; added
createAlphaOnlyRenderer() and other methods to enable the TextRenderer.
Caches rendering results on a string-by-string basis in an OpenGL
texture using a fully automatic least-recently-used algorithm for good
efficiency when rendering the same string or strings multiple times.
Uses a rectangle packing algorithm, currently housed in
com.sun.opengl.impl.packrect, for managing the positions of cached
strings on a larger OpenGL texture to avoid OpenGL pipeline state
changes. Added a TestTextRenderer demo which simply adds a moving text
string and frames-per-second counter to the Gears demo; more
sophisticated examples to come. (Some commented-out debugging code is
being temporarily left in the new demo, to be removed in the next
checkin, in order to have it in the version history.)
git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/svn-server-sync/jogl/trunk@1067 232f8b59-042b-4e1e-8c03-345bb8c30851
Diffstat (limited to 'src/classes/com/sun/opengl/util/j2d/TextureRenderer.java')
-rwxr-xr-x | src/classes/com/sun/opengl/util/j2d/TextureRenderer.java | 76 |
1 files changed, 71 insertions, 5 deletions
diff --git a/src/classes/com/sun/opengl/util/j2d/TextureRenderer.java b/src/classes/com/sun/opengl/util/j2d/TextureRenderer.java index 552a66a3d..f758e4f6c 100755 --- a/src/classes/com/sun/opengl/util/j2d/TextureRenderer.java +++ b/src/classes/com/sun/opengl/util/j2d/TextureRenderer.java @@ -64,7 +64,18 @@ public class TextureRenderer { // OpenGL-related work must be. This implies that the user's code // would need to be split up into multiple callbacks run from the // appropriate threads, which would be somewhat unfortunate. + + // Whether we have an alpha channel in the (RGB/A) backing store private boolean alpha; + + // Whether we're using only a GL_INTENSITY backing store + private boolean intensity; + + // Whether smoothing is enabled for the OpenGL texture (switching + // between GL_LINEAR and GL_NEAREST filtering) + private boolean smoothing = true; + + // The backing store itself private BufferedImage image; private Texture texture; @@ -82,10 +93,24 @@ public class TextureRenderer { @param alpha whether to allocate an alpha channel for the texture */ public TextureRenderer(int width, int height, boolean alpha) { + this(width, height, alpha, false); + } + + // Internal constructor to avoid confusion since alpha only makes + // sense when intensity is not set + private TextureRenderer(int width, int height, boolean alpha, boolean intensity) { this.alpha = alpha; + this.intensity = intensity; init(width, height); } + /** Creates a new renderer with a special kind of backing store + which acts only as an alpha channel. Internally, this associates + a GL_INTENSITY OpenGL texture with the backing store. */ + public static TextureRenderer createAlphaOnlyRenderer(int width, int height) { + return new TextureRenderer(width, height, false, true); + } + /** Returns the width of the backing store of this renderer. @return the width of the backing store of this renderer @@ -135,22 +160,54 @@ public class TextureRenderer { @param width the new width of the backing store of this renderer @param height the new height of the backing store of this renderer + @throws GLException If an OpenGL context is not current when this method is called */ - public void setSize(int width, int height) { + public void setSize(int width, int height) throws GLException { init(width, height); } - /** Sets the size of the backing store of this renderer. This may cause the OpenGL texture object associated with this renderer to be invalidated. @param d the new size of the backing store of this renderer + @throws GLException If an OpenGL context is not current when this method is called */ - public void setSize(Dimension d) { + public void setSize(Dimension d) throws GLException { setSize(d.width, d.height); } + /** Sets whether smoothing is enabled for the OpenGL texture; if so, + uses GL_LINEAR interpolation for the minification and + magnification filters. Defaults to true. + + @param smoothing whether smoothing is enabled for the OpenGL texture + @throws GLException If an OpenGL context is not current when this method is called + */ + public void setSmoothing(boolean smoothing) throws GLException { + this.smoothing = smoothing; + // This can be set lazily + if (texture != null) { + GL gl = GLU.getCurrentGL(); + if (smoothing) { + texture.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR); + texture.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); + } else { + texture.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST); + texture.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); + } + } + } + + /** Returns whether smoothing is enabled for the OpenGL texture; see + {@link #setSmoothing setSmoothing}. Defaults to true. + + @return whether smoothing is enabled for the OpenGL texture + */ + public boolean getSmoothing() { + return smoothing; + } + /** Creates a {@link java.awt.Graphics2D Graphics2D} instance for rendering to the backing store of this renderer. The returned object should be disposed of using the normal {@link @@ -340,13 +397,17 @@ public class TextureRenderer { image = null; } - int imageType = (alpha ? BufferedImage.TYPE_INT_ARGB_PRE : BufferedImage.TYPE_INT_RGB); + // Infer the internal format if not an intensity texture + int internalFormat = (intensity ? GL.GL_INTENSITY : 0); + int imageType = + (intensity ? BufferedImage.TYPE_BYTE_GRAY : + (alpha ? BufferedImage.TYPE_INT_ARGB_PRE : BufferedImage.TYPE_INT_RGB)); image = new BufferedImage(width, height, imageType); // Always realllocate the TextureData associated with this // BufferedImage; it's just a reference to the contents but we // need it in order to update sub-regions of the underlying // texture - textureData = TextureIO.newTextureData(image, false); + textureData = new TextureData(internalFormat, 0, false, image); // For now, always reallocate the underlying OpenGL texture when // the backing store size changes mustReallocateTexture = true; @@ -363,6 +424,11 @@ public class TextureRenderer { if (texture == null) { texture = TextureIO.newTexture(textureData); + if (!smoothing) { + // The TextureIO classes default to GL_LINEAR filtering + texture.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST); + texture.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); + } return true; } |