OpenGl实验2

#include <gl/glut.h>
static GLsizei iMode = 1; 
static GLfloat sx = 1.0;
static GLfloat sy = 1.0;
void Initial(void)
{
		glClearColor(1.0f, 1.0f, 1.0f, 1.0f);      //设置窗口背景颜色为白色
		glMatrixMode(GL_PROJECTION);     //设置投影参数
		gluOrtho2D(-30.0,30.0,-20.0,20.0);//居中显示
}

void Display(void)
{
		glClear(GL_COLOR_BUFFER_BIT);   //用当前背景色填充窗口
		glColor3f(1.0f, .0f, 0.0f);            //设置当前的绘图颜色为红色


		switch(iMode)
		{
		case 1:
			glColor3f(.0f, 0.0f, 0.0f); //黑
			break;
		case 2:
			glColor3f(1.0f, .0f, .0f); //红
			break;
		case 3:
			glColor3f(0.0f, .0f, 1.0f); //蓝
			break;
		case 4:
			glColor3f(.0f, 1.0f, 0.0f); //绿
			break;
		case 5:
			glColor3f(1.0f, 1.0f, 1.0f); //白
			break;
		case 6:
			glDisable(GL_LINE_STIPPLE);
			break;
		case 7:
			glEnable(GL_LINE_STIPPLE);
			glLineStipple(1,255);
			break;
		default :
			break;
		}


		//多边形的绘制
		glBegin(GL_LINE_LOOP);
			glVertex2f(-10,-10);
			glVertex2f(10,-10);
			glVertex2f(20,0);
			glVertex2f(0,10);
			glVertex2f(-20,0);
		glEnd();

		glFlush();                         //处理所有的OpenGL程序
}

void ProcessMenu(int value)
{
	iMode = value;
	glutPostRedisplay();
}


//键盘交互调用函数
void Key(unsigned char key,int x,int y)
{
	switch(key)
	{
	case 'q':
		exit(0);
		break;
	case 'Q':
		exit(0);
		break;
	default:
		break;
	}
}

//鼠标交互调用函数
void MousePlot(GLint button,GLint action,GLint xMouse,GLint yMouse)
{
	if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
	{
		glScalef(sx+0.1,sy+0.1,0.0f);//左键每一次点击扩大0.1
	}
	if(button ==GLUT_RIGHT_BUTTON && action == GLUT_DOWN)
	{
		glScalef(sx-0.1,sy-0.1,0.0f);//右键每一次点击扩大0.1
	}
	glutPostRedisplay();
}

int main(int argc, char* argv[])
{
		glutInit(&argc, argv);
		glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  //初始化窗口的显示模式
		glutInitWindowSize(800,800);                  //设置窗口的尺寸
		glutInitWindowPosition(600,120);               //设置窗口的位置
		glutCreateWindow("矩形");                    //创建一个名为矩形的窗口

		
		//创建菜单
		int color= glutCreateMenu(ProcessMenu);
		glutAddMenuEntry("黑色",1);
		glutAddMenuEntry("红色",2);
		glutAddMenuEntry("蓝色",3);
		glutAddMenuEntry("绿色",4);
		glutAddMenuEntry("白色",5);
		int lineStrpple = glutCreateMenu(ProcessMenu);
		glutAddMenuEntry("直线",6);
		glutAddMenuEntry("虚线",7);
		int nMainMenu = glutCreateMenu(ProcessMenu);
		glutAddSubMenu("颜色",color);
		glutAddSubMenu("线型",lineStrpple);
		glutAttachMenu(GLUT_MIDDLE_BUTTON);//与鼠标中键绑定


		glutKeyboardFunc(Key);			//普通键输入注册函数
		glutMouseFunc(MousePlot);		//注册鼠标响应函数

		glutDisplayFunc(Display);                     //设置当前窗口的显示回调函数
		Initial();                                    //完成窗口初始化
		glutMainLoop();                             //启动主GLUT事件处理循环
		return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值