使用 OpenGL 和 SDL 的 Sprite 表

发布于 2024-11-17 08:00:50 字数 308 浏览 3 评论 0原文

我一直在开发一款新游戏,终于开始对主角的动作进行编码,但我对如何做到这一点有疑问。

以前,我在 Allegro 中制作了两个游戏,因此 spritesheet 很容易实现,因为我在图像上建立了帧和位置,并将每个帧保存在不同的位图中,但我知道使用 OpenGL 这样做是不必要的并且花费多一点。

所以,我一直在思考如何保存我的精灵表并在我的程序中使用,但我只有一个想法。

我加载了图像并转换为纹理,在帮助我制作动画的函数中,我只需抓取纹理的一部分来绘制,而不是将每个纹理存储在程序中。

这是最好的方法吗?

预先感谢您的帮助。

I been working in a new game, and finally reached the point where I started to code the motion of my main character but I have a doubt about how do that.

Previously, I make two games in Allegro, so the spritesheets are kind of easy to implement, because I establish the frame and position on the image, and save every frame in a different bitmap, but I know that do that with OpenGL it's not neccesary and cost a little bit more.

So, I been thinking in how save my spritesheet and used in my program and I have only one idea.

I loaded the image and transformed in a texture, in my function that help me animate I simply grab a portion of the texture to draw instead of store every single texture in my program.

This is the best way to do that?

Thanks beforehand for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

枕头说它不想醒 2024-11-24 08:00:50

你走在正确的轨道上。

需要考虑的事项:

  • 在每个精灵周围留出足够的死区,以便显卡不会以小比例混合相邻精灵的纹素。
  • 适当设置纹理最小/放大过滤。如果您想要块状外观,GL_NEAREST 就可以。
  • 如果您想要花哨并节省一些纹理内存,则没有理由必须将精灵放置在规则网格中。较小的精灵可以在纹理中排列得更紧密。
  • 如果你的精灵是从 3D 模型渲染的,你可以输出正常和将模型的置换贴图映射到另一个纹理,然后将它们组合在片段着色器中,以获得一些很棒的照明和自阴影。

You're on the right track.

Things to consider:

  • leave enough dead space around each sprite so that the video card does not blend in texels from adjacent sprites at small scales.
  • set texture min/mag filtering appropriately. GL_NEAREST is OK if you're going for the blocky look.
  • if you want to be fancy and save some texture memory, there's no reason that the sprites have to be laid out in a regular grid. Smaller sprites can be packed closer in the texture.
  • if your sprites are being rendered from 3D models, you could output normal & displacement maps from the model into another texture, then combine them in a fragment shader for some awesome lighting and self-shadowing.
尬尬 2024-11-24 08:00:50

你的想法是正确的,如果你有一堆精灵,最好将它们全部粘在一个大纹理中。只需将精灵绘制为纹理四边形,其纹理坐标索引到精灵的框架中。您可以进行一些优化,但大多数优化都是围绕尝试充分利用纹理内存并将精灵紧密地打包在一起而避免混合问题。

You got the right idea, if you have a bunch of sprites it is much better to just stick them all in one big textures. Just draw your sprites as textured quads whose texture coordinates index into the frame of the sprite. You can do a few optimizations, but most of them revolve around trying to get the most out of your texture memory and packing the sprites closely together with out blending issues.

白芷 2024-11-24 08:00:50

我知道使用 OpenGL 来实现这一点是没有必要的,而且成本会更高一些。

为什么不呢?将大量精灵放入单个纹理中并没有真正的缺点。您需要做的就是更改纹理坐标以从纹理中选取有问题的区域。

I know that do that with OpenGL it's not neccesary and cost a little bit more.

Why not? There are no real downsides to putting a lot of sprites into a single texture. All you need to do is change the texture coordinates to pick the region in question out of the texture.

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