Max
Max

Reputation: 2749

GLKViewController and GLKView - Rendering nothing the second time created

I have a MainMenuViewController and a GameViewController which is a GLKViewConrtroller.

The first time I go from the main menu to the GameViewController everything is rendered fine. If I go back to the main menu, the GameViewController and its view get dealloced (I logged it).

When now going back to the game, I see a blank screen, nothing gets rendered OpenGL-wise. The overlay test menu with UIKit is still there.

Thisis how I tear down OpenGL in the GameViewController's dealloc method, the last five lines were added as tries to make it work, so it doesn't work with or without them.

- (void)tearDownGL {

[EAGLContext setCurrentContext:self.context];

glDeleteBuffers(1, &_vertexBuffer);
glDeleteVertexArraysOES(1, &_vertexArray);

self.effect = nil;

_program = nil;

glBindVertexArrayOES(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);

[EAGLContext setCurrentContext: nil];
}

Upvotes: 1

Views: 1002

Answers (1)

Tom Andersen
Tom Andersen

Reputation: 7200

I think that the problem is that you are not using a sharegroup - a place where OpenGL can share textures and shaders between contexts?

Here is code that will create a sharegroup that all your GLKViewController 's subclass. If you have multiple subclasses you will have to do something to make the shareGroup global, if that's appropriate.

- (void)viewDidLoad
{
     [super viewDidLoad];

     // Create an OpenGL ES context and assign it to the view loaded from storyboard
     GLKView *view = (GLKView *)self.view;

     // GLES 3 is not supported on iPhone 4s, 5. It may 'just work' to try 3, but stick with 2, as we don't use the new features, me thinks.
     //view.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
    //if (view.context == nil)
    static  EAGLSharegroup* shareGroup = nil;
    view.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup:shareGroup];
    if (shareGroup == nil)
        shareGroup = view.context.sharegroup;
    ...

Upvotes: 1

Related Questions