2012-03-18 20 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

相關問題