我們先了解一下 iOS 中管理 OpenGL ES 渲染循環(huán)的視圖控制器 - GLKViewController
A GLKViewController object works in conjunction with a GLKView object to display frames of animation in the view, and also provides standard view controller functionality.
To use this class, allocate and initialize a new GLKViewController subclass and set its view property to point to a GLKView object. Then, configure the view controller’s preferredFramesPerSecond property to the desired frame rate your application requires. You can set a delegate or configure other properties on the view controller, such as whether the animation loop is automatically paused or resumed when the application moves into the background.
Subclassing Notes
Your application should subclass GLKViewController and override the viewDidLoad and viewDidUnload methods. Your viewDidLoad method should set up your context and any drawable properties and can perform other resource allocation and initialization. Similarly, your class’s viewDidUnload method should delete the drawable object and free any unneeded resources.
As an alternative to implementing a glkViewControllerUpdate: method in a delegate, your subclass can provide an update method instead. The method must have the following signature:
子類注意:
你的應用程序應該集成自 GLKViewController,并且重寫 viewDidLoad 和 viewDidUnload 方法。在 viewDidLoad 中設(shè)置視圖的上下文和初始化任何繪畫可執(zhí)行屬性志秃。同樣,在 viewDidUnload 方法中可以刪除繪制對象和釋放不必要的資源倔矾。
glkViewControllerUpdate 作為一個可供選擇實現(xiàn)的方法,在代理方法中柱锹,子類可以提供一個更新的方法代替哪自,方法必須具備以前特征: - (void)update
屬性:
preferredFramesPerSecond: 設(shè)置視圖更新內(nèi)容的速率, 默認值為 30
framesPerSecond: 視圖更新內(nèi)容實際速率, 只讀屬性
delegate: 設(shè)置代理對象
paused:bool 值,設(shè)置暫停
pauseOnWillResignActive: bool 值禁熏,表示當控制器掛起狀態(tài)時是否自動暫停渲染循環(huán)壤巷,默認 YES
resumeOnDidBecomeActive: bool 值,表示當控制器激活時是否自動恢復渲染循環(huán)瞧毙,默認 YES
獲取關(guān)于視圖更新的信息(可讀屬性)
framesDisplayed:視圖控制器自創(chuàng)建以來已發(fā)送的幀更新數(shù)胧华。
timeSinceFirstResume:自第一次視圖控制器恢復發(fā)送更新事件以來所經(jīng)過的時間量。
timeSinceLastResume:自上一次視圖控制器恢復發(fā)送更新事件以來所經(jīng)過的時間量宙彪。
timeSinceLastUpdate:自從上次視圖控制器調(diào)用代理方法 glkviewcontrollerupdate 經(jīng)過的時間量
timeSinceLastDraw:自上次視圖控制器調(diào)用視圖的 display 方法以來所經(jīng)過的時間量矩动。
** OpenGL ES 渲染視圖 **
GLKView:使用 opengl 繪制內(nèi)容的默認實現(xiàn)視圖
剩下兩個代理屬性:GLKViewDelegate 和 GLKViewControllerDelegate
關(guān)于 GLKViewController 就說這么多,接下來的 OpenGL ES 學習我們都是基于 GLKViewController 環(huán)境實現(xiàn)的您访,因為蘋果的封裝我們可以省掉一些基本的配置铅忿,比如生成默認的 FrameBufferObject
開始項目:
在新建工程完畢,我們需要做幾處修改灵汪。首先導入 GLKit,修改當前控制器集成自 GLKViewController柑潦,如下:
然后修改 Main.storyboard 中控制器 view 的 class 為 GLKView享言,如下
這里我們了解一下 OpenGL 的渲染流程:
最開始的輸入是頂點數(shù)據(jù)。比如三角形渗鬼,就是三個點览露。每個頂點數(shù)據(jù)可以包含任意數(shù)量的信息,最基本的有位置譬胎,顏色差牛。后面介紹貼圖時還會包含 UV 信息。經(jīng)過各種處理堰乔,最終放入 FrameBuffer偏化。
第一步定義頂點坐標數(shù)據(jù):
// 定義C結(jié)構(gòu)體用來保存GLKVector3類型的成員position
typedef struct {
GLKVector3 position;
} Vertex;
// C數(shù)組存儲三角形頂點坐標,每個GLKVector3變量代表一個坐標點的X镐侯、Y和Z
static const Vertex vertices[] = {
{{ 0.0, 0.5, 0.0}},
{{ -0.5, -0.5, 0.0}},
{{ 0.5, -0.5, 0.0}},
};
第二步初始化和設(shè)置當前視圖的 EAGLContext 對象:
// 將當前view強制轉(zhuǎn)換為GLKView
GLKView *view = (GLKView *)self.view;
// 斷言,檢測用storyboard加載的視圖是否是GLKView
NSAssert([view isKindOfClass:[GLKView class]], @"View controller's View is not a GLKView");
// 初始化context為OpenGL ES 2.0
view.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
// 在任何其他的OpenGL ES配置或者渲染發(fā)生前侦讨,應用的GLKView實例的上下文屬性都需要設(shè)置為當前
[EAGLContext setCurrentContext:view.context];
還需要出實話baseEffect屬性,
/*
GLKBaseEffect 是GLKit提供的另一個內(nèi)建類。GLKBaseEffect的存在是為了簡化OpenGL ES的很多常用操作韵卤。
在OpenGL ES 2.0中骗污,如果沒有GLKit和GLKBaseEffect類,完成這個簡單的例子需要用著色器語言編寫一個GPU程序沈条。
*/
@property (nonatomic, strong) GLKBaseEffect *baseEffect;
self.baseEffect = [[GLKBaseEffect alloc] init];
self.baseEffect.useConstantColor = GL_TRUE;
self.baseEffect.constantColor = GLKVector4Make(1.0, // red
0.0, // green
0.0, // blue
0.0); // alpha
// 設(shè)置當前OpenGL ES的上下文"清除顏色"需忿,用于在上下文的幀緩存被清除時初始化每個想色的顏色值
glClearColor(0.0, 0.0, 0.0, 1.0);
GLKBaseEffect 類提供了不依賴所使用的 OpenGL ES 版本的控制 OpenGL ES 渲染方法,會在需要的時候自動地構(gòu)建 GPU 程序并極大地簡化本例蜡歹∥堇澹控制渲染像素顏色的方式有很多中,這個應用的 GLKBaseEffect 使用恒定紅色來渲染三角形季稳,所以三角形中的每一個像素都是相同的顏色擅这。上面的 constantColor 定義為 C 數(shù)據(jù)結(jié)構(gòu)體 GLKVector4Make 設(shè)置的 4 個顏色元素值。
// 聲明GLuint類型變量景鼠,用于存放本例中頂點數(shù)據(jù)的緩存標識符
GLuint vertexBufferID;
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);