直线,顶点两两一对
glClearColor(0.0, 0.0, 0.0, 0.0);//清空背景颜色为黑色
glPointSize(4.0f);//设置点的大小
glBegin(GL_POINTS);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glLineWidth(4.0f);//设置线宽
glEnable(GL_LINE_STIPPLE);//启用点划模式
glLineStipple(3, 0xff00);//点划模式,可建立虚线
glColor3f(1.0, 0.0, 0.0);//设置颜色
glBegin(GL_LINES);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
连续线段,n个点n-1条线段
glBegin(GL_LINE_STRIP);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
连续线段成环,最后一个点和第一个点连接,n个点n条线段。
glBegin(GL_LINE_LOOP);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glBegin(GL_TRIANGLES);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glShadeModel(GL_FLAT);//块模式,颜色为三角形最后一个点的颜色
//glShadeModel(GL_SMOOTH); //为默认,是平滑模式(渐变效果)
glBegin(GL_TRIANGLE_STRIP);
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.5, -0.5);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(-0.5, 0.5);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(0.5, 0.5);
glColor3f(1.0, 1.0, 0.0);
glVertex2f(0.5, 1.0);
glEnd();
扇形
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);//设置线框模式还有GL_FILL填充模式
glBegin(GL_TRIANGLE_FAN);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glBegin(GL_QUADS);
glClearColor(0.0, 0.0, 0.0, 0.0);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glBegin(GL_QUAD_STRIP);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
const int max_v = 100;
float x[max_v];
float y[max_v];
float r = 0.5f;
float alpha = 2 * M_PI / max_v;
for (int i = 0; i < max_v; i++) {
y[i] = r * sin(alpha*i);
x[i] = r * cos(alpha*i);
//printf("%d(%.2lf,%.2lf)\n", i, x[i], y[i]);
}
/* draw unit square polygon */
//glPolygonStipple();
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_POLYGON);
glClearColor(0.0, 0.0, 0.0, 0.0);
for (int i = 0; i < max_v; i++) {
glVertex2f(x[i], y[i]);
}
glEnd();