[OpenGL ES _ 入門_01](http://www.reibang.com/p/f66906b27819)
[OpenGL ES _ 入門_02](http://www.reibang.com/p/dc49c946438e)
[OpenGL ES _ 入門_03](http://www.reibang.com/p/00b5be729e4b)
[OpenGL ES _ 入門_04](http://www.reibang.com/p/516b0b8be9d8)
[OpenGL ES _ 入門_05](http://www.reibang.com/p/08c51c298d47)
[OpenGL ES _ 入門練習_01](http://www.reibang.com/p/eebaf64e3e0a)
[OpenGL ES _ 入門練習_02](http://www.reibang.com/p/6155d60dab20)
[OpenGL ES _ 入門練習_03](http://www.reibang.com/p/36d9dac03345)
[OpenGL ES _ 入門練習_04](http://www.reibang.com/p/1ca30e9387dd)
[OpenGL ES _ 入門練習_05](http://www.reibang.com/p/ac9375962f34)
[OpenGL ES _ 入門練習_06](http://www.reibang.com/p/c63dc219f7a0)
[OpenGL ES _ 著色器 _ 介紹](http://www.reibang.com/p/309d489bc344)
[OpenGL ES _ 著色器 _ 程序](http://www.reibang.com/p/ed0c617bcd67)
[OpenGL ES _ 著色器 _ 語法](http://www.reibang.com/p/c5b89b294995)
[OpenGL ES_著色器_紋理圖像](http://www.reibang.com/p/8c0ad4e3e40f)
[OpenGL ES_著色器_預處理](http://www.reibang.com/p/5e9837b0b219)
[OpenGL ES_著色器_頂點著色器詳解](http://www.reibang.com/p/9d7dca6b70c7)
[OpenGL ES_著色器_片斷著色器詳解](http://www.reibang.com/p/55461927c419 )
[OpenGL ES_著色器_實戰(zhàn)01](http://www.reibang.com/p/18d6b37363c8)
[OpenGL ES_著色器_實戰(zhàn)02](http://www.reibang.com/p/45d959c8f1db)
[OpenGL ES_著色器_實戰(zhàn)03](http://www.reibang.com/p/0f740901da59)
演示效果:
學習目標:繪制一個旋轉移動的立方體
技術: OpenGL ES 1
實現思路:
第一步: 創(chuàng)建GLKViewController 控制器(在里面實現方法)
第二步: 創(chuàng)建EAGContext 跟蹤所有狀態(tài),命令和資源
第三步: 清除命令
第四步: 創(chuàng)建投影坐標系
第五步: 創(chuàng)建對象坐標
第六步: 導入頂點數據
第七步: 導入顏色數據
第八步: 繪制
代碼部分:
/**
*? 創(chuàng)建EAGContext 跟蹤所有狀態(tài),命令和資源
*/
- (void)createEagContext{
self.eagContext = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES1];
[EAGLContext setCurrentContext:self.eagContext];
}
/**
*? 配置view
*/
- (void)configure{
GLKView *view = (GLKView*)self.view;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
view.context = self.eagContext;
}
/**
*? 清除
*/
-(void)clear{
glEnable(GL_DEPTH_TEST);
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
}
/**
*? 創(chuàng)建投影坐標
*/
- (void)initProjectionMatrix{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}
/**
*? 創(chuàng)建物體坐標
*/
-(void)initModelViewMatrix{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
static GLfloat transY = 0.0;
static GLfloat z=-2.0;
//1
static GLfloat spinX=0;
static GLfloat spinY=0;
glTranslatef(0.0, (GLfloat)(sinf(transY)/2.0), z);
glRotatef(spinY, 0.0, 1.0, 0.0);
glRotatef(spinX, 1.0, 0.0, 0.0);
transY += 0.075f;
spinY+=.25;
spinX+=.25;
}
/**
*? 導出頂點坐標
*? glVertexPointer 第一個參數:每個頂點數據的個數,第二個參數,頂點數據的數據類型,第三個偏移量挠说,第四個頂點數組地址
*/
- (void)loadVertexData{
glVertexPointer(3, GL_FLOAT, 0, cubeVertices);
glEnableClientState(GL_VERTEX_ARRAY);
}
/**
*? 導入顏色數據
*/
- (void)loadColorBuffer{
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeColors);
glEnableClientState(GL_COLOR_ARRAY);
}
/**
*? 導入索引數據
*/
-(void)draw{
// 開啟剔除面功能
glEnable(GL_CULL_FACE);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //3
glCullFace(GL_BACK); // 剔除背面
glDrawElements( GL_TRIANGLE_FAN, 18, GL_UNSIGNED_BYTE, tfan1);
glDrawElements( GL_TRIANGLE_FAN, 18, GL_UNSIGNED_BYTE, tfan2);
}
/**
*? 設置窗口及投影坐標的位置
*/
-(void)setClipping
{
float aspectRatio;
const float zNear = .1;
const float zFar = 1000;
const float fieldOfView = 60.0;
GLfloat? ? size;
CGRect frame = [[UIScreen mainScreen] bounds];
aspectRatio=(float)frame.size.width/(float)frame.size.height;
[self initProjectionMatrix];
size = zNear * tanf(GLKMathDegreesToRadians (fieldOfView) / 2.0);
// 設置視圖窗口的大小 和 坐標系統
glFrustumf(-size, size, -size /aspectRatio, size /aspectRatio, zNear, zFar);
glViewport(0, 0, frame.size.width, frame.size.height);
}
GLKViewController 實現代碼
- (void)viewDidLoad {
[super viewDidLoad];
[self createEagContext];
[self configure];
[self setClipping];
}
-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect{
[self clear];
[self initModelViewMatrix];
[self loadVertexData];
[self loadColorBuffer];
[self draw];
}
代碼下載地址: https://github.com/XJALYN/OpenGLES_004