Code Monkey home page Code Monkey logo

opengl_3.0's Introduction

OpenGL_3.0

介绍

本工程完全采用OpenGL ES 3.0的API进行实现,目的是更好的使用OpenGL相关功能并移植到移动端项目。

以示例为主,可以作为调参工具,也可以作为学习参考。

图片显示

  • 将需要显示的图片转成纹理
  • 纹理序号将会在渲染线程中传给GPU进行缓存
  • OpenGL程序绘制纹理并光栅化显示到窗口
// 图片指定存储路径
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"yourname" ofType:@"png"];
// 转换图片为OpenGL纹理ID
GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithContentsOfFile:imagePath options:@{GLKTextureLoaderOriginBottomLeft : @true, GLKTextureLoaderGenerateMipmaps : @true} error:NULL];
// 存储纹理
self.mTextureId = textureInfo.name;
// 纹理尺寸
_imageSize = CGSizeMake(textureInfo.width, textureInfo.height);

图例一

动画

  • 通过绘制两张不同的图像
  • 在切换图像过程中,编写shader,添加一些行为(如:旋转、渐变、缩放、移动等)
  • 按指定帧率渲染每一帧图像,得到流畅的动画

图例二

  • 多张图片绘制时,可能需要修改透明度(需要开启混合模式, 会占用一定资源)
  • 将时间作为参数传入shader,用于控制显示时长及效果
  • 绘制第二张图片到窗口,用于替代第一张图,实现转场效果

绘制第一张图像, 如果在后台绘制,需要使用FBO机制,此处暂时不做讲解

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {

    // 清理颜色缓冲区,防止渲染花屏不同步导致撕裂等问题
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    
    // 自定义参数传入
    glUniform2f(uniforms[UNIFORM_PIXEL_SIZE],      _imageSize.width, _imageSize.height);
    glUniform1f(uniforms[UNIFORM_ZOOM],            _zoom);
    glUniform1f(uniforms[UNIFORM_ANGLE_X],         (_rotationAngle * M_PI) / 180.0);
    glUniform1f(uniforms[UNIFORM_ANGLE_Y],         (_verticalRotationAngle * M_PI) / 180.0);
    glUniform2f(uniforms[UNIFORM_OFFSET],          _offsetPoint.x, _offsetPoint.y);

    // 激活纹理标号绑定到当前纹理ID,会将当前纹理对象传入shader中的inputImageTexture,此处使用的是2D图像,只包含x、y轴
    if (_mTextureId) {
        glActiveTexture(GL_TEXTURE2);
        glBindTexture(GL_TEXTURE_2D, _mTextureId);
        glUniform1i(_textureUniform, 2);
    }
    
    // 进行等比例裁剪显示,防止图像填充到屏幕出现拉伸现象
    [self cropTextureCoordinateForRect:rect];

    // 传入VAO数据,用于显示指定的顶点坐标以及纹素数据提取位置信息
    glVertexAttribPointer(_positionAttribute, 2, GL_FLOAT, 0, 0, _texturePosition);
    glVertexAttribPointer(_coordinateAttibute, 2, GL_FLOAT, 0, 0, textureCoordinates);
    
    // 同步绘制
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
    [self redrawTexture];
}

绘制第二张图 为了保证alpha通道不受第一张图的影响,需要开启混合模式

- (void)redrawTexture
{
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    timeElapsed += [[NSString stringWithFormat:@"%f", self.timeSinceLastDraw] doubleValue];
    glUniform1f(_timeUniform, timeElapsed);
    glUniform1f(_chatrletUniform, 1.0);
    if (_textureId)
    {
         glActiveTexture(GL_TEXTURE3);
         glBindTexture(GL_TEXTURE_2D, _textureId);
         glUniform1i(_textureUniform, 3);
    }
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glDisable(GL_BLEND);
}

人脸识别

模型使用google开源项目MediaPipe中的faceblaze 支持亚毫秒级的人脸检测 人脸关键点定位使用腾讯优图工作室开源模型文件

效果示例

以鼻尖关键点作为中心,添加贴图在视频上 根据人脸定位,计算当前人脸角度、焦距大小来进行贴图旋转和缩放

代码实现

为了方便demo使用,本处没有使用opengl进行绘制,可以结合上文进行处理

添加图片

self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"emoji.png"]];
self.imageView.frame = CGRectMake(-50, 0, 50, 50);
[self.view addSubview:self.imageView];

初始化

// 初始化识别器,指定最大支持人脸个数及预览视频尺寸,用于适配点位
self.predictManager = [[WDPredictManager alloc] initWithFaceNumber:12 withInputSize:self.renderView.bounds.size];
// 显示关键点,用于demo查看
[self.predictManager showFaceLandmarkOnView:self.renderView];
// 设置代理
self.predictManager.delegate = self;

实现代理

- (void)predict:(WDPredictManager *)predict didFinished:(BOOL)hasFace
{
    if (hasFace) {
        // 鼻尖位置
        CGPoint point = [predict pointForIndex:16];
        NSLog(@"point = %@, 角度 = %.2f", NSStringFromCGPoint(point), predict.angle);
        
        self.imageView.center = point;
        self.imageView.transform = CGAffineTransformMakeRotation(predict.angle);
        self.imageView.transform = CGAffineTransformScale(self.imageView.transform, predict.scale, predict.scale);
    }
}

opengl_3.0's People

Contributors

qiyun2014 avatar

Stargazers

 avatar Zshiren avatar LJAYA avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.