我正在尝试将 GLKView 添加到滚动视图但没有成功。
目前,我的绘图代码只是将背景涂成红色,但在它完成之前,它会抛出错误“无法制作完整的帧缓冲区对象 8cd6”错误。通过将 OpenGL 绘制到标准 UIView 中,我设法使完全相同的东西正常工作,但是一旦将其切换为 GLKView,它总是会引发错误。
这是我的滚动视图在 AppDelegate.m 中的初始化方式:
ScrollView *scroll = [[ScrollView alloc] init];
[[self window] setRootViewController:scroll];
然后在我的 ScrollView.m 本身中有以下内容:
CGRect screenRect;
float screenWidth = 1920;
float screenHeight = 1280;
UIScrollView *scrollView;
GLKView *glkView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
-(void)loadView
{
// initialise the view's size parameters
screenRect.size.width = screenWidth;
screenRect.size.height = screenHeight;
// init the scrollview with the new screen rect
scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
// set the delegate as itself
[scrollView setDelegate:self];
// make the scrollview our main view
[self setView:scrollView];
// now create the GLKView and context
EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
glkView = [[GLKView alloc] initWithFrame:screenRect];
glkView.context = context;
glkView.delegate = self;
// add the GLKView to the scrollView
[scrollView addSubview:glkView];
/* old code, which works fine */
// OpenGLView openGLView = [[OpenGLView alloc] initWithFrame: screenRect];
// openGLView.delegate = self;
// openGLView.opaque = NO;
// [scrollView addSubview:openGLView];
/* end of old code */
// and finally tell the scrollview the size of it's content
[scrollView setContentSize:screenRect.size];
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
// ** THROWS THE ERROR BEFORE HERE ** //
// set the background colour to red
glClearColor(1.0, 0.0, 0.0, 1.0);
// and now clear it
glClear(GL_COLOR_BUFFER_BIT);
}
为什么这段代码会导致这种情况发生?