Halsafar
Halsafar

Reputation: 2640

Nested GLKViewController

I am trying to nest a GLKView inside a UIView XIB. Basically following the steps here: Nested GLKView and GLKViewController

My CustomOpenGLController.xib is just a GLKView.

My MainViewController.xib has a GLKView subview.

In my MainViewController.xib I have a GLKView with an outlet:

 @property (weak, nonatomic) IBOutlet GLKView *theSubView;

Then in MainViewController.m I do the following:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"viewDidLoad");
    subviewController = [[CustomOpenGLController alloc] initWithNibName:@"CustomOpenGLController" bundle:nil];
    subviewController.view.frame = _theSubView.frame;
    [subviewController setView:self.theSubView];
    [self.theSubView setNeedsDisplay];
    [subviewController didMoveToParentViewController:self];
}

This causes viewDidLoad of CustomOpenGLController to be called which does the following:

CustomOpenGLController.h

@interface CustomOpenGLController : GLKViewController <GLKViewControllerDelegate, GLKViewDelegate>
{
@private
    GLKBaseEffect *effect;
}

#pragma mark GLKViewControllerDelegate
- (void)glkViewControllerUpdate:(GLKViewController *)controller;

#pragma mark GLKViewDelegate
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect;

@end

CustomOpenGLController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"viewDidLoad");

    EAGLContext *aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    GLKView *glkView = (GLKView *)self.view;
    glkView.delegate = self;
    glkView.context = aContext;

    glkView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
    glkView.drawableDepthFormat = GLKViewDrawableDepthFormat16;
    glkView.drawableMultisample = GLKViewDrawableMultisample4X;

    self.delegate = self;
    self.preferredFramesPerSecond = 30;

    effect = [[GLKBaseEffect alloc] init];

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}

The problem is the delegates glkViewControllerUpdate and glkView drawInRect is not called.

If I add CustomOpenGLController as a subview (instead of the setView call) then glkView drawInRect gets called once. If use CustomOpenGLController as a view all on its own then it all works perfectly.

However I need this CustomOpenGLController to be embedded within normal view controllers.

UPDATE

If I modify the viewDidLoad for MainViewController.m to the following then it works but it only render 1 frame then stops calling glkView drawInRect:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"viewDidLoad ControllerView");
    subviewController = [[ControllerOpenGLViewController alloc] initWithNibName:@"ControllerOpenGLViewController" bundle:nil];
    subviewController.view.frame = _theSubView.frame;

    subviewController.view.opaque = NO;
    subviewController.view.backgroundColor = [UIColor clearColor];

    [self.view addSubview:subviewController.view];
    [subviewController.view setNeedsDisplay];

    NSLog(@"Done viewDidLoad ControllerView");
}

This oddly renders 1 frame then stops. Also glkViewControllerUpdate is never called.

Upvotes: 0

Views: 848

Answers (1)

Vinzzz
Vinzzz

Reputation: 11724

You're setting CustomOpenGLController as a child ViewController, yet you never call addChildViewController in your MainViewController viewDidLoad. Your problem might come from this

Upvotes: 1

Related Questions