1. 程式人生 > >openGL圓,正弦函式,正六邊形繪製

openGL圓,正弦函式,正六邊形繪製

//繪製正六邊形,餘弦和圓
#include<GL/glut.h>
#include<math.h>
const GLfloat pi = 3.1415926f;
int n = 1000;
const GLfloat factor = 0.1f;
void display()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glBegin(GL_POLYGON);
	glColor3f(1.0,0.0,0.0);
	for (int i = 0; i < n; i++)
	{
		glVertex2f(0.4f*cos(2 * (pi / n)*i) - 0.5f, 0.4f*sin(2 * (pi / n)*i)+0.5f);
	}
	glEnd();
	glFlush();
	glBegin(GL_LINE_LOOP);
	glColor3f(0.0f,0.0f,0.0f);
	for (int i = 1; i <= 6; i++)
	{
		glVertex2f(0.4f*cos(2 * (pi / 6)*i)+0.5f, 0.4f*sin(2 * (pi / 6)*i) + 0.5f);
	}
	glEnd();
	glFlush();
	glBegin(GL_LINE_STRIP);
	glColor3f(0.0f, 0.0f, 0.0f);
	for (double x = -1.0f / factor; x<1.0f / factor; x += 0.01f)
	{
		glVertex2f(x*factor, sin(x)*factor - 0.50);
	}
	glEnd();
	glFlush();
	glBegin(GL_LINES);
	glColor3f(0.0f, 0.0f, 1.0f);
		glVertex2f(-1.0f,0.0f);
		glVertex2f(1.0f, 0.0f);
		glVertex2f(0.0f,-1.0f);
		glVertex2f(0.0f,1.0f);
	glEnd();
	glFlush();
}
void init()
{
	glClearColor(0.0,1.0,1.0,0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(-1.0f,1.0f,-1.0f,1.0f);
}
int main(int argc,char*argv[])
{
	glutInit(&argc,argv);     //初始化
	glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);  //選擇rgb,單緩衝
	glutInitWindowPosition(300,100);   //窗體位置
	glutInitWindowSize(600,600);        //窗體大小
	glutCreateWindow("六邊形和圓和餘弦函式");
	init();
	glutDisplayFunc(display);
	glutMainLoop();
	return 0;
}

效果: