LWJGL OpenGL 精灵表

发布于 2024-10-09 16:10:12 字数 881 浏览 6 评论 0原文

我是 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);

给了我一个包含整个精灵表的矩形,因此 glTexCoord2fuv have 更少大于 1.0f。

我现在需要的是一个公式,可以计算纹理中任何精灵 id 的 uv

纹理位图中的 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 中的任何一个提供 uv。最后一点解释得不太清楚,所以如果您愿意,我会更好地解释它。

先感谢您!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从﹋此江山别 2024-10-16 16:10:12

图像中精灵纹理的位置位于 16 x 16 数组中,因此纹理的位置(如果将第一个纹理计为 0)为:

row = texNo / 16;
col = texNo % 16;

坐标为:

u0 = row / 16;
u1 = (row + 1) / 16;

v0 = col / 16;
v1 = (col + 1) / 16; 

然后将 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:

row = texNo / 16;
col = texNo % 16;

The coordinates are then:

u0 = row / 16;
u1 = (row + 1) / 16;

v0 = col / 16;
v1 = (col + 1) / 16; 

Then replace 1.0f with u1 or v1 as appropriate and 0.0f with u0 or v0 in the glTexCoord2Df calls.

暗藏城府 2024-10-16 16:10:12

精灵表从 uv 中的 0..1 开始延伸。如果是 16x16,则左上角位于 n/16, m/16,其中 n、m 的范围为 0..15。如果您的工作表都是 16x16,您可以通过将 1/16 分解到纹理矩阵中来简化您的生活:

        glMatrixMode(GL_TEXTURE)
        glLoadIdentity()
        glScalef(1f/16f, 1f/16f, 1f)

这将让您使用整数来将精灵寻址到 0,0,1,0,2,0 等,因为矩阵中的缩放。

使用这样的纹理矩阵的另一个优点是纹理坐标变成整数,现在您可以在顶点数组或 VBO 中使用整数类型来表示纹理坐标。

The sprite sheet extends from 0..1 in u and v. If it's 16x16 then the upper left corners are at n/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:

        glMatrixMode(GL_TEXTURE)
        glLoadIdentity()
        glScalef(1f/16f, 1f/16f, 1f)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文