按下图所示依次绘制出10种基本图元的图形,开启点、线、多边形抗锯齿,并且绘制多边形时在不同顶点指定不同颜色使多边形呈渐变色彩。
图元GL_POINTS的绘制
glPointSize(5.0f); //设置点的大小
glBegin(GL_POINTS);
glColor3f(1, 1, 0);
glVertex2f(0, 0);
glVertex2f(0.1, -0.1);
glVertex2f(0.2, -0.1);
glVertex2f(0.3, 0);
glVertex2f(0.4, 0.1);
glEnd();
//线的绘制
glLineWidth(0.5f); //设置线的宽度
glBegin(GL_LINES);
glColor3f(1, 1, 0);
glVertex2f(0, 0);
glVertex2f(0.5, 0.2);
glColor3f(1, 0, 0);
glVertex2f(0.4, 0.1);
glVertex2f(1.0, -0.1);
glColor3f(1, 0, 1);
glVertex2f(0.2, -0.2);
glVertex2f(0.6, -0.3);
glColor3f(0, 0, 1);
glVertex2f(0.1, -0.3);
glVertex2f(0.6, -0.15);
glEnd();
//画线段
glLineWidth(0.5f);
glBegin(GL_LINE_STRIP);
glColor3f(1, 1, 0);
glVertex2f(0, 0);
glVertex2f(0.7,-0.4);
glVertex2f(0.6, -0.1);
glVertex2f(0.1,-0.2);
glVertex2f(0.05,-0.4);
glEnd();
glBegin(GL_LINE_LOOP);
glLineWidth(0.5f);
glColor3f(1, 1, 0);
glVertex2f(0, 0);
glVertex2f(0.2, -0.4);
glVertex2f(0.7, -0.4);
glVertex2f(0.6, -0.03);
glVertex2f(0.5, -0.3);
glVertex2f(0.3, 0.2);
glEnd();
//GL_POLYGON
glBegin(GL_POLYGON);
glColor3f(1, 1, 0);
glVertex2f(0, 0);
glColor3f(1, 0, 0);
glVertex2f(0.7,0.2);
glColor3f(0, 0, 1);
glVertex2f(0.6,-0.3);
glColor3f(1, 0, 1);
glVertex2f(-0.05, -0.5);
glColor3f(0, 1, 1);
glVertex2f(-0.1, -0.3);
glEnd();
glBegin(GL_QUADS); //图元
glColor3f(0, 1, 0);
glVertex2f(0, 0);
glColor3f(1, 0, 0);
glVertex2f(0.1, 0.15);
glColor3f(0, 0, 1);
glVertex2f(0.4, 0.2);
glColor3f(1, 0,1);
glVertex2f(0.45, -0.1);
glColor3f(0, 0, 1);
glVertex2f(0.5, -0.2);
glColor3f(1, 0, 1);
glVertex2f(0.6, 0.25);
glColor3f(1, 0, 0);
glVertex2f(0.9, 0.4);
glColor3f(0, 1, 0);
glVertex2f(0.7, -0.2);
glEnd();
glBegin(GL_QUAD_STRIP); //图元
glColor3f(1, 0, 0);
glVertex2f(0, 0);
glColor3f(0, 1, 0);
glVertex2f(-0.05, 0.3);
glColor3f(1, 1, 0);
glVertex2f(0.27, 0);
glColor3f(0, 0, 1);
glVertex2f(0.3, 0.33);
glColor3f(0, 1, 1);
glVertex2f(0.7, 0.03);
glColor3f(1, 0, 1);
glVertex2f(0.6, 0.4);
glColor3f(0, 0, 1);
glVertex2f(0.8, 0.06);
glColor3f(1, 1, 0);
glVertex2f(0.78, 0.5);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(1, 1, 0);
glVertex2f(0, 0);
glColor3f(0, 0, 1);
glVertex2f(0, 0.3);
glColor3f(0, 1, 0);
glVertex2f(0.4, 0);
glColor3f(1, 0, 1);
glVertex2f(0.4, 0.2);
glColor3f(0, 1, 1);
glVertex2f(0.1, 0.5);
glColor3f(1,0,0);
glVertex2f(0.7, 0.5);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glColor3f(1, 1, 0);
glVertex2f(0, 0.3);
glColor3f(1, 0, 1);
glVertex2f(0, 0);
glColor3f(0, 1, 1);
glVertex2f(0.3, 0.3);
glColor3f(1, 0, 0);
glVertex2f(0.4,0);
glColor3f(1, 0, 1);
glVertex2f(0.5, 0.3);
glColor3f(1, 1, 0);
glVertex2f( 0.6, - 0.1);
glEnd();
glBegin(GL_TRIANGLE_FAN);
glColor3f(1, 0, 0);
glVertex2f(0, 0);
glColor3f(1, 0, 1);
glVertex2f(0, 0.4);
glColor3f(1, 1, 0);
glVertex2f(0.3, 0.3);
glColor3f(1, 0, 0);
glVertex2f(0.5, 0.04);
glColor3f(0, 1, 0);
glVertex2f(0.47, -0.23);
glEnd();