OpenGL入门练习——点、直线和多边形

练习画圆、五角星、正弦曲线:

#include<GL/glut.h>
#include<math.h>
//const int n = 6;
//const GLfloat R = 0.5f;
const GLfloat Pi = 3.1415926536f;
const GLfloat factor = 0.1f;

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 };
	GLfloat PointB[2] = { bx, by };
	GLfloat PointC[2] = { 0.5, cy };
	GLfloat PointD[2] = { -0.5, cy };
	GLfloat PointE[2] = { -bx, by };

	glClear(GL_COLOR_BUFFER_BIT);
	glBegin(GL_LINE_LOOP);
	glVertex2fv(PointA);
	glVertex2fv(PointC);
	glVertex2fv(PointE);
	glVertex2fv(PointB);
	glVertex2fv(PointD);
	glVertex2fv(PointA);

	//glBegin(GL_POINTS);
	//glBegin(GL_LINES);
	/*glBegin(GL_POLYGON);
	for (int i = 0; i < n; i++)
	{
		glVertex2f(R*cos(2 * Pi / n*i), R*sin(2 * Pi / n*i));
	}*/
	//glVertex2f(0.0f, 0.0f);
	//glVertex2f(0.5f, 0.0f);
	glEnd();
	glFlush();
}

void myDisplay2(void)
{
	GLfloat x;
	glClear(GL_COLOR_BUFFER_BIT);

	glBegin(GL_LINES);
	glVertex2f(-1.0f, 0.0f);
	glVertex2f(1.0f, 0.0f);
	glVertex2f(0.0f, -1.0f);
	glVertex2f(0.0f, 1.0f);
	glEnd();

	glBegin(GL_LINE_STRIP);
	for (x = -1.0f / factor; x < 1.0f / factor; x = x + 0.01f)
	{
		glVertex2f(x*factor, sin(x)*factor);
	}
	glEnd();

	glFlush();
}
int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(400, 400);
	glutCreateWindow("OpenGL 2");
	glutDisplayFunc(&myDisplay2);
	glutMainLoop();
	return 0;
}

 

全部评论

相关推荐

牛客279957775号:铁暗恋
点赞 评论 收藏
分享
09-25 10:34
东北大学 Java
多面手的小八想要自然醒:所以读这么多年到头来成为时代车轮底下的一粒尘
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务