public class TextureAtlas extends Object
Modifier and Type | Class and Description |
---|---|
protected class |
TextureAtlas.AtlasBackingStore
Implementation of the JOGL BackingStoreManager interface for texture atlas.
|
protected static class |
TextureAtlas.Entry
Represents an image element in a texture atlas.
|
Modifier and Type | Field and Description |
---|---|
protected Color |
clearColor
Indicates the color used to fill regions of this texture atlas that do not contain a sub-image element.
|
protected static double |
DEFAULT_MAX_VERTICAL_FRAGMENTATION
The texture atlas' default maximum vertical fragmentation: 0.7.
|
protected static boolean |
DEFAULT_USE_ANISOTROPY
The texture atlas' default setting for the useAnisotropy property:
true . |
protected static boolean |
DEFAULT_USE_MIP_MAPS
The texture atlas' default setting for the useMipMaps property:
true . |
protected Rectangle |
dirtyRect
Indicates the rectangle within this texture atlas' backing image that is currently out-of-sync with its
corresponding OpenGL texture.
|
protected Queue<Object> |
disposedTextureKeys
Queue of texture keys corresponding to disposed backing images.
|
protected Map<Object,TextureAtlas.Entry> |
entryMap
Maps element keys to their corresponding entry.
|
protected boolean |
evictOldElements
Indicates whether this texture atlas evicts old elements in order to make room for a new element when the atlas
is full.
|
protected Graphics2D |
g
Temporary AWT graphics instance used to move image elements during a beginMovement/endMovement block.
|
protected int |
maxHeight
Indicates this texture atlas' maximum total height, in pixels.
|
protected double |
maxVerticalFragmentation
Indicates the maximum amount of vertical fragmentation this texture atlas allows before compacting its elements.
|
protected int |
maxWidth
Indicates this texture atlas' maximum total width, in pixels.
|
protected RectanglePacker |
rectPacker
The JOGL rectangle packer used by this texture atlas to determine how to pack the elements within this texture
atlas' backing image.
|
protected Object |
textureKey
Indicates the current key corresponding to this texture atlas' OpenGL texture in the GPU resource cache.
|
protected boolean |
useAnisotropy
Indicates whether this texture atlas applies an anisotropic filter to each atlas element.
|
protected boolean |
useMipMaps
Indicates whether this texture atlas generates mip-maps for each atlas element.
|
Constructor and Description |
---|
TextureAtlas(int initialWidth,
int initialHeight,
int maxWidth,
int maxHeight)
Constructs a texture atlas with the specified initial and maximum dimensions.
|
TextureAtlas(int initialWidth,
int initialHeight,
int maxWidth,
int maxHeight,
boolean useMipMaps,
boolean useAnisotropy)
Constructs a texture atlas with the specified initial and maximum dimensions.
|
Modifier and Type | Method and Description |
---|---|
void |
add(Object key,
BufferedImage image)
Adds a new element to this texture atlas with the specified key and image.
|
protected void |
beginMoveEntries(BufferedImage oldBackingImage,
BufferedImage newBackingImage)
Called when the atlas is performing a full re-layout of its elements, just before the layout begins.
|
boolean |
bind(DrawContext dc)
Binds this texture atlas' OpenGL texture to the GLContext attached to the draw context.
|
void |
clear()
Removes all elements from this texture atlas.
|
protected void |
clearDirtyRect()
Removes any regions in this texture atlas' backing image previously marked as needing to be synchronized with the
OpenGL texture.
|
protected void |
clearRect(BufferedImage backingImage,
int x,
int y,
int width,
int height)
Fills the specified rectangle with the clear color in the backing image.
|
boolean |
contains(Object key)
Indicates whether this texture atlas contains an element with the specified key.
|
protected BufferedImage |
createBackingImage(int width,
int height)
Creates and returns a BufferedImage with the specified dimensions to use as this texture atlas' backing image.
|
protected RectanglePacker |
createRectanglePacker(int initialWidth,
int initialHeight)
Returns a new JOGL rectangle packer that is used by this texture atlas to determine how to pack the elements
within this texture atlas' backing image.
|
protected void |
disposeBackingImage()
Disposes of this texture atlas' current backing image.
|
protected void |
disposeOldTextures(DrawContext dc)
Removes textures corresponding to this texture atlas' disposed backing images from the draw context's GPU
resource cache.
|
protected void |
doAdd(Object key,
BufferedImage image)
Adds a new element to this texture atlas with the specified key and image.
|
protected void |
doRemove(TextureAtlas.Entry entry)
Removes the element corresponding to the specified entry from this texture atlas, freeing the space it occupied
to be used by other elements.
|
protected void |
drawImage(BufferedImage backingImage,
BufferedImage image,
int x,
int y,
boolean drawBorder)
Draws the specified image in the backing image at the specified (x, y) location.
|
protected void |
endMoveEntries(BufferedImage oldBackingImage,
BufferedImage newBackingImage)
Called when the atlas is performing a full re-layout of its elements, just after the layout ends.
|
protected Rectangle |
getDirtyRect()
Returns the region of this texture atlas' backing image that is not currently synchronized with the OpenGL
texture.
|
int |
getHeight()
Indicates this texture atlas' current height, in pixels.
|
int |
getMaxHeight()
Indicates this texture atlas' maximum height, in pixels.
|
int |
getMaxWidth()
Indicates this texture atlas' maximum width, in pixels.
|
int |
getNumElements()
Returns the number of elements currently in this texture atlas.
|
Dimension |
getSize(Object key)
Returns the image dimensions associated with an element in this texture atlas.
|
TextureCoords |
getTexCoords(Object key)
Returns the OpenGL texture coordinates associated with an element in this texture atlas.
|
protected Texture |
getTexture(DrawContext dc)
Indicates the OpenGL
Texture associated with this texture atlas. |
int |
getWidth()
Indicates this texture atlas' current width, in pixels.
|
boolean |
isEmpty()
Indicates whether this texture atlas contains any elements.
|
boolean |
isEvictOldElements()
Indicates whether this texture atlas evicts the oldest elements in order to make room for a new element when the
atlas is full.
|
boolean |
isUseAnisotropy()
Indicates whether this texture atlas applies an anisotropic filter to each atlas element.
|
boolean |
isUseMipMaps()
Indicates whether this texture atlas generates mip-maps for each atlas element.
|
protected Texture |
makeTextureWithBackingImage(DrawContext dc)
Creates an OpenGL texture by loading this texture atlas's backing image into a new texture with the same
dimensions.
|
protected void |
markDirty(int x,
int y,
int width,
int height)
Marks a region of this texture atlas' backing image as needing to be synchronized with the OpenGL texture.
|
protected void |
markUsed(TextureAtlas.Entry entry)
Marks the specified entry as used by setting its last used time to the current time in nanoseconds.
|
protected void |
moveEntry(BufferedImage oldBackingImage,
Rect oldRect,
BufferedImage newBackingImage,
Rect newRect)
Called for each atlas element when the atlas is performing a full re-layout of its elements.
|
boolean |
remove(Object key)
Removes the element with the specified key from this texture atlas, freeing the space it occupied to be used by
other elements.
|
protected boolean |
removeLeastRecentlyUsedEntry()
Removes the oldest entry from this texture atlas.
|
void |
setEvictOldElements(boolean evictOldElements)
Specifies whether this texture atlas should evict the oldest elements in order to make room for a new element
when the atlas is full.
|
protected void |
setTexture(DrawContext dc,
Texture texture)
Specifies the OpenGL
Texture associated with this texture atlas. |
protected void |
setTextureParameters(DrawContext dc)
Specifies the OpenGL texture parameters associated with this texture atlas' OpenGL texture.
|
protected Texture |
syncTexture(DrawContext dc)
Synchronizes this texture atlas's backing image with its OpenGL texture, creating an OpenGL texture as necessary.
|
protected Texture |
updateTextureWithSubImage(DrawContext dc,
Rectangle rect)
Loads a sub-region of this texture atlas' backing image into its OpenGL texture.
|
protected Color clearColor
protected static final double DEFAULT_MAX_VERTICAL_FRAGMENTATION
protected static final boolean DEFAULT_USE_ANISOTROPY
true
.protected static final boolean DEFAULT_USE_MIP_MAPS
true
.protected Rectangle dirtyRect
null
when this texture atlas' backing image is
synchronized with its OpenGL texture. Initially null
.protected Queue<Object> disposedTextureKeys
bind
. While disposed backing textures would eventually be evicted by the GPU resource cache,
explicitly disposing them avoids polluting the GPU resource cache with orphaned textures that are used only by
this texture atlas.protected Map<Object,TextureAtlas.Entry> entryMap
protected boolean evictOldElements
true
if this atlas evicts elements to make room for new elements, and false
otherwise. Initially code false
.protected Graphics2D g
null
.protected int maxHeight
protected double maxVerticalFragmentation
protected int maxWidth
protected RectanglePacker rectPacker
protected Object textureKey
protected boolean useAnisotropy
true
if
this texture atlas applies an anisotropic filter, and false
otherwise. This has no effect if
useMipMaps is false
. Specified during construction.protected boolean useMipMaps
true
if this texture
atlas generates mip-maps, and false
otherwise. Specified during construction.public TextureAtlas(int initialWidth, int initialHeight, int maxWidth, int maxHeight)
initialWidth
- the texture atlas' initial width, in pixels. Must be greater than zero.initialHeight
- the texture atlas' initial height, in pixels. Must be greater than zero.maxWidth
- the texture atlas' maximum width, in pixels. Must be greater than or equal to initialWidth.maxHeight
- the texture atlas' maximum height, in pixels. Must be greater than or equal to
initialHeight.IllegalArgumentException
- if any of initialWidth, initialHeight, maxWidth, or maxHeight are less than or
equal to zero, if maxWidth is less than initialWidth, or if maxHeight is less
than initialHeight.public TextureAtlas(int initialWidth, int initialHeight, int maxWidth, int maxHeight, boolean useMipMaps, boolean useAnisotropy)
initialWidth
- the texture atlas' initial width, in pixels. Must be greater than zero.initialHeight
- the texture atlas' initial height, in pixels. Must be greater than zero.maxWidth
- the texture atlas' maximum width, in pixels. Must be greater than or equal to initialWidth.maxHeight
- the texture atlas' maximum height, in pixels. Must be greater than or equal to
initialHeight.useMipMaps
- whether to generate mip-maps for each atlas element. true
to generate mip-maps,
and false
otherwise.useAnisotropy
- whether to apply an anisotropic filter to each atlas element. true
to apply an
anisotropic filter, and false
otherwise. This has no effect if useMipMaps is
false
.IllegalArgumentException
- if any of initialWidth, initialHeight, maxWidth, or maxHeight are less than or
equal to zero, if maxWidth is less than initialWidth, or if maxHeight is less
than initialHeight.public void add(Object key, BufferedImage image)
key
- an object used to reference the image.image
- the image to add.IllegalArgumentException
- if either the key or image is null
, or if the image dimensions are
greater than this texture atlas' maximum dimensions.WWRuntimeException
- if this texture atlas is too full to fit the image in its layout.protected void beginMoveEntries(BufferedImage oldBackingImage, BufferedImage newBackingImage)
oldBackingImage
- the backing image corresponding to the previous layout.newBackingImage
- the backing image corresponding to the new layout.public boolean bind(DrawContext dc)
bind
.dc
- the current draw context.true
if the texture is bound, and false
otherwise.IllegalArgumentException
- if the draw context is null
.public void clear()
protected void clearDirtyRect()
protected void clearRect(BufferedImage backingImage, int x, int y, int width, int height)
backingImage
- the destination backing image to fill with the clear color.x
- the X coordinate of the rectangle's upper-left corner, in pixels.y
- the Y coordinates of the rectangle's upper-left corner, in pixels.width
- the rectangle's width, in pixels.height
- the rectangle's height, in pixels.public boolean contains(Object key)
key
- the key which the element is referenced by.true
if this texture atlas contains an element with the specified key, and
false
otherwise.IllegalArgumentException
- if the key is null
.protected BufferedImage createBackingImage(int width, int height)
width
- the backing image's minimum width, in pixels.height
- the backing image's minimum height, in pixels.protected RectanglePacker createRectanglePacker(int initialWidth, int initialHeight)
TextureAtlas.AtlasBackingStore
as the backing store manager. Called during construction.initialWidth
- this texture atlas' initial width.initialHeight
- this texture atlas' initial height.protected void disposeBackingImage()
protected void disposeOldTextures(DrawContext dc)
dc
- the draw context containing the GPU resource cache to remove textures from.protected void doAdd(Object key, BufferedImage image)
key
- an object used to reference the image.image
- the image to add.WWRuntimeException
- if this texture atlas is too full to fit the image in its layout.protected void doRemove(TextureAtlas.Entry entry)
entry
- the entry to remove.protected void drawImage(BufferedImage backingImage, BufferedImage image, int x, int y, boolean drawBorder)
true
, this copies the image's outer pixels into 1 pixel border surrounding the original image. This
border avoids sampling pixels from neighboring atlas elements when an OpenGL box filter is applied to this
image.backingImage
- the destination backing image to draw into.image
- the source image to draw.x
- the X coordinate of the image's upper-left corner, in pixels.y
- the Y coordinates of the image's upper-left corner, in pixels.drawBorder
- true
this copy the image's outer pixels into 1 pixel border surrounding the
original image, or false
to draw only the image.protected void endMoveEntries(BufferedImage oldBackingImage, BufferedImage newBackingImage)
oldBackingImage
- the backing image corresponding to the previous layout.newBackingImage
- the backing image corresponding to the new layout.protected Rectangle getDirtyRect()
public int getHeight()
public int getMaxHeight()
public int getMaxWidth()
public int getNumElements()
public Dimension getSize(Object key)
null
this texture atlas does not contain the element.key
- the key which the element is referenced by.null
if this texture atlas
does not contain the element.public TextureCoords getTexCoords(Object key)
null
if this texture atlas does not contain the element.
The returned texture coordinates can change any time an element is added or removed from this texture atlas, and
therefore should not be cached unless the caller has explicit knowledge of when this texture atlas has changed.key
- the key which the element is referenced by.null
if this
texture atlas does not contain the element.protected Texture getTexture(DrawContext dc)
Texture
associated with this texture atlas.dc
- the current draw context.null
if the texture does not currently exist.public int getWidth()
public boolean isEmpty()
true
if this texture atlas contains at least one element, and false
otherwise.public boolean isEvictOldElements()
true
if this atlas evicts old elements to make room for new elements, and false
otherwise.setEvictOldElements(boolean)
public boolean isUseAnisotropy()
false
. Specified during construction.true
if this texture atlas applies an anisotropic filter, and false
otherwise.public boolean isUseMipMaps()
true
if this texture atlas generates mip-maps, and false
otherwise.protected Texture makeTextureWithBackingImage(DrawContext dc)
dc
- the current draw context.protected void markDirty(int x, int y, int width, int height)
x
- the X coordinate of the region's upper-left corner, in pixels.y
- the Y coordinate of the region's upper-left corner, in pixels.width
- the region's width, in pixels.height
- the region's height, in pixels.protected void markUsed(TextureAtlas.Entry entry)
entry
- the entry who's last used time is marked.protected void moveEntry(BufferedImage oldBackingImage, Rect oldRect, BufferedImage newBackingImage, Rect newRect)
oldBackingImage
- the backing image corresponding to the previous layout.oldRect
- the element's location in oldBackingImage.newBackingImage
- the backing image corresponding to the new layout.newRect
- the element's location in newBackingImage.public boolean remove(Object key)
false
but otherwise does nothing.key
- an object used to reference the element to remove.true
if this texture atlas contained an element with the specified key, and
false
otherwise.IllegalArgumentException
- if the key is null
.protected boolean removeLeastRecentlyUsedEntry()
true
if this removed an entry, and false
if there are no entries to remove.public void setEvictOldElements(boolean evictOldElements)
add
with an element that does not fit in the current
atlas layout causes this texture atlas to throw an exception if the atlas cannot be expanded. When enabled, the
oldest elements are evicted until there is enough space to fit the element in the layout.evictOldElements
- true
if this atlas should evict old elements to make room for new elements,
and false
otherwise.protected void setTexture(DrawContext dc, Texture texture)
Texture
associated with this texture atlas.dc
- the current draw context.texture
- this instance's OpenGL texture, or null
to specify that this texture atlas has no
texture.protected void setTextureParameters(DrawContext dc)
GLContext
.dc
- the current draw context.protected Texture syncTexture(DrawContext dc)
dc
- the current draw context.protected Texture updateTextureWithSubImage(DrawContext dc, Rectangle rect)
null
if this texture atlas' does not have an OpenGL texture.dc
- the current draw context.rect
- the rectangle to load.null
if this texture atlas' does not have an OpenGL
texture.