首先是初始化openGL
设置layer
- (void)setupLayer
{
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
// CALayer 默认是透明的,必须将它设为不透明才能让其可见
eaglLayer.opaque = YES;
// 设置描绘属性,在这里设置不维持渲染内容以及颜色格式为 RGBA8
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
//设置放大倍数
[self setContentScaleFactor:[[UIScreen mainScreen] scale]];
}
设置上下文
- (void)setupContext{
// 指定 OpenGL 渲染 API 的版本,在这里我们使用 OpenGL ES 2.0
_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
// 设置为当前上下文
if (!_context || ![EAGLContext setCurrentContext:_context]) {
NSLog(@"Failed to initialize OpenGLES 2.0 context");
exit(1);
}
}
设置缓冲区
- (void)setupBuffer{
glDeleteFramebuffers(1, &_viewFramebuffer);
_viewRenderbuffer = 0;
glDeleteRenderbuffers(1, &_viewRenderbuffer);
_viewRenderbuffer = 0;
glGenFramebuffers(1, &_viewFramebuffer);
glGenRenderbuffers(1, &_viewRenderbuffer);
// 设置为当前 framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, _viewFramebuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _viewRenderbuffer);
// 为 颜色缓冲区 分配存储空间
[_co