2012-03-18 57 views
7

我想在UIViewController中使用GLKView,我的代码看起来像这样嵌套GLKView到的UIViewController

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?有什么建议么?

回答

9

在你的viewDidLoad中初始化OpenGL,就像你现在正在做的一样。

看看注册你的视图控制器为GLKView's delegate。每当需要重绘时,代理的glkView:(GLKView *)view drawInRect:方法都会被调用。

This tutorial may help.

+0

我要在教程一看,也是我需要检查的代表是如何工作的 – JohnnyAce 2012-03-18 04:51:22

+0

你也可能想看看使用[GLKViewController](http://developer.apple.com/library /ios/#DOCUMENTATION/GLkit/Reference/GLKViewController_ClassRef/Reference/Reference.html)而不是UIViewController。它实现了一个渲染循环,所以你可以定期更新你的GLKView几次。 – 2012-03-19 03:02:05