上一章产舞,我們使用純粹的頂點(diǎn)數(shù)據(jù)來(lái)做立方體貼圖旋轉(zhuǎn)琳钉,本章我們換一種思路。
效果
先定義一個(gè)結(jié)構(gòu)體
上一章我們用的是頂點(diǎn)數(shù)組戳表,這兒我們用結(jié)構(gòu)體桶至,把頂點(diǎn)坐標(biāo)和紋理坐標(biāo)區(qū)分開。
typedef struct {
GLKVector3 positionCoodinate; // 頂點(diǎn)坐標(biāo)
GLKMatrix2 textureCoodinate; // 紋理坐標(biāo)
GLKVector3 normal; // 法線(光照)
} MyVertex;
相關(guān)屬性設(shè)置
image.png
配置基本信息
- (void)setupConfig{
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
// 判斷是否創(chuàng)建成功
if (!self.context) {
NSLog(@"Create ES context failed");
return;
}
// 設(shè)置當(dāng)前上下文
[EAGLContext setCurrentContext:self.context];
// GLKView
CGRect frame = CGRectMake(20, 100, [UIScreen mainScreen].bounds.size.width - 20 * 2, [UIScreen mainScreen].bounds.size.height - 100 * 2);
self.glkView = [[GLKView alloc] initWithFrame:frame context:self.context];
self.glkView.delegate = self;
self.glkView.context = self.context;
self.glkView.drawableDepthFormat = GLKViewDrawableDepthFormat24;
self.glkView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
[self.view addSubview:self.glkView];
glClearColor(0.5, 0.5, 0.5, 1);
}
配置頂點(diǎn)數(shù)據(jù)
- (void)setupVertexData{
// 開辟空間
self.vetrexs = malloc(sizeof(MyVertex) * kCoodinateCount);
// 前面
self.vetrexs[0] = (MyVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 0, 1}};
self.vetrexs[1] = (MyVertex){{-0.5, -0.5, 0.5}, {0, 0}, {0, 0, 1}};
self.vetrexs[2] = (MyVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 0, 1}};
self.vetrexs[3] = (MyVertex){{-0.5, -0.5, 0.5}, {0, 0}, {0, 0, 1}};
self.vetrexs[4] = (MyVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 0, 1}};
self.vetrexs[5] = (MyVertex){{0.5, -0.5, 0.5}, {1, 0}, {0, 0, 1}};
// 上面
self.vetrexs[6] = (MyVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 1, 0}};
self.vetrexs[7] = (MyVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 1, 0}};
self.vetrexs[8] = (MyVertex){{0.5, 0.5, -0.5}, {1, 0}, {0, 1, 0}};
self.vetrexs[9] = (MyVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 1, 0}};
self.vetrexs[10] = (MyVertex){{0.5, 0.5, -0.5}, {1, 0}, {0, 1, 0}};
self.vetrexs[11] = (MyVertex){{-0.5, 0.5, -0.5}, {0, 0}, {0, 1, 0}};
// 下面
self.vetrexs[12] = (MyVertex){{0.5, -0.5, 0.5}, {1, 1}, {0, -1, 0}};
self.vetrexs[13] = (MyVertex){{-0.5, -0.5, 0.5}, {0, 1}, {0, -1, 0}};
self.vetrexs[14] = (MyVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, -1, 0}};
self.vetrexs[15] = (MyVertex){{-0.5, -0.5, 0.5}, {0, 1}, {0, -1, 0}};
self.vetrexs[16] = (MyVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, -1, 0}};
self.vetrexs[17] = (MyVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, -1, 0}};
// 左面
self.vetrexs[18] = (MyVertex){{-0.5, 0.5, 0.5}, {1, 1}, {-1, 0, 0}};
self.vetrexs[19] = (MyVertex){{-0.5, -0.5, 0.5}, {0, 1}, {-1, 0, 0}};
self.vetrexs[20] = (MyVertex){{-0.5, 0.5, -0.5}, {1, 0}, {-1, 0, 0}};
self.vetrexs[21] = (MyVertex){{-0.5, -0.5, 0.5}, {0, 1}, {-1, 0, 0}};
self.vetrexs[22] = (MyVertex){{-0.5, 0.5, -0.5}, {1, 0}, {-1, 0, 0}};
self.vetrexs[23] = (MyVertex){{-0.5, -0.5, -0.5}, {0, 0}, {-1, 0, 0}};
// 右面
self.vetrexs[24] = (MyVertex){{0.5, 0.5, 0.5}, {1, 1}, {1, 0, 0}};
self.vetrexs[25] = (MyVertex){{0.5, -0.5, 0.5}, {0, 1}, {1, 0, 0}};
self.vetrexs[26] = (MyVertex){{0.5, 0.5, -0.5}, {1, 0}, {1, 0, 0}};
self.vetrexs[27] = (MyVertex){{0.5, -0.5, 0.5}, {0, 1}, {1, 0, 0}};
self.vetrexs[28] = (MyVertex){{0.5, 0.5, -0.5}, {1, 0}, {1, 0, 0}};
self.vetrexs[29] = (MyVertex){{0.5, -0.5, -0.5}, {0, 0}, {1, 0, 0}};
// 后面
self.vetrexs[30] = (MyVertex){{-0.5, 0.5, -0.5}, {0, 1}, {0, 0, -1}};
self.vetrexs[31] = (MyVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, 0, -1}};
self.vetrexs[32] = (MyVertex){{0.5, 0.5, -0.5}, {1, 1}, {0, 0, -1}};
self.vetrexs[33] = (MyVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, 0, -1}};
self.vetrexs[34] = (MyVertex){{0.5, 0.5, -0.5}, {1, 1}, {0, 0, -1}};
self.vetrexs[35] = (MyVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, 0, -1}};
glGenBuffers(1, &_bufferID); // 開辟1個(gè)頂點(diǎn)緩沖區(qū)匾旭,所以傳入1
NSLog(@"bufferID:%d", _bufferID);
// 綁定頂點(diǎn)緩沖區(qū)
glBindBuffer(GL_ARRAY_BUFFER, _bufferID);
// 緩沖區(qū)大小
GLsizeiptr bufferSizeBytes = sizeof(MyVertex) * kCoodinateCount;
// 將頂點(diǎn)數(shù)組的數(shù)據(jù)copy到頂點(diǎn)緩沖區(qū)中(GPU顯存中)
glBufferData(GL_ARRAY_BUFFER, bufferSizeBytes, self.vetrexs, GL_STATIC_DRAW);
// 打開讀取通道
glEnableVertexAttribArray(GLKVertexAttribPosition); // 頂點(diǎn)坐標(biāo)數(shù)據(jù)
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex)/*由于是結(jié)構(gòu)體镣屹,所以步長(zhǎng)就是結(jié)構(gòu)體大小*/, NULL + offsetof(MyVertex, positionCoodinate));
glEnableVertexAttribArray(GLKVertexAttribTexCoord0); // 紋理坐標(biāo)數(shù)據(jù)
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(MyVertex), NULL + offsetof(MyVertex, textureCoodinate));
glEnableVertexAttribArray(GLKVertexAttribNormal); // 法線數(shù)據(jù)
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex), NULL + offsetof(MyVertex, normal));
}
配置紋理
- (void)setupTexture{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"];
// 初始化紋理
NSDictionary *options = @{GLKTextureLoaderOriginBottomLeft: @(1)}; // 紋理坐標(biāo)原點(diǎn)是左下角,但是圖片顯示原點(diǎn)應(yīng)該是左上角
GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithContentsOfFile:filePath options:options error:nil];
NSLog(@"textureInfo.name: %d", textureInfo.name);
// 使用蘋果`GLKit`提供的`GLKBaseEffect`完成著色器工作(頂點(diǎn)/片元)
self.baseEffect = [[GLKBaseEffect alloc] init];
self.baseEffect.texture2d0.enabled = GL_TRUE;
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); // 光源位置
// 透視投影矩陣
CGFloat aspect = fabs(self.glkView.bounds.size.width / self.glkView.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0), aspect, 0.1, 100.0);
self.baseEffect.transform.projectionMatrix = projectionMatrix;
}
添加定時(shí)器
- (void)addDisplayLink{
self.angle = 0;
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateScene)];
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
更新
- (void)updateScene{
// 角度變化
self.angle = self.angle + 2;
// 修改`baseEffect.transform.modelviewMatrix`
GLKMatrix4 modelViewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, 0, 0, -4.0);
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(_angle), 0.3, 0.5, 0.7);
self.baseEffect.transform.modelviewMatrix = modelViewMatrix;
// 重新渲染
[self.glkView display];
}
實(shí)現(xiàn)GLKViewDelegate
代理
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect{
// 開啟深度測(cè)試
glEnable(GL_DEPTH_TEST);
// 清除顏色緩沖區(qū)、深度緩沖區(qū)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 準(zhǔn)備繪制
[self.baseEffect prepareToDraw];
// 開始繪制
glDrawArrays(GL_TRIANGLES, 0, kCoodinateCount); // 從第一個(gè)開始价涝,所以是0
}
](https://upload-images.jianshu.io/upload_images/3238726-099805b126a459a0.gif?imageMogr2/auto-orient/strip)
ViewDidLoad
調(diào)用相關(guān)方法
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
[self setupConfig];
[self setupVertexData];
[self setupTexture];
[self addDisplayLink];
}
最后女蜈,不要忘了在dealloc
里面做銷毀工作
- (void)dealloc{
if ([EAGLContext currentContext] == self.glkView.context) {
[EAGLContext setCurrentContext:nil];
}
if (self.vetrexs) {
free(self.vetrexs);
self.vetrexs = nil;
}
if (_bufferID) {
glDeleteBuffers(1, &_bufferID);
_bufferID = 0;
}
[self.displayLink invalidate];
}