Skip to main content
Made the code so it would actually load the entire texture, not just a triangle from it.
Source Link

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


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

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

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[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;
}

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

Source Link
House
  • 73.5k
  • 17
  • 188
  • 276

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