1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| @implementation ViewController
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //1.OpenGL ES 相关初始化 [self setUpConfig]; //2.加载顶点/纹理坐标数据 [self setUpVertexData]; //3.加载纹理数据(使用GLBaseEffect) [self setUpTexture]; }
static float rod = 0.1; - (IBAction)left:(id)sender {
// GLKMatrix4 mat = GLKMatrix4RotateX(cEffect.transform.modelviewMatrix, 0.08); // mat = GLKMatrix4RotateZ(mat, 0.05); // cEffect.transform.modelviewMatrix = mat; // [((GLKView *)self.view) display]; }
#pragma mark -- OpenGL ES setUp
-(void)setUpTexture { //1.获取纹理图片路径 NSString *filePath = [[NSBundle mainBundle]pathForResource:@"kunkun" ofType:@"jpg"]; NSString *file2Path = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"jpg"];
//2.设置纹理参数 //纹理坐标原点是左下角,但是图片显示原点应该是左上角. NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:@(1),GLKTextureLoaderOriginBottomLeft, nil]; GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithContentsOfFile:filePath options:options error:nil]; GLKTextureInfo *texture2Info = [GLKTextureLoader textureWithContentsOfFile:file2Path options:options error:nil];
//3.使用苹果GLKit 提供GLKBaseEffect 完成着色器工作(顶点/片元) cEffect = [[GLKBaseEffect alloc]init]; cEffect.texture2d0.enabled = GL_TRUE; cEffect.texture2d0.name = texture2Info.name; cEffect.texture2d1.enabled = GL_TRUE; cEffect.texture2d1.name = textureInfo.name;
}
-(void)setUpVertexData { //1.设置顶点数组(顶点坐标,纹理坐标)
GLfloat vertexData[] = { // 后面的 0.5, -0.5, 0.5f, 1.0f, 0.0f, //右下 0.5, 0.5, 0.5f, 1.0f, 1.0f, //右上 -0.5, 0.5, 0.5f, 0.0f, 1.0f, //左上 0.5, -0.5, 0.5f, 1.0f, 0.0f, //右下 -0.5, 0.5, 0.5f, 0.0f, 1.0f, //左上 -0.5, -0.5, 0.5f, 0.0f, 0.0f, //左下 // 前面的 0.5, -0.5, -0.5f, 1.0f, 0.0f, //右下 0.5, 0.5, -0.5f, 1.0f, 1.0f, //右上 -0.5, 0.5, -0.5f, 0.0f, 1.0f, //左上 0.5, -0.5, -0.5f, 1.0f, 0.0f, //右下 -0.5, 0.5, -0.5f, 0.0f, 1.0f, //左上 -0.5, -0.5, -0.5f, 0.0f, 0.0f, //左下 // 左边的 0.5, -0.5, 0.5f, 1.0f, 0.0f, //右下 0.5, 0.5, 0.5f, 1.0f, 1.0f, //右上 0.5, 0.5, -0.5f, 0.0f, 1.0f, //左上 0.5, -0.5, 0.5f, 1.0f, 0.0f, //右下 0.5, 0.5, -0.5f, 0.0f, 1.0f, //左上 0.5, -0.5, -0.5f, 0.0f, 0.0f, //左下 // 右边的 -0.5, -0.5, 0.5f, 1.0f, 0.0f, //右下 -0.5, 0.5, 0.5f, 1.0f, 1.0f, //右上 -0.5, 0.5, -0.5f, 0.0f, 1.0f, //左上 -0.5, -0.5, 0.5f, 1.0f, 0.0f, //右下 -0.5, 0.5, -0.5f, 0.0f, 1.0f, //左上 -0.5, -0.5, -0.5f, 0.0f, 0.0f, //左下 };
//2.开辟顶点缓存区 //(1).创建顶点缓存区标识符ID GLuint bufferID; glGenBuffers(1, &bufferID); //(2).绑定顶点缓存区.(明确作用) glBindBuffer(GL_ARRAY_BUFFER, bufferID); //(3).将顶点数组的数据copy到顶点缓存区中(GPU显存中) glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW); //3.打开读取通道.
//顶点坐标数据 glEnableVertexAttribArray(GLKVertexAttribPosition); glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, (GLfloat *)NULL + 0); //纹理坐标数据 glEnableVertexAttribArray(GLKVertexAttribTexCoord0); glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, (GLfloat *)NULL + 3); }
-(void)setUpConfig { //1.初始化上下文&设置当前上下文
context = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES3]; //判断context是否创建成功 if (!context) { NSLog(@"Create ES context Failed"); } //设置当前上下文 [EAGLContext setCurrentContext:context]; //2.获取GLKView & 设置context GLKView *view =(GLKView *) self.view; view.context = context;
//3.配置视图创建的渲染缓存区. view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888; view.drawableDepthFormat = GLKViewDrawableDepthFormat16; //4.设置背景颜色 glClearColor(1, 0, 0, 1.0); }
#pragma mark -- GLKViewDelegate //绘制视图的内容
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { glEnable(GL_DEPTH_TEST); //1. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); rod = rod + 0.001; GLKMatrix4 mat = GLKMatrix4RotateY(cEffect.transform.modelviewMatrix, 0.05); mat = GLKMatrix4RotateX(mat, 0.05); cEffect.transform.modelviewMatrix = mat; //2.准备绘制 [cEffect prepareToDraw]; //3.开始绘制 glDrawArrays(GL_TRIANGLES, 0, 6 * 4); }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
@end
|