How to avoid momentary stretching on autorotation of iOS OpenGL ES apps

后端 未结 1 1216
滥情空心
滥情空心 2020-12-30 12:00

This has been bugging me recently. It is quite straightforward to put together an OpenGL ES app that supports both portrait and landscape. But during autorotation, the syst

相关标签:
1条回答
  • 2020-12-30 12:35

    I managed to fix this fairly easily in my 3D app by fetching the view size from the GLKView presentationLayer. This will give the current view size during the rotation animation, and the correct projection matrix can be calculated based on this size during the update step. This prevents any incorrect aspect ratio distortion.

    If you want to try this, you can add the following lines to the OpenGL Game template in Xcode:

    - (void)update
    {
        // get the size from the presentation layer, so that animating does not squish
        CALayer *presentationLayer = [self.view.layer presentationLayer];
        CGSize layerSize = presentationLayer.bounds.size;
        float aspect = fabsf(layerSize.width / layerSize.height);
        GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);
    
        ...
    }
    
    0 讨论(0)
提交回复
热议问题