美文网首页
全景图片显示

全景图片显示

作者: 凡凡_c009 | 来源:发表于2018-11-07 15:21 被阅读0次

前言

这是介绍全景图片显示demo,通过Metal进行图片渲染请查看落影大神的博客

正文

先上全景图片的效果图


普通模式图片 小行星 鱼眼

具体步骤

1、新建MTKView
2、设置渲染管道
3、设置顶点数据
4、设置纹理数据
5、渲染过程
6、Shader处理

普通图片渲染过程基本上是上面6个步骤,全景图片和普通图片不同的地方在于步骤3和步骤6.

设置顶点数据
- (void)setupVertex {
    float *vertices = 0;// 顶点
    float *texCoord = 0;// 纹理
    uint16_t *indices  = 0;// 索引
    int numVertices = 0;
    self.numIndices = initSphere(200, 1.0f, 360, &vertices, &texCoord, &indices, &numVertices);
    
    self.vertices = [self.mtkView.device newBufferWithBytes:vertices
                                                     length:sizeof(float) * 4 * numVertices
                                                    options:MTLResourceStorageModeShared]; // 创建顶点缓存
    self.textures = [self.mtkView.device newBufferWithBytes:texCoord
                                                     length:sizeof(float) * 2 * numVertices
                                                    options:MTLResourceStorageModeShared]; // 创建纹理坐标缓存
    self.indices = [self.mtkView.device newBufferWithBytes:indices
                                                     length:sizeof(uint16_t) * self.numIndices
                                                    options:MTLResourceStorageModeShared];  //索引缓存
}

创建顶点数据和纹理数据主要是通过initSphere 函数,函数具体实现查看Sphere文件

Shader处理
vertex RasterizerData // 返回给片元着色器的结构体
vertexShader(uint vertexID [[ vertex_id ]], // vertex_id是顶点shader每次处理的index,用于定位当前的顶点
             constant float4 *vertexArray [[ buffer(0) ]],// 顶点
             constant float2 *textureArray [[ buffer(1)]],// 纹理
             constant LYMatrix *matrix [[ buffer(2)]]) { // 矩阵
    RasterizerData out;
    out.clipSpacePosition = matrix->mvpMatrix * matrix->lookAtMatrix * matrix->rotateMatrix * vertexArray[vertexID];
    out.textureCoordinate = textureArray[vertexID];
    return out;
}

全景图片主要是通过三个矩阵来实现缩放mvpMatrix、模式lookAtMatrix和旋转rotateMatrix的。
得到观看视角的矩阵,不同模式其实是在球的不同位置看球,普通模式是在球心、小行星模式是在球表面、鱼眼模式是在球外。

///设置观看模式
- (void)setPanoramicModel:(DFPanoramaModel)type {
    self.type = type;
    self.mvpDegree = 90;
    switch (type) {
        case DFPanoramaModelNoramal:
        {
            self.orginEyeMatrix = GLKMatrix4MakeLookAt(0, 0, 0,
                                                       0, 0, -1.0f,
                                                       0, -1.0f, 0);
        }
            break;
        case DFPanoramaModelLitlePlanet:
        {
            self.mvpDegree = kPlanetMaxDegree;
            self.orginEyeMatrix = GLKMatrix4MakeLookAt(0, 0, 1.0f,
                                                       0, 0, -1.0f,
                                                       0, -1.0f, 0);
        }
            break;
        case DFPanoramaModelFisheye:
        {
            self.orginEyeMatrix = GLKMatrix4MakeLookAt(0, 0, 2.0f,
                                                       0, 0, -1.0f,
                                                       0, -1.0f, 0);
        }
            break;
            
        default:
            break;
    }
    
    self.horizontalDegree = 0;
    self.verticalDegree = 0;
}

将三个矩阵值传入顶点着色器

- (void)setupMatrixWithEncoder:(id<MTLRenderCommandEncoder>)renderEncoder {
    [self smooth]; //滑动效果
    [self scaleToMaxOrdMin]; //设置最大和最小角度
    
    GLKMatrix4 mvpMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(self.mvpDegree), CGRectGetWidth(self.view.bounds) / CGRectGetHeight(self.view.bounds), 0.1, 10);
    
    GLKMatrix4 lookAtMatrix = GLKMatrix4RotateX(self.orginEyeMatrix, _verticalDegree);
    
    GLKMatrix4 rotateX = GLKMatrix4RotateY(GLKMatrix4Identity, _horizontalDegree);
    
    LYMatrix matrix = {[self getMetalMatrixFromGLKMatrix:mvpMatrix], [self getMetalMatrixFromGLKMatrix:lookAtMatrix], [self getMetalMatrixFromGLKMatrix:rotateX]};
    
    [renderEncoder setVertexBytes:&matrix
                           length:sizeof(matrix)
                          atIndex:2];
}

最后附上demo地址

相关文章

  • 全景图片显示

    前言 这是介绍全景图片显示demo,通过Metal进行图片渲染请查看落影大神的博客 正文 先上全景图片的效果图 具...

  • android opengl播放全景视频

    原理是opengl显示全景图片和opengl播放视频的结合,下面是代码

  • Unity显示360度全景照片

    Unity显示360度全景,其实就是把全景图片贴到一个球上,只不过,这个球是从里往外看。 先把摄像机移动到坐标(0...

  • android vr全景图片初探(仿微博360全景图片的实现)

    android vr全景图片初探(仿微博360全景图片的实现) 最近逛微博的时候,看见有个360全景图很酷炫,想着...

  • pano2vr 5.1 使用简易说明

    pano2vr 是一款快速处理全景图片的软件 导入图片 拖拽全景图片到中间的面板,或者点击导入按钮导入图片。 添加...

  • 那快点拍吧

    带客户看房,顺便和业主说拍个全景。 业主说,不是有图片吗? 这样的,全景不同于图片,全景是360度全方位展示,有户...

  • 做一个360度的全景展示功能

    原理比较简单:把一组的拍摄好了的360度的全景图,通过鼠标的移动不停的去显示和隐藏图片。

  • 使用openGLES2.0做一个全景图片展示-ios版

    一. 背景分析首先我们得有一张全景图片,这个图片是由两个摄像头拍摄合成到一张图片上的全景图片。有了图片,如何在手机...

  • Krpano 全景热点轮播开场插件 兼容小行星开场

    一、功能特性 用户打开全景的时候,当前场景会先根据设定的坐标,移动视角以及显示弹窗图片,这样可以强调场景的关注点。...

  • 全景图片

    用插件photo-sphere-viewer发现有问题,图片过大,c盘内存小于大概5G就不行,加载不出来,可能图片...

网友评论

      本文标题:全景图片显示

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