iOS 和多个 OpenGL 视图 [英] iOS and multiple OpenGL views

查看:9
本文介绍了iOS 和多个 OpenGL 视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 iPad 应用程序,它使用 OpenGL 在多个 OpenGL 视图中绘制一些非常简单(不超过 1000 或 2000 个顶点)旋转模型.当前在一个网格中有 6 个视图,每个视图都有自己的显示链接来更新绘图.由于模型的简单性,这是迄今为止最简单的方法,我没有时间编写完整的 OpenGL 接口.

I'm currently developping an iPad app which is using OpenGL to draw some very simple (no more than 1000 or 2000 vertices) rotating models in multiple OpenGL views. There are currently 6 view in a grid, each one running its own display link to update the drawing. Due to the simplicity of the models, it's by far the simplest method to do it, I don't have the time to code a full OpenGL interface.

目前,它在性能方面表现良好,但存在一些令人讨厌的故障.前 3 个 OpenGL 视图显示没有问题,后 3 个仅显示几个三角形(同时仍保留旋转模型的能力).还有一些情况下 glDrawArrays 调用直接进入 EXC_BAD_ACCESS(尤其是在模拟器上),这告诉我缓冲区有问题.

Currently, it's doing well performance-wise, but there are some annoying glitches. The first 3 OpenGL views display without problems, and the last 3 only display a few triangles (while still retaining the ability to rotate the model). Also there are some cases where the glDrawArrays call is going straight into EXC_BAD_ACCESS (especially on the simulator), which tell me there is something wrong with the buffers.

我检查的(以及双重和三重检查)是:

What I checked (as well as double- and triple-checked) is :

  • 缓冲区分配似乎没问题
  • 在 dealloc 时释放所有资源
  • 仪器显示了一些警告,但似乎没有任何相关性

我认为这可能与我同时绘制多个视图有关,那么我应该在那里做任何已知的事情吗?每个视图都有自己的上下文,但也许我做错了什么......

I'm thinking it's probably related to my having multiple views drawing at the same time, so is there any known thing I should have done there? Each view has its own context, but perhaps I'm doing something wrong with that...

另外,我刚刚注意到,在模拟器中,受影响的视图在所有顶点的正确绘图和只有少数顶点的错误绘图之间闪烁.

Also, I just noticed that in the simulator, the afflicted views are flickering between the right drawing with all the vertices and the wrong drawing with only a few.

无论如何,如果您有任何想法,谢谢分享!

Anyway, if you have any ideas, thanks for sharing!

推荐答案

好吧,我要回答我自己的问题,因为我终于找到了发生的事情.导致所有这些问题的原因是缺少一条小线.

Okay, I'm going to answer my own question since I finally found what was going on. It was a small missing line that was causing all those problems.

基本上,要同时显示多个 OpenGL 视图,您需要:

Basically, to have multiple OpenGL views displayed at the same time, you need :

  • 任一个,每个视图相同的上下文.在这里,您必须注意不要同时使用多个线程进行绘制(即以某种方式锁定上下文,如 this answer.您必须每次在每一帧上重新绑定帧缓冲区和渲染缓冲区.
  • 或者,您可以为每个视图使用不同的上下文.然后,您必须在每一帧重新设置上下文,因为其他显示链接可能(并且会在我的情况下)导致您的 OpenGL 调用使用错误的数据.此外,由于您的上下文被保留,因此无需重新绑定帧缓冲区和渲染缓冲区.
  • Either, the same context for every view. Here, you have to take care not to draw with multiple threads at the same time (i.e. lock the context somehow, as explained on this answer. And you have to re-bind the frame- and render-buffers every time on each frame.
  • Or, you can use different contexts for each view. Then, you have to re-set the context on each frame, because other display links, could (and would, as in my case) cause your OpenGL calls to use the wrong data. Also, there is no need for re-binding frame- and render-buffers since your context is preserved.

另外,在每一帧之后调用 glFlush(),告诉 GPU 完成每一帧的完全渲染.

Also, call glFlush() after each frame, to tell the GPU to finish rendering each frame fully.

就我而言(第二个),渲染每一帧的代码(在 iOS 中)如下所示:

In my case (the second one), the code for rendering each frame (in iOS) looks like :

- (void) drawFrame:(CADisplayLink*)displayLink {
  // Set current context, assuming _context
  // is the class ivar for the OpenGL Context
  [EAGLContext setCurrentContext:_context]

  // Clear whatever you want
  glClear (GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

  // Do matrix stuff
  ...
  glUniformMatrix4fv (...);

  // Set your viewport
  glViewport (0, 0, self.frame.size.width, self.frame.size.height);

  // Bind object buffers
  glBindBuffer (GL_ARRAY_BUFFER, _vertexBuffer);
  glVertexAttribPointer (_glVertexPositionSlot, 3, ...);

  // Draw elements
  glDrawArrays (GL_TRIANGLES, 0, _currentVertexCount);

  // Discard unneeded depth buffer
  const GLenum discard[] = {GL_DEPTH_ATTACHMENT};
  glDiscardFramebufferEXT (GL_FRAMEBUFFER, 1, discard);

  // Present render buffer
  [_context presentRenderbuffer:GL_RENDERBUFFER];

  // Unbind and flush
  glBindBuffer (GL_ARRAY_BUFFER, 0);
  glFlush();
}

编辑

我要编辑这个答案,因为我发现运行多个 CADisplayLinks 可能会导致一些问题.您必须确保将 CADisplayLink 实例的 frameInterval 属性设置为 0 或 1 以外的值.否则,运行循环将只有时间调用第一个渲染方法,然后它会一次又一次地调用它.就我而言,这就是为什么只有一个物体在移动的原因.现在,它设置为 3 或 4 帧,运行循环有时间调用所有的渲染方法.

I'm going to edit this answer, since I found out that running multiple CADisplayLinks could cause some issues. You have to make sure to set the frameInterval property of your CADisplayLink instance to something other than 0 or 1. Else, the run loop will only have time to call the first render method, and then it'll call it again, and again. In my case, that was why only one object was moving. Now, it's set to 3 or 4 frames, and the run loop has time to call all the render methods.

这仅适用于设备上运行的应用程序.模拟器速度很快,不关心这些.

这篇关于iOS 和多个 OpenGL 视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆