1 创建
//立方体顶点属性
Vertex cubeVerticesIndexed[] =
{
{ -1.0f,-1.0f, 1.0f, 0.0f,1.0f, 1.0f, 1.0f, 1.0f,1.0f }, // 0
{ 1.0f,-1.0f, 1.0f, 0.0f,1.0f, 1.0f, 1.0f, 1.0f,1.0f }, // 1
{ 1.0f, 1.0f, 1.0f, 1.0f,1.0f, 1.0f, 1.0f, 1.0f,1.0f }, // 2
{ -1.0f, 1.0f, 1.0f, 1.0f,1.0f, 1.0f, 1.0f, 1.0f,1.0f }, // 3
{ -1.0f,-1.0f,-1.0f, 0.0f,1.0f, 1.0f, 1.0f, 1.0f,1.0f }, // 4
{ -1.0f, 1.0f,-1.0f, 1.0f,1.0f, 1.0f, 1.0f, 1.0f,1.0f }, // 5
{ 1.0f, 1.0f,-1.0f, 1.0f,1.0f, 1.0f, 1.0f, 1.0f,1.0f }, // 6
{ 1.0f,-1.0f,-1.0f, 1.0f,1.0f, 1.0f, 1.0f, 1.0f,1.0f }, // 7
};
//索引点
GLubyte indexBuf[] =
{
0, 1, 2, 0, 2, 3, // Quad 0
4, 5, 6, 4, 6, 7, // Quad 1
5, 3, 2, 5, 2, 6, // Quad 2
4, 7, 1, 4, 1, 0, // Quad 3
7, 6, 2, 7, 2, 1, // Quad 4
4, 0, 3, 4, 3, 5 // Quad 5
};
unsigned _vbo;
//创建顶点缓冲区
glGenBuffers(1,&_vbo);
//制定vbo类型
glBindBuffer(GL_ARRAY_BUFFER,_vbo);
//给vbo赋值glBufferData(GL_ARRAY_BUFFER,sizeof(cubeVerticesIndexed),cubeVerticesIndexed,GL_STATIC_DRAW);
//恢复初始状态
glBindBuffer(GL_ARRAY_BUFFER,0);
//创建ibo
glGenBuffers(1,&_indexbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,_indexbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indexBuf),indexBuf,GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
2 渲染:
//矩阵变换
CELL::matrix4 MVP = matProj * matView * matRot;
//给一个纹理
glBindTexture(GL_TEXTURE_2D,_textureId);
glBindBuffer(GL_ARRAY_BUFFER,_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,_indexbo);
glUniformMatrix4fv(_shader._MVP, 1, false, MVP.data());
glVertexAttribPointer(_shader._positionAttr,3, GL_FLOAT, false, sizeof(Vertex),(void*)0);
glVertexAttribPointer(_shader._uvAttr,2, GL_FLOAT, false, sizeof(Vertex),(void*)12);
glVertexAttribPointer(_shader._colorAttr,4, GL_FLOAT, false, sizeof(Vertex),(void*)20);
glDrawElements(GL_TRIANGLES,36,GL_UNSIGNED_BYTE,0);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);