我正在尝试在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?有什么建议?

最佳答案

就像您当前正在做的那样,在viewDidLoad中初始化OpenGL。

看一下将 View Controller 注册为GLKView's delegate。每当需要重绘时,都会调用委托(delegate)的glkView:(GLKView *)view drawInRect:方法。

This tutorial may help.

关于ios - 将GLKView嵌套到UIViewController中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9755288/

10-10 23:13