罗大柚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



你可能感兴趣的