GLKit簡介
GLKit 框架的設計?標是為了簡化基于OpenGL / OpenGL ES 的應?開發(fā). 它的出現(xiàn)
加快OpenGL ES或OpenGL應用程序開發(fā)。 使?數(shù)學庫,背景紋理加載,預先創(chuàng)建的著
色?效果,以及標準視圖和視圖控制?來實現(xiàn)渲染循環(huán)。
GLKit框架提供了功能和類,可以減少創(chuàng)建新的基于著??的應用程序所需的?作量,或者支持依賴早期版本的OpenGL ES或OpenGL提供的固定函數(shù)頂點或片段處理的現(xiàn)有應用程序。
GLKit功能
- 加載紋理:
GLKTextuerLoader提供相關API加載紋理數(shù)據(jù)。 - 提供高性能的數(shù)學運算
- 提供常見的著色器
- 提供視圖以及視圖控制器:
GLKView和GLKViewController
GLKView常用API
GLKView繼承自UIView
- 初始化視圖
- (instancetype)initWithFrame:(CGRect)frame context:(EAGLContext *)context;
- 視圖代理
@property (nullable, nonatomic, assign) IBOutlet id <GLKViewDelegate> delegate;
- 繪制視圖內容時使用的上下文
@property (nonatomic, retain) EAGLContext *context;
- 各緩存區(qū)格式
drawableColorFormat 顏?渲染緩存區(qū)格式:
GLKViewDrawableColorFormatRGBA8888 = 0,
GLKViewDrawableColorFormatRGB565,
GLKViewDrawableColorFormatSRGBA8888,
drawableDepthFormat 深度渲染緩存區(qū)格式:
GLKViewDrawableDepthFormatNone = 0,
GLKViewDrawableDepthFormat16,
GLKViewDrawableDepthFormat24,
drawableStencilFormat 模板渲染緩存區(qū)的格式:
GLKViewDrawableStencilFormatNone = 0,
GLKViewDrawableStencilFormat8,
drawableMultisample 多重采樣緩存區(qū)的格式:
GLKViewDrawableMultisampleNone = 0,
GLKViewDrawableMultisample4X,
- 緩存區(qū)屬性
drawableHeight 底層緩存區(qū)對象的?度(以像素為單位)
drawableWidth 底層緩存區(qū)對象的寬度(以像素為單位)
- 將底層FrameBuffer 對象綁定到OpenGL ES
- (void)bindDrawable;
- 刪除與視圖關聯(lián)的可繪制對象/刪除視圖FrameBuffer對象
- (void)deleteDrawable;
- 繪制視圖內容并將其作為新圖像對象返回
@property (readonly, strong) UIImage *snapshot;
- 布爾值(BOOL),指定視圖是否響應使得視圖內容無效的消息
@property (nonatomic) BOOL enableSetNeedsDisplay;
- 立即重繪視圖內容
- (void)display;
- 繪制視圖內容(代理方法)
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect;
GLKViewController常用API
- 視圖控制器的代理
@property (nullable, nonatomic, assign) IBOutlet id <GLKViewControllerDelegate> delegate;
- 配置視圖的幀速率
//視圖控制器調用視圖以及更新視圖內容的速率
@property (nonatomic) NSInteger preferredFramesPerSecond;
//視圖控制器調用視圖以及更新其內容的實際速率
@property (nonatomic, readonly) NSInteger framesPerSecond;
- 控制幀更新
//Bool值,渲染循環(huán)是否已暫停
@property (nonatomic, getter=isPaused) BOOL paused;
//Bool值,當前程序重新激活活動狀態(tài)時視圖控制器是否自動暫停循環(huán)渲染
@property (nonatomic) BOOL pauseOnWillResignActive;
//Bool值,當前程序變?yōu)榛顒訝顟B(tài)時視圖控制器是否自動恢復呈現(xiàn)渲染
@property (nonatomic) BOOL resumeOnDidBecomeActive;
- 獲取有關視圖view的更新信息
//視圖控制器自創(chuàng)建以來發(fā)送的幀更新數(shù)
@property (nonatomic, readonly) NSInteger framesDisplayed;
//?視圖控制器第?次恢復發(fā)送更新事件以來經(jīng)過的時間量
@property (nonatomic, readonly) NSTimeInterval timeSinceFirstResume;
//自上次視圖控制器恢復發(fā)送更新事件以來更新的時間量
@property (nonatomic, readonly) NSTimeInterval timeSinceLastResume;
//?上次視圖控制器調用委托?法以及經(jīng)過的時間量
@property (nonatomic, readonly) NSTimeInterval timeSinceLastUpdate;
//?上次視圖控制器調用視圖display 方法以來經(jīng)過的時間量
@property (nonatomic, readonly) NSTimeInterval timeSinceLastDraw;
- 渲染循環(huán)回調方法--更新視圖內容,處理更新事件
//在顯示每個幀之前調用
- (void)glkViewControllerUpdate:(GLKViewController *)controller;
- 暫停/恢復通知
//在渲染循環(huán)暫?;蛘呋謴椭罢{用
- (void)glkViewController:(GLKViewController *)controller willPause:(BOOL)pause;
GLTextureLoader(紋理信息)
主要包含紋理的一下信息:
- name : OpenGL上下文中紋理名稱
- target : 紋理綁定的目標
- height: 加載的紋理高度
- width : 加載的紋理寬度
- textureOrigin : 加載紋理中的原點位置
- alphaState : 加載紋理中alpha分量狀態(tài)
- 初始化
//初始化一個新的紋理加載到對象中
- (instancetype)initWithSharegroup:(EAGLSharegroup *)sharegroup;
//初始化一個新的紋理加載對象
- (instancetype)initWithShareContext:(NSOpenGLContext *)context;
- 從文件資源中加載紋理
//從文件加載2D紋理圖像并從數(shù)據(jù)中創(chuàng)建新的紋理
+ (nullable GLKTextureInfo *)textureWithContentsOfFile:(NSString *)path options:(nullable NSDictionary<NSString*, NSNumber*> *)options error:(NSError * __nullable * __nullable)outError;
//從文件中異步加載2D紋理圖像,并根據(jù)數(shù)據(jù)創(chuàng)建新紋理
- (void)textureWithContentsOfFile:(NSString *)path options:(nullable NSDictionary<NSString*, NSNumber*> *)options queue:(nullable dispatch_queue_t)queue completionHandler:(GLKTextureLoaderCallback)block;
- 從URL地址中加載紋理
//用URL加載2D紋理圖像并從數(shù)據(jù)中創(chuàng)建新的紋理
+ (nullable GLKTextureInfo *)textureWithContentsOfURL:(NSURL *)url options:(nullable NSDictionary<NSString*, NSNumber*> *)options error:(NSError * __nullable * __nullable)outError;
//用URL異步加載2D紋理圖像,并根據(jù)數(shù)據(jù)創(chuàng)建新紋理
- (void)textureWithContentsOfFile:(NSString *)path options:(nullable NSDictionary<NSString*, NSNumber*> *)options queue:(nullable dispatch_queue_t)queue completionHandler:(GLKTextureLoaderCallback)block;
- 從內存中數(shù)據(jù)創(chuàng)建紋理
//從內存空間加載2D紋理圖像并從數(shù)據(jù)中創(chuàng)建新的紋理
+ (nullable GLKTextureInfo *)textureWithContentsOfData:(NSData *)data options:(nullable NSDictionary<NSString*, NSNumber*> *)options error:(NSError * __nullable * __nullable)outError;
//從內存空間異步加載2D紋理圖像,并根據(jù)數(shù)據(jù)創(chuàng)建新紋理
- (void)textureWithContentsOfData:(NSData *)data options:(nullable NSDictionary<NSString*, NSNumber*> *)options queue:(nullable dispatch_queue_t)queue completionHandler:(GLKTextureLoaderCallback)block;
- 從CGImages創(chuàng)建紋理
//從Quartz加載2D紋理圖像并從數(shù)據(jù)中創(chuàng)建新的紋理
+ (nullable GLKTextureInfo *)textureWithCGImage:(CGImageRef)cgImage options:(nullable NSDictionary<NSString*, NSNumber*> *)options error:(NSError * __nullable * __nullable)outError;
//從Quartz異步加載2D紋理圖像,并根據(jù)數(shù)據(jù)創(chuàng)建新紋理
- (void)textureWithCGImage:(CGImageRef)cgImage options:(nullable NSDictionary<NSString*, NSNumber*> *)options queue:(nullable dispatch_queue_t)queue completionHandler:(GLKTextureLoaderCallback)block;
- 從URL加載多維數(shù)據(jù)創(chuàng)建紋理
//從單個URL加載?方體貼圖紋理圖像,并根據(jù)數(shù)據(jù)創(chuàng)建新紋理
+ (nullable GLKTextureInfo*)cubeMapWithContentsOfURL:(NSURL *)url options:(nullable NSDictionary<NSString*, NSNumber*> *)options error:(NSError * __nullable * __nullable)outError;
//從單個URL異步加載?方體貼圖紋理圖像,并根據(jù)數(shù)據(jù)創(chuàng)建新紋理
- (void)cubeMapWithContentsOfURL:(NSURL *)url options:(nullable NSDictionary<NSString*, NSNumber*> *)options queue:(nullable dispatch_queue_t)queue completionHandler:(GLKTextureLoaderCallback)block;
- 從文件資源加載多維數(shù)據(jù)創(chuàng)建紋理
//從單個文件加載?方體貼圖紋理對象,并從數(shù)據(jù)中創(chuàng)建新紋理
+ (nullable GLKTextureInfo*)cubeMapWithContentsOfFile:(NSString *)path options:(nullable NSDictionary<NSString*, NSNumber*> *)options error:(NSError * __nullable * __nullable)outError;
//從單個文件異步立方體貼圖紋理圖像,并根據(jù)數(shù)據(jù)創(chuàng)建新紋理
- (void)cubeMapWithContentsOfFile:(NSString *)path options:(nullable NSDictionary<NSString*, NSNumber*> *)options queue:(nullable dispatch_queue_t)queue completionHandler:(GLKTextureLoaderCallback)block;
//從一系列文件加載立方體貼圖紋理圖像.并根據(jù)數(shù)據(jù)創(chuàng)建新的紋理
+ (nullable GLKTextureInfo*)cubeMapWithContentsOfFiles:(NSArray<id> *)paths options:(nullable NSDictionary<NSString*, NSNumber*> *)options error:(NSError * __nullable * __nullable)outError;
//從一系列文件異步立方體貼圖紋理圖像,并根據(jù)數(shù)據(jù)創(chuàng)建新紋理
- (void)cubeMapWithContentsOfFiles:(NSArray<id> *)paths options:(nullable NSDictionary<NSString*, NSNumber*> *)options queue:(nullable dispatch_queue_t)queue completionHandler:(GLKTextureLoaderCallback)block;
GLKBaseEffect(光照)
- 命名Effect
//用字符串給Effect(效果)命名
NSString *label;
- 配置模型視圖轉換
//綁定效果時應用于頂點數(shù)據(jù)的模型視圖,投影和紋理變換
GLKEffectPropertyTransform *transform;
- 配置光照效果
//?于計算每個片段的光照策略,
GLKLightingType lightingType;
//枚舉值
GLKLightingTypePerVertex :表示在三?形中每個頂點執(zhí)?光照計算,然后在三角形進行插值。
GLKLightingTypePerPixel :表示光照計算的輸?在三角形內插入,并且在每個片段執(zhí)行光照計算。
- 配置光照
//布爾值,表示為基元的兩側計算光照
GLboolean lightModelTwoSided;
//計算渲染圖元光照使?用的材質屬性
GLKEffectPropertyMaterial *material;
//環(huán)境顏色,應?效果渲染的所有圖元
GLKVector4 lightModelAmbientColor;
//場景中第一個光照屬性,第二個光照屬性,第三個光照屬性
GLKEffectPropertyLight *light0, *light1, *light2;
- 配置紋理
//第?個紋理屬性,第二個紋理屬性
GLKEffectPropertyTexture *texture2d0, *texture2d1;
//紋理應?于渲染圖元的順序
NSArray<GLKEffectPropertyTexture*> *textureOrder;
- 配置霧化
//應用于場景的霧屬性
GLKEffectPropertyFog *fog;
- 配置顏色信息
//布爾值,表示計算光照與材質交互時是否使用顏色頂點屬性
GLboolean colorMaterialEnabled;
//布爾值,指示是否使用常量顏?
GLboolean useConstantColor;
//不提供每個頂點顏色數(shù)據(jù)時使用常量顏?
GLKVector4 constantColor;
- 準備繪制效果
//準備渲染效果
- (void) prepareToDraw;