简体   繁体   English

OpenGL ES 2 / iOS GLKit的设计建议

[英]Design advice for OpenGL ES 2 / iOS GLKit

I'd like to build an app using the new GLKit framework, and I'm in need of some design advice. 我想使用新的GLKit框架构建应用程序,并且需要一些设计建议。 I'd like to create an app that will present up to a couple thousand "bricks" (objects with very simple geometry). 我想创建一个可显示多达数千个“砖”(具有非常简单的几何体的对象)的应用。 Most will have identical texture, but up to a couple hundred will have unique texture. 大多数将具有相同的纹理,但多达数百个将具有独特的纹理。 I'd like the bricks to appear every few seconds, move into place and then stay put (in world coords). 我希望砖块每隔几秒钟出现一次,移动到位,然后保持原状(以世界坐标显示)。 I'd like to simulate a camera whose position and orientation are controlled by user gestures. 我想模拟一个相机,其位置和方向由用户手势控制。

The advice I need is about how to organize the code. 我需要的建议是关于如何组织代码。 I'd like my model to be a collection of bricks that have a lot more than graphical data associated with them: 我希望我的模型是一组砖块,这些砖块具有比与它们关联的图形数据更多的功能:

  • Does it make sense to associate a view-like object with each handle geometry, texture, etc.? 将类似视图的对象与每个手柄的几何形状,纹理等相关联是否有意义?
  • Should every brick have it's own vertex buffer? 每个积木都应该有自己的顶点缓冲区吗?
  • Should each have it's own GLKBaseEffect? 每个人都应该拥有自己的GLKBaseEffect吗?
  • I'm looking for help organizing what object should do what during setup, then rendering. 我正在寻找帮助来组织什么对象在设置过程中应该做什么,然后进行渲染。

I hope I can stay close to the typical MVC pattern, with my GLKViewController observing model state changes, controlling eye coordinates based on gestures, and so on. 我希望我可以通过使用GLKViewController观察模型状态变化,基于手势控制眼睛坐标等来保持接近典型的MVC模式。

Would be much obliged if you could give some advice or steer me toward a good example. 如果您能提供一些建议或引导我寻求一个好的榜样,那将是非常有义务的。 Thanks in advance! 提前致谢!

With respect to the models, I think an approach analogous to the relationship between UIImage and UIImageView is appropriate. 关于模型,我认为类似于UIImageUIImageView之间的关系的方法是合适的。 So every type of brick has a single vertex buffer, GLKBaseEffect , texture and whatever else. 因此,每种类型的积木都具有单个顶点缓冲区GLKBaseEffect ,纹理以及其他任何东西。 Each brick may then appear multiple times just as multiple UIImageViews may use the same UIImage . 然后,每个积木可能会出现多次,就像多个UIImageViews使用同一UIImage In terms of keeping multiple reference frames, it's actually a really good idea to build a hierarchy essentially equivalent to UIView , each containing some transform relative to the parent and one sort being able to display a model. 就保留多个参考框架而言,构建一个基本上等效于UIView的层次结构实际上是一个好主意,每个层次结构都包含相对于父UIView的某种转换以及一种能够显示模型的转换。

From the GLKit documentation, I think the best way to keep the sort of camera you want (and indeed the object locations) is to store it directly as a GLKMatrix4 or a GLKQuaternion — so you don't derive the matrix or quaternion (plus location) from some other description of the camera, rather the matrix or quaternion directly is the storage for the camera. 从GLKit文档中,我认为保持所需摄像头(以及对象位置)的最佳方法是将其直接存储为GLKMatrix4GLKQuaternion因此,您无需派生矩阵或四元数(加上位置) )从摄像机的其他描述中得出,而是矩阵或四元数直接作为摄像机的存储空间。

Both of those classes have methods built in to apply rotations, and GLKMatrix4 can directly handle translations. 这两个类都有内置的方法来应用旋转,并且GLKMatrix4可以直接处理平移。 So you can directly map the relevant gestures to those functions. 因此,您可以直接将相关手势映射到那些功能。

The only slightly non-obvious thing I can think of when dealing with the camera in that way is that you want to send the inverse to OpenGL rather than the thing itself. 用这种方式处理相机时,我能想到的唯一一点不太明显的事情就是您想将反函数发送给OpenGL,而不是事物本身。 Supposing you use a matrix, the reasoning is that if you wanted to draw an object at that location you'd load the matrix directly then draw the object. 假设您使用矩阵,则理由是,如果要在该位置绘制对象,则直接加载矩阵,然后绘制该对象。 When you draw an object at the same location as the camera you want it to end up being drawn at the origin. 当您在与相机相同的位置绘制对象时,您希望它最终在原点被绘制。 So the matrix you have to load for the camera is the inverse of the matrix you'd load to draw at that location because you want the two multiplied together to be the identity matrix. 因此,您必须为摄像机加载的矩阵是要加载在该位置绘制的矩阵的逆矩阵,因为您希望将两者相乘才能成为单位矩阵。

I'm not sure how complicated the models for your bricks are but you could hit a performance bottleneck if they're simple and all moving completely independently. 我不确定您的积木模型有多复杂,但是如果它们简单且全部完全独立地移动,则可能会遇到性能瓶颈。 The general rule when dealing with OpenGL is that the more geometry you can submit at once, the faster everything goes. 处理OpenGL的一般规则是,一次可以提交的几何图形越多,一切进行得越快。 So, for example, an entirely static world like that in most games is much easier to draw efficiently than one where everything can move independently. 因此,例如,与大多数游戏可以独立移动的世界相比,像大多数游戏中那样的完全静态世界更容易有效绘制。 If you're drawing six-sided cubes and moving them all independently then you may see worse performance than you might expect. 如果要绘制六面立方体并单独移动它们,则性能可能会比预期的差。

If you have any bricks that move in concert then it is more efficient to draw them as a single piece of geometry. 如果您有任何一起移动的砖块,则将它们绘制为单个几何体会更有效。 If you have any bricks that definitely aren't visible then don't even try to draw them. 如果您有任何绝对看不见的砖块,那就不要尝试绘制它们。 As of iOS 5, GL_EXT_occlusion_query_boolean is available, which is a way to pass some geometry to OpenGL and ask if any of it is visible. 从iOS 5开始, GL_EXT_occlusion_query_boolean可用,这是将某些几何图形传递给OpenGL并询问是否可见的一种方法。 You can use that in realtime scenes by building a hierarchical structure describing your data (which you'll already have if you've directly followed the UIView analogy), calculating or storing some bounding geometry for each view and doing the draw only if the occlusion query suggests that at least some of the bounding geometry would be visible. 您可以在实时场景中使用它,方法是构建描述数据的层次结构(如果您直接遵循UIView类比,则已经具有该结构),为每个视图计算或存储一些边界几何,并且仅当遮挡时才进行绘制查询表明至少某些边界几何是可见的。 By following that sort of logic you can often discard large swathes of your geometry long before submitting it. 通过遵循这种逻辑,您经常可以在提交几何图形之前就丢弃大量几何图形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM