OpenGl实验3

#include <gl/glut.h>

static GLsizei iMode = 1; 

static GLfloat sx = 1.0;

static GLfloat sy = 1.0;

static GLfloat tx = 0.0f;

static GLfloat ty = 0.0f;

void Initial(void)

{

		glClearColor(1.0f, 1.0f, 1.0f, 1.0f);      //设置窗口背景颜色为白色

		gluOrtho2D(-30.0,30.0,-20.0,20.0);//居中显示

}

void ChangeSize(int w,int h)

{

	glViewport(0,0,w,h);

	glMatrixMode(GL_PROJECTION);     //设置投影参数

	glLoadIdentity();

	gluOrtho2D(-30.0,30.0,-20.0,20.0);//居中显示

}

void Display(void)

{

		glClear(GL_COLOR_BUFFER_BIT);   //用当前背景色填充窗口

		glMatrixMode(GL_PROJECTION);     //设置投影参数

		glLoadIdentity();



		glTranslatef(tx, 0.0f, 0.0f);     

		glTranslatef(0.0f, ty, 0.0f); 

		glScalef(sx,sy,0.0f);//左键每一次点击扩大0.1

		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;

	case 'S':

		sx+=0.1;

		sy+=0.1;

		glutPostRedisplay();

		break;

	case 'D':

		sx-=0.1;

		sy-=0.1;

		glutPostRedisplay();

		break;

	default:

		break;

	}

}

//如果是鼠标键控制旋转,可以参考p93

void SpecialKeys(int key,int x,int y)

{

	if(key == GLUT_KEY_UP)	ty += 0.1f;

	if(key == GLUT_KEY_DOWN) ty -= 0.1f;

	if(key == GLUT_KEY_LEFT) tx -= 0.1f;

	if(key == GLUT_KEY_RIGHT) tx += 0.1f;

	

	glutPostRedisplay();

}

//鼠标交互调用函数,左键缩小,右键扩大

void MousePlot(GLint button,GLint action,GLint xMouse,GLint yMouse)

{

	if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)

	{

		sx+=0.1;

		sy+=0.1;

	}

	if(button ==GLUT_RIGHT_BUTTON && action == GLUT_DOWN)

	{

		sx-=0.1;

		sy-=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);			//普通键输入注册函数

		glutSpecialFunc(SpecialKeys);

		glutMouseFunc(MousePlot);		//注册鼠标响应函数

		//glutMotionFunc(MouseMove);//鼠标按下并移动时的响应函数

		//glutReshapeFunc(ChangeSize);



		glutDisplayFunc(Display);                     //设置当前窗口的显示回调函数

		Initial();                                    //完成窗口初始化

		glutMainLoop();                             //启动主GLUT事件处理循环

		return 0;

}


你可能感兴趣的:(OpenGL)