我們先了解一下 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:
子類注意:
你的應(yīng)用程序應(yīng)該集成自 GLKViewController,并且重寫 viewDidLoad 和 viewDidUnload 方法。在 viewDidLoad 中設(shè)置視圖的上下文和初始化任何繪畫可執(zhí)行屬性。同樣,在 viewDidUnload 方法中可以刪除繪制對象和釋放不必要的資源。
glkViewControllerUpdate 作為一個(gè)可供選擇實(shí)現(xiàn)的方法,在代理方法中,子類可以提供一個(gè)更新的方法代替,方法必須具備以前特征: - (void)update
屬性:
preferredFramesPerSecond: 設(shè)置視圖更新內(nèi)容的速率, 默認(rèn)值為 30
framesPerSecond: 視圖更新內(nèi)容實(shí)際速率, 只讀屬性
delegate: 設(shè)置代理對象
paused:bool 值,設(shè)置暫停
pauseOnWillResignActive: bool 值,表示當(dāng)控制器掛起狀態(tài)時(shí)是否自動(dòng)暫停渲染循環(huán),默認(rèn) YES
resumeOnDidBecomeActive: bool 值,表示當(dāng)控制器激活時(shí)是否自動(dòng)恢復(fù)渲染循環(huán),默認(rèn) YES
獲取關(guān)于視圖更新的信息(可讀屬性)
framesDisplayed:視圖控制器自創(chuàng)建以來已發(fā)送的幀更新數(shù)。
timeSinceFirstResume:自第一次視圖控制器恢復(fù)發(fā)送更新事件以來所經(jīng)過的時(shí)間量。
timeSinceLastResume:自上一次視圖控制器恢復(fù)發(fā)送更新事件以來所經(jīng)過的時(shí)間量。
timeSinceLastUpdate:自從上次視圖控制器調(diào)用代理方法 glkviewcontrollerupdate 經(jīng)過的時(shí)間量
timeSinceLastDraw:自上次視圖控制器調(diào)用視圖的 display 方法以來所經(jīng)過的時(shí)間量。
** OpenGL ES 渲染視圖 **
GLKView:使用 opengl 繪制內(nèi)容的默認(rèn)實(shí)現(xiàn)視圖
剩下兩個(gè)代理屬性:GLKViewDelegate 和 GLKViewControllerDelegate
關(guān)于 GLKViewController 就說這么多,接下來的 OpenGL ES 學(xué)習(xí)我們都是基于 GLKViewController 環(huán)境實(shí)現(xiàn)的,因?yàn)樘O果的封裝我們可以省掉一些基本的配置,比如生成默認(rèn)的 FrameBufferObject
開始項(xiàng)目:
在新建工程完畢,我們需要做幾處修改。首先導(dǎo)入 GLKit,修改當(dāng)前控制器集成自 GLKViewController,如下:

然后修改 Main.storyboard 中控制器 view 的 class 為 GLKView,如下

這里我們了解一下 OpenGL 的渲染流程:
最開始的輸入是頂點(diǎn)數(shù)據(jù)。比如三角形,就是三個(gè)點(diǎn)。每個(gè)頂點(diǎn)數(shù)據(jù)可以包含任意數(shù)量的信息,最基本的有位置,顏色。后面介紹貼圖時(shí)還會(huì)包含 UV 信息。經(jīng)過各種處理,最終放入 FrameBuffer。
第一步定義頂點(diǎn)坐標(biāo)數(shù)據(jù):
// 定義C結(jié)構(gòu)體用來保存GLKVector3類型的成員position
typedef struct {
GLKVector3 position;
} Vertex;
// C數(shù)組存儲(chǔ)三角形頂點(diǎn)坐標(biāo),每個(gè)GLKVector3變量代表一個(gè)坐標(biāo)點(diǎn)的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è)置當(dāng)前視圖的 EAGLContext 對象:
// 將當(dāng)前view強(qiáng)制轉(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ā)生前,應(yīng)用的GLKView實(shí)例的上下文屬性都需要設(shè)置為當(dāng)前
[EAGLContext setCurrentContext:view.context];
還需要出實(shí)話baseEffect屬性,
/*
GLKBaseEffect 是GLKit提供的另一個(gè)內(nèi)建類。GLKBaseEffect的存在是為了簡化OpenGL ES的很多常用操作。
在OpenGL ES 2.0中,如果沒有GLKit和GLKBaseEffect類,完成這個(gè)簡單的例子需要用著色器語言編寫一個(gè)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è)置當(dāng)前OpenGL ES的上下文"清除顏色",用于在上下文的幀緩存被清除時(shí)初始化每個(gè)想色的顏色值
glClearColor(0.0, 0.0, 0.0, 1.0);
GLKBaseEffect 類提供了不依賴所使用的 OpenGL ES 版本的控制 OpenGL ES 渲染方法,會(huì)在需要的時(shí)候自動(dòng)地構(gòu)建 GPU 程序并極大地簡化本例。控制渲染像素顏色的方式有很多中,這個(gè)應(yīng)用的 GLKBaseEffect 使用恒定紅色來渲染三角形,所以三角形中的每一個(gè)像素都是相同的顏色。上面的 constantColor 定義為 C 數(shù)據(jù)結(jié)構(gòu)體 GLKVector4Make 設(shè)置的 4 個(gè)顏色元素值。
// 聲明GLuint類型變量,用于存放本例中頂點(diǎn)數(shù)據(jù)的緩存標(biāo)識(shí)符
GLuint vertexBufferID;
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);