網頁

2014年3月21日 星期五

OpenGL - Render Cube by Index Buffer

Last Update: 2014/03/21 17:28+08
Type: Note



glDrawArrays 僅是依 vertices 的順序繪
就是沒有 index buffer
要用 glDrawElements, 依 indecies 順序
std::vector vertices = std::vector(8);

PxVec3 geometry = PxVec3(2.0f,2.0f,2.0f);

vertices[0] = PxVec3(-geometry.x, geometry.y, geometry.z);//left-up-front
vertices[1] = PxVec3(geometry.x, geometry.y, geometry.z);//right-up-front
vertices[2] = PxVec3(-geometry.x, -geometry.y, geometry.z);//left-bottom-front
vertices[3] = PxVec3(geometry.x, -geometry.y, geometry.z);//right-bottom-front

vertices[4] = PxVec3(-geometry.x, geometry.y, -geometry.z);//left-up-back
vertices[5] = PxVec3(geometry.x, geometry.y, -geometry.z);//right-up-back
vertices[6] = PxVec3(-geometry.x, -geometry.y, -geometry.z);//left-bottom-back
vertices[7] = PxVec3(geometry.x, -geometry.y, -geometry.z);//right-bottom-back

unsigned int indecies[24] = {
 0,2,3,1,
 1,3,7,5,
 4,6,2,0,
 4,0,1,5,
 2,6,7,3,
 6,4,5,7
};

{
 /*Render*/
 glPointSize(5);
 glColor3f(1.0f,1.0f,1.0f);


 glEnableClientState(GL_VERTEX_ARRAY);

 glVertexPointer(3, GL_FLOAT, 0, &vertices[0].x);
 glDrawArrays(GL_POINTS, 0, vertices.size());

 GLuint indexBuffer;
 glGenBuffers(1, &indexBuffer);
 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
 glBufferData(GL_ELEMENT_ARRAY_BUFFER, 24 * sizeof(unsigned int) , indecies, GL_STATIC_DRAW);

 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
 glDrawElements(
  GL_QUADS,
  24,
  GL_UNSIGNED_INT,
  (void*)0);

 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
 glDeleteBuffers(1, &indexBuffer);


 glDisableClientState(GL_VERTEX_ARRAY);

 glPointSize(1.0f);
}

沒有留言:

張貼留言