# Cocos2dx-OpenGL ES2.0教程：你的第一个立方体(5)

1730人阅读 评论(1)

## 准备立方体数据

 1 2 3 4   typedef struct { float Position[3]; float Color[4]; } Vertex; 

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33  Vertex data[] = { // Front { {1, -1, 0}, {1, 0, 0, 1}}, { {1, 1, 0}, {0, 1, 0, 1}}, { {-1, 1, 0}, {0, 0, 1, 1}}, { {-1, -1, 0}, {0, 0, 0, 1}}, // Back { {1, 1, -2}, {1, 0, 0, 1}}, { {-1, -1, -2}, {0, 1, 0, 1}}, { {1, -1, -2}, {0, 0, 1, 1}}, { {-1, 1, -2}, {0, 0, 0, 1}}, // Left { {-1, -1, 0}, {1, 0, 0, 1}}, { {-1, 1, 0}, {0, 1, 0, 1}}, { {-1, 1, -2}, {0, 0, 1, 1}}, { {-1, -1, -2}, {0, 0, 0, 1}}, // Right { {1, -1, -2}, {1, 0, 0, 1}}, { {1, 1, -2}, {0, 1, 0, 1}}, { {1, 1, 0}, {0, 0, 1, 1}}, { {1, -1, 0}, {0, 0, 0, 1}}, // Top { {1, 1, 0}, {1, 0, 0, 1}}, { {1, 1, -2}, {0, 1, 0, 1}}, { {-1, 1, -2}, {0, 0, 1, 1}}, { {-1, 1, 0}, {0, 0, 0, 1}}, // Bottom { {1, -1, -2}, {1, 0, 0, 1}}, { {1, -1, 0}, {0, 1, 0, 1}}, { {-1, -1, 0}, {0, 0, 1, 1}}, { {-1, -1, -2}, {0, 0, 0, 1}} }; 

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  GLubyte indices[] = { // Front 0, 1, 2, 2, 3, 0, // Back 4, 5, 6, 4, 5, 7, // Left 8, 9, 10, 10, 11, 8, // Right 12, 13, 14, 14, 15, 12, // Top 16, 17, 18, 18, 19, 16, // Bottom 20, 21, 22, 22, 23, 20 }; 

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19   glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex,Position)); //下面是vertex shader attribute vec3 a_position; //注意之前我们使用的是vec2 attribute vec4 a_color; varying vec4 v_fragmentColor; void main() { gl_Position = CC_MVPMatrix * vec4(a_position.xyz,1); //这里用swizzle的时候是xyz v_fragmentColor = a_color; } 

cube01

## 让立方体动起来

 1 2 3 4 5 6 7 8 9 10 11  modelViewMatrix.scale(0.5); modelViewMatrix.translate(0.0,0, -0.5); static float rotation = 0; modelViewMatrix.rotate(Vec3(1,0,0),CC_DEGREES_TO_RADIANS(rotation)); rotation++; if (rotation < 360) { rotation = 0; } Director::getInstance()->multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, modelViewMatrix); 

 1 2 3 4 5 6 7 8 9 10 11 12 13 14  case Projection::_3D: { float zeye = this->getZEye(); Mat4 matrixPerspective, matrixLookup; loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); // issue #1334 Mat4::createPerspective(60, (GLfloat)size.width/size.height, 10, zeye+size.height/2, &matrixPerspective); multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, matrixPerspective); Vec3 eye(size.width/2, size.height/2, zeye), center(size.width/2, size.height/2, 0.0f), up(0.0f, 1.0f, 0.0f); Mat4::createLookAt(eye, center, up, &matrixLookup); multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, matrixLookup); loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); break; } 

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  Mat4 projectionMatrix; Mat4::createPerspective(60, 480/320, 1.0, 42, &projectionMatrix); Director::getInstance()->multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, projectionMatrix); Mat4 modelViewMatrix; Mat4::createLookAt(Vec3(0,0,1), Vec3(0,0,0), Vec3(0,1,0), &modelViewMatrix); modelViewMatrix.translate(0, 0, -5); static float rotation = 0; modelViewMatrix.rotate(Vec3(1,1,1),CC_DEGREES_TO_RADIANS(rotation)); rotation++; if (rotation < 360) { rotation = 0; } Director::getInstance()->multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, modelViewMatrix); 

## 推荐阅读

1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：355273次
• 积分：4089
• 等级：
• 排名：第7853名
• 原创：48篇
• 转载：44篇
• 译文：0篇
• 评论：54条
博客专栏
 cocos2dx游戏开发 文章：7篇 阅读：66569
 Lua基础学习 文章：9篇 阅读：25871
 unity游戏开发者 文章：28篇 阅读：133390
文章分类
最新评论