思维导图
渲染金字塔的思维导图已经描述的很清楚了,这里我就不做过多的赘述了。
代码
粘出主要代码,经供参考
//1、初始化上下文
- (void)setupContext
{
//初始化上下文
self.myContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
//设置GLKView的上下文
GLKView *view = (GLKView *)self.view;
view.context = self.myContext;
//设置绘制属性
view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
//设置当前上下文
[EAGLContext setCurrentContext:self.myContext];
//开启深度测试
glEnable(GL_DEPTH_TEST);
}
//绘制金字塔
- (void)renderPyramid
{
//1.顶点数据
//1.顶点数据
//前3个元素,是顶点数据xyz;中间3个元素,是顶点颜色值rgb,最后2个是纹理坐标st
GLfloat attrArr[] =
{
-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 1.0f,//左上
0.5f, 0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 1.0f, 1.0f,//右上
-0.5f, -0.5f, 0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f,//左下
0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 1.0f, 0.0f,//右下
0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.5f, 0.5f,//顶点
};
//2.绘图索引
GLuint indices[] =
{
0, 3, 2,
0, 1, 3,
0, 2, 4,
0, 4, 1,
2, 3, 4,
1, 4, 3,
};
//3.顶点个数
self.count = sizeof(indices) / sizeof(GLuint);
//4.把顶点数据放到缓存区
GLuint vertixBuffer;
glGenBuffers(1, &vertixBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertixBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(attrArr), attrArr, GL_STATIC_DRAW);
//5.把索引数据放到缓冲区
GLuint elementBuffer;
glGenBuffers(1, &elementBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
//6.使用顶点数据
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 8, (GLfloat *)NULL + 0);
//7.使用颜色数据
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 8, (GLfloat *)NULL + 3);
//8.使用纹理数据
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 8, (GLfloat *)NULL + 6);
//9.获取纹理
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cTest" ofType:@"jpg"];
NSDictionary *options = @{
GLKTextureLoaderOriginBottomLeft : @(true),
};
//10.通过GLKTextureLoader加载纹理
NSError *error = nil;
GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithContentsOfFile:filePath options:options error:&error];
if (error)
{
NSLog(@"加载纹理: %@", error.description);
return;
}
//11.初始化Effect
self.myEffect = [[GLKBaseEffect alloc] init];
self.myEffect.texture2d0.enabled = GL_TRUE;
self.myEffect.texture2d0.name = textureInfo.name;
//12.设置投射投影
GLfloat aspect = self.view.bounds.size.width / self.view.bounds.size.height;
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(90.0), aspect, 0.01, 100.0);
projectionMatrix = GLKMatrix4Scale(projectionMatrix, 1, 1, 1);
self.myEffect.transform.projectionMatrix = projectionMatrix;
//13.设置模型矩阵
GLKMatrix4 modelViewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, 0.0f, 0.0f, -2.0f);
self.myEffect.transform.modelviewMatrix = modelViewMatrix;
//14.定时器
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC, 0);
dispatch_source_set_event_handler(timer, ^{
self.xDegree = self.XB * 0.1;
self.yDegree = self.YB * 0.1;
self.zDegree = self.ZB * 0.1;
});
dispatch_resume(timer);
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(0.3f, 0.4f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[self.myEffect prepareToDraw];
//2.绘图索引
const static GLuint indices[] =
{
0, 3, 2,
0, 1, 3,
0, 2, 4,
0, 4, 1,
2, 3, 4,
1, 4, 3,
};
// glDrawElements(GL_TRIANGLES, self.count, GL_UNSIGNED_INT, (GLvoid *)indices);
glDrawElements(GL_TRIANGLES, self.count, GL_UNSIGNED_INT, NULL);
}
- (void)update
{
GLKMatrix4 modelViewMatrix = self.myEffect.transform.modelviewMatrix;
modelViewMatrix = GLKMatrix4RotateX(modelViewMatrix, self.xDegree);
modelViewMatrix = GLKMatrix4RotateY(modelViewMatrix, self.yDegree);
modelViewMatrix = GLKMatrix4RotateZ(modelViewMatrix, self.zDegree);
self.myEffect.transform.modelviewMatrix = modelViewMatrix;
}
Demo下载地址
点击下载