iOS视觉(十) -- OpenGL ES绘制立方体

Glen_ 2020/7/29 23:04:00

一、绘制空白界面 上篇主要是显示一张简单的图片,这篇就进一步将图片换成一个立方体。 在上篇中是基于GLKViewController来创建GLKView的,这里就仅仅自定义一个GLKView添加在界面上。 首先还是需要一个GLKView界面、GLKBaseEffect着色器 @property (nonatomic, strong) …

一、绘制空白界面

上篇主要是显示一张简单的图片,这篇就进一步将图片换成一个立方体。

在上篇中是基于GLKViewController来创建GLKView的,这里就仅仅自定义一个GLKView添加在界面上。

首先还是需要一个GLKView界面、GLKBaseEffect着色器

@property (nonatomic, strong) GLKView *glkView;
@property (nonatomic, strong) GLKBaseEffect *baseEffect;
复制代码

接下来需要对GLKView进行一些处理:

  1. 配置GLKView上下文
  2. 设置颜色缓冲、深度测试类型
    //配置上下文
    EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
    if (!context) {
        NSLog(@"context init faile");
    }
    [EAGLContext setCurrentContext:context];
    
    self.glkView = [[GLKView alloc] initWithFrame:self.view.bounds context:context]; //关联上下文
    self.glkView.backgroundColor = [UIColor brownColor];//背景色
    self.glkView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;//设置颜色缓冲
    self.glkView.drawableDepthFormat = GLKViewDrawableDepthFormat24;//设置深度测试
    glDepthRangef(1, 0);//深度测试范围
    self.glkView.delegate = self;//代理
    [self.view addSubview:self.glkView];
复制代码

代理方法中需要清除一下颜色缓存与深度测试:

-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
复制代码

运行程序就可以看到一个棕色的界面。

二、配置纹理

使用熟悉的OC代码来获取一个图片数据,转化为UIImage:

    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"2.jpg"];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
复制代码

接下来就是将纹理添加到着色器上并使用光照着色器,使立方体更有立体感:

    //纹理信息
    NSDictionary *option = @{GLKTextureLoaderOriginBottomLeft : @(YES)};
    GLKTextureInfo *info = [GLKTextureLoader textureWithCGImage:image.CGImage options:option error:nil];
    
    //初始化着色器配置
    self.baseEffect = [[GLKBaseEffect alloc] init];
    self.baseEffect.texture2d0.name = info.name;
    self.baseEffect.texture2d0.target = info.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);

复制代码

三、配置顶点数据

为了使顶点数据的可读性更高,这里就使用一个结构体数组来保存顶点数据:

typedef struct {
    GLKVector3 positionCoord;   //顶点坐标
    GLKVector2 textureCoord;    //纹理坐标
    GLKVector3 normal;          //法线
} GVertex;

复制代码

并且定义一个结构体属性以及顶点缓冲区:

@property (nonatomic, assign) GVertex *vertices;
@property (nonatomic, assign) GLuint vertexBuffer;//顶点缓冲区
复制代码

设置6个面的顶点数据:

    //顶点数据
    self.vertices = malloc(sizeof(GVertex) * kCoordCount);
    
    // 前面
    self.vertices[0] = (GVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 0, 1}};
    self.vertices[1] = (GVertex){{-0.5, -0.5, 0.5}, {0, 0}, {0, 0, 1}};
    self.vertices[2] = (GVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 0, 1}};
    self.vertices[3] = (GVertex){{-0.5, -0.5, 0.5}, {0, 0}, {0, 0, 1}};
    self.vertices[4] = (GVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 0, 1}};
    self.vertices[5] = (GVertex){{0.5, -0.5, 0.5}, {1, 0}, {0, 0, 1}};
    
    // 上面
    self.vertices[6] = (GVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 1, 0}};
    self.vertices[7] = (GVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 1, 0}};
    self.vertices[8] = (GVertex){{0.5, 0.5, -0.5}, {1, 0}, {0, 1, 0}};
    self.vertices[9] = (GVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 1, 0}};
    self.vertices[10] = (GVertex){{0.5, 0.5, -0.5}, {1, 0}, {0, 1, 0}};
    self.vertices[11] = (GVertex){{-0.5, 0.5, -0.5}, {0, 0}, {0, 1, 0}};
    
    // 下面
    self.vertices[12] = (GVertex){{0.5, -0.5, 0.5}, {1, 1}, {0, -1, 0}};
    self.vertices[13] = (GVertex){{-0.5, -0.5, 0.5}, {0, 1}, {0, -1, 0}};
    self.vertices[14] = (GVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, -1, 0}};
    self.vertices[15] = (GVertex){{-0.5, -0.5, 0.5}, {0, 1}, {0, -1, 0}};
    self.vertices[16] = (GVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, -1, 0}};
    self.vertices[17] = (GVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, -1, 0}};
    
    // 左面
    self.vertices[18] = (GVertex){{-0.5, 0.5, 0.5}, {1, 1}, {-1, 0, 0}};
    self.vertices[19] = (GVertex){{-0.5, -0.5, 0.5}, {0, 1}, {-1, 0, 0}};
    self.vertices[20] = (GVertex){{-0.5, 0.5, -0.5}, {1, 0}, {-1, 0, 0}};
    self.vertices[21] = (GVertex){{-0.5, -0.5, 0.5}, {0, 1}, {-1, 0, 0}};
    self.vertices[22] = (GVertex){{-0.5, 0.5, -0.5}, {1, 0}, {-1, 0, 0}};
    self.vertices[23] = (GVertex){{-0.5, -0.5, -0.5}, {0, 0}, {-1, 0, 0}};
    
    // 右面
    self.vertices[24] = (GVertex){{0.5, 0.5, 0.5}, {1, 1}, {1, 0, 0}};
    self.vertices[25] = (GVertex){{0.5, -0.5, 0.5}, {0, 1}, {1, 0, 0}};
    self.vertices[26] = (GVertex){{0.5, 0.5, -0.5}, {1, 0}, {1, 0, 0}};
    self.vertices[27] = (GVertex){{0.5, -0.5, 0.5}, {0, 1}, {1, 0, 0}};
    self.vertices[28] = (GVertex){{0.5, 0.5, -0.5}, {1, 0}, {1, 0, 0}};
    self.vertices[29] = (GVertex){{0.5, -0.5, -0.5}, {0, 0}, {1, 0, 0}};
    
    // 后面
    self.vertices[30] = (GVertex){{-0.5, 0.5, -0.5}, {0, 1}, {0, 0, -1}};
    self.vertices[31] = (GVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, 0, -1}};
    self.vertices[32] = (GVertex){{0.5, 0.5, -0.5}, {1, 1}, {0, 0, -1}};
    self.vertices[33] = (GVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, 0, -1}};
    self.vertices[34] = (GVertex){{0.5, 0.5, -0.5}, {1, 1}, {0, 0, -1}};
    self.vertices[35] = (GVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, 0, -1}};
复制代码

下面就是重要的部分,需要将顶点数据从内存中拷贝到显存当中,为了GPU能更快的读取到数据。由于使用了顶点、纹理、光照,所以需要打开三个通道:

    //从内存拷贝入显存
    glGenBuffers(1, &_vertexBuffer);//声明顶点数据
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);//绑定顶点数据
    GLsizeiptr bufferSizeBytes = sizeof(GVertex) * kCoordCount;//顶点数据大小
    glBufferData(GL_ARRAY_BUFFER, bufferSizeBytes, self.vertices, GL_STATIC_DRAW);//存入显存
    
    //打开通道、设置数据读取方式
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GVertex), NULL + offsetof(GVertex, positionCoord));
    
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(GVertex), NULL + offsetof(GVertex, textureCoord));
    
    glEnableVertexAttribArray(GLKVertexAttribNormal);
    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(GVertex), NULL + offsetof(GVertex, normal));

复制代码

四、设置定时旋转

这里使用一个CADisplayLin定时器实现立方体每一次的角度偏转

定义一个定时器属性以及角度属性用来记录角度:

@property (nonatomic, strong) CADisplayLink *displayLink;//定时器
@property (nonatomic, assign) NSInteger angle;//角度
复制代码

每一次渲染都会讲角度旋转5度:

- (void)addCADisplayLink {
    self.angle = 0;
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)update {
    //每次旋转5度
    self.angle = (self.angle + 5) % 360;
    //着色器旋转(角度转化为弧度)
    self.baseEffect.transform.modelviewMatrix = GLKMatrix4MakeRotation(GLKMathDegreesToRadians(self.angle), 0.3, 1, 0.7);
    //开始绘制(会调用代理方法)
    [self.glkView display];
}
复制代码

开始绘制之后来到代理方法,就需要开启深度测试以及开始绘制:

-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
    glEnable(GL_DEPTH_TEST);//开启深度测试
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清空缓冲区
    [self.baseEffect prepareToDraw];//准备绘制
    glDrawArrays(GL_TRIANGLES, 0, kCoordCount);//开始绘制(三角形)
}
复制代码

最终结果如下:

随时随地学软件编程-关注百度小程序和微信小程序
关于找一找教程网

本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。
本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。
[iOS视觉(十) -- OpenGL ES绘制立方体]http://www.zyiz.net/tech/detail-145371.html

上一篇:面试遇到RunLoop的第一天-原理

下一篇:Dart 异步与多线程

赞(0)

共有 条评论 网友评论

验证码: 看不清楚?
    关注微信小程序
    程序员编程王-随时随地学编程

    扫描二维码或查找【程序员编程王】

    可以随时随地学编程啦!

    技术文章导航 更多>
    扫一扫关注最新编程教程