简体   繁体   English

我将如何阅读LWJGL中的Sprite表?

[英]How would I read a sprite sheet in LWJGL?

I currently use LWJGL Textures to draw images on the screen. 我目前使用LWJGL纹理在屏幕上绘制图像。 I would like to read Textures* from a sprite sheet. 我想从Sprite工作表中读取Textures *。 I am using slick's TextureLoader class to load the textures. 我正在使用slick的TextureLoader类加载纹理。

I draw an LWJGL Shape and bind a Texture onto it. 我绘制一个LWJGL形状并将纹理绑定到其上。

eg: 例如:

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, 我认为有一种方法可以在调用glTexCoord2f时给它一个sprite偏移量,然后将sprite工作表加载到纹理中,

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 从Sprite表Java读取图像

Just instead of BufferedImage, Texture object. 只是代替了BufferedImage,而是纹理对象。

Thank you for the help! 感谢您的帮助!

Texture coordinates for GL_TEXTURE_2D, used internally by the Slick texture loader, require normalized texture coordinates. Slick纹理加载器内部使用的GL_TEXTURE_2D的纹理坐标需要标准化的纹理坐标。 That is, the coordinates range from 0.0 to 1.0. 即,坐标范围为0.0至1.0。 So (0,0) is the top-left corner of the texture, and (1,1) is the bottom-right corner. 因此(0,0)是纹理的左上角,而(1,1)是纹理的右下角。 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. 假设手腕上的精灵坐标位于像素坐标中,则必须将x坐标除以纹理宽度,将y坐标除以纹理高度,从而得到归一化的纹理坐标。 You would then supply these coordinates to OpenGL using glTexCoord. 然后,您可以使用glTexCoord将这些坐标提供给OpenGL。

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. 观看此视频 (我创建的视频 ),以了解如何使用像素坐标代替纹理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM