概括、openGL中所有的图形都是三维的,这里所说的二维是三维图形投影到二维窗口的结果。
void gl_point(void) {
int vpoint[]={200,200,0};
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,1.0f,0.0f);
glPointSize(10.0);//设置点的大小
glBegin(GL_POINTS);
glVertex2i(0,0);
glVertex2i(50,50);
glVertex3i(100,100,0);
glVertex3iv(vpoint);
glEnd();
glFlush();
}
void gl_line(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glBegin(GL_LINES);
glVertex2i(0,0);//前后两点成一线,最后的奇数点省略
glVertex2i(100,100);
glVertex2i(100,100);
glVertex2i(200,0);
glEnd();
glBegin(GL_LINE_STRIP);//前后两点依次连线
glVertex2i(200,0);
glVertex2i(300,100);
glVertex2i(400,0);
glVertex2i(500,100);
glVertex2i(600,0);
glEnd();
glLineWidth(2.0);//设置线宽
glEnable(GL_LINE_STIPPLE);
glLineStipple(1,0x8888); //设置虚线,后面的16进制参数代表256个像素的虚实情况
glBegin(GL_LINE_LOOP);//前后两点依次连线且首尾两点相连
glVertex2i(200,150);
glVertex2i(400,150);
glVertex2i(300,350);
glEnd();
glDisable(GL_LINE_STIPPLE);
glShadeModel(GL_SMOOTH);//设置渐变色
glBegin(GL_LINE_STRIP);
glColor3f(1,0,0);
glVertex2i(50,120);
glColor3f(0,1,0);
glVertex2i(350,120);
glColor3f(0,0,0);
glVertex2i(550,120);
glEnd();
glFlush();
}
void gl_rect(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,1,0);
glRecti(0,0,100,100);//矩形填充区
glBegin(GL_POLYGON); //多边形填充区
glVertex2i(200,0);
glVertex2i(400,0);
glVertex2i(300,100);
glEnd();
glFlush();
}