罗大柚OpenGL ES教程系列LessonTwo(Part 2):采用VAO绘制一个Cube

前面的创建过程和LessonTwo(part 1)一样, 结果也差不多,这个project和前面相比除了是使用VAO以外,还有就是启动了光照。废话不多说,直接上代码:

#import "ViewController.h"

#defineBUFFER_OFFSET(i) ((char *)NULL + (i))

//这里我们直接用系统中创建OpenGL Game程序时默认的顶点数据

GLfloat gCubeVertexData[216] =

{

    // Data layout for each line below is:

    // positionX, positionY, positionZ,     normalX, normalY, normalZ,

    //前面

   0.5f, -0.5f, -0.5f,        1.0f,0.0f, 0.0f,

   0.5f, 0.5f, -0.5f,         1.0f,0.0f, 0.0f,

   0.5f, -0.5f, 0.5f,         1.0f,0.0f, 0.0f,

   0.5f, -0.5f, 0.5f,         1.0f, 0.0f, 0.0f,

   0.5f, 0.5f, -0.5f,         1.0f,0.0f, 0.0f,

   0.5f, 0.5f, 0.5f,          1.0f,0.0f, 0.0f,

   

    //后面

   0.5f, 0.5f, -0.5f,         0.0f,1.0f, 0.0f,

   -0.5f, 0.5f, -0.5f,        0.0f,1.0f, 0.0f,

   0.5f, 0.5f, 0.5f,          0.0f, 1.0f, 0.0f,

   0.5f, 0.5f, 0.5f,          0.0f,1.0f, 0.0f,

   -0.5f, 0.5f, -0.5f,        0.0f,1.0f, 0.0f,

   -0.5f, 0.5f, 0.5f,         0.0f,1.0f, 0.0f,

   

    //左面

   -0.5f, 0.5f, -0.5f,        -1.0f,0.0f, 0.0f,

   -0.5f, -0.5f, -0.5f,       -1.0f,0.0f, 0.0f,

   -0.5f, 0.5f, 0.5f,         -1.0f,0.0f, 0.0f,

   -0.5f, 0.5f, 0.5f,         -1.0f,0.0f, 0.0f,

   -0.5f, -0.5f, -0.5f,       -1.0f,0.0f, 0.0f,

   -0.5f, -0.5f, 0.5f,        -1.0f,0.0f, 0.0f,

   

    //右面

   -0.5f, -0.5f, -0.5f,       0.0f,-1.0f, 0.0f,

   0.5f, -0.5f, -0.5f,        0.0f,-1.0f, 0.0f,

   -0.5f, -0.5f, 0.5f,        0.0f,-1.0f, 0.0f,

   -0.5f, -0.5f, 0.5f,        0.0f,-1.0f, 0.0f,

   0.5f, -0.5f, -0.5f,        0.0f,-1.0f, 0.0f,

   0.5f, -0.5f, 0.5f,         0.0f,-1.0f, 0.0f,

   

    //下面

   0.5f, 0.5f, 0.5f,          0.0f,0.0f, 1.0f,

   -0.5f, 0.5f, 0.5f,         0.0f,0.0f, 1.0f,

   0.5f, -0.5f, 0.5f,         0.0f,0.0f, 1.0f,

   0.5f, -0.5f, 0.5f,         0.0f,0.0f, 1.0f,

   -0.5f, 0.5f, 0.5f,         0.0f,0.0f, 1.0f,

   -0.5f, -0.5f, 0.5f,        0.0f,0.0f, 1.0f,

   

   0.5f, -0.5f, -0.5f,        0.0f,0.0f, -1.0f,

   -0.5f, -0.5f, -0.5f,       0.0f,0.0f, -1.0f,

   0.5f, 0.5f, -0.5f,         0.0f,0.0f, -1.0f,

   0.5f, 0.5f, -0.5f,         0.0f,0.0f, -1.0f,

   -0.5f, -0.5f, -0.5f,       0.0f,0.0f, -1.0f,

   -0.5f, 0.5f, -0.5f,        0.0f,0.0f, -1.0f

};

 

 

@interface ViewController ()

{

    GLKMatrix4 _modelViewProjectionMatrix;

    GLKMatrix3 _normalMatrix;

    float_rotation;

   

    GLuint _vertexArray;

    GLuint _vertexBuffer;

}

 

@property (strong,nonatomic) EAGLContext *context;

@property (strong,nonatomic) GLKBaseEffect *effect;

 

 

- (void)setupGL;

- (void)tearDownGL;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

         self.context = [[EAGLContextalloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

   

    if (!self.context) {

       NSLog(@"Failed to create ES context");

    }

   

    GLKView *view = (GLKView *)self.view;

   view.context = self.context;

    //

   view.drawableDepthFormat = GLKViewDrawableDepthFormat24;

   

    [self setupGL];

}

 

- (void)setupGL

{

    [EAGLContext setCurrentContext:self.context];

   

    self.effect = [[GLKBaseEffectalloc] init];

    self.effect.light0.enabled =GL_TRUE;

    //散射光

    self.effect.light0.diffuseColor =GLKVector4Make(1.0f, 0.4f, 0.4f, 1.0f);

   

    //启动深度测试

    glEnable(GL_DEPTH_TEST);

  

    //设置VAO

    glGenVertexArraysOES(1, &_vertexArray);

    glBindVertexArrayOES(_vertexArray);

   

    //绑定顶点buffer

    glGenBuffers(1, &_vertexBuffer);

    glBindBuffer(GL_ARRAY_BUFFER,_vertexBuffer);

    glBufferData(GL_ARRAY_BUFFER,sizeof(gCubeVertexData),gCubeVertexData, GL_STATIC_DRAW);

    //启动顶点

    glEnableVertexAttribArray(GLKVertexAttribPosition);

    glVertexAttribPointer(GLKVertexAttribPosition, 3,GL_FLOAT, GL_FALSE, 24,BUFFER_OFFSET(0));

    //启动顶点法线

    glEnableVertexAttribArray(GLKVertexAttribNormal);

    glVertexAttribPointer(GLKVertexAttribNormal, 3,GL_FLOAT, GL_FALSE, 24,BUFFER_OFFSET(12));

   

    //Bind back to the default state

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArrayOES(0);

   

}

 

//tear down 卸载;卸载GL

- (void)tearDownGL

{

    [EAGLContext setCurrentContext:self.context];

   

    glDeleteBuffers(1, &_vertexBuffer);

    glDeleteVertexArraysOES(1, &_vertexArray);

   

    self.effect =nil;

   

}

 

#pragma mark - GLKView and GLKViewController delegatemethods


- (void)update

{

    float aspect= fabsf(self.view.bounds.size.width /self.view.bounds.size.height);

    GLKMatrix4 projectionMatrix =GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);

   

    self.effect.transform.projectionMatrix = projectionMatrix;

   

   

    GLKMatrix4 modelViewMatrix =GLKMatrix4MakeTranslation(0.0f, 0.0f, -4.5f);

   modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix,_rotation, 1.0f, 1.0f, 1.0f);

   

    self.effect.transform.modelviewMatrix = modelViewMatrix;

   

    _normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3(modelViewMatrix),NULL);

   

   

    _rotation += self.timeSinceLastUpdate * 0.5f;

   

}

 

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

{

    glClearColor(0.65f, 0.65f, 0.65f, 1.0f);

    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);

   

    glBindVertexArrayOES(_vertexArray);

   

    // Render the object with GLKit

    [self.effectprepareToDraw];

    //画立方体

    glDrawArrays(GL_TRIANGLES, 0, 36);

   

}

 

- (void)dealloc

{

    [self tearDownGL];

   

    if ([EAGLContextcurrentContext] == self.context) {

       [EAGLContext setCurrentContext:nil];

    }

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

   

    if ([selfisViewLoaded] && ([[selfview] window] ==nil)) {

       self.view =nil;

       

       [self tearDownGL];

       

       if ([EAGLContextcurrentContext] == self.context) {

           [EAGLContext setCurrentContext:nil];

       }

       self.context =nil;

    }

  

}

@end

 源码下载:http://download.csdn.net/detail/luozhonglan/6987129



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值