顶点数组
当需要画一个较复杂的图形时(处理多个图元),顶点数组可以避免多次函数调用的开销,使程序更快。
因为还没有学到如何画三维,目前画出来的图形貌似被遮挡了一堆)
#include<Windows.h>
#include<GL/glut.h>
GLint p[8][3] =
{
{0, 0, 0},
{0, 1, 0},
{1, 0, 0},
{1, 1, 0},
{0, 0, 1},
{0, 1, 1},
{1, 0, 1},
{1, 1, 1},
};
GLubyte vertexIndex[] = {6, 2, 3, 7, 5, 1, 0, 4, 7, 3, 1, 5, 4, 0, 2, 6, 2, 0, 1, 3, 7, 5, 4, 6};
void drawFace(int n1, int n2, int n3, int n4)
{
glBegin(GL_QUADS);
{
glVertex3iv(p[n1]);
glVertex3iv(p[n2]);
glVertex3iv(p[n3]);
glVertex3iv(p[n4]);
}
glEnd();
}
void simpleDraw()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3d(0.3, 0.4, 0.5);
//为什么只看得到一个面,因为把后面都挡住了)
//每次画面的时候,都是从外表面上来看的逆时针方向画的
drawFace(6, 2, 3, 7);
drawFace(5, 1, 0, 4);
drawFace(7, 3, 1, 5);
drawFace(4, 0, 2, 6);
drawFace(2, 0, 1, 3);
drawFace(7, 5, 4, 6);
glFlush();
}
void vertexArrayDraw()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3d(0.2, 0.4, 0.5);
//激活客户/服务器系统中客户端的能力(此时是顶点数组)
//个人理解是将额外的信息(即顶点数组的信息)存储到客户/服务器中
glEnableClientState(GL_VERTEX_ARRAY);
/*
*提供对象的顶点坐标的位置和格式
*@param size 每一个顶点描述中的坐标数目(有几维)
*@param type 顶点坐标的数据类型
*@param stride 连续顶点之间的字节位移(这里的数组只有坐标,所以位移是0)
*@param pointer 指向包含顶点坐标值的顶点的数组
*/
glVertexPointer(3, GL_INT, 0, p);
/*
*处理多个图元而仅需少量的函数调用
*@param mode 图元的使用
*@param count 指定顶点数组中元素数量
*@param type 索引值的类型(数组下标的大小)
*@param indices 对应下标的数组
*/
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, vertexIndex);
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("draw a cube");
glClearColor(1.0, 1.0, 1.0, 0.0);
glutDisplayFunc(vertexArrayDraw);
glutMainLoop();
}