美文网首页iOS视觉
二十四 OpenGL 索引绘图之绘制金字塔GLKit

二十四 OpenGL 索引绘图之绘制金字塔GLKit

作者: 王俏 | 来源:发表于2020-08-05 07:48 被阅读0次

    效果图

    image

    流程

    image

    0.准备工作

    • ViewController继承自GLKViewController
    • 头文件导入
      #import <GLKit/GLKit.h>
    • 定义扩展属性
    //上下文
    @property(nonatomic,strong)EAGLContext *mContext;
    //GLKBaseEffect
    @property(nonatomic,strong)GLKBaseEffect *mEffect;
    //顶点个数
    @property(nonatomic,assign)int count;
    
    //旋转的度数
    @property(nonatomic,assign)float XDegree;
    @property(nonatomic,assign)float YDegree;
    @property(nonatomic,assign)float ZDegree;
    
    //是否旋转X,Y,Z
    @property(nonatomic,assign) BOOL XB;
    @property(nonatomic,assign) BOOL YB;
    @property(nonatomic,assign) BOOL ZB;
    
    • 定时器 dispatch_source_t
    dispatch_source_t timer;
    

    1.新建图层,设置GLKView和上下文

    self.mContext = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES2];
    
    GLKView *view = (GLKView *)self.view;
    view.context = self.mContext;
    view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
    view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
    
    [EAGLContext setCurrentContext:self.mContext];
    glEnable(GL_DEPTH_TEST);
    

    2.渲染图形

    • 顶点数据
             GLfloat attrArr[] =
        {
            -0.5f, 0.5f, 0.0f,      1.0f, 0.0f, 1.0f, //左上
            0.5f, 0.5f, 0.0f,       1.0f, 0.0f, 1.0f, //右上
            -0.5f, -0.5f, 0.0f,     1.0f, 1.0f, 1.0f, //左下
            
            0.5f, -0.5f, 0.0f,      1.0f, 1.0f, 1.0f, //右下
            0.0f, 0.0f, 1.0f,       0.0f, 1.0f, 0.0f, //顶点
        };
    
    • 绘图索引
            GLuint indices[] =
        {
            0, 3, 2,
            0, 1, 3,
            0, 2, 4,
            0, 4, 1,
            2, 3, 4,
            1, 4, 3,
        };
    
    • 顶点个数
    self.count = sizeof(indices) /sizeof(GLuint);
    
    • 将顶点数组放入数组缓冲区中 GL_ARRAY_BUFFER
        GLuint buffer;
        glGenBuffers(1, &buffer);
        glBindBuffer(GL_ARRAY_BUFFER, buffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(attrArr), attrArr, GL_STATIC_DRAW);
    
    • 将索引数组存储到索引缓冲区 GL_ELEMENT_ARRAY_BUFFER
        GLuint index;
        glGenBuffers(1, &index);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    
    • 使用顶点数据 GLKVertexAttribPosition
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, NULL);
    
    • 使用颜色数据 GLKVertexAttribColor
        glEnableVertexAttribArray(GLKVertexAttribColor);
        glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, (GLfloat *)NULL + 3);
    
    • 初始化着色器
    self.mEffect = [[GLKBaseEffect alloc]init];
    
    • 投影视图
        CGSize size = self.view.bounds.size;
        float aspect = fabs(size.width / size.height);
        GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(90.0), aspect, 0.1f, 100.0);
        projectionMatrix = GLKMatrix4Scale(projectionMatrix, 1.0f, 1.0f, 1.0f);
        self.mEffect.transform.projectionMatrix = projectionMatrix;
    
    • 模型视图
        GLKMatrix4 modelViewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, 0.0f, 0.0f, -2.0f);
        self.mEffect.transform.modelviewMatrix = modelViewMatrix;
    
    • 定时器
    double seconds = 0.1;
    timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC, 0.0);
    dispatch_source_set_event_handler(timer, ^{
            self.XDegree += 0.1f * self.XB;
            self.YDegree += 0.1f * self.YB;
            self.ZDegree += 0.1f * self.ZB ;
            
        });
        dispatch_resume(timer);
    

    GLKViewDelegate

    • 场景变化更新 -update
    -(void)update
    {
        GLKMatrix4 modelViewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, 0.0f, 0.0f, -2.5);
        
        modelViewMatrix = GLKMatrix4RotateX(modelViewMatrix, self.XDegree);
        modelViewMatrix = GLKMatrix4RotateY(modelViewMatrix, self.YDegree);
        modelViewMatrix = GLKMatrix4RotateZ(modelViewMatrix, self.ZDegree);
        
        self.mEffect.transform.modelviewMatrix = modelViewMatrix;
        
    }
    
    • 重绘 - glkView:drawInRect:
    - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
        glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        [self.mEffect prepareToDraw];
        glDrawElements(GL_TRIANGLES, self.count, GL_UNSIGNED_INT, 0);
    }
    

    IBAction点击事件

        - (IBAction)XClick:(id)sender {
        _XB = !_XB;
    }
    - (IBAction)YClick:(id)sender {
        _YB = !_YB;
    }
    - (IBAction)ZClick:(id)sender {
        _ZB = !_ZB;
    }
    

    完整代码实现:

    #import "CCViewController.h"
    
    @interface CCViewController()
    
    //上下文
    @property(nonatomic,strong)EAGLContext *mContext;
    //GLKBaseEffect
    @property(nonatomic,strong)GLKBaseEffect *mEffect;
    //顶点个数
    @property(nonatomic,assign)int count;
    
    //旋转的度数
    @property(nonatomic,assign)float XDegree;
    @property(nonatomic,assign)float YDegree;
    @property(nonatomic,assign)float ZDegree;
    
    //是否旋转X,Y,Z
    @property(nonatomic,assign) BOOL XB;
    @property(nonatomic,assign) BOOL YB;
    @property(nonatomic,assign) BOOL ZB;
    
    @end
    
    @implementation CCViewController
    {
        dispatch_source_t timer;
    }
    -(void)viewDidLoad
    {
        [super viewDidLoad];
        
        //1.新建图层
        [self setupContext];
        
        //2.渲染图形
        [self render];
    }
    
    //2.渲染图形
    -(void)render
    {
        //1.顶点数据
        //前3个元素,是顶点数据;中间3个元素,是顶点颜色值,最后2个是纹理坐标
    //    GLfloat attrArr[] =
    //    {
    //        -0.5f, 0.5f, 0.0f,      1.0f, 0.0f, 1.0f,       0.0f, 1.0f,//左上
    //        0.5f, 0.5f, 0.0f,       1.0f, 0.0f, 1.0f,       1.0f, 1.0f,//右上
    //        -0.5f, -0.5f, 0.0f,     1.0f, 1.0f, 1.0f,       0.0f, 0.0f,//左下
    //
    //        0.5f, -0.5f, 0.0f,      1.0f, 1.0f, 1.0f,       1.0f, 0.0f,//右下
    //        0.0f, 0.0f, 1.0f,       0.0f, 1.0f, 0.0f,       0.5f, 0.5f,//顶点
    //    };
        
        GLfloat attrArr[] =
        {
            -0.5f, 0.5f, 0.0f,      1.0f, 0.0f, 1.0f, //左上
            0.5f, 0.5f, 0.0f,       1.0f, 0.0f, 1.0f, //右上
            -0.5f, -0.5f, 0.0f,     1.0f, 1.0f, 1.0f, //左下
            
            0.5f, -0.5f, 0.0f,      1.0f, 1.0f, 1.0f, //右下
            0.0f, 0.0f, 1.0f,       0.0f, 1.0f, 0.0f, //顶点
        };
        
        //2.绘图索引
        GLuint indices[] =
        {
            0, 3, 2,
            0, 1, 3,
            0, 2, 4,
            0, 4, 1,
            2, 3, 4,
            1, 4, 3,
        };
        
        //顶点个数
        self.count = sizeof(indices) /sizeof(GLuint);
    
        //将顶点数组放入数组缓冲区中 GL_ARRAY_BUFFER
        GLuint buffer;
        glGenBuffers(1, &buffer);
        glBindBuffer(GL_ARRAY_BUFFER, buffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(attrArr), attrArr, GL_STATIC_DRAW);
        
        //将索引数组存储到索引缓冲区 GL_ELEMENT_ARRAY_BUFFER
        GLuint index;
        glGenBuffers(1, &index);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
        
        //使用顶点数据
        glEnableVertexAttribArray(GLKVertexAttribPosition);
        glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, NULL);
        
        //使用颜色数据
        glEnableVertexAttribArray(GLKVertexAttribColor);
        glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, (GLfloat *)NULL + 3);
    
        
        //着色器
        self.mEffect = [[GLKBaseEffect alloc]init];
    
        //投影视图
        CGSize size = self.view.bounds.size;
        float aspect = fabs(size.width / size.height);
        GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(90.0), aspect, 0.1f, 100.0);
        projectionMatrix = GLKMatrix4Scale(projectionMatrix, 1.0f, 1.0f, 1.0f);
        self.mEffect.transform.projectionMatrix = projectionMatrix;
        
        //模型视图
        GLKMatrix4 modelViewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, 0.0f, 0.0f, -2.0f);
        self.mEffect.transform.modelviewMatrix = modelViewMatrix;
        
        
        //定时器
        double seconds = 0.1;
        timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
        dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC, 0.0);
        dispatch_source_set_event_handler(timer, ^{
           
            self.XDegree += 0.1f * self.XB;
            self.YDegree += 0.1f * self.YB;
            self.ZDegree += 0.1f * self.ZB ;
            
        });
        dispatch_resume(timer);
    
    }
    
    //场景数据变化
    -(void)update
    {
        GLKMatrix4 modelViewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, 0.0f, 0.0f, -2.5);
        
        modelViewMatrix = GLKMatrix4RotateX(modelViewMatrix, self.XDegree);
        modelViewMatrix = GLKMatrix4RotateY(modelViewMatrix, self.YDegree);
        modelViewMatrix = GLKMatrix4RotateZ(modelViewMatrix, self.ZDegree);
        
        self.mEffect.transform.modelviewMatrix = modelViewMatrix;
        
    }
    
    - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
        glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        
        [self.mEffect prepareToDraw];
        glDrawElements(GL_TRIANGLES, self.count, GL_UNSIGNED_INT, 0);
    }
    
    //1.新建图层
    -(void)setupContext
    {
        //1.新建OpenGL ES上下文
        self.mContext = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES2];
        
        GLKView *view = (GLKView *)self.view;
        view.context = self.mContext;
        view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
        view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
        
        [EAGLContext setCurrentContext:self.mContext];
        glEnable(GL_DEPTH_TEST);
        
        
    }
    
    #pragma mark -XYZClick
    - (IBAction)XClick:(id)sender {
        _XB = !_XB;
    }
    - (IBAction)YClick:(id)sender {
        _YB = !_YB;
    }
    - (IBAction)ZClick:(id)sender {
        _ZB = !_ZB;
    }
    
    
    @end
    
    

    相关文章

      网友评论

        本文标题:二十四 OpenGL 索引绘图之绘制金字塔GLKit

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