一:GLKit框架概述
1:GLKit 框架的設(shè)計(jì)?標(biāo)是為了簡(jiǎn)化基于OpenGL / OpenGL ES 的應(yīng)用開(kāi)發(fā)。它的出現(xiàn)加快OpenGL ES或OpenGL應(yīng)用程序開(kāi)發(fā)芜飘。 使用數(shù)學(xué)庫(kù)堪簿,背景紋理加載,預(yù)先創(chuàng)建的著?器效果喘批,以及標(biāo)準(zhǔn)視圖和視圖控制器來(lái)實(shí)現(xiàn)渲染循環(huán)撩荣。
2:GLKit框架提供了功能和類(lèi),可以減少創(chuàng)建新的基于著?器的應(yīng)用程序所需的?作量饶深,或者?持依賴早期版本的OpenGL ES或OpenGL提供的固定函數(shù)頂點(diǎn)或片段處理的現(xiàn)有應(yīng)?程序餐曹。
GLKit框架常用類(lèi)
一:GLKView
提供繪制場(chǎng)所(View) GLKView繼承UIView
** GLKView常用屬性**
1:幀緩存區(qū)屬性
//繪制視圖內(nèi)容時(shí)使用的OpenGL ES 上下文
@property (nonatomic, retain) EAGLContext *context;
//底層緩存區(qū)對(duì)象的?高度(以像素為單位)
@property (nonatomic, readonly) NSInteger drawableWidth;
//底層緩存區(qū)對(duì)象的寬度(以像素為單位)
@property (nonatomic, readonly) NSInteger drawableHeight;
2:配置幀緩存區(qū)對(duì)象
//顏?色渲染緩存區(qū)格式
@property (nonatomic) GLKViewDrawableColorFormat drawableColorFormat;
//深度渲染緩存區(qū)格式
@property (nonatomic) GLKViewDrawableDepthFormat drawableDepthFormat;
//模板渲染緩存區(qū)的格式
@property (nonatomic) GLKViewDrawableStencilFormat drawableStencilFormat;
//多重采樣緩存區(qū)的格式
@property (nonatomic) GLKViewDrawableMultisample drawableMultisample;
3:使?OpenGL ES 繪制內(nèi)容的視圖默認(rèn)實(shí)現(xiàn)
//將底層FrameBuffer 對(duì)象綁定到OpenGL ES
- (void)bindDrawable;
//刪除與視圖關(guān)聯(lián)的可繪制對(duì)象(刪除視圖FrameBuffer對(duì)象)
- (void)deleteDrawable;
//布爾值,指定視圖是否響應(yīng)使得視圖內(nèi)容無(wú)效的消息
@property (nonatomic) BOOL enableSetNeedsDisplay;
//繪制視圖內(nèi)容并將其作為新圖像對(duì)象返回
@property (readonly, strong) UIImage *snapshot;
//立即重繪視圖內(nèi)容
- (void)display;
4:GLKViewDelegate 用于GLKView 對(duì)象回調(diào)?法?于GLKView 對(duì)象回調(diào)方法
@required
//繪制視圖內(nèi)容
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect;
二:GLKViewController 管理OpenGL ES 渲染循環(huán)的視圖控制器
1:(擴(kuò)展于標(biāo)準(zhǔn)的UIKit 設(shè)計(jì)模式. ?于繪制視圖內(nèi)容的管理與呈現(xiàn).)
2:繼承UIViewController,并且遵守了GLKViewDelegate,實(shí)現(xiàn)GLKView的代理方法
1:更新方式:
- (void) update 更新視圖內(nèi)容
- (void)glkViewControllerUpdate:(GLKViewController *)controller;
2:配置幀速率屬性
//視圖控制器調(diào)用視圖以及更新視圖內(nèi)容的速率
@property (nonatomic) NSInteger preferredFramesPerSecond
// 視圖控制器調(diào)?視圖以及更新其內(nèi)容的實(shí)際速率
@property (nonatomic, readonly) NSInteger framesPerSecond;
3:控制幀更新屬性
//渲染循環(huán)是否已暫停
@property (nonatomic, getter=isPaused) BOOL paused;
//當(dāng)前程序重新激活活動(dòng)狀態(tài)時(shí)視圖控制器是都自動(dòng)暫停渲染循環(huán)
@property (nonatomic) BOOL pauseOnWillResignActive;
//當(dāng)前程序變?yōu)榛顒?dòng)狀態(tài)時(shí)視圖控制器是否自動(dòng)恢復(fù)呈現(xiàn)循環(huán)
@property (nonatomic) BOOL resumeOnDidBecomeActive;
4:獲取有關(guān)View的更新信息
//視圖控制器自創(chuàng)建以來(lái)發(fā)送的幀更新數(shù)
@property (nonatomic, readonly) NSInteger framesDisplayed;
//?視圖控制器第一次恢復(fù)發(fā)送更新事件以來(lái)經(jīng)過(guò)的時(shí)間量
@property (nonatomic, readonly) NSTimeInterval timeSinceFirstResume;
//?上次視圖控制器恢復(fù)發(fā)送更新事件以來(lái)更新的時(shí)間量
@property (nonatomic, readonly) NSTimeInterval timeSinceLastResume;
//?上次視圖控制器調(diào)用委托方法以及經(jīng)過(guò)的時(shí)間量
@property (nonatomic, readonly) NSTimeInterval timeSinceLastUpdate;
//?上次視圖控制器調(diào)?委托?法以及經(jīng)過(guò)的時(shí)間量
@property (nonatomic, readonly) NSTimeInterval timeSinceLastDraw;
5:GLKViewControllerDelegate 渲染循環(huán)回調(diào)方法
@required
//顯示每個(gè)幀之前調(diào)用
- (void)glkViewControllerUpdate:(GLKViewController *)controller;
@optional
//在渲染循環(huán)暫停或者恢復(fù)之前調(diào)用
- (void)glkViewController:(GLKViewController *)controller willPause:(BOOL)pause;
三:GLKBaseEffect?種簡(jiǎn)單光照/著色系統(tǒng),用于基于著?器OpenGL 渲染
1:配置模型視圖轉(zhuǎn)換
//綁定效果時(shí)用于頂點(diǎn)數(shù)據(jù)模型視圖,投影和紋理變換
@property (nonatomic, readonly) GLKEffectPropertyTransform *transform;
2:配置光照效果
//?于計(jì)算每個(gè)片段的光照策略
@property (nonatomic, assign) GLKLightingType lightingType;
typedef NS_ENUM(GLint, GLKLightingType)
{
//表示在三?形中每個(gè)頂點(diǎn)執(zhí)?光照計(jì)算,然后在三角形進(jìn)?插值
GLKLightingTypePerVertex,
//表示光照計(jì)算的輸入在三角形內(nèi)插入,并且在每個(gè)?段執(zhí)?光照計(jì)算
GLKLightingTypePerPixel
} NS_ENUM_AVAILABLE(10_8, 5_0);
3:配置光照
//表示為基元的兩側(cè)計(jì)算光照
@property (nonatomic, assign) GLboolean lightModelTwoSided;
//計(jì)算渲染圖元光照使用的材質(zhì)屬性
@property (nonatomic, readonly) GLKEffectPropertyMaterial *material;
//環(huán)境顏色,應(yīng)用效果渲染的所有圖元
@property (nonatomic, assign) GLKVector4 lightModelAmbientColor;
//場(chǎng)景中第一,第二,第三光照屬性
@property (nonatomic, readonly)
GLKEffectPropertyLight *light0, *light1, *light2; // Disabled
4:配置紋理
//紋理應(yīng)用于渲染圖元的順序
@property (nullable, nonatomic, copy) NSArray<GLKEffectPropertyTexture*> *textureOrder;
//第一,第二紋理屬性
@property (nonatomic, readonly) GLKEffectPropertyTexture *texture2d0, *texture2d1;
5:配置霧化
//應(yīng)用于場(chǎng)景的霧屬性
@property (nonatomic, readonly) GLKEffectPropertyFog *fog;
6:配置顏色信息
//表示計(jì)算光照與材質(zhì)交互時(shí)是否使用顏?頂點(diǎn)屬性
@property (nonatomic, assign) GLboolean colorMaterialEnabled; // GL_FALSE
//指是否使用常量顏色
@property (nonatomic, assign) GLboolean useConstantColor; // GL_TRUE
//不提供每個(gè)頂點(diǎn)顏色數(shù)據(jù)時(shí)使用常量顏色
@property (nonatomic, assign) GLKVector4 constantColor; // { 1.0, 1.0, 1.0, 1.0 }
7:準(zhǔn)備繪制效果
- (void) prepareToDraw;
二: GLKit作用
1:加載紋理
2:提供高性能的數(shù)學(xué)運(yùn)算
3:提供常見(jiàn)的著色器
4:提供視圖以及視圖控制器
三:配置GLKit視圖
- (void)viewDidLoad{ [super viewDidLoad];
//創(chuàng)建OpenGL ES上下文并將其分配給從故事板加載的視圖
GLKView * view =(GLKView *)self.view;
view.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
//配置視圖創(chuàng)建的渲染緩沖區(qū)
view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
view.drawableStencilFormat = GLKViewDrawableStencilFormat8;
//啟?用多重采樣
view.drawableMultisample = GLKViewDrawableMultisample4X;
}
-(void)drawRect:(CGRect)rect
{
//清除幀緩沖區(qū)
glClearColor(0.0f敌厘,0.0f台猴,0.1f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//使?用先前配置的紋理理俱两,著?色器器和頂點(diǎn)數(shù)組繪制 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創(chuàng)建OpenGL紋理信息
** GLKTextureInfo常用屬性**
{
@private
GLuint name;//OpenGL 上下?文中紋理理名稱
GLenum target;//綁定紋理的目標(biāo)
GLuint width;//加載紋理的寬度
GLuint height;//加載紋理的高度
GLuint depth;//加載我紋理的深度
GLKTextureInfoAlphaState alphaState;//加載紋理中alpha分量狀態(tài)
GLKTextureInfoOrigin textureOrigin;加載紋理中的原點(diǎn)位置
BOOL containsMipmaps;加載紋理是否包含mip貼圖
GLuint mimapLevelCount;//紋理的mipmap層級(jí)數(shù)
GLuint arrayLength;
}
1:GLTextureLoader 簡(jiǎn)化從各種資源?件中加載紋理
** GLTextureLoader初始化方法**
#if TARGET_OS_IPHONE
//初始化一個(gè)新的紋理加載到對(duì)象中
- (instancetype)initWithSharegroup:(EAGLSharegroup *)sharegroup;
#else
//初始化一個(gè)新的紋理加載對(duì)象
- (instancetype)initWithShareContext:(NSOpenGLContext *)context;
#endif
** GLTextureLoader加載各種資源方法**
1:從文件中加載
//從文件加載2D紋理圖像并從數(shù)據(jù)中創(chuàng)建新的紋理
+ (nullable GLKTextureInfo *)textureWithContentsOfFile:(NSString *)path /* File path of image. */
options:(nullable NSDictionary<NSString*, NSNumber*> *)options /* Options that control how the image is loaded. */
error:(NSError * __nullable * __nullable)outError;
//從文件中異步加載2D紋理圖像,并根據(jù)數(shù)據(jù)創(chuàng)建新紋理
- (void)textureWithContentsOfFile:(NSString *)path /* File path of image. */
options:(nullable NSDictionary<NSString*, NSNumber*> *)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紋理圖像并從數(shù)據(jù)中創(chuàng)建新的紋理
- (void)textureWithContentsOfURL:(NSURL *)url /* File path of image. */
options:(nullable NSDictionary<NSString*, NSNumber*> *)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紋理圖像,并根據(jù)數(shù)據(jù)創(chuàng)建新紋理;
+ (nullable GLKTextureInfo *)textureWithContentsOfURL:(NSURL *)url /* The URL from which to read. */
options:(nullable NSDictionary<NSString*, NSNumber*> *)options /* Options that control how the image is loaded. */
error:(NSError * __nullable * __nullable)outError;
3:從內(nèi)存中加載紋理
//從內(nèi)存中加載2D紋理圖像,并根據(jù)數(shù)據(jù)創(chuàng)建新的紋理
- (void)textureWithContentsOfData:(NSData *)data /* NSData containing image contents. */
options:(nullable NSDictionary<NSString*, NSNumber*> *)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;
//從內(nèi)存中異步加載2D紋理圖像,并從數(shù)據(jù)中創(chuàng)建新紋理
+ (nullable GLKTextureInfo *)textureWithContentsOfData:(NSData *)data /* NSData containing image contents. */
options:(nullable NSDictionary<NSString*, NSNumber*> *)options /* Options that control how the image is loaded. */
error:(NSError * __nullable * __nullable)outError;
4:從CGImages加載紋理
//從Quartz圖像 加載2D紋理理圖像并從數(shù)據(jù)創(chuàng)建新紋理
- (void)textureWithCGImage:(CGImageRef)cgImage /* CGImage reference. */
options:(nullable NSDictionary<NSString*, NSNumber*> *)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紋理理圖像并從數(shù)據(jù)創(chuàng)建新紋理
+ (nullable GLKTextureInfo *)textureWithCGImage:(CGImageRef)cgImage /* CGImage reference. */
options:(nullable NSDictionary<NSString*, NSNumber*> *)options /* Options that control how the image is loaded. */
error:(NSError * __nullable * __nullable)outError;
5:從文件中加載多維數(shù)據(jù)加載紋理
//從單個(gè)文件加載?方體貼圖紋理對(duì)象,并從數(shù)據(jù)中創(chuàng)建新紋理
- (void)cubeMapWithContentsOfFile:(NSString *)path /* File path of image. */
options:(nullable NSDictionary<NSString*, NSNumber*> *)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. */
//從單個(gè)文件異步加載?方體貼圖紋理對(duì)象,并從數(shù)據(jù)中創(chuàng)建新紋理
+ (nullable GLKTextureInfo*)cubeMapWithContentsOfFile:(NSString *)path /* File path of image. */
options:(nullable NSDictionary<NSString*, NSNumber*> *)options /* Options that control how the image is loaded. */
error:(NSError * __nullable * __nullable)outError; /* Error description. */
//從?系列文件異步加載?方體貼圖紋理圖像,并從數(shù)據(jù)中創(chuàng)建新紋理
- (void)cubeMapWithContentsOfFiles:(NSArray<id> *)paths /* An array of paths (NSStrings or NSURLs). */
options:(nullable NSDictionary<NSString*, NSNumber*> *)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. */
//從?系列文件加載?方體貼圖紋理圖像,并從數(shù)據(jù)中創(chuàng)建新紋理
+ (nullable GLKTextureInfo*)cubeMapWithContentsOfFiles:(NSArray<id> *)paths /* An array of paths (NSStrings or NSURLs). */
options:(nullable NSDictionary<NSString*, NSNumber*> *)options /* Otions that control how the image is loaded. */
error:(NSError * __nullable * __nullable)outError; /* Error description. */