美文网首页音视频从入门到放弃
OpenGL ES GLKit三角形变换

OpenGL ES GLKit三角形变换

作者: 奉灬孝 | 来源:发表于2020-08-05 22:18 被阅读0次

效果图如下:


GLKit.gif

首先,使用GLKit绘制图形分为以下两个步骤

  • 初始化上下文
  • 使用GLKBaseEffect渲染图形

初始化上下文

  1. 新建OpenGL ES上下文,EAGLContext是苹果iOS平台下实现OpenGL ES渲染层
  2. 新建GLKView
  3. 设置view的上下文为步骤1中创建的上下文
  4. 设置GLKView代理(GLKViewDelegate)
  5. 设置颜色、深度缓冲格式
  6. 设置渲染层的当前上下文为步骤1中新建的上下文
  7. 开启深度测试
//1.新建图层
-(void)setupContext
{
    //1.新建OpenGL ES上下文
    self.mContext = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES2];
    
    GLKView *view = (GLKView *)self.view;
    view.context = self.mContext;
    view.delegate = self;
    //设置颜色缓冲格式
    view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
    //设置深度缓冲格式
    view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
    
    [EAGLContext setCurrentContext:self.mContext];
    glEnable(GL_DEPTH_TEST);
    
}

绘制图形

  1. 顶点、索引数据和GLSL综合案例中的顶点、索引数据相同
  2. 将顶点数组放入数组缓冲区中 GL_ARRAY_BUFFER
  3. 将索引数组存储到索引缓冲区 GL_ELEMENT_ARRAY_BUFFER
  4. 使用顶点、颜色数据,GLKBaseEffect使用命名顶点属性,因此当配置/启用/绑定要与GLKBaseEffect一起使用的顶点属性数据时,客户端应用程序可以引用以下顶点属性名称:
    GLKVertexAttribPosition
    GLKVertexAttribNormal
    GLKVertexAttribColor
    GLKVertexAttribTexCoord0
    GLKVertexAttribTexCoord1
    请注意,GLKVertexAttribNormal的法线总是标准化。
  5. 新建GLKBaseEffect着色器: 一种简单的光照/着色系统,用于基于着色器的OpenGL渲染
  6. 设置着色器的投影、模型视图
  7. 开启定时器,让图形随时间旋转
//2.渲染图形
-(void)render
{
    //1.顶点数据
    //前3个元素,是顶点数据;后面3个元素,是顶点颜色值
    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);
}
GLKViewDelegate代理方法- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
- (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);
}
GLKViewControllerDelegate代理方法- (void)glkViewControllerUpdate:(GLKViewController *)controller;
- (void)glkViewControllerUpdate:(GLKViewController *)controller{
    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;
}
控制X、Y、Z轴方向变化方法
#pragma mark -XYZClick
- (IBAction)XClick:(id)sender {
    _XB = !_XB;
}
- (IBAction)YClick:(id)sender {
    _YB = !_YB;
}
- (IBAction)ZClick:(id)sender {
    _ZB = !_ZB;
}

Demo:
GLKitDemo

相关文章

网友评论

    本文标题:OpenGL ES GLKit三角形变换

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