Trying get my head around the texture wrapper in (the awesome) LibGDX framework and I need help.
I would like to bind a texture (according to Mesh, Color & Texture) that is extracted from a TextureAtlas packed with the TexturePacker. The texture is binding to a rectangular mesh.
I want the texture (instance of Texture) to basically be extracted from a packed file.
Is it doable to use a the createsprite or findregion methods and somehow skip the filehandle step?
Additionally: Anything special one should keep in mind when combining the above method with the AssetManager?
Thanks for sorting me out!
First, you create a TextureAtlas
object by pointing at the text file that describes your atlas (the tool that creates the atlas will create two files: an image and a text file describing its contents):
TextureAtlas myTextures = new TextureAtlas("images/packed.txt");
Then you can look up a TextureRegion
in that atlas (that is, a specific sub-texture of the atlas). The region should have the basename of the original file that was used (there are more details and options if you follow some of the special naming conventions to create arrays of texture elements, but leave that off for now):
TextureRegion region = myTextures.findRegion(fname);
To draw this texture region on a mesh, you'll need to initialize the Mesh
with support for texture coordinates:
Mesh myMesh = new Mesh(...,
new VertexAttribute(Usage.TextureCoordinates, 2, "y"));
The new VertexAttribute(Usage.TextureCoordinates, 2, ...)
tells libGDX that this mesh will have two texture coordinates per vertex (traditionally, the two texture coordinates are called u
and v
). You can have a bunch of different attributes per vertex, but I'm going to assume the only other attribute is a 3-valued Usage.Position
for the x,y,z spatial coordinates.
Now, in the array of floats that defines your mesh (the array you pass to setVertices
) you need to set x, y, and z spatial coordinates plus u, and v texture coordinates for each vertex:
final int floatsPerVertex = 5; // 3 spatial + 2 texture
float[] meshData = new float[numVerticies * floatsPerVertex];
for (int i = 0; i < numVerticies; i++) {
meshData[(i * floatsPerVertex) + 0] = ... ; // x coordinate of i'th vertex
meshData[(i * floatsPerVertex) + 1] = ... ; // y coordinate of i'th vertex
meshData[(i * floatsPerVertex) + 2] = ... ; // z coordinate of i'th vertex
meshData[(i * floatsPerVertex) + 3] = ... ; // u texture coordinate of i'th vertex
meshData[(i * floatsPerVertex) + 4] = ... ; // v texture coordinate of i'th vertex
}
myMesh.setVertices(meshData);
You can compute the right u
and v
for a specific TextureRegion
using the getU
, getV
, getU2
, and getV2
methods. Note that texture coordinates have the origin (u1, v1) in the top-left, and the y-axis points "down" (screen and spatial coordinates in OpenGL usually have the origin in the bottom-left and the y-axis points "up"). Its a little complicated, but very flexible in that you can flip or stretch or distort the texture as it maps onto your mesh.
Since the texture is large (say 512x512) and the specific region is a small subset of that (say 20x20 at 128x128), you'll actually end up giving your mesh texture coordinates that utilize just the 20x20 subset of the entire 512x512 image.
Finally, when you render you need to bind the image, and enable texturing before rendering:
region.getTexture().bind();
Gdx.graphics.getGL10().glEnable(GL10.GL_TEXTURE_2D);
myMesh.render();
Gdx.graphics.getGL10().glDisable(GL10.GL_TEXTURE_2D);
Note that this is much less efficient than it should be. Part of the benefit of a texture atlas is that it should contain a lot of regions that can be rendered together, so you only need to bind one texture, and then render a lot of different textured meshes from that one bound texture.
SpriteBatch
supports sprites defined with a TextureRegion
and the AssetManager
supports loading and finding a TextureAtlas
as a first-class element.