OpenGL 是用於2D/3D圖形編程的一套基於C語言的統一接口. windows,Linux,Unix上都可兼容.android
OpenGL ES 是在OpenGL嵌入式設備上的版本, android/iOS 等. 在iOS平臺上OpenGL有三個版本OpenGL 1.0/2.0/3.0編程
基本原理, 能夠經過OpenGL ES驅動GPU圖形處理器 實現圖形編程.windows
在iOS上, 基於GLKit, 對OpenGL ES 的再次封裝, 輔助咱們快速的使用OpenGL ES數組
//完成一個簡單的繪製, 三角形緩存
typedef struct { GLKVector3 positonCoords; }sceneVertex; @interface GLViewController () @property(nonatomic,strong)GLKBaseEffect *baseEffect; @property(nonatomic,assign)GLuint vertexBufferID;//緩存ID屬性 @end @implementation GLViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //1.建立OpenGLE ES上下文 GLKView *view = (GLKView *)self.view; view.context = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES2]; [EAGLContext setCurrentContext:view.context]; //2.GLKBaseEffect屬性, 使咱們不須要編寫shader language(着色器)代碼就能夠簡單完成繪製 self.baseEffect = [[GLKBaseEffect alloc]init]; self.baseEffect.useConstantColor = GL_TRUE;//使用靜態顏色繪製 self.baseEffect.constantColor = GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f);//設置繪製顏色 rgba glClearColor(0.0f, 0.0f, 0.0f, 1.0f);//背景顏色 //3.定點數據 static const sceneVertex vertices[] = { {{-0.5f,-0.5f,0.0}}, {{0.5f,-0.5f,0.0}}, {{-0.5f,0.5f,0.0}}, }; //4.生成緩存,併爲緩存提供數據 glGenBuffers(1, &_vertexBufferID);//申請一個標識符 glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferID);//將標識符綁定到GL_ARRAY_BUFFER glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);//複製定點數據從CPU 到 GPU //5.代理方法 //6.dealloc 中,釋放緩存數據 } //系統給咱們回調的繪製消息,該方法會一直被調用,和display方法一致 - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect{ [self.baseEffect prepareToDraw]; //clear Frame buffer glClear(GL_COLOR_BUFFER_BIT); //開啓對應的定點緩存屬性 glEnableVertexAttribArray(GLKVertexAttribPosition); //設置緩存數據指針,從頂點數組中讀取數據 glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(sceneVertex), NULL); //繪圖 glDrawArrays(GL_TRIANGLES, 0, 3); } - (void)dealloc{ GLKView *view = (GLKView *)self.view; [EAGLContext setCurrentContext:view.context]; if (_vertexBufferID != 0) { glDeleteBuffers(1, &_vertexBufferID); _vertexBufferID = 0; } }
最終效果:ui