Hmm, think i might crack open my book on opengl and read a bit about texture mapping, as this is starting to go over my head. I have tried messing around with the values. With 0,3 i see the image tiled 3 times across the rectangular area. Switching it to 0, 1 has it once. Now if i switch it to 0.5f, 1, I just see the character image once, not the one for my ground at all. This image just has a stickman, and then to the right of it, the ground.
Looks like you've got the values I would expect.
Say your atlas looks like this:
1 2 3 4 5
| __________ | 1 2 | | | | 3 4 | |________| |
Let's add some lines in (purely imaginary, not part of the image data) to make it easier to visualize.
1 2 3 4 5
| __________ | 1 || 2 | |___||___| | 3 || 4 | |___||___| |
So our different sprites are the 1, the 2, the 3, and the 4. Pretend that they're more centered in the quadrants of the image.
If we wanted to draw the entire image, our texture coordinates would be:
1 2 3 4
| GL11.glTexCoord2f(0,0); GL11.glTexCoord2f(0,1); GL11.glTexCoord2f(1,1); GL11.glTexCoord2f(1,0); |
Pretty simple, right? You're making your texture coordinates match your vertex coordinates in terms of which index you're looking at.
Now when you want to show just the 1, you keep the same vertex values but change your texture coordinates.
1 2 3 4
| GL11.glTexCoord2f(0,0); GL11.glTexCoord2f(0,0.5f); GL11.glTexCoord2f(0.5f,0.5f); GL11.glTexCoord2f(0.5f,0); |
So, to show the 2 you would use:
1 2 3 4
| GL11.glTexCoord2f(0.5f,0); GL11.glTexCoord2f(1,0); GL11.glTexCoord2f(1,0.5f); GL11.glTexCoord2f(0.5f,0.5f); |
From there, you should be able to figure out how to show 3 and 4.
An easy way to find the tex coords of any rect in an image would be something like this:
1 2 3 4 5 6 7 8 9
| public Vector2f[] getTexCoords(int imageX, int imageY, int imageWidth, int imageHeight, int atlasWidth, int atlasHeight) { Vector2f[] results = new Vector2f[4]; results[0] = new Vector2f( ((float)imageX) / atlasWidth, ((float)imageY) / atlasHeight ); results[1] = new Vector2f( results[0].x + ((float)imageWidth) / atlasWidth, results[0].y ); results[2] = new Vector2f( results[1].x, results[0].y + ((float)imageHeight) / atlasHeight ); results[3] = new Vector2f( results[0].x, results[2].y ); return results; } |
Where all those values are related to the atlas image itself - where, in pixels, the image you want starts, how big it is, and how big your whole atlas is.