问题
I´m trying to make simple game using OpenGLES. I have two EAGLViews(menu and game view). Each view has its own viewController. Initializing of the views is done by initWithNIBName method of the viewController. And when I want to show the view, I simply use addSubview method of the main window. The game view is initialized only once at the launch time. Menu view is initialized only if it´s needed. Problem is, that when I go from game view to menu and then back, and then I redraw the game view, something goes wrong. (I'm setting EAGLContext in drawView method before drawing, so the context may be right). Don´t you know where is the problem? Or if the whole switching is managed wrong, gimme and advice please. Thanks for replies.
回答1:
I guess you are having a trouble with texture not showing correctly?
I don't know the real thing behind OpenGL, but this is my hypothesis: Each time you came back to the EAGLView, The EAGLContext of EAGLView is changed. (if you have been copying and pasting from the OpenGLES template) The textures can only be loaded after the context is in its correct state, or else you cannot load any texture. Now, by leaving the EAGLView, and coming back, you are instantiating a new EAGLContext from initWithCoder:(NSCoder*)coder :
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!context || ![EAGLContext setCurrentContext:context]) {
[self release];
return nil;
}
So how can we preserve this context? I make it global. Simple as that. And when EAGLView is to be instantiated once again, have it check that whether the "global" EAGLContext is nil or not. If it's nil, just instantiate it, else do nothing. And never ever release or dealloc this global EAGLContext unless you want to quit your program.
This works for me, but again, my hypothesis above may not be correct. If anybody knows the real thing, please lecture me. I am also humbly in need of guidance. I want to truly know why this occurs and why do we have to do this as well.
And by the way, does this answer your question, Jenicek?
来源:https://stackoverflow.com/questions/1228067/opengles-view-switching-problem