OpenGLES4-地球與月亮

資源?文件

紋理圖片:
    Earth512x256
    Moon256x128
 頂點obj?文件
    sphere.h
    sphere.obj

Kit
    AGLKVertexAttribArrayBuffer.h/m
        創(chuàng)建緩存數(shù)組
            - (id)initWithAttribStride:(GLsizeiptr)stride numberOfVertices: (GLsizei)count bytes:(const GLvoid *)dataPtr usage:(GLenum)usage;
        分配數(shù)據(jù),準(zhǔn)備繪制
            - (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;

        重新緩存數(shù)據(jù)
            - (void)reinitWithAttribStride:(GLsizeiptr)stride numberOfVertices:(GLsizei)count bytes:(const GLvoid *)dataPtr;

1.viewDidLoad

A.新建OpenGL ES上下?文 
B.獲取GLKView并設(shè)置相關(guān)參數(shù) 
C.創(chuàng)建GLKBaseEffect
D.配置關(guān)照信息
    -(void)configureLight
    1.是否啟?光照 self.baseEffect.light0.ena bled
    2.設(shè)置漫射光顏?色 self.baseEffect.light0.dif fuseColor
    4.設(shè)置光的外部環(huán)境顏?色 self.baseEffect.light0.amb ientColor
    3.設(shè)置光源位置 self.baseEffect.light0.pos ition
F.設(shè)置模型矩陣 
E.設(shè)置投影矩陣-透視投影
G.設(shè)置清屏顏?色
    -(void)setClearColor:(GLKVector4)clearColorRGBA
H.處理理頂點數(shù)據(jù)
    -(void)bufferData
    1、創(chuàng)建空的矩陣
    2、頂點數(shù)據(jù)緩存,頂點數(shù)據(jù)從sphere.h?文件的 sphereNumVerts 數(shù)組中獲取頂點數(shù)據(jù) x, y , z
    3、法線,光照坐標(biāo) sphereNormals數(shù)組 x ,y ,z 
    4、紋理坐標(biāo) sphereTexCoords數(shù)組 x,y 
    5、獲取地球紋理
    6、將紋理圖片加載到紋理數(shù)據(jù)對象earchTextureInfo中 
    7、獲取月亮紋理
    8、將紋理圖?片加載到紋理數(shù)據(jù)對象earchTextureInfo中
     9、矩陣堆
    10、初始化在軌道上?月球位置

2.渲染場景

-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect
1.清屏
2.地球月亮旋轉(zhuǎn)的角度
    _earthRotationAngleDegress += 360.0f/60.0f; 
    _moonRotationAngleDegress += (360.0f/60.0f)/SceneDaysPerMoonOrbit;
3.準(zhǔn)備繪制
    1.頂點數(shù)據(jù)
        [self.vertexPositionBuffer prepareToDrawWithAttrib:GLKVertexAttribPosition numberOfCoordinates:3 attribOffset:0 shouldEnable:YES];
    2.法線數(shù)據(jù)(光照)
        [self.vertexNormalBuffer prepareToDrawWithAttrib:GLKVertexAttribNormal numberOfCoordinates:3 attribOffset:0 shouldEnable:YES]; 
    3.紋理數(shù)據(jù)
        [self.vertextTextureCoordBuffer prepareToDrawWithAttrib:GLKVertexAttribTexCoord0 numberOfCoordinates:2 attribOffset:0 shouldEnable:YES];
4.開始繪制
    1.繪制地球 
        -(void)drawEarth 
    2.繪制月球 
        -(void)drawMoon

3.切換投影方法(正投影、透視投 影)

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

Demo: 06-地球月亮渲染

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容