-
在ViewController中定义相关属性
typedef struct { GLKVector3 positionCoord; GLKVector3 textureCoord; } HYVertex; static GLint const kCoordCount = 24; static GLint const kFaceCount = 6; static GLint const kFaceVertexCount = 4; @interface CubeViewController ()<GLKViewDelegate> { HYVertex *vertices; GLuint vertexBuffer; GLint angle; } @property (nonatomic, strong) GLKView *glkView; @property (nonatomic, strong) GLKBaseEffect *baseEffect; @property (nonatomic, strong) CADisplayLink *displayLink; @end
-
初始化EAGLContext
EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]; [EAGLContext setCurrentContext:context];
-
添加GLKView
self.glkView = [[GLKView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.width) context:context]; self.glkView.delegate = self; self.glkView.backgroundColor = [UIColor clearColor]; [self.view addSubview:self.glkView];
-
设置深度缓存区和颜色缓存区格式
self.glkView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888; self.glkView.drawableDepthFormat = GLKViewDrawableDepthFormat24;
-
初始化顶点数据
// 开辟顶点数据空间 vertices = malloc(sizeof(HYVertex) * kCoordCount); // 前面 vertices[0] = (HYVertex){{0.5, -0.5, 0.5}, {1, 0}, {0, 0, 1}}; vertices[1] = (HYVertex){{-0.5, -0.5, 0.5}, {0, 0}, {0, 0, 1}}; vertices[2] = (HYVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 0, 1}}; vertices[3] = (HYVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 0, 1}}; // 后面 vertices[4] = (HYVertex){{-0.5, 0.5, -0.5}, {0, 1}, {0, 0, -1}}; vertices[5] = (HYVertex){{0.5, 0.5, -0.5}, {1, 1}, {0, 0, -1}}; vertices[6] = (HYVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, 0, -1}}; vertices[7] = (HYVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, 0, -1}}; // 上面 vertices[8] = (HYVertex){{0.5, 0.5, 0.5}, {1, 0}, {0, 1, 0}}; vertices[9] = (HYVertex){{-0.5, 0.5, 0.5}, {0, 0}, {0, 1, 0}}; vertices[10] = (HYVertex){{0.5, 0.5, -0.5}, {1, 1}, {0, 1, 0}}; vertices[11] = (HYVertex){{-0.5, 0.5, -0.5}, {0, 1}, {0, 1, 0}}; // 下面 vertices[12] = (HYVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, -1, 0}}; vertices[13] = (HYVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, -1, 0}}; vertices[14] = (HYVertex){{-0.5, -0.5, 0.5}, {0, 1}, {0, -1, 0}}; vertices[15] = (HYVertex){{0.5, -0.5, 0.5}, {1, 1}, {0, -1, 0}}; // 左面 vertices[16] = (HYVertex){{-0.5, -0.5, -0.5}, {0, 0}, {-1, 0, 0}}; vertices[17] = (HYVertex){{-0.5, -0.5, 0.5}, {1, 0}, {-1, 0, 0}}; vertices[18] = (HYVertex){{-0.5, 0.5, -0.5}, {0, 1}, {-1, 0, 0}}; vertices[19] = (HYVertex){{-0.5, 0.5, 0.5}, {1, 1}, {-1, 0, 0}}; // 右面 vertices[20] = (HYVertex){{0.5, 0.5, -0.5}, {1, 1}, {1, 0, 0}}; vertices[21] = (HYVertex){{0.5, 0.5, 0.5}, {0, 1}, {1, 0, 0}}; vertices[22] = (HYVertex){{0.5, -0.5, -0.5}, {1, 0}, {1, 0, 0}}; vertices[23] = (HYVertex){{0.5, -0.5, 0.5}, {0, 0}, {1, 0, 0}};
-
开辟顶点缓存区(VBO)
glGenBuffers(1, &vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(HYVertex) * kCoordCount, vertices, GL_STATIC_DRAW);
-
开启顶点、纹理坐标属性通道,设置顶点、纹理坐标的读取方式
glEnableVertexAttribArray(GLKVertexAttribPosition); glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(HYVertex), NULL + offsetof(HYVertex, positionCoord)); glEnableVertexAttribArray(GLKVertexAttribTexCoord0); glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(HYVertex), NULL + offsetof(HYVertex, textureCoord));
-
载入纹理 & 创建GLKBaseEffect
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"2.jpg"]; GLKTextureInfo *tetureInfo = [GLKTextureLoader textureWithContentsOfFile:path options:@{GLKTextureLoaderOriginBottomLeft : @(GL_TRUE)} error:nil]; self.baseEffect = [[GLKBaseEffect alloc] init]; self.baseEffect.texture2d0.name = tetureInfo.name; self.baseEffect.texture2d0.target = tetureInfo.target;
-
实现GLKViewDelegate方法进行图形绘制
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { glEnable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); [self.baseEffect prepareToDraw]; for (GLint i = 0; i < kFaceCount; i++) { glDrawArrays(GL_TRIANGLE_STRIP, i * kFaceVertexCount, kFaceVertexCount); } }
-
添加CADisplayLink让立方体自动旋转
angle = 0; self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)]; [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
-
重新绘制
- (void)update { angle = (angle + 5) % 360; self.baseEffect.transform.modelviewMatrix = GLKMatrix4MakeRotation(GLKMathDegreesToRadians(angle), 0.3, 0.7, 0.2); [self.glkView display]; }
GLKit绘制立方体
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 采用顶点缓冲,所谓顶点缓冲就是直接将顶点数据存储在GPU的一段缓冲区,不需要从CPU拷贝到GPU。提高了程序的运行...
- 前言: 这篇文章是作者iOS开发之OpenGL ES系列文章的第二篇,将结合一个使用OpenGL ES渲染一个三角...
- iOS OpenGL ES 学习笔记(一) 介绍了OpenGL的基本工作流程,知道了这些基础知识,就可以开始写De...
- 标准化设备坐标(Normalized Device Coordinates, NDC) 图片来自:http://l...