同样的, LessonTwo的第三部分我们采用了VAO,但是我们觉得在part 2中那中为创建一个Cube而重复大量顶点数据的方式并不完美,于是我们很自然的想到了用index,part 3就是采用了VAO和index创建的Cube, 这似乎也减少了我们的体力的输出,程序员很幸苦,除了每天要耗费大量的脑力外, 还需要敲打那么多个字符,所以我们尽量的让代码变得简单一点吧,对自己好一点。好了, 代码端上来:
#import "ViewController.h"
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
typedef struct {
floatPosition[3];
}Vertex;
const Vertex Vertices[] = {
{0.5f, -0.5f, 0.0f},
{0.5f, 0.5f, 0.0f},
{-0.5f, 0.5f, 0.0f},
{-0.5f, -0.5f, 0.0f},
{0.5f, -0.5f, -0.5f},
{0.5f, 0.5f, -0.5f},
{-0.5f, 0.5f, -0.5f},
{-0.5f, -0.5f, -0.5f}
};
const GLubyte Indices[] = {
// Front
0,1, 2,
2,3, 0,
// Back
4,5, 6,
6,7, 4,
// Left
4,5, 1,
1,0, 4,
// Right
7,6, 2,
2, 3, 7,
// Top
5,6, 2,
2,1, 5,
// Bottom
4,7, 3,
3,0, 4,
};
@interface ViewController ()
{
float_rotation;
GLuint _vertexArray;
GLuint vertexBufferID;
GLuint indexBufferID;
}
@property (strong, nonatomic) EAGLContext *context;
@property (strong, nonatomic) GLKBaseEffect *effect;
- (void)setupGL;
- (void)tearDownGL;
@end