Xcode iOS:如何配置从情节提要获取的GLKit视图?
我想创建按需重新绘制的OpenGL视图。我从“工具”页面拖动了GLKit视图,并将其放置在情节提要上。然后我将其加入名为viewOpenGl的类变量。在头文件中是:Xcode iOS:如何配置从情节提要获取的GLKit视图?,ios,xcode,opengl-es,Ios,Xcode,Opengl Es,我想创建按需重新绘制的OpenGL视图。我从“工具”页面拖动了GLKit视图,并将其放置在情节提要上。然后我将其加入名为viewOpenGl的类变量。在头文件中是: @property (retain, nonatomic) IBOutlet GLKView *viewOpenGl; 在viewDidLoad()中,我调用SetupOpenGl()函数,该函数配置此GLKit视图,并应将其重新绘制为蓝色: -(void)SetupOpenGl { self.viewOpenGl.context
@property (retain, nonatomic) IBOutlet GLKView *viewOpenGl;
在viewDidLoad()
中,我调用SetupOpenGl()
函数,该函数配置此GLKit视图,并应将其重新绘制为蓝色:
-(void)SetupOpenGl
{
self.viewOpenGl.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
[EAGLContext setCurrentContext:self.viewOpenGl.context];
self.viewOpenGl.enableSetNeedsDisplay = true;
glClearColor(0,0,1,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[self.viewOpenGl setNeedsDisplay];
}
在最后三行之后,我的GlKit视图的矩形应该用蓝色填充,但它仍然是黑色的
我的问题是:为什么这些OpenGl语句不能工作?为了使用OpenGL在GLKitView中绘制,还需要配置什么?为了实现着色,您需要子类化
GLKView
,并覆盖drawRect
方法
示例:
h
在故事板中,将GLKView
实例设置为CustomBGGLKView
类
然后将SetupOpenGl()
函数修改为以下内容:
-(void)setupOpenGl {
self.viewOpenGl.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
self.viewOpenGl.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
[EAGLContext setCurrentContext:self.viewOpenGl.context];
self.viewOpenGl.enableSetNeedsDisplay = true;
[self.viewOpenGl setNeedsDisplay];
}
为了实现着色,您需要子类化
GLKView
,并重写drawRect
方法
示例:
h
在故事板中,将GLKView
实例设置为CustomBGGLKView
类
然后将SetupOpenGl()
函数修改为以下内容:
-(void)setupOpenGl {
self.viewOpenGl.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
self.viewOpenGl.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
[EAGLContext setCurrentContext:self.viewOpenGl.context];
self.viewOpenGl.enableSetNeedsDisplay = true;
[self.viewOpenGl setNeedsDisplay];
}
如果这是关于iOS的,那么您很可能正在使用OpenGL ES。如果是这种情况,请适当地重新标记问题。如果这是关于iOS的,那么您很可能正在使用OpenGL ES。如果是这种情况,请适当地重新标记问题。
-(void)setupOpenGl {
self.viewOpenGl.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
self.viewOpenGl.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
[EAGLContext setCurrentContext:self.viewOpenGl.context];
self.viewOpenGl.enableSetNeedsDisplay = true;
[self.viewOpenGl setNeedsDisplay];
}