2012-03-18 11 views
7

Estoy intentando utilizar GLKView en UIViewController, mi código es el siguienteanidamiento GLKView en 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 

¿Cómo se puede definir el método de dibujar/renderizar de GLKView? y ¿dónde puedo iniciar OpenGL? ¿Alguna sugerencia?

Respuesta

9

Inicializa OpenGL en tu viewDidLoad, tal como lo estás haciendo actualmente.

Observe registrar su controlador de vista como GLKView's delegate. El método glkView:(GLKView *)view drawInRect: del delegado se invocará cada vez que se requiera un redibujado.

This tutorial may help.

+0

voy a echar un vistazo en el tutorial, también tengo que comprobar cómo funcionan los delegados – JohnnyAce

+0

También puede que desee ver en el uso de [GLKViewController] (http://developer.apple.com/library /ios/#DOCUMENTATION/GLkit/Reference/GLKViewController_ClassRef/Reference/Reference.html) en lugar de UIViewController. Implementa un ciclo de representación, por lo que puede actualizar periódicamente su GLKView varias veces por segundo. –

Cuestiones relacionadas