LWJGL OpenGL 精灵表
我是 OpenGL 新手,目前正在尝试渲染一个具有四个面的立方体,每个面都有不同的纹理。
众所周知,为每种脸部类型使用单独的纹理会占用大量内存,并使应用程序变慢。
目前我正在尝试为精灵使用纹理表。我有每个纹理 16x16 像素的图形文件,其中 256 个精灵排列在正方形 (16x16) 中。
我知道这
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(1.0f, 1.0f, 1.0f);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(0.0f, 1.0f, 1.0f);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(0.0f, 0.0f, 1.0f);
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(1.0f, 0.0f, 1.0f);
给了我一个包含整个精灵表的矩形,因此 glTexCoord2f
的 u
和 v
have 更少大于 1.0f。
我现在需要的是一个公式,可以计算纹理中任何精灵 id 的 u
和 v
。
纹理位图中的 ID 如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 16 20 21 22 23 24...
我希望为这些 ID 中的任何一个提供 u
和 v
。最后一点解释得不太清楚,所以如果您愿意,我会更好地解释它。
先感谢您!
I am new to OpenGL and am currently trying to render a cube with four faces, each with a different texture.
As you all know, having a separate texture for each face type is very memory intensive and makes the application slow.
At the moment I am trying to use a texture sheet for the sprite. I have the graphic file with each texture 16x16 pixels with 256 sprites arranged in a square (16x16).
I know that
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(1.0f, 1.0f, 1.0f);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(0.0f, 1.0f, 1.0f);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(0.0f, 0.0f, 1.0f);
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(1.0f, 0.0f, 1.0f);
gives me a rectangle with the whole sprite sheet so u
and v
of glTexCoord2f
have to be less than 1.0f.
What I need now is a formula which will calculate the u
and v
of any sprite id in the texture.
The IDs go as following in the texture bitmap:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 16 20 21 22 23 24...
and I'd like to have a u
and v
for any of these IDs. The last bit isn't explained very well, so I'll explain it better if you would like.
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
图像中精灵纹理的位置位于 16 x 16 数组中,因此纹理的位置(如果将第一个纹理计为 0)为:
坐标为:
然后将 1.0f 替换为 u1 或 v1 作为glTexCoord2Df 调用中的 u0 或 v0 适当且 0.0f 。
The locations of the sprites' texture in the image are in a 16 x 16 array, so the location of a texture (if you count the first one as 0) are:
The coordinates are then:
Then replace 1.0f with u1 or v1 as appropriate and 0.0f with u0 or v0 in the glTexCoord2Df calls.
精灵表从
u
和v
中的 0..1 开始延伸。如果是 16x16,则左上角位于n/16, m/16
,其中 n、m 的范围为 0..15。如果您的工作表都是 16x16,您可以通过将 1/16 分解到纹理矩阵中来简化您的生活:这将让您使用整数来将精灵寻址到 0,0,1,0,2,0 等,因为矩阵中的缩放。
使用这样的纹理矩阵的另一个优点是纹理坐标变成整数,现在您可以在顶点数组或 VBO 中使用整数类型来表示纹理坐标。
The sprite sheet extends from 0..1 in
u
andv
. If it's 16x16 then the upper left corners are atn/16, m/16
where n, m range from 0..15. If your sheets are all 16x16 you can simplify your life by factoring out the 1/16th into the texture matrix:That would let you address a sprite at 0,0, 1,0, 2,0, etc. with integers because of the scaling in the matrix.
A further advantage of using the texture matrix like this is that your texture coordinates become integers and now you can use an integer type in your vertex array or VBO to represent the texture coordinates.