开发者

Handling iPhone orientation change in OpenGLES

开发者 https://www.devze.com 2023-04-02 08:51 出处:网络
I\'m having a problem with my OpenGLES views when the device orientation changes. I\'m using Jeff Lamarche\'s Xcode template.

I'm having a problem with my OpenGLES views when the device orientation changes. I'm using Jeff Lamarche's Xcode template.

If the AutoresizeMask is set, - (void)layoutSubviews is called on the GLView. When this happens the objects I'm drawing disappear. The OpenGL view is still partly working as I can change the background开发者_StackOverflow中文版 colour and see the change.

If the AutoresizeMask is not set, the view rotates and the OpenGL objects are still visible but then the view is the wrong size.

This shows that there's something in layoutSubviews that is causing a problem when called more than once.

Here's some very basic code for drawView which shows the problem

- (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?


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. 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. The speaker also mentions that if you are using the GLKView it will resize the framebuffer on your behalf.

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; 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号