我們先了解一下 iOS 中管理 OpenGL ES 渲染循環的視圖控制器 - 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 中設置視圖的上下文和初始化任何繪畫可執行屬性。同樣,在 viewDidUnload 方法中可以刪除繪制對象和釋放不必要的資源。
glkViewControllerUpdate 作為一個可供選擇實現的方法,在代理方法中,子類可以提供一個更新的方法代替,方法必須具備以前特征: - (void)update
屬性:
preferredFramesPerSecond: 設置視圖更新內容的速率, 默認值為 30
framesPerSecond: 視圖更新內容實際速率, 只讀屬性
delegate: 設置代理對象
paused:bool 值,設置暫停
pauseOnWillResignActive: bool 值,表示當控制器掛起狀態時是否自動暫停渲染循環,默認 YES
resumeOnDidBecomeActive: bool 值,表示當控制器激活時是否自動恢復渲染循環,默認 YES
獲取關于視圖更新的信息(可讀屬性)
framesDisplayed:視圖控制器自創建以來已發送的幀更新數。
timeSinceFirstResume:自第一次視圖控制器恢復發送更新事件以來所經過的時間量。
timeSinceLastResume:自上一次視圖控制器恢復發送更新事件以來所經過的時間量。
timeSinceLastUpdate:自從上次視圖控制器調用代理方法 glkviewcontrollerupdate 經過的時間量
timeSinceLastDraw:自上次視圖控制器調用視圖的 display 方法以來所經過的時間量。
** OpenGL ES 渲染視圖 **
GLKView:使用 opengl 繪制內容的默認實現視圖
剩下兩個代理屬性:GLKViewDelegate 和 GLKViewControllerDelegate
關于 GLKViewController 就說這么多,接下來的 OpenGL ES 學習我們都是基于 GLKViewController 環境實現的,因為蘋果的封裝我們可以省掉一些基本的配置,比如生成默認的 FrameBufferObject
開始項目:
在新建工程完畢,我們需要做幾處修改。首先導入 GLKit,修改當前控制器集成自 GLKViewController,如下:
然后修改 Main.storyboard 中控制器 view 的 class 為 GLKView,如下
這里我們了解一下 OpenGL 的渲染流程:
最開始的輸入是頂點數據。比如三角形,就是三個點。每個頂點數據可以包含任意數量的信息,最基本的有位置,顏色。后面介紹貼圖時還會包含 UV 信息。經過各種處理,最終放入 FrameBuffer。
第一步定義頂點坐標數據:
// 定義C結構體用來保存GLKVector3類型的成員position
typedef struct {
GLKVector3 position;
} Vertex;
// C數組存儲三角形頂點坐標,每個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}},
};
第二步初始化和設置當前視圖的 EAGLContext 對象:
// 將當前view強制轉換為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配置或者渲染發生前,應用的GLKView實例的上下文屬性都需要設置為當前
[EAGLContext setCurrentContext:view.context];
還需要出實話baseEffect屬性,
/*
GLKBaseEffect 是GLKit提供的另一個內建類。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
// 設置當前OpenGL ES的上下文"清除顏色",用于在上下文的幀緩存被清除時初始化每個想色的顏色值
glClearColor(0.0, 0.0, 0.0, 1.0);
GLKBaseEffect 類提供了不依賴所使用的 OpenGL ES 版本的控制 OpenGL ES 渲染方法,會在需要的時候自動地構建 GPU 程序并極大地簡化本例。控制渲染像素顏色的方式有很多中,這個應用的 GLKBaseEffect 使用恒定紅色來渲染三角形,所以三角形中的每一個像素都是相同的顏色。上面的 constantColor 定義為 C 數據結構體 GLKVector4Make 設置的 4 個顏色元素值。
// 聲明GLuint類型變量,用于存放本例中頂點數據的緩存標識符
GLuint vertexBufferID;
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);