5

我正在为 iPhone 开发 2D 滚动游戏。我有一个大的图像背景,比如 480×6000 像素,只有一部分是可见的(正好一个屏幕的价值,480×320 像素)。在屏幕上获得这样的背景的最佳方法是什么?

目前,我将背景拆分为多个纹理(以绕过最大纹理大小限制),并将每个帧中的整个背景绘制为带纹理的三角形条。滚动是通过平移模型视图矩阵来完成的。剪刀框设置为窗口大小,480×320 像素。这并不意味着要快,我只是想要一个工作代码,然后再进行优化。

我认为也许 OpenGL 实现足够聪明,可以丢弃背景的不可见部分,但根据我编写的一些测量代码,它看起来背景平均需要 7 毫秒来绘制,最多需要 84 毫秒。(这是在模拟器中测量的。)这大约是整个渲染循环的一半,即。对我来说很慢。

绘制背景应该像将一些 480×320 像素从 VRAM 的一部分复制到另一部分一样简单,或者换句话说,速度极快。接近这种性能的最佳方法是什么?

4

2 回答 2

4

这是做这件事的快速方法。您可以做的事情来提高性能:

  • 尝试不同的纹理格式。大概 SDK 文档有关于首选格式的详细信息,并且大概越小越好。
  • 自己剔除屏幕外的瓷砖
  • 将图像分割成更小的纹理

我假设您正在以 1:1 的缩放级别进行绘制;是这样吗?

编辑:哎呀。更仔细地阅读了您的问题后,我必须提供另一条建议:在模拟器上进行的计时毫无价值。

于 2008-10-20T09:21:11.730 回答
3

快速解决方案:

  • 创建瓷砖的几何矩阵(最好是四边形),以便在可视区域的所有侧面至少有一行/列的屏幕外瓷砖。

  • 将纹理映射到所有这些图块。

  • 只要一个图块超出可视区域,您就可以释放此纹理并绑定一个新的。

  • Move the tiles using a modulo of the tile width and tile height as position (so that the tile will reposition itself at its starting pos when it have moved exactly one tile in length). Also remember to remap the textures during that operation. This allows you to have a very small grid/very little texture memory loaded at any given time. Which I guess is especially important in GL ES.

If you have memory to spare and are still plagued with slow load speed (although you shouldn't for that amount of textures). You could build a texture streaming engine that preloads textures into faster memory (whatever that may be on your target device) when you reach a new area. Mapping as textures will in that case go from that faster memory when needed. Just be sure that you are able to preload it without using up all memory and remember to release it dynamically when not needed.

Here is a link to a GL (not ES) tile engine. I haven't used it myself so I cannot vouch for its functionality but it might be able to help you: http://www.mesa3d.org/brianp/TR.html

于 2008-10-20T09:59:13.980 回答