Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question
    
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
1  
I rolled back your edits, if you have another issue that's unrelated to this one, ask it in a new question. –  Byte56 Feb 7 '13 at 4:24

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer
    
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

Texture 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[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); // 1,0
    UVList[3] = new Vector2f(uOffset + xOffset, vOffset + yOffset); // 1,1

    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).

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.