在二维空间的OpenGL ES定位元素元素、空间、OpenGL、ES

2023-09-08 09:16:05 作者:明眸绕指柔

在我的业余时间,我喜欢玩的iPhone上使用OpenGL ES游戏开发。我扔在一起的小2D横向滚动演示的乐趣,而我是比较新的OpenGL的,我希望得到这方面的一些有经验的开发者的投入。

In my spare time I like to play around with game development on the iPhone with OpenGL ES. I'm throwing together a small 2D side-scroller demo for fun, and I'm relatively new to OpenGL, and I wanted to get some more experienced developers' input on this.

因此​​,这里是我的问题:是否有意义,指定在模型空间中每个二维元的顶点,然后将每个元素,它的最终视觉空间的每次帧绘制

So here is my question: does it make sense to specify the vertices of each 2D element in model space, then translate each element to it's final view space each time a frame is drawn?

举例来说,假设我有一组块(正方形)构成的地面在我的身边,滚轮。每平方定义为:

For example, say I have a set of blocks (squares) that make up the ground in my side-scroller. Each square is defined as:

const GLfloat squareVertices[] = {
        -1.0, 1.0, -6.0,            // Top left
        -1.0, -1.0, -6.0,           // Bottom left
        1.0, -1.0, -6.0,            // Bottom right
        1.0, 1.0, -6.0              // Top right
    }

说我有10这些广场,我需要共同绘制的理由下架。我应该做这样的事情,对于每平方米可见在目前的情景吗?

Say I have 10 of these squares that I need to draw together as the ground for the next frame. Should I do something like this, for each square visible in the current scene?

glPushMatrix();
{
    glTranslatef(currentSquareX, currentSquareY, 0.0);
    glVertexPointer(3, GL_FLOAT, 0, squareVertices);
    glEnableClientState(GL_VERTEX_ARRAY);

    // Do the drawing
}
glPopMatrix();

在我看来,这样做对场景中的每个2D元素,每一帧,变得有点紧张,我会想象中的聪明人谁使用OpenGL远远超过我可能有这样做的更好的方法。

It seems to me that doing this for every 2D element in the scene, for every frame, gets a bit intense and I would imagine the smarter people who use OpenGL much more than I do may have a better way of doing this.

这一切都这样说,我期待听到,我应该配置文件中的code和看到的任何瓶颈可能是:对这些人,我说:我没有写任何此类code然而,我只是在包装我的脑海里围绕它的过程,这样,当我去它去顺畅写吧。

That all being said, I'm expecting to hear that I should profile the code and see where any bottlenecks may be: to those people, I say: I haven't written any of this code yet, I'm simply in the process of wrapping my mind around it so that when I do go to write it it goes smoother.

在分析和优化的问题,我真的不是想prematurely优化在这里,我只是想环绕如何能建立一个2D的场景,并使其我的脑海里。就像我说的,我是比较新的OpenGL和我只是想感受一下事情是如何完成的。如果任何人有更好的方式来做到这一点有什么建议,我很想听听你的看法。

On the subject of profiling and optimization, I'm really not trying to prematurely optimize here, I'm just trying to wrap my mind around how one would set up a 2D scene and render it. Like I said, I'm relatively new to OpenGL and I'm just trying to get a feel for how things are done. If anyone has any suggestions on a better way to do this, I'd love to hear your thoughts.

请记住,我没有兴趣在3D,2D只是现在。谢谢!

Please keep in mind that I'm not interested in 3D, just 2D for now. Thanks!

推荐答案

您关心的是它需要转换模型(在这种情况下,正方形)从模型坐标到世界,当你有很多的车型协调开销。这似乎是静态模型一个明显的优化。

You are concerned with the overhead it takes to transform a model (in this case a square) from model coordinates to world coordinates when you have a lot of models. This seems like an obvious optimization for static models.

如果你建立你的正方形的顶点在世界坐标,那么当然这将是更快,因为每个方将避免这三种功能(glPushMatrix,glPopMatrix和的glTranslatef)的额外费用,因为没有必要的翻译模型世界坐标的渲染时间。我不知道快了多少,这将是,我怀疑这会不会是一个巨大的优化,你失去保持方块模型的模块化坐标:如果在你决定将来你想要这些正方形是可移动?这将是一个困难得多,如果你保持他们的顶点在世界坐标。

If you build your square's vertices in world coordinates, then of course it is going to be faster as each square will avoid the extra cost of these three functions (glPushMatrix, glPopMatrix, and glTranslatef) since there is no need to translate from model to world coordinates at render time. I have no idea how much faster this will be, I suspect that it won't be a humongous optimization, and you lose the modularity of keeping the squares in model coordinates: What if in the future you decide you want these squares to be moveable? That will be a lot harder if you're keeping their vertices in world coordinates.

在总之,这是一个权衡:

In short, it's a tradeoff:

在更多的内存 - 每平方米需要它 自己的一组顶点。 在较少的计算 - 无需执行 glPushMatrix,glPopMatrix,或 的glTranslatef为每平方的渲染时间。 不够灵活 - 缺乏支持(或 复杂)的动态移动这些方块 More Memory - each square needs its own set of vertices. Less computation - no need to perform glPushMatrix, glPopMatrix, or glTranslatef for each square at render time. Less flexible - lacks support (or complicates) for dynamically moving these squares 在更少的内存 - 正方形可以共享相同的顶点数据 在更多的计算 - 每平方米要 在执行三个附加功能 渲染时间。 更灵活 - 广场可以很容易地 通过操纵移动 的glTranslatef电话。 Less memory - the squares can share the same vertex data More Computation - each square must perform three extra functions at render time. More Flexible - squares can easily be moved by manipulating the glTranslatef call.

我想知道什么是正确的决定,唯一的办法就是做和分析。我知道你说你有没有写这个,但我怀疑你的正方形是否在模型或世界坐标是不会做出太大的区别 - 如果是的话,我无法想象一个架构,你可以创建哪里会很难从模式切换你的正方形的世界坐标,反之亦然。

I guess the only way to know what is the right decision is by doing and profiling. I know you said you haven't written this yet, but I suspect that whether your squares are in model or world coordinates it won't make much of a difference - and if it does, I can't imagine an architecture that you could create where it would be hard to switch your squares from model to world coordinates or vice-versa.

祝你好运和iPhone游戏开发的冒险!

Good luck to you and your adventures in iPhone game development!

 
精彩推荐