我如何阅读LWJGL中的精灵表? [英] How would I read a sprite sheet in LWJGL?
问题描述
我目前使用LWJGL Textures在屏幕上绘制图像。我想从精灵表中读取Textures *。我正在使用光滑的TextureLoader类来加载纹理。
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.
我绘制一个LWJGL形状并将纹理绑定到它上。
I draw an LWJGL Shape and bind a Texture onto it.
例如:
我绘制图像:
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();
我认为在调用glTexCoord2f时有一种方法,我可以给它一个精灵偏移并加载而不是纹理中的精灵表,
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:
而不是BufferedImage,纹理对象。
Just instead of BufferedImage, Texture object.
感谢您的帮助!
推荐答案
由Slick纹理加载器内部使用的GL_TEXTURE_2D的纹理坐标需要标准化纹理坐标。也就是说,坐标范围从0.0到1.0。所以(0,0)是纹理的左上角,(1,1)是右下角。假设您手头的像素坐标中有精灵坐标,则必须将x坐标除以纹理宽度,将y坐标除以纹理高度,从而得到标准化的纹理坐标。然后,您将使用glTexCoord将这些坐标提供给OpenGL。
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.
这篇关于我如何阅读LWJGL中的精灵表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!