OpenGL技术过时了,只是了解,新的技术是metal。
搭建环境
Link Binary 加入:OpenGLES.framework
OpenGL的代码被标记为废弃,需要在 Target 的 Build Settings 一次性加上GLES_SILENCE_DEPRECATION,可以临时取消这些经过。
GLView实现纯色视图
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>
@interface GLView : UIView
@property (nonatomic, strong) EAGLContext *eaglContext;
@property (nonatomic, assign) GLuint framebuffer;
@property (nonatomic, assign) GLuint renderbuffer;
@property (nonatomic, strong) CADisplayLink *displayLink;
@end
#import "GLView.h"
double color1 = 0.05f;
@implementation GLView
//UIView 在初始化的时候,内部会调用 `[[self class] layerClass]`,拿到 layer 类型,自动实例化这个 layer。
// 如果不重写,拿到的就是CALayer
+ (Class)layerClass {
return [CAEAGLLayer class];
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// ✅ CAEAGLLayer属性:只初始化一次,放init
CAEAGLLayer *eaglLayer = (CAEAGLLayer*)self.layer;
// opaque 不透明度
// YES 表示这个图层完全不透明,直接渲染。
// NO 告诉UIKit,图层可能有透明像素,需要把下层的图层先渲染出来。性能损失较大
eaglLayer.opaque = YES;
eaglLayer.drawableProperties = @{
// 每次渲染完成,是否回收drawable内容,每帧都重新绘制用NO
// YES 保留上一帧
kEAGLDrawablePropertyRetainedBacking:@NO,
// 像素格式,RGBA,每个像素4个字节
kEAGLDrawablePropertyColorFormat: kEAGLColorFormatRGBA8
};
// 1. 创建EAGLContext ES2.0
self.eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
NSAssert(self.eaglContext != nil, @"EAGLContext 创建失败");
// 设置当前上下文,用来创建FB/RB
[EAGLContext setCurrentContext:self.eaglContext];
// 2. 创建Framebuffer 和 Renderbuffer
// Framebuffer是容器
// Renderbuffer才是保存像素点的地方。
// 2.1向 GLES 上下文申请一个未被占用的数字 ID,写入_framebuffer
// glGenFramebuffers 第一个参数生成framebuffer的对象的数量
// GLuint bufferId[3]
// glGenFramebuffers(1, bufferId);
glGenFramebuffers(1, &_framebuffer);
// 2.2
// 如果这个 ID**还没有对应的 GPU 对象**:第一次 bind 的时候,GPU 自动新建 Framebuffer 容器。
// 如果 ID 已经存在 GPU 对象:切换当前状态机,把这个 Framebuffer 绑定为当前渲染目标。
glBindFramebuffer(GL_FRAMEBUFFER, _framebuffer);
glGenRenderbuffers(1, &_renderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _renderbuffer);
// 分配renderbuffer存储空间,关联layer
[self.eaglContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:eaglLayer];
// 将renderbuffer附着到FBO的颜色附着点
// 给当前的绑定的Framebuffer,在指定的附着点(这里是颜色)挂载一个Renderbuffer
// glFramebufferRenderbuffer (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer)
// 参数1,目标类型,这里固定是GL_FRAMEBUFFER,操作普通的FBO
// 参数2,FBO的那个挂载点,有颜色,深度,模板3个插槽。这里是颜色 渲染的像素就写到这个插槽挂载的缓冲
// 参数3 挂载的对象类型,这里是Renderbuffer
// 参数4 Renderbuffer的ID。FBO引用的就是这个RBO
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _renderbuffer);
// 检查FBO完整性(调试很重要)
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
NSAssert(status == GL_FRAMEBUFFER_COMPLETE, @"FBO不完整");
// 3. CADisplayLink 驱动渲染,每帧调用render
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(render)];
self.displayLink.preferredFramesPerSecond = 60;
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
return self;
}
- (void)render{
// 切换上下文
if (![EAGLContext setCurrentContext:self.eaglContext]) {
NSLog(@"context set fail");
return;
}
// 绑定FBO
glBindFramebuffer(GL_FRAMEBUFFER, self.framebuffer);
glBindRenderbuffer(GL_RENDERBUFFER, self.renderbuffer);
// -------- 清屏逻辑 --------
// double color1 = 0.05f
// glClearColor(0.05f, 0.08f, 0.12f, 1.0f);
color1 += 0.01;
if (color1 > 1) {
color1 = 0;
}
// 设置清屏的默认颜色
glClearColor(color1, 0.08f, 0.12f, 1.0f);
// 开始清屏 可以 GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT 这三个缓冲区一起
// GL_COLOR_BUFFER_BIT 颜色缓冲区
// GL_DEPTH_BUFFER_BIT 深度缓冲区
// GL_STENCIL_BUFFER_BIT 模板缓冲区
glClear(GL_COLOR_BUFFER_BIT);
// --------------------------
// 把renderbuffer提交到屏幕,这里阻塞。真实项目不要这么弄。
[self.eaglContext presentRenderbuffer:GL_RENDERBUFFER];
}
- (void)stop{
// CADisplayLink必须手动停止,否则会发生循环引用
[self.displayLink invalidate];
}
- (void)dealloc {
NSLog(@"111");
// 停止刷新
[EAGLContext setCurrentContext:self.eaglContext];
// 删除buffer资源,防止显存泄漏
if (_framebuffer) {
glDeleteFramebuffers(1, &_framebuffer);
}
if (_renderbuffer) {
glDeleteRenderbuffers(1, &_renderbuffer);
}
}
@end