在 2d 游戏中使用 OpenGL 最有效的方法是什么?
我有一个为 iPhone 编写的游戏引擎,它使用 OpenGL 来绘制 2d 精灵、背景等。我本来可以使用 Cocos2d,但我认为通过编写自己的引擎可以学到更多。它可以工作并且看起来相当高效,但是通过 Instruments OpenGL 分析器运行我的游戏,它会返回很多关于如何做得更好的指示,谈论 VBO 和类似的事情。
假设我的游戏只是绘制许多应用了不同纹理的 2d 四边形,那么最有效的方法是什么?画两个三角形更快还是画一个四边形更快? VBO 对此是否有些矫枉过正,或者说它们实际上会带来性能提升吗?
I've got a game engine I've written for the iPhone that uses OpenGL for drawing 2d sprites, backgrounds etc. I could have used Cocos2d, but I thought I'd learn more by writing my own. It works and seems to be fairly efficient, but running my game through the Instruments OpenGL profiler, it comes back with a lot of pointers for how to do things better, talking about VBO's and things like that.
Assuming my game just draws many 2d quads with different textures applied, what is the most efficient way to do this? Is it quicker to draw 2 triangles or a single quad? Aren't VBO's overkill for this or would they actually bring a performance improvement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我不知道简单四边形的实际性能增益如何。但是,如果您的“几何图形”是静态的,您就可以充分利用显示列表。它们使用起来非常简单,几乎不需要更改代码。
值得注意的是,最昂贵的操作是上下文切换。这包括从绑定纹理(非常昂贵)到更改颜色(不太昂贵)的任何内容。因此,您可能能做的最好的事情就是一次性使用一种纹理渲染所有精灵。
如果您有一个大的静态关卡,您可以将整个事物绘制到显示列表中并仅调用它。需要注意的是,您仍然应该尝试优化上下文切换。
您还应该在代码上投入一些时间,以防止尝试渲染不可见的内容。 (对照整个关卡的深度学习解决方案进行检查。)
Ok, I don't know about the real performance gain for simple quads. But if your "geometry" is static you get most out of display lists. They are quite simple to use and require almost no code change.
It is important to note that the most expensive operation are context switches. That is anything from binding a texture (very expensive) to changing the color (not so expensive). So the best thing you probably can do is render all sprites using one texture in one go.
If you have one big static level, you can draw the entire thing into a display list and only call that. There is to note that you still should try to optimize for context switches.
You should also invest some time in code that prevents trying to render things that are not visible. (Check that against the DL solution for the entire level.)