简体   繁体   English

在OpenGLES中处理iPhone方向的变化

[英]Handling iPhone orientation change in OpenGLES

I'm having a problem with my OpenGLES views when the device orientation changes. 当设备方向发生变化时,我的OpenGLES视图出现问题。 I'm using Jeff Lamarche's Xcode template . 我正在使用Jeff Lamarche的Xcode模板

If the AutoresizeMask is set, - (void)layoutSubviews is called on the GLView. 如果设置了AutoresizeMask ,则在GLView上调用- (void)layoutSubviews When this happens the objects I'm drawing disappear. 当发生这种情况时,我正在绘制的对象消失。 The OpenGL view is still partly working as I can change the background colour and see the change. OpenGL视图仍然部分工作,因为我可以更改背景颜色并查看更改。

If the AutoresizeMask is not set, the view rotates and the OpenGL objects are still visible but then the view is the wrong size. 如果设置AutoresizeMask ,则视图将旋转,OpenGL对象仍然可见,但视图的大小不正确。

This shows that there's something in layoutSubviews that is causing a problem when called more than once. 这表明layoutSubviews中有一些东西在多次调用时会导致问题。

Here's some very basic code for drawView which shows the problem 这是drawView的一些非常基本的代码,它显示了问题

- (void)drawView:(UIView *)theView
{    
    Vertex3D    vertex1 = Vertex3DMake(0.0, 1.0, -3.0);
    Vertex3D    vertex2 = Vertex3DMake(1.0, 0.0, -3.0);
    Vertex3D    vertex3 = Vertex3DMake(-1.0, 0.0, -3.0);
    Triangle3D  triangle = Triangle3DMake(vertex1, vertex2, vertex3);

    glLoadIdentity();
    glClearColor(0.7, 0.7, 0.7, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColor4f(1.0, 0.0, 0.0, 1.0);
    glVertexPointer(3, GL_FLOAT, 0, &triangle);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDisableClientState(GL_VERTEX_ARRAY);
}

When the device is rotated with this code the triangle on the screen disappears. 使用此代码旋转设备时,屏幕上的三角形消失。 Re-orienting back to portrait doesn't make it reappear either. 重新定向回到肖像也不会再次出现。

Should I not allow GLView's to be re-oriented or is this a bug in my OpenGL code? 我不应该允许GLView重定向或者这是我的OpenGL代码中的错误吗?

Correct me if I'm wrong, but I believe in layoutSubviews you need to delete and re-create your framebuffer to match the new size. 如果我错了,请纠正我,但我相信你需要删除layoutSubviews并重新创建你的帧缓冲以匹配新的大小。 If you go here (I presume you are enrolled in the Apple developer program) and check out the video "Best Practices for OpenGL on iOS 5" the speaker talks about rotation, how it was previously suggested to do it manually with your OpenGL because of performance issues, but goes on to say that now it can handled easily by using the built-in rotation. 如果你去这里 (我认为你参加了Apple开发者计划)并查看了视频“iOS 5上的OpenGL最佳实践”,那么演讲者会谈到轮换,以前建议如何使用你的OpenGL手动完成它性能问题,但接着说现在它可以通过使用内置旋转轻松处理。 The speaker also mentions that if you are using the GLKView it will resize the framebuffer on your behalf. 演讲者还提到,如果您使用GLKView ,它将代表您调整帧缓冲区的大小。

Also, I'm not sure what you mean by "a lot of developers are having problems with this". 另外,我不确定你的意思是“很多开发人员都遇到了这个问题”。 Some cursory searches with Google and inside Stack Overflow haven't revealed to me anyone else having this problem; 谷歌和Stack Overflow内部的一些粗略搜索没有向我透露任何有这个问题的人; I suspect there may be something wrong with your code specifically. 我怀疑你的代码可能有问题。

I also followed your link to Jeff Learche's Xcode Template and the link to the Xcode 4 template is broken--this may be because it is from May 2011. Perhaps simply look into the GLK framework, if your OpenGL needs are not too specialized? 我还跟踪了你与Jeff Learche的Xcode模板的链接,并且Xcode 4模板的链接被破坏了 - 这可能是因为它是从2011年5月开始的。如果你的OpenGL需求不是太专业化,也许只需查看GLK框架?

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

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