美文网首页iOS视觉
二十二 案例:旋转的立方体(GLKit+CoreAnimatio

二十二 案例:旋转的立方体(GLKit+CoreAnimatio

作者: 王俏 | 来源:发表于2020-09-04 16:39 被阅读0次

    GLKit实现

    效果图

    image

    流程

    image

    具体步骤

    1.准备工作

    • 定义顶点坐标,纹理坐标,法线数据结构体
    typedef struct {
        GLKVector3 positionCoord;   //顶点坐标
        GLKVector2 textureCoord;    //纹理坐标
        GLKVector3 normal;          //法线
    } CCVertex;
    
    • 定义正方体的三角形组成的顶点个数
    // 顶点数:每个面有两个三角形组成,两三角形6个顶点,共6个面,立方体顶点个数是6*6= 36
    static NSInteger const kCoordCount = 36;
    
    • 实现GLKViewDelegate
    @interface CubeViewController () <GLKViewDelegate>
    @end
    
    
    • 定义GLKView对象glkView
    //定义GLKView对象
    @property (nonatomic, strong) GLKView *glkView;
    
    • 定义GLKBaseEffect对象 baseEffect;
    //定义GLKBaseEffect对象
    @property (nonatomic, strong) GLKBaseEffect *baseEffect;
    
    • 定义数据结构体数组 vertices
    //定义数据结构体数组
    @property (nonatomic, assign) CCVertex *vertices;
    
    • 定义定时器对象 displayLink;
    //定义定时器对象
    @property (nonatomic, strong) CADisplayLink *displayLink;
    
    • 记录旋转角度变量 angle
    //记录旋转角度变量
    @property (nonatomic, assign) NSInteger angle;
    
    • 顶点缓冲区ID vertexBuffer
    //顶点缓冲区ID
    @property (nonatomic, assign) GLuint vertexBuffer;
    

    OpenGL ES相关初始化 commonInit函数

    • 1.设置View背景色
    self.view.backgroundColor = [UIColor blackColor];
    
    • 2.初始化上下文,设置当前上下文
    //创建context
         EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
        //设置当前context
        [EAGLContext setCurrentContext:context];
    
    • 3.创建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;
    
    • 4.使用深度缓存
    self.glkView.drawableDepthFormat = GLKViewDrawableDepthFormat24;
        //默认是(0, 1),这里用于翻转 z 轴,使正方形朝屏幕外
        glDepthRangef(1, 0); 
    
    
      1. 配置视图创建的渲染缓存区
    self.glkView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
        self.glkView.drawableStencilFormat = GLKViewDrawableStencilFormat8;
        self.glkView.drawableMultisample = GLKViewDrawableMultisample4X;
    
    • 6.将GLKView 添加self.view 上
    [self.view addSubview:self.glkView];
    
    • 7.加载纹理数据 setUpTexture函数
    1. 获取纹理图片路径
    NSString *imagePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"meimei.jpg"];
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    
    1. 设置纹理参数
    NSDictionary *options = @{GLKTextureLoaderOriginBottomLeft : @(YES)};
        GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithCGImage:[image CGImage] options:options error:NULL];
    
    1. 使用苹果GLKit提供GLKBaseEffect完成着色器工作
    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);
    
    • 8.加载图片顶点坐标和纹理坐标 setUpVertexData函数
    1. 设置顶点数组(顶点坐标,纹理坐标)
    //开辟顶点数据空间(数据结构SenceVertex 大小 * 顶点个数kCoordCount)
        self.vertices = malloc(sizeof(CCVertex) * kCoordCount);
        
        // 前面
        self.vertices[0] = (CCVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 0, 1}};
        self.vertices[1] = (CCVertex){{-0.5, -0.5, 0.5}, {0, 0}, {0, 0, 1}};
        self.vertices[2] = (CCVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 0, 1}};
        self.vertices[3] = (CCVertex){{-0.5, -0.5, 0.5}, {0, 0}, {0, 0, 1}};
        self.vertices[4] = (CCVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 0, 1}};
        self.vertices[5] = (CCVertex){{0.5, -0.5, 0.5}, {1, 0}, {0, 0, 1}};
        
        // 上面
        self.vertices[6] = (CCVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 1, 0}};
        self.vertices[7] = (CCVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 1, 0}};
        self.vertices[8] = (CCVertex){{0.5, 0.5, -0.5}, {1, 0}, {0, 1, 0}};
        self.vertices[9] = (CCVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 1, 0}};
        self.vertices[10] = (CCVertex){{0.5, 0.5, -0.5}, {1, 0}, {0, 1, 0}};
        self.vertices[11] = (CCVertex){{-0.5, 0.5, -0.5}, {0, 0}, {0, 1, 0}};
        
        // 下面
        self.vertices[12] = (CCVertex){{0.5, -0.5, 0.5}, {1, 1}, {0, -1, 0}};
        self.vertices[13] = (CCVertex){{-0.5, -0.5, 0.5}, {0, 1}, {0, -1, 0}};
        self.vertices[14] = (CCVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, -1, 0}};
        self.vertices[15] = (CCVertex){{-0.5, -0.5, 0.5}, {0, 1}, {0, -1, 0}};
        self.vertices[16] = (CCVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, -1, 0}};
        self.vertices[17] = (CCVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, -1, 0}};
        
        // 左面
        self.vertices[18] = (CCVertex){{-0.5, 0.5, 0.5}, {1, 1}, {-1, 0, 0}};
        self.vertices[19] = (CCVertex){{-0.5, -0.5, 0.5}, {0, 1}, {-1, 0, 0}};
        self.vertices[20] = (CCVertex){{-0.5, 0.5, -0.5}, {1, 0}, {-1, 0, 0}};
        self.vertices[21] = (CCVertex){{-0.5, -0.5, 0.5}, {0, 1}, {-1, 0, 0}};
        self.vertices[22] = (CCVertex){{-0.5, 0.5, -0.5}, {1, 0}, {-1, 0, 0}};
        self.vertices[23] = (CCVertex){{-0.5, -0.5, -0.5}, {0, 0}, {-1, 0, 0}};
        
        // 右面
        self.vertices[24] = (CCVertex){{0.5, 0.5, 0.5}, {1, 1}, {1, 0, 0}};
        self.vertices[25] = (CCVertex){{0.5, -0.5, 0.5}, {0, 1}, {1, 0, 0}};
        self.vertices[26] = (CCVertex){{0.5, 0.5, -0.5}, {1, 0}, {1, 0, 0}};
        self.vertices[27] = (CCVertex){{0.5, -0.5, 0.5}, {0, 1}, {1, 0, 0}};
        self.vertices[28] = (CCVertex){{0.5, 0.5, -0.5}, {1, 0}, {1, 0, 0}};
        self.vertices[29] = (CCVertex){{0.5, -0.5, -0.5}, {0, 0}, {1, 0, 0}};
        
        // 后面
        self.vertices[30] = (CCVertex){{-0.5, 0.5, -0.5}, {0, 1}, {0, 0, -1}};
        self.vertices[31] = (CCVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, 0, -1}};
        self.vertices[32] = (CCVertex){{0.5, 0.5, -0.5}, {1, 1}, {0, 0, -1}};
        self.vertices[33] = (CCVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, 0, -1}};
        self.vertices[34] = (CCVertex){{0.5, 0.5, -0.5}, {1, 1}, {0, 0, -1}};
        self.vertices[35] = (CCVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, 0, -1}};
    
    1. 开辟顶点缓存区
    • 创建顶点缓存区标识符ID
    glGenBuffers(1, &_vertexBuffer);
    
    • 绑定顶点缓存区
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    
    • 将顶点数组的数据copy到顶点缓存区中
    GLsizeiptr bufferSizeBytes = sizeof(CCVertex) * kCoordCount;
    glBufferData(GL_ARRAY_BUFFER, bufferSizeBytes, self.vertices, GL_STATIC_DRAW);
    
    1. 打开读取通道
    • 顶点坐标数据 GLKVertexAttribPosition
    glEnableVertexAttribArray(GLKVertexAttribPosition);glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(CCVertex), NULL + offsetof(CCVertex, positionCoord));
    
    • 纹理坐标数据 GLKVertexAttribTexCoord0
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(CCVertex), NULL + offsetof(CCVertex, textureCoord));
    
    • 法线数据 GLKVertexAttribNormal
    glEnableVertexAttribArray(GLKVertexAttribNormal);
    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(CCVertex), NULL + offsetof(CCVertex, normal));
    

    添加CADisplayLink定时器 addCADisplayLink

    • 初始化定时器,并设置回调
            //CADisplayLink 类似定时器,提供一个周期性调用.属于QuartzCore.framework中.
        //具体可以参考该博客 https://www.cnblogs.com/panyangjun/p/4421904.html
        self.angle = 0;
        self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateAngle)];
        [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    
    • 定时器回调,设置旋转后提交重新渲染
    //计算旋转度数
    self.angle = (self.angle + 5) % 360;
    
    //修改baseEffect.transform.modelviewMatrix
    self.baseEffect.transform.modelviewMatrix = GLKMatrix4MakeRotation(GLKMathDegreesToRadians(self.angle), 0.3, 1, 0.7);
    
    //重新渲染
    [self.glkView display];
    

    释放资源 dealloc

    • 当前上下文置为nil
    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];
    

    GLKit实现旋转的立方体完整代码参考:github

    CoreAnimation实现

    效果图

    image

    流程

    image

    具体步骤

    准备工作

    • IBOutlet容器View
    @property (weak, nonatomic) IBOutlet UIView *containerView;
    
    • IBOutlet 6个面
    @property (strong, nonatomic) IBOutlet UIView *view0;
    @property (strong, nonatomic) IBOutlet UIView *view1;
    @property (strong, nonatomic) IBOutlet UIView *view2;
    @property (strong, nonatomic) IBOutlet UIView *view3;
    @property (strong, nonatomic) IBOutlet UIView *view4;
    @property (strong, nonatomic) IBOutlet UIView *view5;
    
    • 6个面的数组数组faces
    @property(nonatomic,strong)NSArray *faces;
    
    • 定时器对象
    @property (nonatomic, strong) CADisplayLink *displayLink;
    
    • 记录旋转角度
    @property (nonatomic, assign) NSInteger angle;
    

    viewDidLoad

    • 添加立方体的6个面
    self.faces = @[_view0,_view1,_view2,_view3,_view4,_view5];
    
    • 将容器View先后绕X旋转45度,绕Y轴旋转45度
    /父View的layer图层
        CATransform3D perspective = CATransform3DIdentity;
        //m34影响到透视关系 参考 https://www.jianshu.com/p/3dd14cfbdc53
        perspective.m34 = -1.0 / 500.0;
        perspective = CATransform3DRotate(perspective, -M_PI_4, 1, 0, 0);
        perspective = CATransform3DRotate(perspective, -M_PI_4, 0, 1, 0);
            //特别注意:旋转时,我们需要同时作用于6个子layer,所以请留意self.animateCube.layer.sublayerTransform = transform中使用的是sublayerTransform而非transform
        self.containerView.layer.sublayerTransform = perspective;
    
    • 将6个View存储到faces数组

    1) face 1:正面---沿着Z轴的正方向平移100个单元

    CATransform3D transform = CATransform3DMakeTranslation(0, 0, 100);
        [self addFace:0 withTransform:transform];
    

    2) face 2:右边的面 --- 沿着X轴的正方向平移100个单元 然后沿着Y轴逆时针旋转90度

    transform = CATransform3DMakeTranslation(100, 0, 0);
        transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
        [self addFace:1 withTransform:transform];
    

    3) face 3:底面 --- 沿着Y轴的负方向平移100个单元 然后沿着X轴逆时针旋转90度

    transform = CATransform3DMakeTranslation(0, -100, 0);
        transform = CATransform3DRotate(transform, M_PI_2, 1, 0, 0);
        [self addFace:2 withTransform:transform];
    

    4) face 4:顶面 --- 沿着Y轴的正方向平移100个单元 然后沿着X轴顺时针旋转90度

    transform = CATransform3DMakeTranslation(0, 100, 0);
        transform = CATransform3DRotate(transform, -M_PI_2, 1, 0, 0);
        [self addFace:3 withTransform:transform];
    

    5) face 5:左边的面 --- 沿着X轴的负数方向平移100个单元 然后沿着Y轴顺时针旋转90度

    transform = CATransform3DMakeTranslation(-100, 0, 0);
        transform = CATransform3DRotate(transform, -M_PI_2, 0, 1, 0);
        [self addFace:4 withTransform:transform];
    

    6) face 6:背面 --- 沿着Z轴的负数方向平移100个单元 然后沿着Y轴顺时针旋转180度

    transform = CATransform3DMakeTranslation(0, 0, -100);
        transform = CATransform3DRotate(transform, M_PI, 0, 1, 0);
        [self addFace:5 withTransform:transform];
    
    • -addFace:withTransform:函数
    //获取face视图并将其添加到容器中
    UIView *face = self.faces[index];
    [self.containerView addSubview:face];
    
    //将face视图放在容器的中心
    CGSize containerSize = self.containerView.bounds.size;
    face.center = CGPointMake(containerSize.width / 2.0, containerSize.height / 2.0);
    
    //添加transform
    face.layer.transform = transform;
    
    • 添加定时器和回调函数
    1. 初始化定时器,并设置定时回调
    self.angle = 0;
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
    [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    
    1. 回调处理
    //计算旋转度数
    self.angle = (self.angle + 5) % 360;
    
    //将度数转化为弧度
    float deg = self.angle * (M_PI / 180);
    
    // 初始化一个单元矩阵
    CATransform3D temp = CATransform3DIdentity;
    
    //将单元矩阵旋转
    temp = CATransform3DRotate(temp, deg, 0.3, 1, 0.7);
    
    //旋转容器的子layer
    self.containerView.layer.sublayerTransform = temp;
    

    CoreAnimation实现旋转的立方体完整代码参考:github

    相关文章

      网友评论

        本文标题:二十二 案例:旋转的立方体(GLKit+CoreAnimatio

        本文链接:https://www.haomeiwen.com/subject/logwrktx.html