繁体   English   中英

在iOS上的实际横向OpenGL视图

[英]Actual landscape OpenGL View on iOS

我正在将基于C ++的引擎从Android移植到iOS,我对iOS设备上的横向模式如何工作有一些疑问。

我可以在横向模式下成功启动我的设备,但无论我如何旋转(或强制旋转),我的帧缓冲都会保持纵向模式。 例如,我希望960x640分辨率而不是默认的320x480分辨率。

我一直在研究它,我看到有些人这样做但是使用OpenGL明确地旋转场景,这意味着实际的轴不是横向模式,对我来说这是一个丑陋的解决方法。

确保您的OpenGL UIView附加到UIViewController,而不是直接插入UIWindow。 为此,请创建自定义视图控制器:

@interface MyViewController : UIViewController
@end

@implementation MyViewController
- (void)loadView {
    // Create your EAGL view
    MyEAGLView *eaglView = [[MyEAGLView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    self.view = eaglView;
    [eaglView release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
@end

然后通过AppDelegate将其添加到您应用的窗口:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    MyViewController *viewController = [[MyViewController alloc] init];
    [self.window setRootViewController:viewController];
    [viewController release];
    [self.window makeKeyAndVisible];
}    

self.window只是应用程序的UIWindow,如果您将窗口存储在其他位置,则可能需要更改它。

之后,UIViewController在设备转动时负责旋转和调整视图大小,您可以覆盖MyEAGLView对象中的layoutSubviews方法来处理调整OpenGL上下文的大小。 您可能还想查看Apple的OpenGL应用程序示例代码,因为它正确处理了这些内容。

您可以在顶点着色器中旋转OpenGL输出,如下所示:

#version 300 es


in vec4 position;
in mediump vec4 texturecoordinate;

in vec4 color;

uniform float preferredRotation;

out mediump vec2 coordinate;

void main()
{
    //const float pi = 4.0 * atan(1.0);
    //float radians  = (( -90.0 ) / 180.0 * pi );

    // Preferred rotation of video acquired, for example, by:
    // AVAssetTrack *videoTrack = [tracks objectAtIndex:0];
    // CGAffineTransform preferredTransform = [videoTrack preferredTransform];
    // self.glKitView.preferredRotation = -1 * atan2(preferredTransform.b, preferredTransform.a);

    // Preferred rotation for both portrait and landscape           
    mat4 rotationMatrix = mat4( cos(preferredRotation), -sin(preferredRotation), 0.0, 0.0,
                                sin(preferredRotation),  cos(preferredRotation), 0.0, 0.0,
                                0.0,                     0.0,                    1.0, 0.0,
                                0.0,                     0.0,                    0.0, 1.0);

    // Mirror vertical (portrait only)
    mat4 rotationMatrix = mat4( cos(preferredRotation),  sin(preferredRotation), 0.0, 0.0,
                               -sin(preferredRotation),  cos(preferredRotation), 0.0, 0.0,
                                0.0,           0.0,          1.0, 0.0,
                                0.0,           0.0,          0.0, 1.0);

    // Mirror horizontal (landscape only)
    mat4 rotationMatrix = mat4( 1.0, 0.0,                     0.0,                    0.0,
                                0.0, cos(preferredRotation), -sin(preferredRotation), 0.0,
                                0.0, sin(preferredRotation),  cos(preferredRotation), 0.0,
                                0.0, 0.0,                     0.0,                    1.0);

    // Mirror vertical (landscape only)
    mat4 rotationMatrix = mat4( cos(preferredRotation), 0.0, sin(preferredRotation), 0.0,
                                0.0,                    1.0, 0.0,                    0.0,
                               -sin(preferredRotation), 0.0, cos(preferredRotation), 0.0,
                                0.0,                    0.0, 0.0,                    1.0);

    gl_Position = position * rotationMatrix;
    coordinate = texturecoordinate.xy;
}

显然,您只选择一个矩阵4,具体取决于视频的方向,然后是旋转。 每个矩阵4翻转视频窗口 - 不旋转它。 对于旋转,您必须首先根据方向选择matrix4,然后将preferredRotation变量替换为度数(以弧度表示,还提供其公式)。

有很多方法可以旋转视图,图层,对象等; 但是,如果您通过OpenGL渲染图像,那么您应该选择此方法,并且只选择此方法。

暂无
暂无

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

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