OpenGL画三角形 圆 五角星 菱形

不开心,想玩,当家的不许我玩,还说你要玩就玩,那我不管你了,哭哭TAT


用OPenGL原有的画三角形,正方形,点的函数来作图。

画圆是用化曲为直的思想,把圆分为很多分,越分的多越细,越像圆。

画菱形和圆的思想一样,来确定菱形顶点坐标。只是把圆周分的份数少一些


#include "stdafx.h"
#include<gl/glut.h>
#include<math.h>
#include <stdlib.h>
const double PI = 3.14159265357f;
const double R = 0.5f;
const int n = 150;

void myDisplay(void)
{

	glClearColor(1.0, 1.0, 1.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	//画倒三角
	glBegin(GL_TRIANGLES);
	glColor3f(1.0f, 0.0f, 0.0f);  glVertex2f(0.0f, -1.0f);
	glColor3f(0.0f, 1.0f, 0.0f);  glVertex2f(-1.0f, 1.0f);
	glColor3f(1.0f, 1.0f, 0.0f);  glVertex2f(1.0f, 1.0f);
	glEnd();
	//画圆,思想:化曲为直
	glBegin(GL_POLYGON);
	for (int i = 1; i <= n; i++){
		glVertex2f((R*cos(2 * PI / n*i)), R*sin(2 * PI / n*i));
		glColor3f(1.0f, 0.0f, 1.0f);
	}
	glEnd();
	//画五角星
	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 pointB[2] = { bx, by },
		pointC[2] = { 0.5, cy },
		pointD[2] = { -0.5, cy },
		pointE[2] = { -bx, by },
		pointA[2] = { 0, a };

	//按照A->C->E->B->D->A的顺序一笔画出五角星
	glBegin(GL_LINE_LOOP);
	glVertex2fv(pointA);
	glVertex2fv(pointC);
	glVertex2fv(pointE);
	glVertex2fv(pointB);
	glVertex2fv(pointD);
	glVertex2fv(pointA);
	glEnd();
	//画两个小三角
	glBegin(GL_TRIANGLES);
	glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.8f, -0.8f);
	glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(-0.7f, -0.8f);
	glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(-0.75f, -0.7f);
	glEnd();

	glBegin(GL_TRIANGLES);
	glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.8f, -0.8f);
	glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.7f, -0.8f);
	glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(0.75f, -0.7f);
	glEnd();
	glFlush();

}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(200, 200);
	glutInitWindowSize(400, 400);
	glutCreateWindow("Hellow World!");
	glutDisplayFunc(&myDisplay);
	glutMainLoop();
	return 0;
}

下图为程序1 2 的运行结果




#include "stdafx.h"
#include<gl/glut.h>
#include<math.h>
#include <stdlib.h>
void myDisplay(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);

	//画白色正方形
	glColor3f(1.0f, 1.0f, 1.0f);
	glRectf(-0.5f, -0.5f, 0.5f, 0.5f);//这个函数只能画正的正方形

	//画红色正方形
	glBegin(GL_POLYGON);
	glColor3f(1.0f, 0.0f, 0.0f); 
	glVertex2f(0.0f, 0.5f);glVertex2f(0.5f, 0.0f);
	glVertex2f(0.0f, -0.5f);
	
	glVertex2f(-0.5f, 0.0f);
	glEnd();

	//画三角形
	glBegin(GL_TRIANGLES);
	glColor3f(0.2f, 0.4f, 0.8f);  glVertex2f(0.5f, 0.5f);
	glColor3f(0.2f, 0.4f, 0.8f);  glVertex2f(-0.5f, 0.5f);
	glColor3f(0.2f, 0.4f, 0.8f);  glVertex2f(0.0f, 1.0f);
	glEnd();

	glBegin(GL_TRIANGLES);
	glColor3f(0.9f, 0.9f, 0.0f);  glVertex2f(0.5f, 0.5f);
	glColor3f(0.9f, 0.9f, 0.0f);  glVertex2f(0.5f, -0.5f);
	glColor3f(0.9f, 0.9f, 0.0f);  glVertex2f(1.0f, 0.0f);
	glEnd();

	glBegin(GL_TRIANGLES);
	glColor3f(0.0f, 1.0f, 0.0f);  glVertex2f(0.5f, -0.5f);
	glColor3f(0.0f, 1.0f, 0.0f);  glVertex2f(-0.5f, -0.5f);
	glColor3f(0.0f, 1.0f, 0.0f);  glVertex2f(0.0f, -1.0f);
	glEnd();

	glBegin(GL_TRIANGLES);
	glColor3f(0.9f, 0.5f, 0.9f);  glVertex2f(-0.5f, 0.5f);
	glColor3f(0.9f, 0.5f, 0.9f);  glVertex2f(-0.5f, -0.5f);
	glColor3f(0.9f, 0.5f, 0.9f);  glVertex2f(-1.0f, 0.0f);
	glEnd();

	glFlush();

}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(200, 200);
	glutInitWindowSize(400, 400);
	glutCreateWindow("Hellow World!");
	glutDisplayFunc(&myDisplay);
	glutMainLoop();
	return 0;
}




#include "stdafx.h"
#include<gl/glut.h>
#include<math.h>
#include <stdlib.h>
const double PI = 3.14159265357f;


void myDisplay(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	//红色菱形
	glBegin(GL_POLYGON);
	glColor3f(1.0f, 0.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(0.5f, 0.5 *(tan(22.5*PI/180)));
	glVertex2f(1.0f, 0.0f);
	glVertex2f(0.5f, -0.5 *(tan(22.5*PI/180)));
	glEnd();

	glBegin(GL_POLYGON);
	glColor3f(1.0f, 0.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(-0.5f, 0.5 *(tan(22.5*PI / 180)));
	glVertex2f(-1.0f, 0.0f);
	glVertex2f(-0.5f, -0.5 *(tan(22.5*PI / 180)));
	glEnd();

	glBegin(GL_POLYGON);
	glColor3f(1.0f, 0.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(0.5*(tan(22.5*PI / 180)),0.5f);
	glVertex2f(0.0f, 1.0f);
	glVertex2f(-0.5*(tan(22.5*PI / 180)),0.5f);
	glEnd();

	glBegin(GL_POLYGON);
	glColor3f(1.0f, 0.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(0.5*(tan(22.5*PI / 180)), -0.5f);
	glVertex2f(0.0f, -1.0f);
	glVertex2f(-0.5*(tan(22.5*PI / 180)), -0.5f);
	glEnd(); 

	//绿色菱形
	glBegin(GL_POLYGON);
	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(0.5f, 0.5 *(tan(22.5*PI / 180)));
	glVertex2f(cos(45 * PI / 180), cos(45 * PI / 180));
	glVertex2f(0.5*(tan(22.5*PI / 180)), 0.5f);
	glEnd();

	glBegin(GL_POLYGON);
	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(0.5f, -0.5 *(tan(22.5*PI / 180)));
	glVertex2f(cos(45 * PI / 180), -cos(45 * PI / 180));
	glVertex2f(0.5*(tan(22.5*PI / 180)), -0.5f);
	glEnd();

	glBegin(GL_POLYGON);
	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(-0.5f, 0.5 *(tan(22.5*PI / 180)));
	glVertex2f(-cos(45 * PI / 180), cos(45 * PI / 180));
	glVertex2f(-0.5*(tan(22.5*PI / 180)), 0.5f);
	glEnd();

	glBegin(GL_POLYGON);
	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(-0.5f, -0.5 *(tan(22.5*PI / 180)));
	glVertex2f(-cos(45 * PI / 180), -cos(45 * PI / 180));
	glVertex2f(-0.5*(tan(22.5*PI / 180)), -0.5f);
	glEnd();

	glFlush();

}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(200, 200);
	glutInitWindowSize(400, 400);
	glutCreateWindow("Hellow World!");
	glutDisplayFunc(&myDisplay);
	glutMainLoop();
	return 0;
}

运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值