我正在两个iPhone视图控制器之间切换:一个初始viewController运行一个3D动画的OpenGL视图,另一个viewController具有UIKit程序设置视图。当我启动应用程序时,OpenGL动画运行正常,我可以切换到第二个VC来改变程序设置。当我通过委托从设置VC切换回来时,我无法重新启动OpenGL动画 - 我只是获得黑色视图。
当我切换回GL视图时,还有什么需要重新初始化OpenGL-ES状态吗?
我从GL viewController切换到设置viewController工作正常:
- (IBAction) switchToSettingsView:(id) sender {
SettingsViewController *controller = [[SettingsViewController alloc] initWithNibName:@"SettingsView" bundle:nil];
controller.settingsDelegate = self;
[self stopTimer]; // Stop the animation
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
当我从设置视图通过以下委托返回时,我只看到黑色背景:
- (void) settingsViewControllerDidFinish:(SettingsViewController *)controller {
[self dismissModalViewControllerAnimated:YES];
[self startTimer];
[glView startAnimation];
[glView drawView]; // Explicitly re-drawing the GL view doesn't help
}
添加viewDidAppear / viewWillDisappear没有帮助。它们会按预期被调用。
- (void)viewDidAppear:(BOOL)animated
{
[glView startAnimation];
}
- (void)viewWillDisappear:(BOOL)animated
{
[glView stopAnimation];
}
我目前没有在startAnimation中使用CADisplayLink,我正在使用NSTimer。
我通过委托返回后,我的OpenGL drawView在GL视图中运行,但显示屏上没有任何内容。
更新:已解决!!! 正如quixoto建议的那样,重新创建EAGL背景有帮助。我还必须重新创建帧缓冲区。做任何一个没有另一个没有解决问题。这是我用来重新初始化图形状态的代码:
// Re-initialize the EAGL context and framebuffers
- (void)reInitContext {
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = YES;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking,
kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
[context release]; // Releases the old EAGL Context
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!context) {
NSLog(@"RECREATING EAGL CONTEXT FAILED!");
} else {
NSLog(@"RECREATING EAGL CONTEXT SUCCEEDED!");
};
if (![EAGLContext setCurrentContext:context]) {
NSLog(@"RE-SETTING EAGL CONTEXT FAILED!");
} else {
NSLog(@"RE-SETTING EAGL CONTEXT SUCCEEDED!");
};
[self destroyFramebuffer];
[self createFramebuffer];
}