OpenGL ES之GLKit框架学习

一:GLKit框架概述

1:GLKit 框架的设计⽬标是为了简化基于OpenGL / OpenGL ES 的应用开发。它的出现加快OpenGL ES或OpenGL应用程序开发。 使用数学库,背景纹理加载,预先创建的着⾊器效果,以及标准视图和视图控制器来实现渲染循环。
2:GLKit框架提供了功能和类,可以减少创建新的基于着⾊器的应用程序所需的⼯作量,或者⽀持依赖早期版本的OpenGL ES或OpenGL提供的固定函数顶点或片段处理的现有应⽤程序。

GLKit框架常用类

一:GLKView

提供绘制场所(View) GLKView继承UIView

** GLKView常用属性**
1:帧缓存区属性

//绘制视图内容时使用的OpenGL ES 上下文
@property (nonatomic, retain) EAGLContext *context;
//底层缓存区对象的⾼高度(以像素为单位)
@property (nonatomic, readonly) NSInteger drawableWidth;
//底层缓存区对象的宽度(以像素为单位)
@property (nonatomic, readonly) NSInteger drawableHeight;

2:配置帧缓存区对象

//颜⾊色渲染缓存区格式
@property (nonatomic) GLKViewDrawableColorFormat drawableColorFormat;
//深度渲染缓存区格式
@property (nonatomic) GLKViewDrawableDepthFormat drawableDepthFormat;
//模板渲染缓存区的格式
@property (nonatomic) GLKViewDrawableStencilFormat drawableStencilFormat;
//多重采样缓存区的格式
@property (nonatomic) GLKViewDrawableMultisample drawableMultisample;

3:使⽤OpenGL ES 绘制内容的视图默认实现

//将底层FrameBuffer 对象绑定到OpenGL ES
- (void)bindDrawable;
//删除与视图关联的可绘制对象(删除视图FrameBuffer对象)
- (void)deleteDrawable;
//布尔值,指定视图是否响应使得视图内容无效的消息
@property (nonatomic) BOOL enableSetNeedsDisplay;
//绘制视图内容并将其作为新图像对象返回
@property (readonly, strong) UIImage *snapshot;
//立即重绘视图内容
- (void)display;

4:GLKViewDelegate 用于GLKView 对象回调⽅法⽤于GLKView 对象回调方法

@required
//绘制视图内容
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect;
二:GLKViewController 管理OpenGL ES 渲染循环的视图控制器

1:(扩展于标准的UIKit 设计模式. ⽤于绘制视图内容的管理与呈现.)
2:继承UIViewController,并且遵守了GLKViewDelegate,实现GLKView的代理方法

1:更新方式:

- (void) update 更新视图内容
- (void)glkViewControllerUpdate:(GLKViewController *)controller;

2:配置帧速率属性

//视图控制器调用视图以及更新视图内容的速率
@property (nonatomic) NSInteger preferredFramesPerSecond
// 视图控制器调⽤视图以及更新其内容的实际速率
@property (nonatomic, readonly) NSInteger framesPerSecond;

3:控制帧更新属性

//渲染循环是否已暂停
@property (nonatomic, getter=isPaused) BOOL paused;
//当前程序重新激活活动状态时视图控制器是都自动暂停渲染循环
@property (nonatomic) BOOL pauseOnWillResignActive;
//当前程序变为活动状态时视图控制器是否自动恢复呈现循环
@property (nonatomic) BOOL resumeOnDidBecomeActive;

4:获取有关View的更新信息

//视图控制器自创建以来发送的帧更新数
@property (nonatomic, readonly) NSInteger framesDisplayed;
//⾃视图控制器第一次恢复发送更新事件以来经过的时间量
@property (nonatomic, readonly) NSTimeInterval timeSinceFirstResume;
//⾃上次视图控制器恢复发送更新事件以来更新的时间量
@property (nonatomic, readonly) NSTimeInterval timeSinceLastResume;
//⾃上次视图控制器调用委托方法以及经过的时间量
@property (nonatomic, readonly) NSTimeInterval timeSinceLastUpdate;
//⾃上次视图控制器调⽤委托⽅法以及经过的时间量
@property (nonatomic, readonly) NSTimeInterval timeSinceLastDraw;

5:GLKViewControllerDelegate 渲染循环回调方法

@required
//显示每个帧之前调用
- (void)glkViewControllerUpdate:(GLKViewController *)controller;
@optional
//在渲染循环暂停或者恢复之前调用
- (void)glkViewController:(GLKViewController *)controller willPause:(BOOL)pause;
三:GLKBaseEffect⼀种简单光照/着色系统,用于基于着⾊器OpenGL 渲染

1:配置模型视图转换

//绑定效果时用于顶点数据模型视图,投影和纹理变换
@property (nonatomic, readonly)       GLKEffectPropertyTransform         *transform;

2:配置光照效果

//⽤于计算每个片段的光照策略
@property (nonatomic, assign)         GLKLightingType                    lightingType; 
typedef NS_ENUM(GLint, GLKLightingType)
{
    //表示在三⻆形中每个顶点执⾏光照计算,然后在三角形进⾏插值
    GLKLightingTypePerVertex,
    //表示光照计算的输入在三角形内插入,并且在每个⽚段执⾏光照计算
    GLKLightingTypePerPixel
} NS_ENUM_AVAILABLE(10_8, 5_0);

3:配置光照

//表示为基元的两侧计算光照
@property (nonatomic, assign)         GLboolean                          lightModelTwoSided;     
//计算渲染图元光照使用的材质属性 
@property (nonatomic, readonly)       GLKEffectPropertyMaterial          *material;
//环境颜色,应用效果渲染的所有图元
@property (nonatomic, assign)         GLKVector4                         lightModelAmbientColor;
//场景中第一,第二,第三光照属性
@property (nonatomic, readonly)    
   GLKEffectPropertyLight             *light0, *light1, *light2;   // Disabled

4:配置纹理

//纹理应用于渲染图元的顺序
@property (nullable, nonatomic, copy) NSArray *textureOrder;  
//第一,第二纹理属性
@property (nonatomic, readonly)       GLKEffectPropertyTexture           *texture2d0, *texture2d1; 

5:配置雾化

//应用于场景的雾属性
@property (nonatomic, readonly)       GLKEffectPropertyFog               *fog; 

6:配置颜色信息

//表示计算光照与材质交互时是否使用颜⾊顶点属性
@property (nonatomic, assign)         GLboolean                          colorMaterialEnabled;        // GL_FALSE
//指是否使用常量颜色
@property (nonatomic, assign)         GLboolean                          useConstantColor;            // GL_TRUE
//不提供每个顶点颜色数据时使用常量颜色
@property (nonatomic, assign)         GLKVector4                         constantColor;               // { 1.0, 1.0, 1.0, 1.0 }

7:准备绘制效果

- (void) prepareToDraw;

二: GLKit作用

1:加载纹理
2:提供高性能的数学运算
3:提供常见的着色器
4:提供视图以及视图控制器

三:配置GLKit视图

- (void)viewDidLoad{ [super viewDidLoad];
//创建OpenGL ES上下文并将其分配给从故事板加载的视图
GLKView * view =(GLKView *)self.view;
view.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
//配置视图创建的渲染缓冲区
view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
view.drawableStencilFormat = GLKViewDrawableStencilFormat8;
//启⽤用多重采样
view.drawableMultisample = GLKViewDrawableMultisample4X;
}
-(void)drawRect:(CGRect)rect
{
//清除帧缓冲区
glClearColor(0.0f,0.0f,0.1f,1.0f); 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//使⽤用先前配置的纹理理,着⾊色器器和顶点数组绘制 glBindTexture(GL_TEXTURE_2D,_planetTexture);
glUseProgram(_diffuseShading);
glUniformMatrix4fv(_uniformModelViewProjectionMatrix,1,0,_modelViewProjectionMatrix.m);
glBindVertexArrayOES(_planetMesh);
glDrawElements(GL_TRIANGLE_STRIP,256,GL_UNSIGNED_SHORT);

四: GLKit纹理加载

1:GLKTextureInfo创建OpenGL纹理信息

** GLKTextureInfo常用属性**

{
@private
    GLuint                      name;//OpenGL 上下⽂文中纹理理名称
    GLenum                      target;//绑定纹理的目标
    GLuint                      width;//加载纹理的宽度
    GLuint                      height;//加载纹理的高度
    GLuint                      depth;//加载我纹理的深度
    GLKTextureInfoAlphaState    alphaState;//加载纹理中alpha分量状态
    GLKTextureInfoOrigin        textureOrigin;加载纹理中的原点位置
    BOOL                        containsMipmaps;加载纹理是否包含mip贴图
    GLuint                      mimapLevelCount;//纹理的mipmap层级数
    GLuint                      arrayLength;
}
1:GLTextureLoader 简化从各种资源⽂件中加载纹理

** GLTextureLoader初始化方法**

#if TARGET_OS_IPHONE
//初始化一个新的纹理加载到对象中
- (instancetype)initWithSharegroup:(EAGLSharegroup *)sharegroup;
#else
//初始化一个新的纹理加载对象
- (instancetype)initWithShareContext:(NSOpenGLContext *)context;
#endif

** GLTextureLoader加载各种资源方法**
1:从文件中加载

//从文件加载2D纹理图像并从数据中创建新的纹理
+ (nullable GLKTextureInfo *)textureWithContentsOfFile:(NSString *)path                                       /* File path of image. */
                                               options:(nullable NSDictionary *)options /* Options that control how the image is loaded. */
                                                 error:(NSError * __nullable * __nullable)outError;
//从文件中异步加载2D纹理图像,并根据数据创建新纹理
- (void)textureWithContentsOfFile:(NSString *)path                                       /* File path of image. */
                          options:(nullable NSDictionary *)options /* Options that control how the image is loaded. */
                            queue:(nullable dispatch_queue_t)queue                       /* Dispatch queue, or NULL to use the main queue. */
                completionHandler:(GLKTextureLoaderCallback)block;           

2:从URL加载纹理

//从URL加载2D纹理图像并从数据中创建新的纹理
- (void)textureWithContentsOfURL:(NSURL *)url                                           /* File path of image. */
                         options:(nullable NSDictionary *)options /* Options that control how the image is loaded. */
                           queue:(nullable dispatch_queue_t)queue                       /* Dispatch queue, or NULL to use the main queue. */
               completionHandler:(GLKTextureLoaderCallback)block;                       /* Block to be invoked on the above dispatch queue. */
//从URL异步加载2D纹理图像,并根据数据创建新纹理;
+ (nullable GLKTextureInfo *)textureWithContentsOfURL:(NSURL *)url                                           /* The URL from which to read. */
                                              options:(nullable NSDictionary *)options /* Options that control how the image is loaded. */
                                                error:(NSError * __nullable * __nullable)outError;  

3:从内存中加载纹理

//从内存中加载2D纹理图像,并根据数据创建新的纹理
- (void)textureWithContentsOfData:(NSData *)data                                         /* NSData containing image contents. */
                          options:(nullable NSDictionary *)options /* Options that control how the image is loaded. */
                            queue:(nullable dispatch_queue_t)queue                       /* Dispatch queue, or NULL to use the main queue. */
                completionHandler:(GLKTextureLoaderCallback)block;                       
//从内存中异步加载2D纹理图像,并从数据中创建新纹理
+ (nullable GLKTextureInfo *)textureWithContentsOfData:(NSData *)data                                         /* NSData containing image contents. */
                                               options:(nullable NSDictionary *)options /* Options that control how the image is loaded. */
                                                 error:(NSError * __nullable * __nullable)outError;   

4:从CGImages加载纹理

//从Quartz图像 加载2D纹理理图像并从数据创建新纹理
- (void)textureWithCGImage:(CGImageRef)cgImage                                    /* CGImage reference. */
                   options:(nullable NSDictionary *)options /* Options that control how the image is loaded. */
                     queue:(nullable dispatch_queue_t)queue                       /* Dispatch queue, or NULL to use the main queue. */
         completionHandler:(GLKTextureLoaderCallback)block;   
//   从Quartz图像异步加载2D纹理理图像并从数据创建新纹理
+ (nullable GLKTextureInfo *)textureWithCGImage:(CGImageRef)cgImage                                    /* CGImage reference. */
                                        options:(nullable NSDictionary *)options /* Options that control how the image is loaded. */
                                          error:(NSError * __nullable * __nullable)outError;                 

5:从文件中加载多维数据加载纹理

//从单个文件加载⽴方体贴图纹理对象,并从数据中创建新纹理
- (void)cubeMapWithContentsOfFile:(NSString *)path                                       /* File path of image. */
                          options:(nullable NSDictionary *)options /* Options that control how the image is loaded. */
                            queue:(nullable dispatch_queue_t)queue                       /* Dispatch queue, or NULL to use the main queue. */
                completionHandler:(GLKTextureLoaderCallback)block;                       /* Block to be invoked on the above dispatch queue. */
//从单个文件异步加载⽴方体贴图纹理对象,并从数据中创建新纹理
+ (nullable GLKTextureInfo*)cubeMapWithContentsOfFile:(NSString *)path                                       /* File path of image. */
                                              options:(nullable NSDictionary *)options /* Options that control how the image is loaded. */
                                                error:(NSError * __nullable * __nullable)outError;           /* Error description. */
//从⼀系列文件异步加载⽴方体贴图纹理图像,并从数据中创建新纹理
- (void)cubeMapWithContentsOfFiles:(NSArray *)paths                                   /* An array of paths (NSStrings or NSURLs). */
                           options:(nullable NSDictionary *)options /* Options that control how the image is loaded. */
                             queue:(nullable dispatch_queue_t)queue                       /* Dispatch queue, or NULL to use the main queue. */
                 completionHandler:(GLKTextureLoaderCallback)block;                       /* Block to be invoked on the above dispatch queue. */
//从⼀系列文件加载⽴方体贴图纹理图像,并从数据中创建新纹理
+ (nullable GLKTextureInfo*)cubeMapWithContentsOfFiles:(NSArray *)paths                                   /* An array of paths (NSStrings or NSURLs). */
                                               options:(nullable NSDictionary *)options /* Otions that control how the image is loaded. */
                                                 error:(NSError * __nullable * __nullable)outError;           /* Error description. */

你可能感兴趣的:(OpenGL ES之GLKit框架学习)