TextureAtlas
This is a WIP project - Expect breaking changes to occur.
Overview
A fixed-grid texture atlas that stitches multiple same-sized tile images into a single GPU texture, minimizing texture-binding overhead at render time.
Layout:
Tiles are arranged in a square grid of gridSize x gridSize cells,
numbered left-to-right, top-to-bottom, starting at index 0.
┌───┬───┬───┬───┐ │ 0 │ 1 │ 2 │ 3 │ row 0 ├───┼───┼───┼───┤ │ 4 │ 5 │ 6 │ 7 │ row 1 (gridSize = 4) └───┴───┴───┴───┘
UV Coordinates:
Call #getUVMin(int) and #getUVMax(int) to get the bottom-left and top-right UV corners for a tile.
These can be assigned directly to the four vertices of a quad.
TextureAtlas()
public TextureAtlas(String[] resourcePaths, int tileSize, int gridSize) {}Builds a texture atlas by loading each image at the given classpath paths, copying their pixel data into a single CPU-side buffer, then uploading the whole atlas to the GPU as a single texture.
All source images must be exactly tileSize x tileSize pixels.
If fewer paths are provided than gridSize x gridSize, the remaining cells are left transparent.
copyTileIntoAtlas(...)
private void copyTileIntoAtlas(ByteBuffer atlasData, String resourcePath, int tileIndex) {}Loads a single tile image and copies its pixel data into the correct region of the atlas buffer.
Getters and Setters
getUVMin(int tileIndex)
public Vector2f getUVMin(int tileIndex) {}Returns the bottom-left UV coordinate of the tile at the given index.
Assign to the bottom-left vertex of the quad that should show this tile.
getUVMax(int tileIndex)
public Vector2f getUVMax(int tileIndex) {}Returns the top-right UV coordinate of the tile at the given index.
Assign to the top-right vertex of the quad that should show this tile.
getUVs(int tileIndex)
public Vector2f[] getUVs(int tileIndex) {}Returns all four UV coordinates for the tile at the given index, in order:
- Bottom-left
- Bottom-right
- Top-right
- Top-left
Convenient for directly populating the four vertices of a quad.
bind(int unit)
public void bind(int unit) {}Binds the atlas texture to the specified unit.
unbind()
public void unbind() {}Unbinds any texture from GL_TEXTURE_2D on the active unit.
destroy()
public void destroy() {}Deletes the GPU texture. Call when the atlas is no longer needed.
getTextureID()
public int getTextureID() { return textureID; }Returns the OpenGL handle for the atlas texture.
getTileSize()
public int getTileSize() { return tileSize; }Returns the pixel size of each tile.
getGridSize()
public int getGridSize() { return gridSize; }Returns the number of tiles per row/colum.
getAtlasSize()
public int getAtlasSize() { return atlasSize; }Returns the total pixel width/height of the atlas image.