glColor3f(1.0,0.0,0.0);//设置颜色
glBegin(GL_TRIANGLES);//开始绘制,参数可选
glEnd();
1.绘制矩形
glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
2.绘制点
glVertex2f(-1.0,-1.0);//设置第一个顶点的坐标为(-1.0,-1.0)
3.绘制线
glVertex2f(0.0,-1.0);//设置第1个顶点的坐标为(0.0,-1.0)
glVertex2f(-0.5,1.0);//设置第2个顶点的坐标为(-0.5,1.0)
4.绘制多边形
glVertex2f(-1.0,-1.0);//设置第一个顶点的坐标为(-1.0,-1.0)
glVertex2f(0.0,-1.0);//设置第二个顶点的坐标为(0.0,-1.0)
glVertex2f(-0.5,1.0);//设置第三个顶点的坐标为(-0.5,1.0)
5.绘制圆
#include <math.h>
const int n = 20;
const GLfloat R = 0.5f;
const GLfloat Pi = 3.1415926536f;
void myDisplay(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
for(i=0; i<n; ++i)
glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));
glEnd();
glFlush();
}
6.绘制五角星
#include <math.h>
const GLfloat Pi = 3.1415926536f;
void myDisplay(void)
{
GLfloat a = 1 / (2-2*cos(72*Pi/180));
GLfloat bx = a * cos(18 * Pi/180);
GLfloat by = a * sin(18 * Pi/180);
GLfloat cy = -a * cos(18 * Pi/180);
GLfloat
PointA[2] = { 0, a },
PointB[2] = { bx, by },
PointC[2] = { 0.5, cy },
PointD[2] = { -0.5, cy },
PointE[2] = { -bx, by };
glClear(GL_COLOR_BUFFER_BIT);
// 按照A->C->E->B->D->A的顺序,可以一笔将五角星画出
glBegin(GL_LINE_LOOP);
glVertex2fv(PointA);
glVertex2fv(PointC);
glVertex2fv(PointE);
glVertex2fv(PointB);
glVertex2fv(PointD);
glEnd();
glFlush();
}
7.绘制正玄线
#include <math.h>
const GLfloat factor = 0.1f;
void myDisplay(void)
{
GLfloat x;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glVertex2f(-1.0f, 0.0f);
glVertex2f(1.0f, 0.0f); // 以上两个点可以画x轴
glVertex2f(0.0f, -1.0f);
glVertex2f(0.0f, 1.0f); // 以上两个点可以画y轴
glEnd();
glBegin(GL_LINE_STRIP);
for(x=-1.0f/factor; x<1.0f/factor; x+=0.01f)
{
glVertex2f(x*factor, sin(x)*factor);
}
glEnd();
glFlush();
}