# How to create texture coordinates for a spritesheet with lwjgl

Using lwjgl and slick-util I've been trying to use certain pieces of a spritesheet as textures. lwjgl uses glTexCoord2f(); to map coordinates to individual vertices, and the coordinates it takes in are normalized (between 1 and 0), so I've been using this code to make the coordinates only use a small piece of the full Texture:

glTexCoord2f(0 / 128, 0 / 128); glVertex2f(0, 0);
glTexCoord2f(0 / 128, 8 / 128); glVertex2f(0, 32);
glTexCoord2f(8 / 128, 8 / 128); glVertex2f(32, 32);
glTexCoord2f(8 / 128, 0 / 128); glVertex2f(32, 0);


but whenever I used normalized coordinates that are a fraction of the images size I just get a solid square that had 50% alpha.

My question is: what am I doing that's making the texture coordinates screw up? Is there a better way to go about this?

Any help is appreciated, thanks in advance!

• Whats with 8/128? – Savlon Feb 7 '13 at 2:55
• 8 is a coordinate and 128 is the texture's width. I'm getting the normalized texture coordinates by dividing the x and y by the width and height of the image. It doesn't seem to be working though :( – Kwauhn Feb 7 '13 at 2:59
• I rolled back your edits, if you have another issue that's unrelated to this one, ask it in a new question. – MichaelHouse Feb 7 '13 at 4:24

You are dividing an integer with an integer: 8 / 128, which results zero. Change it to floating point division: 8.0f / 128.0f.

• Yes! That's it! Now I'm getting textures! Thanks for the help (I knew it was something small like that). – Kwauhn Feb 7 '13 at 15:14

3Texture coordinates for a sprite sheet can be obtained simply by using the number of sprites wide and the number of sprites high. It's a simple modulus operation using that information. Something like the following will return the coordinates for a textured quad:

public static 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;

UVList = new Vector2f(uOffset, vOffset); // 0,0
UVList = new Vector2f(uOffset, vOffset + yOffset); // 0,1
UVList = new Vector2f(uOffset + xOffset, vOffset + yOffset); // 1,1
UVList = new Vector2f(uOffset + xOffset, vOffset); // 1,0

return UVList;
}


Where texture is the index of the sprite you want, atlasWidth is the number of evenly sized sprites in the entire texture width and atlasHeight is the number of evenly sized sprites in the entire texture height.

Then you can see the coordinates labeled in the comments, it will return an array of coordinates like: [(0,0),(0,1),(1,0),(1,1)].

texture counts the index from left to right, top to bottom. So for example if I wanted to get the 6th sprite in a atlas that was 5 sprites wide and 10 sprites tall, I would use calculateUVMapping(6,5,10); That sprite would be the one in the second column of the second row (because we start at 0).

• Thanks! I'll use this in for getting the sprite now that I know what I was doing wrong :P – Kwauhn Feb 7 '13 at 15:13