OpenGL ES — GLKit

GLKit簡介

GLKit 框架的設計⽬標是爲了簡化基於OpenGL / OpenGL ES 的應⽤開發. 它的出現 加快OpenGL ES或OpenGL應用程序開發。 使⽤數學庫,背景紋理加載,預先建立的着 色器效果,以及標準視圖和視圖控制器來實現渲染循環。 GLKit框架提供了功能和類,能夠減小建立新的基於着⾊器的應用程序所需的⼯做量,或者支持依賴早期版本的OpenGL ES或OpenGL提供的固定函數頂點或片斷處理的現有應用程序。緩存

GLKit功能

  • 加載紋理:GLKTextuerLoader提供相關API加載紋理數據。
  • 提供高性能的數學運算
  • 提供常見的着色器
  • 提供視圖以及視圖控制器:GLKViewGLKViewController

GLKView經常使用API

GLKView繼承自UIViewbash

  1. 初始化視圖
- (instancetype)initWithFrame:(CGRect)frame context:(EAGLContext *)context;

複製代碼
  1. 視圖代理
@property (nullable, nonatomic, assign) IBOutlet id <GLKViewDelegate> delegate;

複製代碼
  1. 繪製視圖內容時使用的上下文
@property (nonatomic, retain) EAGLContext *context;

複製代碼
  1. 各緩存區格式
drawableColorFormat 顏⾊渲染緩存區格式:
GLKViewDrawableColorFormatRGBA8888 = 0,
GLKViewDrawableColorFormatRGB565,
GLKViewDrawableColorFormatSRGBA8888,

drawableDepthFormat 深度渲染緩存區格式:
GLKViewDrawableDepthFormatNone = 0,
GLKViewDrawableDepthFormat16,
GLKViewDrawableDepthFormat24,

drawableStencilFormat 模板渲染緩存區的格式:
GLKViewDrawableStencilFormatNone = 0,
GLKViewDrawableStencilFormat8,

drawableMultisample 多重採樣緩存區的格式:
GLKViewDrawableMultisampleNone = 0,
GLKViewDrawableMultisample4X,
複製代碼
  1. 緩存區屬性
drawableHeight 底層緩存區對象的⾼度(以像素爲單位)
drawableWidth 底層緩存區對象的寬度(以像素爲單位)
複製代碼
  1. 將底層FrameBuffer 對象綁定到OpenGL ES
- (void)bindDrawable;
複製代碼
  1. 刪除與視圖關聯的可繪製對象/刪除視圖FrameBuffer對象
- (void)deleteDrawable;
複製代碼
  1. 繪製視圖內容並將其做爲新圖像對象返回
@property (readonly, strong) UIImage *snapshot;
複製代碼
  1. 布爾值(BOOL),指定視圖是否響應使得視圖內容無效的消息
@property (nonatomic) BOOL enableSetNeedsDisplay;
複製代碼
  1. 當即重繪視圖內容
- (void)display;
複製代碼
  1. 繪製視圖內容(代理方法)
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect;
複製代碼

GLKViewController經常使用API

  1. 視圖控制器的代理
@property (nullable, nonatomic, assign) IBOutlet id <GLKViewControllerDelegate> delegate;
複製代碼
  1. 配置視圖的幀速率
//視圖控制器調用視圖以及更新視圖內容的速率
@property (nonatomic) NSInteger preferredFramesPerSecond;
//視圖控制器調用視圖以及更新其內容的實際速率
@property (nonatomic, readonly) NSInteger framesPerSecond;
複製代碼
  1. 控制幀更新
//Bool值,渲染循環是否已暫停
@property (nonatomic, getter=isPaused) BOOL paused;
//Bool值,當前程序從新激活活動狀態時視圖控制器是否自動暫停循環渲染
@property (nonatomic) BOOL pauseOnWillResignActive;
//Bool值,當前程序變爲活動狀態時視圖控制器是否自動恢復呈現渲染
@property (nonatomic) BOOL resumeOnDidBecomeActive;
複製代碼
  1. 獲取有關視圖view的更新信息
//視圖控制器自建立以來發送的幀更新數
@property (nonatomic, readonly) NSInteger framesDisplayed;
//⾃視圖控制器第⼀次恢復發送更新事件以來通過的時間量
@property (nonatomic, readonly) NSTimeInterval timeSinceFirstResume;
//自上次視圖控制器恢復發送更新事件以來更新的時間量
@property (nonatomic, readonly) NSTimeInterval timeSinceLastResume;
//⾃上次視圖控制器調用委託⽅法以及通過的時間量
@property (nonatomic, readonly) NSTimeInterval timeSinceLastUpdate;
//⾃上次視圖控制器調用視圖display 方法以來通過的時間量
@property (nonatomic, readonly) NSTimeInterval timeSinceLastDraw;
複製代碼
  1. 渲染循環回調方法--更新視圖內容,處理更新事件
//在顯示每一個幀以前調用
- (void)glkViewControllerUpdate:(GLKViewController *)controller;
複製代碼
  1. 暫停/恢復通知
//在渲染循環暫停或者恢復以前調用
- (void)glkViewController:(GLKViewController *)controller willPause:(BOOL)pause;
複製代碼

GLTextureLoader(紋理信息)

主要包含紋理的一下信息:框架

  • name : OpenGL上下文中紋理名稱
  • target : 紋理綁定的目標
  • height: 加載的紋理高度
  • width : 加載的紋理寬度
  • textureOrigin : 加載紋理中的原點位置
  • alphaState : 加載紋理中alpha份量狀態
  1. 初始化
//初始化一個新的紋理加載到對象中
- (instancetype)initWithSharegroup:(EAGLSharegroup *)sharegroup;
//初始化一個新的紋理加載對象
- (instancetype)initWithShareContext:(NSOpenGLContext *)context;
複製代碼
  1. 從文件資源中加載紋理
//從文件加載2D紋理圖像並從數據中建立新的紋理
+ (nullable GLKTextureInfo *)textureWithContentsOfFile:(NSString *)path options:(nullable NSDictionary<NSString*, NSNumber*> *)options  error:(NSError * __nullable * __nullable)outError; 
//從文件中異步加載2D紋理圖像,並根據數據建立新紋理
- (void)textureWithContentsOfFile:(NSString *)path options:(nullable NSDictionary<NSString*, NSNumber*> *)options queue:(nullable dispatch_queue_t)queue completionHandler:(GLKTextureLoaderCallback)block; 
複製代碼
  1. 從URL地址中加載紋理
//用URL加載2D紋理圖像並從數據中建立新的紋理
+ (nullable GLKTextureInfo *)textureWithContentsOfURL:(NSURL *)url options:(nullable NSDictionary<NSString*, NSNumber*> *)options error:(NSError * __nullable * __nullable)outError;
//用URL異步加載2D紋理圖像,並根據數據建立新紋理
- (void)textureWithContentsOfFile:(NSString *)path options:(nullable NSDictionary<NSString*, NSNumber*> *)options queue:(nullable dispatch_queue_t)queue completionHandler:(GLKTextureLoaderCallback)block;
複製代碼
  1. 從內存中數據建立紋理
//從內存空間加載2D紋理圖像並從數據中建立新的紋理
+ (nullable GLKTextureInfo *)textureWithContentsOfData:(NSData *)data options:(nullable NSDictionary<NSString*, NSNumber*> *)options error:(NSError * __nullable * __nullable)outError;
//從內存空間異步加載2D紋理圖像,並根據數據建立新紋理
- (void)textureWithContentsOfData:(NSData *)data options:(nullable NSDictionary<NSString*, NSNumber*> *)options queue:(nullable dispatch_queue_t)queue completionHandler:(GLKTextureLoaderCallback)block;
複製代碼
  1. 從CGImages建立紋理
//從Quartz加載2D紋理圖像並從數據中建立新的紋理
+ (nullable GLKTextureInfo *)textureWithCGImage:(CGImageRef)cgImage options:(nullable NSDictionary<NSString*, NSNumber*> *)options error:(NSError * __nullable * __nullable)outError;
//從Quartz異步加載2D紋理圖像,並根據數據建立新紋理
- (void)textureWithCGImage:(CGImageRef)cgImage options:(nullable NSDictionary<NSString*, NSNumber*> *)options queue:(nullable dispatch_queue_t)queue completionHandler:(GLKTextureLoaderCallback)block;
複製代碼
  1. 從URL加載多維數據建立紋理
//從單個URL加載⽴方體貼圖紋理圖像,並根據數據建立新紋理
+ (nullable GLKTextureInfo*)cubeMapWithContentsOfURL:(NSURL *)url options:(nullable NSDictionary<NSString*, NSNumber*> *)options error:(NSError * __nullable * __nullable)outError;
//從單個URL異步加載⽴方體貼圖紋理圖像,並根據數據建立新紋理
- (void)cubeMapWithContentsOfURL:(NSURL *)url options:(nullable NSDictionary<NSString*, NSNumber*> *)options queue:(nullable dispatch_queue_t)queue completionHandler:(GLKTextureLoaderCallback)block;
複製代碼
  1. 從文件資源加載多維數據建立紋理
//從單個文件加載⽴方體貼圖紋理對象,並從數據中建立新紋理
+ (nullable GLKTextureInfo*)cubeMapWithContentsOfFile:(NSString *)path options:(nullable NSDictionary<NSString*, NSNumber*> *)options error:(NSError * __nullable * __nullable)outError;
//從單個文件異步立方體貼圖紋理圖像,並根據數據建立新紋理
- (void)cubeMapWithContentsOfFile:(NSString *)path options:(nullable NSDictionary<NSString*, NSNumber*> *)options queue:(nullable dispatch_queue_t)queue completionHandler:(GLKTextureLoaderCallback)block;
//從一系列文件加載立方體貼圖紋理圖像.並根據數據建立新的紋理
+ (nullable GLKTextureInfo*)cubeMapWithContentsOfFiles:(NSArray<id> *)paths options:(nullable NSDictionary<NSString*, NSNumber*> *)options error:(NSError * __nullable * __nullable)outError;
//從一系列文件異步立方體貼圖紋理圖像,並根據數據建立新紋理
- (void)cubeMapWithContentsOfFiles:(NSArray<id> *)paths options:(nullable NSDictionary<NSString*, NSNumber*> *)options queue:(nullable dispatch_queue_t)queue completionHandler:(GLKTextureLoaderCallback)block;
複製代碼

GLKBaseEffect(光照)

  1. 命名Effect
//用字符串給Effect(效果)命名
 NSString  *label; 
複製代碼
  1. 配置模型視圖轉換
//綁定效果時應用於頂點數據的模型視圖,投影和紋理變換
GLKEffectPropertyTransform  *transform;
複製代碼
  1. 配置光照效果
//⽤於計算每一個片斷的光照策略,
GLKLightingType  lightingType;
//枚舉值
GLKLightingTypePerVertex :表示在三⻆形中每一個頂點執⾏光照計算,而後在三角形進行插值。
GLKLightingTypePerPixel :表示光照計算的輸⼊在三角形內插入,而且在每一個片斷執行光照計算。
複製代碼
  1. 配置光照
//布爾值,表示爲基元的兩側計算光照
GLboolean  lightModelTwoSided;
//計算渲染圖元光照使⽤用的材質屬性
GLKEffectPropertyMaterial *material;  
//環境顏色,應⽤效果渲染的全部圖元
GLKVector4  lightModelAmbientColor;
//場景中第一個光照屬性,第二個光照屬性,第三個光照屬性
GLKEffectPropertyLight   *light0, *light1, *light2; 
複製代碼
  1. 配置紋理
//第⼀個紋理屬性,第二個紋理屬性
GLKEffectPropertyTexture  *texture2d0, *texture2d1;
//紋理應⽤於渲染圖元的順序
NSArray<GLKEffectPropertyTexture*> *textureOrder; 
複製代碼
  1. 配置霧化
//應用於場景的霧屬性
GLKEffectPropertyFog  *fog; 
複製代碼
  1. 配置顏色信息
//布爾值,表示計算光照與材質交互時是否使用顏色頂點屬性
GLboolean  colorMaterialEnabled;
//布爾值,指示是否使用常量顏⾊
GLboolean  useConstantColor;
//不提供每一個頂點顏色數據時使用常量顏⾊
GLKVector4  constantColor; 
複製代碼
  1. 準備繪製效果
//準備渲染效果
- (void) prepareToDraw;
複製代碼
相關文章
相關標籤/搜索