准备工作
- 引入#import <GLKit/GLKit.h> 头文件
#import <GLKit/GLKit.h>
- 声明顶点数据结构
- 属性设置
#import <GLKit/GLKit.h>
#import "dnCubeViewController.h"
typedef struct {
GLKVector3 positionCoord; //顶点坐标
GLKVector2 textureCoord; //纹理坐标
GLKVector3 normal; //法线
} DNCubeVertex;
// 正方体有36个顶点数
static NSInteger const kCoordCount = 36;
@interface dnCubeViewController () <GLKViewDelegate>
@property (nonatomic, assign) DNCubeVertex * vertices;//顶点数据
@property (nonatomic, strong) GLKView * glkView;//GLKit封装的view
@property (nonatomic, strong) GLKBaseEffect * baseEffect;//设置渲染的一些配置
@property (nonatomic, assign) GLuint vertexBuffer;//顶点缓存
@property (nonatomic, assign) NSInteger angle; //旋转角度
@property (nonatomic, strong) CADisplayLink * displayLink;//定时器更angle重新绘制实现旋转效果
@end
懒加载vertices顶点数据
- (DNCubeVertex*)vertices{
if (!_vertices) {
_vertices = malloc(sizeof(DNCubeVertex) * kCoordCount);
// 前面
_vertices[0] = (DNCubeVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 0, 1}};
_vertices[1] = (DNCubeVertex){{-0.5, -0.5, 0.5}, {0, 0}, {0, 0, 1}};
_vertices[2] = (DNCubeVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 0, 1}};
_vertices[3] = (DNCubeVertex){{-0.5, -0.5, 0.5}, {0, 0}, {0, 0, 1}};
_vertices[4] = (DNCubeVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 0, 1}};
_vertices[5] = (DNCubeVertex){{0.5, -0.5, 0.5}, {1, 0}, {0, 0, 1}};
// 上面
_vertices[6] = (DNCubeVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 1, 0}};
_vertices[7] = (DNCubeVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 1, 0}};
_vertices[8] = (DNCubeVertex){{0.5, 0.5, -0.5}, {1, 0}, {0, 1, 0}};
_vertices[9] = (DNCubeVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 1, 0}};
_vertices[10] = (DNCubeVertex){{0.5, 0.5, -0.5}, {1, 0}, {0, 1, 0}};
_vertices[11] = (DNCubeVertex){{-0.5, 0.5, -0.5}, {0, 0}, {0, 1, 0}};
// 下面
_vertices[12] = (DNCubeVertex){{0.5, -0.5, 0.5}, {1, 1}, {0, -1, 0}};
_vertices[13] = (DNCubeVertex){{-0.5, -0.5, 0.5}, {0, 1}, {0, -1, 0}};
_vertices[14] = (DNCubeVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, -1, 0}};
_vertices[15] = (DNCubeVertex){{-0.5, -0.5, 0.5}, {0, 1}, {0, -1, 0}};
_vertices[16] = (DNCubeVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, -1, 0}};
_vertices[17] = (DNCubeVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, -1, 0}};
// 左面
_vertices[18] = (DNCubeVertex){{-0.5, 0.5, 0.5}, {1, 1}, {-1, 0, 0}};
_vertices[19] = (DNCubeVertex){{-0.5, -0.5, 0.5}, {0, 1}, {-1, 0, 0}};
_vertices[20] = (DNCubeVertex){{-0.5, 0.5, -0.5}, {1, 0}, {-1, 0, 0}};
_vertices[21] = (DNCubeVertex){{-0.5, -0.5, 0.5}, {0, 1}, {-1, 0, 0}};
_vertices[22] = (DNCubeVertex){{-0.5, 0.5, -0.5}, {1, 0}, {-1, 0, 0}};
_vertices[23] = (DNCubeVertex){{-0.5, -0.5, -0.5}, {0, 0}, {-1, 0, 0}};
// 右面
_vertices[24] = (DNCubeVertex){{0.5, 0.5, 0.5}, {1, 1}, {1, 0, 0}};
_vertices[25] = (DNCubeVertex){{0.5, -0.5, 0.5}, {0, 1}, {1, 0, 0}};
_vertices[26] = (DNCubeVertex){{0.5, 0.5, -0.5}, {1, 0}, {1, 0, 0}};
_vertices[27] = (DNCubeVertex){{0.5, -0.5, 0.5}, {0, 1}, {1, 0, 0}};
_vertices[28] = (DNCubeVertex){{0.5, 0.5, -0.5}, {1, 0}, {1, 0, 0}};
_vertices[29] = (DNCubeVertex){{0.5, -0.5, -0.5}, {0, 0}, {1, 0, 0}};
// 后面
_vertices[30] = (DNCubeVertex){{-0.5, 0.5, -0.5}, {0, 1}, {0, 0, -1}};
_vertices[31] = (DNCubeVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, 0, -1}};
_vertices[32] = (DNCubeVertex){{0.5, 0.5, -0.5}, {1, 1}, {0, 0, -1}};
_vertices[33] = (DNCubeVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, 0, -1}};
_vertices[34] = (DNCubeVertex){{0.5, 0.5, -0.5}, {1, 1}, {0, 0, -1}};
_vertices[35] = (DNCubeVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, 0, -1}};
}
return _vertices;
}
commonInit函数 环境设置
获取当前contenxt GLKBaseEffect 设置 开辟缓存区与绑定 纹理载入
- (void)commonInit {
//1.创建context
EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
//设置当前context
[EAGLContext setCurrentContext:context];
//2.创建GLKView并设置代理
CGRect frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.width);
self.glkView = [[GLKView alloc] initWithFrame:frame context:context];
self.glkView.backgroundColor = [UIColor clearColor];
self.glkView.delegate = self;
//3.使用深度缓存
self.glkView.drawableDepthFormat = GLKViewDrawableDepthFormat24;
//默认是(0, 1),这里用于翻转 z 轴,使正方形朝屏幕外
glDepthRangef(1, 0);
//4.将GLKView 添加self.view 上
[self.view addSubview:self.glkView];
//5.获取纹理图片
NSString *imagePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"kunkun.jpg"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
//6.设置纹理参数
NSDictionary *options = @{GLKTextureLoaderOriginBottomLeft : @(YES)};
GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithCGImage:[image CGImage]
options:options
error:NULL];
//7.使用baseEffect
self.baseEffect = [[GLKBaseEffect alloc] init];
self.baseEffect.texture2d0.name = textureInfo.name;
self.baseEffect.texture2d0.target = textureInfo.target;
//开启光照效果
self.baseEffect.light0.enabled = YES;
//漫反射颜色
self.baseEffect.light0.diffuseColor = GLKVector4Make(1, 1, 1, 1);
//光源位置
self.baseEffect.light0.position = GLKVector4Make(-0.5, -0.5, 5, 1);
/*
解释一下:
这里我们不复用顶点,使用每 3 个点画一个三角形的方式,需要 12 个三角形,则需要 36 个顶点
以下的数据用来绘制以(0,0,0)为中心,边长为 1 的立方体
*/
//8. 开辟顶点数据空间(数据结构SenceVertex 大小 * 顶点个数kCoordCount)
//开辟顶点缓存区
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
GLsizeiptr bufferSizeBytes = sizeof(DNCubeVertex) * kCoordCount;
glBufferData(GL_ARRAY_BUFFER, bufferSizeBytes, self.vertices, GL_STATIC_DRAW);
//顶点数据
glEnableVertexAttribArray(GLKVertexAttribPosition);
NSLog(@"offsetof(DNCubeVertex, positionCoord): %lu",offsetof(DNCubeVertex, positionCoord));
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(DNCubeVertex), NULL + offsetof(DNCubeVertex, positionCoord));
//纹理数据
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(DNCubeVertex), NULL + offsetof(DNCubeVertex, textureCoord));
NSLog(@"offsetof(DNCubeVertex, textureCoord): %lu",offsetof(DNCubeVertex, textureCoord));
//法线数据
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(DNCubeVertex), NULL + offsetof(DNCubeVertex, normal));
NSLog(@"offsetof(DNCubeVertex, normal): %lu",offsetof(DNCubeVertex, normal));
}
添加定时器
-(void) addCADisplayLink{
//CADisplayLink 类似定时器,提供一个周期性调用.属于QuartzCore.framework中.
//具体可以参考该博客 https://www.cnblogs.com/panyangjun/p/4421904.html
self.angle = 0;
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
更新角度
#pragma mark - update
- (void)update {
//1.计算旋转度数
self.angle = (self.angle + 5) % 360;
//2.修改baseEffect.transform.modelviewMatrix
self.baseEffect.transform.modelviewMatrix = GLKMatrix4MakeRotation(GLKMathDegreesToRadians(self.angle), 0.3, 1, 0.7);
//3.重新渲染
[self.glkView display];
}
GLKView代理方法实现
#pragma mark - GLKViewDelegate
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
//1.开启深度测试
glEnable(GL_DEPTH_TEST);
//2.清除颜色缓存区&深度缓存区
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//3.准备绘制
[self.baseEffect prepareToDraw];
//4.绘图
glDrawArrays(GL_TRIANGLES, 0, kCoordCount);
}
viewDidLoad 函数
- (void)viewDidLoad {
[super viewDidLoad];
//1.View背景色
self.view.backgroundColor = [UIColor blackColor];
//2. OpenGL ES 相关初始化
[self commonInit];
//3. 添加CADisplayLink
[self addCADisplayLink];
}
dealloc 函数
- (void)dealloc {
if ([EAGLContext currentContext] == self.glkView.context) {
[EAGLContext setCurrentContext:nil];
}
if (_vertices) {
free(_vertices);
_vertices = nil;
}
if (_vertexBuffer) {
glDeleteBuffers(1, &_vertexBuffer);
_vertexBuffer = 0;
}
//displayLink 失效
[self.displayLink invalidate];
}