1

I currently use LWJGL Textures to draw images on the screen. I would like to read Textures* from a sprite sheet. I am using slick's TextureLoader class to load the textures.

I draw an LWJGL Shape and bind a Texture onto it.

e.g:

Me drawing an image:

    Texture texture = ResourceManager.loadTexture("Images/Tests/test.png");

    GL11.glBegin(GL11.GL_QUADS);
    {
      GL11.glTexCoord2f(0, 0);
      GL11.glVertex2f(0, 0);
      GL11.glTexCoord2f(0, texture.getHeight());
      GL11.glVertex2f(0, height);
      GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
      GL11.glVertex2f(width,height);
      GL11.glTexCoord2f(texture.getWidth(), 0);
      GL11.glVertex2f(width,0);
    }
    GL11.glEnd();

I think there is a way by when calling glTexCoord2f, I could give it a sprite offset and load the sprite sheet in the texture instead,

for example one call would be like this:

      GL11.glTexCoord2f(0+spriteXOffset, texture.getHeight()-spriteYOffset);

But I would really like to know if there is a simpler way, maybe extracting Textures from a single texture for example like they do in here:

Reading images from a sprite sheet Java

Just instead of BufferedImage, Texture object.

Thank you for the help!

Community
  • 1
  • 1
Amit Assaraf
  • 512
  • 11
  • 34

1 Answers1

6

Texture coordinates for GL_TEXTURE_2D, used internally by the Slick texture loader, require normalized texture coordinates. That is, the coordinates range from 0.0 to 1.0. So (0,0) is the top-left corner of the texture, and (1,1) is the bottom-right corner. Assuming that you have your sprite coordinates in pixel coordinates at hand, you then have to divide the x coordinate by the texture width and the y coordinate by the texture height, resulting in normalized texture coordinates. You would then supply these coordinates to OpenGL using glTexCoord.

glTexCoord2f(spriteX / textureWidth, spriteY / textureHeight);
glVertex2f(coordinateX, coordinateY);
glTexCoord2f(spriteX+spriteWidth / textureWidth, spriteY / textureHeight);
glVertex2f(coordinateX2, coordinateY);
// Et cetera

There is, however, also an easier way of doing this. Take a look at this video (I created it), to see how you can use the pixel coordinates for textures instead of normalized ones.

Oskar
  • 1,321
  • 9
  • 19
  • Thanks for the answer!I already watched your video and I prefer not to add any more jars besides LWJGL and slick util and I find the way Im doing it easier but I did what you said and the picture does not show up this is my render code: – Amit Assaraf Aug 14 '12 at 17:34
  • GL11.glTexCoord2f(spriteX/width, spriteY/height); GL11.glVertex2f(0, 0); GL11.glTexCoord2f(spriteX/width, spriteY+cellSize/height); GL11.glVertex2f(0, 32); GL11.glTexCoord2f(spriteX+cellSize/width, spriteY+cellSize/height); GL11.glVertex2f(32,32); GL11.glTexCoord2f(spriteX+cellSize/width, spriteY/width); GL11.glVertex2f(32,0); – Amit Assaraf Aug 14 '12 at 17:34
  • sprite x is 32 sprite y is 0 width is 96 and height is 128 and the texture that Im binding is the sheet and nothing shows up... cell size is 32 – Amit Assaraf Aug 14 '12 at 17:35
  • i noticed in one of the texture calls i divide the y by the width, changed it and it still doesn't show up. – Amit Assaraf Aug 14 '12 at 17:37
  • Try and store the texture coordinates in four floats and printing them out to the console. – Oskar Aug 14 '12 at 17:46
  • float xx = spriteX/width; float yy = spriteY/height; float x2 = spriteX+cellSize/width; float y2 = spriteY+cellSize/height; System.out.println(xx+","+yy); System.out.println(x2+","+y2); – Amit Assaraf Aug 14 '12 at 20:02
  • result:0.333333334,0.0 and 32.3333333332 , 0.25 Im guessing the 32 is bad – Amit Assaraf Aug 14 '12 at 20:02
  • Fixed it by adding ()! but now its weird. I see the charcter but i see half of one tile in my sprite sheet and half of another – Amit Assaraf Aug 14 '12 at 20:04
  • That means one of your texture coordinates is still bad. They should be (1/3, 0, 2/3, 1/4). – Oskar Aug 15 '12 at 05:37
  • They are its really wierd i get 0.333333,0.0,0.666666,0.25 – Amit Assaraf Aug 15 '12 at 21:38