9

I am learning OpenGLES and I am trying to put a GLKViewer inside an UIViewController.

I know I can come around the main issues by using GLViewController, but I am trying to learn how to do it this way.

I found this question, Nesting GLKView into UIViewController and Nested GLKView and GLKViewController but I must be missing something even though I think I am doing all the right steps because when I run my project, I am not getting to the drawInRect print line.

In the storyboard I am pointing the ViewController as the delegate of the glkview component.

I tried to keep the code as simple as possible and any help will be apreciated:

MyController.h

#import <Foundation/Foundation.h>
#import <GLKit/GLKit.h>

@interface MyGLController : UIViewController <GLKViewDelegate>
{
    GLuint vertexBufferID;

}

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

@property (strong, nonatomic) GLKBaseEffect *baseEffect;

@end

MyGLController.m

#import "MyGLController.h"

@implementation MyGLController

//@synthesize baseEffect;

-(void) viewDidLoad{
    [super viewDidLoad];

    self.glview.context = [[EAGLContext alloc] initWithAPI:
     kEAGLRenderingAPIOpenGLES2];

     [EAGLContext setCurrentContext:self.glview.context];
    printf("View Loaded");
}


- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    printf("DrawInRect");
}

@end

* Update *

As far as I can tell the glkview is hooked up properly as suggested per and added the josh-knapp and called setNeedsDisplay.

In case there's something I am missing, I have uploaded a copy of the project here: https://github.com/jcrogel/OpenGLDebug.git

I am a total noob in this so I apologize for any silly oversight :)

Community
  • 1
  • 1
Juan Carlos Moreno
  • 2,754
  • 3
  • 22
  • 21
  • Ok I have noticed that I have gotten a 'close' vote and 2 down votes without further explanation of what is wrong in the question or how my question would have been better. Any feedback to regain my points would be appreciated. – Juan Carlos Moreno Nov 12 '12 at 16:46
  • 2
    I think downvoters should explain their reason instead of blindly marking this for close. The OP obviously made an effort here to provide both an explanation and code. A simple comment requesting missing info would have helped. – jdi Nov 12 '12 at 17:27
  • 1
    For me, I didn't initialize with a context and that's why it wasn't called. – Sam Soffes Feb 19 '14 at 01:34

3 Answers3

11

You didn't say you hooked up the glview property of MyGLController in the storyboard, so verify that.

Next, you're setting up the glview context after it loads. Without a GLKViewController, there is nothing telling the glview it needs to be drawn. Make sure you call [self.glview setNeedsDisplay] somewhere after the context is set up.

Josh Knapp
  • 154
  • 2
  • 5
7

I had a similar problem.

By default, xcode sets the enable setNeedsDisplay checkbox to NO in GLKView property list.
Once I changed it, it worked.

AlexB
  • 7,302
  • 12
  • 56
  • 74
ashabtay
  • 86
  • 1
  • 3
1

I see this question was already answered. But I had the same problem, with a different solution. I figured I'd post the info here on the off chance others might be able to benefit from it.

Problem Description

While using GLKView and GLKViewController, the render loop function (drawInRect) is called once, but not called again.

Possible Cause

Some methods to CViewController have been implemented incorrectly, without calling their supers. The following code illustrates three such functions. The examples below have the three functions implemented correctly, calling their supers.

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:NO];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:NO];
}
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:NO];
}
MikeyE
  • 1,756
  • 1
  • 18
  • 37