准备工作
- 首先我们使用
Xcode
新建一个Single View App
工程,在ViewController
里面导入<GLKit/GLKit.h>
库,并且需要在工程中导入一张图片,将图片渲染到正方形表面上。
- 我们首先定义一个枚举类型的结构体,用来表示正方体的顶点坐标(类型为
GLKVector3
三维坐标),纹理坐标(类型为GLKVector2
二维坐标),法线(类型为GLKVector3
三维类型),并且定义一个正方体的顶点数量的常量kCoordCount
typedef struct {
GLKVector3 positionGoord; //顶点坐标
GLKVector2 textureCoord; //纹理坐标
GLKVector3 normal; //法线
} CSVertex;
//顶点数量, 正方体有6个面,每个面有2个三角形,每个三角形有3个顶点
static NSInteger const kCoordCount = 36;
- 定义几个全局变量用来保存当前状态
@property (nonatomic, strong) GLKView *glkView;
@property (nonatomic, strong) GLKBaseEffect *baseEffect;
@property (nonatomic, assign) CSVertex *vertexes;
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic, assign) NSInteger angle;
@property (nonatomic, assign) GLuint vertexBuffer;
在
viewDidLoad
方法里面调用2个方法commonInit
和addCADisplayLink
, 这两个方法的具体实现步骤请继续看。commonInit
这个方法里面主要是OpenGL ES相关的初始化工作,先设置当前content
上下文,创建glkView并添加代理方法GLKViewDelegate
, 要开启深度缓存后将glkView添加到当前控制器的view中。接下来获取纹理图片并设置参数,具体代码如下:
- (void)commonInit {
//1.创建content
EAGLContext *content = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
//设置当前content
[EAGLContext setCurrentContext:content];
//2. 创建GLKView
self.glkView = [[GLKView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.width) context:content];
self.glkView.backgroundColor = [UIColor clearColor];
self.glkView.delegate = self;
//3.使用深度缓存
self.glkView.drawableDepthFormat = GLKViewDrawableDepthFormat24;
//默认是(0,1),这里用于翻转Z轴,使正方形朝屏幕外面
glDepthRangef(1, 0);
//4.将GLKView添加到view上
[self.view addSubview:self.glkView];
//5.获取纹理图片
NSString *imagePath = [[NSBundle mainBundle]pathForResource:@"timg" ofType:@"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)
self.vertexes = malloc(sizeof(CSVertex) * kCoordCount);
//前面
// 前面
self.vertexes[0] = (CSVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 0, 1}};
self.vertexes[1] = (CSVertex){{-0.5, -0.5, 0.5}, {0, 0}, {0, 0, 1}};
self.vertexes[2] = (CSVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 0, 1}};
self.vertexes[3] = (CSVertex){{-0.5, -0.5, 0.5}, {0, 0}, {0, 0, 1}};
self.vertexes[4] = (CSVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 0, 1}};
self.vertexes[5] = (CSVertex){{0.5, -0.5, 0.5}, {1, 0}, {0, 0, 1}};
// 上面
self.vertexes[6] = (CSVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 1, 0}};
self.vertexes[7] = (CSVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 1, 0}};
self.vertexes[8] = (CSVertex){{0.5, 0.5, -0.5}, {1, 0}, {0, 1, 0}};
self.vertexes[9] = (CSVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 1, 0}};
self.vertexes[10] = (CSVertex){{0.5, 0.5, -0.5}, {1, 0}, {0, 1, 0}};
self.vertexes[11] = (CSVertex){{-0.5, 0.5, -0.5}, {0, 0}, {0, 1, 0}};
// 下面
self.vertexes[12] = (CSVertex){{0.5, -0.5, 0.5}, {1, 1}, {0, -1, 0}};
self.vertexes[13] = (CSVertex){{-0.5, -0.5, 0.5}, {0, 1}, {0, -1, 0}};
self.vertexes[14] = (CSVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, -1, 0}};
self.vertexes[15] = (CSVertex){{-0.5, -0.5, 0.5}, {0, 1}, {0, -1, 0}};
self.vertexes[16] = (CSVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, -1, 0}};
self.vertexes[17] = (CSVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, -1, 0}};
// 左面
self.vertexes[18] = (CSVertex){{-0.5, 0.5, 0.5}, {1, 1}, {-1, 0, 0}};
self.vertexes[19] = (CSVertex){{-0.5, -0.5, 0.5}, {0, 1}, {-1, 0, 0}};
self.vertexes[20] = (CSVertex){{-0.5, 0.5, -0.5}, {1, 0}, {-1, 0, 0}};
self.vertexes[21] = (CSVertex){{-0.5, -0.5, 0.5}, {0, 1}, {-1, 0, 0}};
self.vertexes[22] = (CSVertex){{-0.5, 0.5, -0.5}, {1, 0}, {-1, 0, 0}};
self.vertexes[23] = (CSVertex){{-0.5, -0.5, -0.5}, {0, 0}, {-1, 0, 0}};
// 右面
self.vertexes[24] = (CSVertex){{0.5, 0.5, 0.5}, {1, 1}, {1, 0, 0}};
self.vertexes[25] = (CSVertex){{0.5, -0.5, 0.5}, {0, 1}, {1, 0, 0}};
self.vertexes[26] = (CSVertex){{0.5, 0.5, -0.5}, {1, 0}, {1, 0, 0}};
self.vertexes[27] = (CSVertex){{0.5, -0.5, 0.5}, {0, 1}, {1, 0, 0}};
self.vertexes[28] = (CSVertex){{0.5, 0.5, -0.5}, {1, 0}, {1, 0, 0}};
self.vertexes[29] = (CSVertex){{0.5, -0.5, -0.5}, {0, 0}, {1, 0, 0}};
// 后面
self.vertexes[30] = (CSVertex){{-0.5, 0.5, -0.5}, {0, 1}, {0, 0, -1}};
self.vertexes[31] = (CSVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, 0, -1}};
self.vertexes[32] = (CSVertex){{0.5, 0.5, -0.5}, {1, 1}, {0, 0, -1}};
self.vertexes[33] = (CSVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, 0, -1}};
self.vertexes[34] = (CSVertex){{0.5, 0.5, -0.5}, {1, 1}, {0, 0, -1}};
self.vertexes[35] = (CSVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, 0, -1}};
//开辟顶点缓存区
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
GLsizeiptr bufferSizeBytes = sizeof(CSVertex) * kCoordCount;
glBufferData(GL_ARRAY_BUFFER, bufferSizeBytes, self.vertexes, GL_STATIC_DRAW);
//顶点数据
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(CSVertex), NULL + offsetof(CSVertex, positionGoord));
//纹理坐标
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(CSVertex), NULL + offsetof(CSVertex, textureCoord));
//法线数据
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(CSVertex), NULL + offsetof(CSVertex, normal));
}
-
addCADisplayLink
方法里面主要是添加一个周期性调用的定时器,我们使用的是CADisplayLink
来计时,这个计时器属于QuartzCore.framework
中, 具体可以参考该篇博客 (https://www.cnblogs.com/panyangjun/p/4421904.html)
- (void)addCADisplayLink {
//定时器提供一个周期性调用
self.angle = 0;
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
- 定时器计时的方法实现
update
,主要是根据时间变化,实时改变正方体旋转角度
- (void)update {
//1.计算旋转度数
self.angle = (self.angle + 5) % 360;
//2. 修改baseEffect
self.baseEffect.transform.modelviewMatrix = GLKMatrix4MakeRotation(GLKMathDegreesToRadians(self.angle), 0.3, 1, 0.7);
//3.重新渲染
[self.glkView display];
}
- 我们要实现
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);
}
- 最后一步我们要在
dealloc
函数里面释放顶点缓存区,计时器要失效处理
- (void)dealloc
{
if ([EAGLContext currentContext] == self.glkView.context) {
[EAGLContext setCurrentContext:nil];
}
if (_vertexes) {
free(_vertexes);
_vertexes = nil;
}
if (_vertexBuffer) {
glDeleteBuffers(1, &_vertexBuffer);
_vertexBuffer = 0;
}
[self.displayLink invalidate];
}
- 到此,我们使用
GLKit
已经完成了对一个正方体的绘制和旋转,具体实现效果请看下面gif图片,完整的demo请戳这里CubeImageDemo