so I'm doing lwjgl, so far I have the loading of 16x16 textures from a 256x256 spritesheet all working, using this code to get the location of the textures:
public Vector2f[] calculateUVMapping(int texture, int atlasWidth, int atlasHeight) {
int textureIndex = texture;
int u = textureIndex % atlasWidth;
int v = textureIndex / atlasHeight;
float xOffset = 1f / atlasWidth;
float yOffset = 1f / atlasHeight;
float uOffset = (u * xOffset);
float vOffset = (v * yOffset);
Vector2f[] UVList = new Vector2f[4];
UVList[0] = new Vector2f(uOffset, vOffset); // 0,0
UVList[1] = new Vector2f(uOffset, vOffset + yOffset); // 0,1
UVList[2] = new Vector2f(uOffset + xOffset, vOffset + yOffset); // 1,1
UVList[3] = new Vector2f(uOffset + xOffset, vOffset); // 1,0
return UVList;
}
This takes the texture ID from left to right, top to bottom in the atlas, starting from 0 and the amount of textures the atlas holds, so to get the first texture in a 256x256 sheet (holds 16 textures in each row and column) I'd do calculateUVMapping(0,16,16)
, along with he corresponding endings for the vector and variable.
Now, say I want to have a 200x20 texture in another 256x256 sheet, or any nonsquare texture, actually, and I want to apply that to a quad- could this code work for getting the co-ords for it, or what code should I use for those?