OpenGLES4-地球与月亮

2020-06-29  本文已影响0人  AlanGe

资源⽂文件

纹理图片:
    Earth512x256
    Moon256x128
 顶点obj⽂文件
    sphere.h
    sphere.obj

Kit
    AGLKVertexAttribArrayBuffer.h/m
        创建缓存数组
            - (id)initWithAttribStride:(GLsizeiptr)stride numberOfVertices: (GLsizei)count bytes:(const GLvoid *)dataPtr usage:(GLenum)usage;
        分配数据,准备绘制
            - (void)prepareToDrawWithAttrib:(GLuint)index numberOfCoordinates: (GLint)count attribOffset:(GLsizeiptr)offset shouldEnable: (BOOL)shouldEnable;

        绘制
            + (void)drawPreparedArraysWithMode:(GLenum)mode startVertexIndex:(GLint)first  numberOfVertices:(GLsizei)count;
            - (void)drawArrayWithMode:(GLenum)mode startVertexIndex:(GLint)first numberOfVertices:(GLsizei)count;

        重新缓存数据
            - (void)reinitWithAttribStride:(GLsizeiptr)stride numberOfVertices:(GLsizei)count bytes:(const GLvoid *)dataPtr;

1.viewDidLoad

A.新建OpenGL ES上下⽂文 
B.获取GLKView并设置相关参数 
C.创建GLKBaseEffect
D.配置关照信息
    -(void)configureLight
    1.是否启⽤光照 self.baseEffect.light0.ena bled
    2.设置漫射光颜⾊色 self.baseEffect.light0.dif fuseColor
    4.设置光的外部环境颜⾊色 self.baseEffect.light0.amb ientColor
    3.设置光源位置 self.baseEffect.light0.pos ition
F.设置模型矩阵 
E.设置投影矩阵-透视投影
G.设置清屏颜⾊色
    -(void)setClearColor:(GLKVector4)clearColorRGBA
H.处理理顶点数据
    -(void)bufferData
    1、创建空的矩阵
    2、顶点数据缓存,顶点数据从sphere.h⽂文件的 sphereNumVerts 数组中获取顶点数据 x, y , z
    3、法线,光照坐标 sphereNormals数组 x ,y ,z 
    4、纹理坐标 sphereTexCoords数组 x,y 
    5、获取地球纹理
    6、将纹理图片加载到纹理数据对象earchTextureInfo中 
    7、获取月亮纹理
    8、将纹理图⽚片加载到纹理数据对象earchTextureInfo中
     9、矩阵堆
    10、初始化在轨道上⽉月球位置

2.渲染场景

-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect
1.清屏
2.地球月亮旋转的角度
    _earthRotationAngleDegress += 360.0f/60.0f; 
    _moonRotationAngleDegress += (360.0f/60.0f)/SceneDaysPerMoonOrbit;
3.准备绘制
    1.顶点数据
        [self.vertexPositionBuffer prepareToDrawWithAttrib:GLKVertexAttribPosition numberOfCoordinates:3 attribOffset:0 shouldEnable:YES];
    2.法线数据(光照)
        [self.vertexNormalBuffer prepareToDrawWithAttrib:GLKVertexAttribNormal numberOfCoordinates:3 attribOffset:0 shouldEnable:YES]; 
    3.纹理数据
        [self.vertextTextureCoordBuffer prepareToDrawWithAttrib:GLKVertexAttribTexCoord0 numberOfCoordinates:2 attribOffset:0 shouldEnable:YES];
4.开始绘制
    1.绘制地球 
        -(void)drawEarth 
    2.绘制月球 
        -(void)drawMoon

3.切换投影方法(正投影、透视投 影)

- (IBAction)switchClick: (UISwitch *)sender {}

Demo: 06-地球月亮渲染

上一篇下一篇

猜你喜欢

热点阅读