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

查看:107
本文介绍了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 :


  • ,每个视图的相同上下文。在这里,你必须注意不要同时绘制多个线程(即以某种方式锁定上下文,如这个答案。你必须每次在每一帧上重新绑定帧和渲染缓冲区。

  • ,您可以为每个视图使用不同的上下文。然后,您必须重新设置上下文每个框架,因为其他显示链接可能(并且会像我的情况一样)导致您的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天全站免登陆