参看:http://www.cppblog.com/doing5552/archive/2009/01/08/71532.html
20边形
#include<gl/GLUT.H>
#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(); //立即执行前面的命令,而不是在缓冲区中等待
}
int main(int argc, char *argv[])
{
glutInit(&argc,argv); //对GLUT进行初始化,必须在其它的GLUT使用之前调用一次
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); //设置显示方式,使用RGB颜色,单缓冲
glutInitWindowPosition(100, 100); //设置窗口在屏幕中的位置
glutInitWindowSize(400, 400); //设置窗口的大小
glutCreateWindow("opengl"); //创建窗口,窗口标题为“opengl”
glutDisplayFunc(&myDisplay); //设置画图的函数
glutMainLoop(); //消息循环
return 0;
}
sin曲线
#include<gl/GLUT.H>
#include<math.h>
const GLfloat factor = 0.1f;
void myDisplay(void)
{
GLfloat x;
glClear(GL_COLOR_BUFFER_BIT); //清除颜色
glBegin(GL_LINES); //每两个顶点为一条直线模式
glVertex2f(-1.0, 0); //x轴
glVertex2f(1, 0);
glVertex2f(0, -1.0); //y轴
glVertex2f(0, 1.0);
glEnd();
glBegin(GL_LINE_STRIP); //所有的点相连模式
for(x=-1.0f/factor; x<1.0/factor; x+=0.01f)
glVertex2f(x*factor, sin(x)*factor);
glEnd();
glFlush(); //立即执行前面的命令,而不是在缓冲区中等待
}