numberWithFloat:.5] forKey:@"inputBrightness"];//亮度-1~1 CIImage *outputImage = [filter outputImage]; //GPU优化 EAGLContext * eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]; eaglContext.multiThreaded = YES; CIContext *context = [CIContext contextWithEAGLContext:eaglContext]; [EAGLContext setCurrentContext :eaglContext]; CGImageRef ref = [context createCGImage:outputImage fromRect:outputImage.extent]; UIImage
EAGLContext类还提供了用于将OpenGL ES内容与Core Animation集成的方法。 获取线程的当前上下文可以用这个: [EAGLContext currentContext]; OpenGL ES持有与当前上下文对应的EAGLContext对象的强引用。 (如果使用手动引用计数,OpenGL ES会释放EAGLContext对象。)为防止EAGLContext对象在不是当前上下文时被释放,您的应用程序必须对这些对象进行强引用(或保留)。 下面的代码表示如何执行此操作 EAGLContext* CreateBestEAGLContext() { EAGLContext *context = [[EAGLContext alloc] EAGLContext* firstContext = CreateBestEAGLContext(); EAGLContext* secondContext = [[EAGLContext alloc
创建 OpenGL 上下文 在iOS中使用OpenGL ES做视频渲染时,首先要创建EAGLContext对象。这是因为,EAGLContext管理着 OpengGL ES 渲染上下文。 为了执行OpenGL ES命令,你需要将创建的EAGLContext设置为当前渲染上下文。 EAGLContext并不直接管理绘制资源,它通过与上下文相关的EAGLSharegroup对象来管理。 当创建EAGLContext时,你可以选择创建一个新的sharegroup或与之前创建的EAGLContext共享EAGLSharegroup。 if ([EAGLContext currentContext] ! //创建GLKView,在创建时,就将 EAGLContext 设置好。
展示图 *学习目标 绘制一个运动的立方体 ---- * 实现思路 * 实现步骤: * 第一步 .创建一个继承 GLKViewController(为我们封装了好多代码)的对象 * 第二步 .创建一个EAGLContext 第四步 .创建立方体的顶点坐标和法线 * 第五步 .绘图 * 第六步 .让立方体运动起来 * 第七步 .在视图消失的时候,做一些清理工作 ---- 代码详解 // MARK: - 第一步: 创建一个EAGLContext -(void)createEAGContext{ self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2] self.context) { NSLog(@"手机不支持opengl es2"); } [EAGLContext setCurrentContext:self.context]; // 设置为当前上下文 currentContext] == self.context) { [EAGLContext setCurrentContext:nil]; } } 代码地址: https://github.com
import #import #include #include @interface OpenGLView : UIView{ CAEAGLLayer* _eaglLayer; EAGLContext · EAGLContext: EAGLContext是OpenGL ES 的渲染上下文,管理所有使用OpenGL ES 进行描绘的状态、命令以及资源信息。 · 设置上下文EAGLContext: - (void)setupContext { // 指定OpenGL渲染API的版本,在这里我们使用OpenGL ES 2.0 EAGLRenderingAPI api = kEAGLRenderingAPIOpenGLES2; _context = [[EAGLContext alloc] initWithAPI:api]; if (! [EAGLContext setCurrentContext:_context]) { NSLog(@"Failed to set current OpenGL context");
setupGL{ // 创建设备上下文,用OpenGL ES 2.0的API GLKView *view = (GLKView *)self.view; view.context = [[EAGLContext kEAGLRenderingAPIOpenGLES2]; // GLKView的深度缓存 view.drawableDepthFormat = GLKViewDrawableDepthFormat24; [EAGLContext GL_TRIANGLES, 0, 3); } // 删除不需要的顶点缓存和上下文 -(void)dealloc { GLKView *view = (GLKView *)self.view; [EAGLContext vertexBufferID){ glDeleteBuffers (1,&vertexBufferID); vertexBufferID = 0; } [EAGLContext
ES3/gl.h> #import <OpenGLES/ES3/glext.h> //2.将控制器(UIViewController)改为GLKViewController //3.创建私有属性 EAGLContext GLKBaseEffect *bEffect; 以下配置上下文、配置坐标、加载纹理数据都是在-viewDidLoad中调用 配置上下文 //创建EAGL上下文,并设置使用GLES3标准 content = [[EAGLContext return; } //创建GLKView GLKView *view = (GLKView *)self.view; view.context = content; //将content设置为当前EAGLContext [EAGLContext setCurrentContext:content]; //设置GLKView的缓存区参数 view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888
kEAGLDrawablePropertyColorFormat: kEAGLColorFormatRGBA8, }; self.myContent = [[EAGLContext [EAGLContext setCurrentContext:self.myContent]){ NSLog(@"Set Current Context failed"); 进行图片解压缩 相比顶点绘制方式而言,索引绘图只有在最后的绘制API的选择上不同:glDrawElements 放出以上代码的部分详细注释:传送门 GLKit实现 相比GLSL来说代码量会小很多 1.EAGLContext EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]; [EAGLContext setCurrentContext:context]; self = [super initWithFrame:frame context:context];
sharegroup:resource_context_.get().sharegroup]); } else { resource_context_.reset([[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]); context_.reset([[EAGLContext alloc] initWithAPI IOSRenderTargetGL 主要是用来持有 CAEAGLLayer 对象,和从 IOSContextGL 获得的用于光栅化的 EAGLContext 对象,并为 CAEAGLLayer 分配 Framebuffer (fml::scoped_nsobject<CAEAGLLayer> layer, fml::scoped_nsobject<EAGLContext 在构造函数里面会创建光栅化用的 Skia GrContext 对象并持有,该 GrContext 对象对应当前线程的 GL 上下文对象,也就是 IOSRenderTargetGL 持有的光栅化用的 EAGLContext
content = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]; if(! content){return;} [EAGLContext setCurrentContext:content]; //GLKView创建 glkView = [[GLKView dealloc { //displayLink 失效 [disLink invalidate]; disLink = nil; //重置content if ([EAGLContext currentContext] == [(GLKView *)self.view context] ) { [EAGLContext setCurrentContext:nil];
setupGL{ // 创建设备上下文,用OpenGL ES 2.0的API GLKView *view = (GLKView *)self.view; view.context = [[EAGLContext kEAGLRenderingAPIOpenGLES2]; // GLKView的深度缓存 view.drawableDepthFormat = GLKViewDrawableDepthFormat24; [EAGLContext self.baseEffect prepareToDraw]; glDrawArrays(GL_TRIANGLES, 0, 6); } // 清除缓存 - (void)dealloc { [EAGLContext = 0){ glDeleteBuffers (1,&_VBO); _VBO = 0; } [EAGLContext setCurrentContext:nil
alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; // Make the new context current [EAGLContext viewDidUnload]; // Make the view's context current GLKView *view = (GLKView *)self.view; [EAGLContext // Stop using the context created in -viewDidLoad ((GLKView *)self.view).context = nil; [EAGLContext Context 1、view.context 的定义: GLKit/GLKView.h -->@property (nonatomic, retain) EAGLContext *context; 2、 2.0 所以要选择 kEAGLRenderingAPIOpenGLES2 (注意这个不能选错); 4、setCurrentContext 的定义: + (BOOL) setCurrentContext:(EAGLContext
下面就是一个使用光照的简单例子: -(void)setupGL{ // 设置设备上下文 GLKView *view = (GLKView *)self.view; view.context = [[EAGLContext initWithAPI:kEAGLRenderingAPIOpenGLES2]; view.drawableDepthFormat = GLKViewDrawableDepthFormat24; [EAGLContext glDeleteBuffers(1,&_normalID); _normalID = 0; } } // 删除缓存 - (void)dealloc { [EAGLContext = 0){ glDeleteBuffers (1,&_normalID); _normalID = 0; } [EAGLContext setCurrentContext
当滤镜渲染不复杂,耗时不多时,这种方案是可以搞的,因为滤镜主要涉及到 OpenGL 相关的操作,只要保证滤镜的 OpenGL 操作与渲染的 OpenGL 操作在同一个 EAGLContext 环境内即可 可以参考下图 OpenGL 操作缓冲区 FBO、着色器与 EAGLContext 的关系。
添加如下变量(变量后面会有介绍) @interface MyGLView () { CAEAGLLayer *_eaglLayer; //OpenGL内容只会在此类layer上描绘 EAGLContext EAGLRenderingAPI api = kEAGLRenderingAPIOpenGLES3; _context = [[EAGLContext alloc] initWithAPI:api context) { NSLog(@"Failed to initialize OpenGLES 3.0 context"); } // 设置为当前上下文 [EAGLContext
iOS ShareContext 实现: - (EAGLContext *)shareContext:(nullable EAGLContext *)context { EAGLContext *shareContext = [[EAGLContext alloc] initWithAPI:context.API sharegroup:context.sharegroup]; return
myEagLayer.backgroundColor = [UIColor clearColor].CGColor; } return \_myEagLayer; } * Context 还需要参数传递的上下文EAGLContext 其中EAGLContext初始化: - (EAGLContext \*)myContext { if (\_myContext == nil) { EAGLRenderingAPI api = kEAGLRenderingAPIOpenGLES3; \_myContext = [[EAGLContext alloc] initWithAPI:api]; [EAGLContext setCurrentContext:\_myContext]) { NSLog(@"Failed to set current OpenGL context
myEagLayer.backgroundColor = [UIColor clearColor].CGColor; } return _myEagLayer; } Context 还需要参数传递的上下文EAGLContext 其中EAGLContext初始化: - (EAGLContext *)myContext { if (_myContext == nil) { EAGLRenderingAPI api = kEAGLRenderingAPIOpenGLES3; _myContext = [[EAGLContext alloc] initWithAPI:api]; [EAGLContext setCurrentContext:_myContext]) { NSLog(@"Failed to set current OpenGL context
EAGLContext *context = [[EAGLContext alloc] initWithAPI:api]; if (! ; return; } BOOL r = [EAGLContext setCurrentContext:context]; if (! ; return; } _eaglContext = context; // 3、申请并绑定渲染缓冲区对象 RBO 用来存储即将绘制到屏幕上的图像数据 [_eaglContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer]; // 将渲染图层(_eaglLayer)的存储绑定到 [_eaglContext presentRenderbuffer:GL_RENDERBUFFER]; // 9、清理。
9 var context:CIContext = { 10 return CIContext(eaglContext:EAGLContext(api: EAGLRenderingAPI.openGLES2