将GLKView嵌入UIViewController中

7
我正在尝试在UIViewController中使用GLKView,我的代码如下:

CustomViewController.h

#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>

@interface CustomViewController : UIViewController
{
    NSString * name;
}

@property NSString * name;

CustomViewController.m

#import "CustomViewController.h"

@interface CustomViewController ()

@end

@implementation CustomViewController
@synthesize name;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    CGRect mainScreen = [[UIScreen mainScreen] bounds];
    GLKView * viewGL = [[GLKView alloc] initWithFrame:CGRectMake(mainScreen.size.width / 2, mainScreen.size.height / 2, 50, 50)];
    viewGL.context = context;
    [self.view addSubview:viewGL];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

我该如何定义GLKView的绘制/渲染方法?OpenGL应该在哪里初始化?有什么建议吗?
1个回答

9
viewDidLoad中初始化OpenGL,就像您目前正在做的那样。
查看将您的视图控制器注册为GLKView的委托。每当需要重新绘制时,委托的glkView:(GLKView *)view drawInRect:方法将被调用。 本教程可能有所帮助。

我会查看教程,同时需要检查委托如何工作。 - JohnnyAce
您可能还想考虑使用GLKViewController而不是UIViewController。它实现了一个渲染循环,因此您可以每秒钟多次更新GLKView。 - Scott Hyndman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接