Search code examples
iosopengl-es-2.0glkit

Correct place to set up shaders for GLKView?


I'm having trouble finding the correct place to do my shader setup for an OpenGLES application using GLKView and GLKViewController.

It seems like viewDidLoad is a natural place to do this, but shader creation fails when I try to do this here. My setup is something like this:

//shader helper method
int setupShaders(const char* vShader, const char* fShader); //returns a program handle

//inside GLKViewController subclass

static int program;

-(void)viewDidLoad{
    [super viewDidLoad];
    program = setupShaders(vsh, fsh); //program will be zero indicating setup failure
}

I know the setup code works because it succeeds if I call it inside -(void)glkView:(GLKView *)view drawInRect:(CGRect)rect.

So I'm assuming OpenGL isn't fully initialized when -(void)viewDidLoad is called, or something has to be done to set the correct OpenGL context for the setup I'm trying to do, I just can't find any documentation on where or how to do setup correctly.


Solution

  • So it turns out it works perfectly if you initialize from inside -(void)viewDidAppear. Jacob's solution works fine as well, but it seems slightly cleaner to me to use a callback rather than adding a conditional to the draw method.