美文网首页
GLKit 绘制立方体旋转

GLKit 绘制立方体旋转

作者: 番茄炒西红柿啊 | 来源:发表于2020-08-07 15:04 被阅读0次

    效果图:


    代码

    #import "ViewController.h"
    #import <GLKit/GLKit.h>
    
    typedef struct {
        GLKVector3 positionCoord;   //顶点坐标
        GLKVector2 textureCoord;    //纹理坐标
        GLKVector3 normal;          //法线
    } CCVertex;
    
    
    @interface ViewController ()<GLKViewDelegate> {
        EAGLContext *_context;
        GLKBaseEffect *_effect;
    }
    @property (nonatomic, strong) GLKView   *glkView;
    @property (nonatomic, assign) CCVertex  *vertices;  /// 坐标
    @property (nonatomic, strong) CADisplayLink *displayLink;  // 旋转定时器
    @property (nonatomic, assign) NSInteger angle;  // 每次旋转的角度
    @property (nonatomic, assign) GLuint    bufferId; 
    @end
    
    @implementation ViewController
    
    - (void)update {
       
        //1.计算旋转度数
        self.angle = (self.angle + 5) % 360;
        //2.修改baseEffect.transform.modelviewMatrix
        _effect.transform.modelviewMatrix = GLKMatrix4MakeRotation(GLKMathDegreesToRadians(self.angle), 1, 1, 0);
        //3.重新渲染
        [self.glkView display];
    }
    
    - (void)dealloc {
        [EAGLContext setCurrentContext:nil];
        
        [self.displayLink invalidate];
        
        glDeleteBuffers(1, &_bufferId);
        
        free(self.vertices);
        self.vertices = nil;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        _context = [[EAGLContext alloc] initWithAPI:(kEAGLRenderingAPIOpenGLES3)];
        if (nil == _context) {return;}
        [EAGLContext setCurrentContext:_context];
        
        [self setupGLKView];
        self.glkView.context = _context;
        
        // 准备图片数据
        NSString *imgPath = [[NSBundle mainBundle] pathForResource:@"timg" ofType:@"jpeg"];
        NSDictionary *options = @{
            GLKTextureLoaderOriginBottomLeft : @(1),
        };
        GLKTextureInfo *info = [GLKTextureLoader textureWithContentsOfFile:imgPath options:options error:nil];
        if (nil == info) {return;}
        _effect = [[GLKBaseEffect alloc] init];
        _effect.texture2d0.enabled = YES;
        _effect.texture2d0.name = info.name;
        _effect.texture2d0.target = info.target;
        _effect.light0.enabled = YES;
        _effect.light0.diffuseColor = GLKVector4Make(1, 1, 1, 1);
        _effect.light0.position = GLKVector4Make(-0.5, -0.5, 5, 1);
        
        // 准备顶点数据
        [self setupVertexData];
        
        GLuint bufferId;
        glGenBuffers(1, &bufferId);
        glBindBuffer(GL_ARRAY_BUFFER, bufferId);
        glBufferData(GL_ARRAY_BUFFER, sizeof(CCVertex) * 36, self.vertices, GL_STATIC_DRAW);
        self.bufferId = bufferId;
        
        glEnableVertexAttribArray(GLKVertexAttribPosition);
        glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(CCVertex), NULL + offsetof(CCVertex, positionCoord));
        
        glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
        glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(CCVertex), NULL + offsetof(CCVertex, textureCoord));
        
        glEnableVertexAttribArray(GLKVertexAttribNormal);
        glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(CCVertex), NULL + offsetof(CCVertex, normal));
        
        glDepthRangef(1, 0);
        
        self.angle = 0;
        self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
        [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    }
    
    - (void)setupVertexData {
        self.vertices = malloc(sizeof(CCVertex) * 36); // 6 * 2 * 3
        // 前面
        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}};
    }
    
    - (void)setupGLKView {
        self.glkView = [[GLKView alloc] initWithFrame:CGRectZero context:_context];
        self.glkView.delegate = self;
        [self.view addSubview:self.glkView];
        self.glkView.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-40-[view]-40-|" options:kNilOptions metrics:nil views:@{@"view" : self.glkView}]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[view(==295)]" options:kNilOptions metrics:nil views:@{@"view" : self.glkView}]];
        
        self.glkView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
        self.glkView.drawableDepthFormat = GLKViewDrawableDepthFormat24;
        
        glClearColor(1, 0, 0, 1);
    }
    
    - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
        //1.开启深度测试
        glEnable(GL_DEPTH_TEST);
        //2.清除颜色缓存区&深度缓存区
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        //3.准备绘制
        [_effect prepareToDraw];
        //4.绘图
        glDrawArrays(GL_TRIANGLES, 0, 36);
        
        glDisable(GL_DEPTH_TEST);
    }
    @end
    

    相关文章

      网友评论

          本文标题:GLKit 绘制立方体旋转

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