立方体的简单三维交互式几何变换这个学期对opengl的学习,使我对计算机图形学的一些算法过程有了更多的了解。因为对三维图形的显示比较感兴趣,就做了立方体的简单三维交互式几何变换。功能:键盘的方向键实现立方体的上下左右平移;A键,S键分别实现向前,向后旋转;J键,K键分别实现放大,缩小;C键退出。程序模块:1.该模块为绘制一个立方体。voidDrawBox(){glBegin(GL_QUADS);//前面glColor3f(1,0,0);glVertex3f(-1.0f,-1.0f,1.0f);//四边形的左下glVertex3f(1.0f,-1.0f,1.0f);//四边形的右下glVertex3f(1.0f,1.0f,1.0f);//四边形的右上glVertex3f(-1.0f,1.0f,1.0f);//四边形的左上//后面glColor3f(0,1,0);glVertex3f(-1.0f,-1.0f,-1.0f);//四边形的右下glVertex3f(-1.0f,1.0f,-1.0f);//四边形的右上glVertex3f(1.0f,1.0f,-1.0f);//四边形的左上glVertex3f(1.0f,-1.0f,-1.0f);//四边形的左下//顶面glColor3f(0,0,1);glVertex3f(-1.0f,1.0f,-1.0f);//四边形的左上glVertex3f(-1.0f,1.0f,1.0f);//四边形的左下glVertex3f(1.0f,1.0f,1.0f);//四边形的右下glVertex3f(1.0f,1.0f,-1.0f);//四边形的右上//底面glColor3f(1,1,0);glVertex3f(-1.0f,-1.0f,-1.0f);//四边形的右上glVertex3f(1.0f,-1.0f,-1.0f);//四边形的左上glVertex3f(1.0f,-1.0f,1.0f);//四边形的左下glVertex3f(-1.0f,-1.0f,1.0f);//四边形的右下//右面glColor3f(0,1,1);glVertex3f(1.0f,-1.0f,-1.0f);//四边形的右下glVertex3f(1.0f,1.0f,-1.0f);//四边形的右上glVertex3f(1.0f,1.0f,1.0f);//四边形的左上glVertex3f(1.0f,-1.0f,1.0f);//四边形的左下//左面glColor3f(1,0,1);glVertex3f(-1.0f,-1.0f,-1.0f);//四边形的左下glVertex3f(-1.0f,-1.0f,1.0f);//四边形的右下glVertex3f(-1.0f,1.0f,1.0f);//四边形的右上glVertex3f(-1.0f,1.0f,-1.0f);//四边形的左上glEnd();}2.该模块设置屏幕颜色和深度缓存,设置立方体的平移,缩放,旋转变换。voiddisplay(){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//清空颜色和深度缓存glMatrixMode(GL_MODELVIEW);glLoadIdentity();//gluLookAt(2.0,2.0,2.0,0.0,0.0,0.0,0.0,1.0,0.0);glTranslatef(0.0f+tx,0.0f+ty,-4.0f);//平移变换glScalef(1+sf,1+sf,1+sf);//缩放变换glRotatef(ratX,1.0f,0.0f,0.0f);//绕X轴的旋转变换glRotatef(ratY,0.0f,1.0f,0.0f);//绕Y轴的旋转变换glRotatef(ratZ,0.0f,0.0f,1.0f);//绕Z轴的旋转变换DrawBox();glFlush();glutSwapBuffers();}3.该模块为重绘回调函数,在窗口首次创建或用户改变窗口尺寸时被调用。voidreshape(intw,inth){glViewport(0,0,w,h);//指定视口的位置和大小glMatrixMode(GL_PROJECTION);glLoadIdentity();//glFrustum(-1.0,1.0,-1.0,1.0,3.1,10.0);//gluPerspective(45,1,0.1,10.0);glOrtho(-2.0,2.0,-2.0,2.0,2.0,10.0);}4.该模块为设置深度缓存和启动深度测试模式。voidinit(){glClearColor(1.0,1.0,1.0,1.0);glEnable(GL_DEPTH_TEST);//启动深度测试模式}5.该模块为设置键盘的交互式操作。voidmyKeyboard(unsignedcharkey,intx,inty){if(key=='a'||key=='A')ratX+=2;ratY+=2;ratZ+=2;if(key=='s'||key=='S')ratX-=2;ratY-=2;ratZ-=2;if(key=='j'||key=='J')sf+=0.1;if(key=='k'||key=='K')sf-=0.1;if(key=='c'||key=='C')exit(0);glutPostRedisplay();//重新调用绘制函数}6.该模块为设置键盘方向键的交互式操作。voidmyspecialKeyboard(intkey,intx,inty){if(key==GLUT_KEY_UP)ty+=0.1;if(key==GLUT_KEY_DOWN)ty-=0.1;if(key==GLUT_KEY_LEFT)tx-=0.1;if(key==GLUT_KEY_RIGHT)tx+=0.1;glutPostRedisplay();}7.主函数模块,实现设置显示模式(窗口大小,位置),调用函数操作。intmain(intargc,char**argv){glutInit(&argc,argv);//初始GLUT.glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGB);//设定显示模式glutInitWindowSize(400,400);//设定窗口大小glutInitWindowPosition(50,100);//设定窗口位置glutCreateWindow(立方体的简单三维交互式几何变换);//用前面指定参数创建glutReshapeFunc(reshape);//指定重绘回调函数glutDisplayFunc(display);//进行一些初始化工作glutKeyboardFunc(myKeyboard);//指定键盘回调函数glutSpecialFunc(myspecialKeyboard);//指定键盘回调函数init();glutMainLoop();//进入无穷循环,等待事件处理}功能截图:立方体的平移:上平移下平移:左平移:右平移:放大:缩小:旋转:源程序:#includestdlib.h#includeGL/glut.h#includewindows.hfloatratX=60;floatratY=60;floatratZ=60;floattx=0;floatty=0;floatsf=0;voidDrawBox(){glBegin(GL_QUADS);//前面glColor3f(1,0,0);glVertex3f(-1.0f,-1.0f,1.0f);//四边形的左下glVertex3f(1.0f,-1.0f,1.0f);//四边形的右下glVertex3f(1.0f,1.0f,1.0f);//四边形的右上glVertex3f(-1.0f,1.0f,1.0f);//四边形的左上//后面glColor3f(0,1,0);glVertex3f(-1.0f,-1.0f,-1.0f);//四边形的右下glVertex3f(-1.0f,1.0f,-1.0f);//四边形的右上glVertex3f(1.0f,1.0f,-1.0f);//四边形的左上glVertex3f(1.0f,-1.0f,-1.0f);//四边形的左下//顶面glColor3f(0,0,1);glVertex3f(-1.0f,1.0f,-1.0f);//四边形的左上glVertex3f(-1.0f,1.0f,1.0f);//四边形的左下glVertex3f(1.0f,1.0f,1.0f);//四边形的右下glVertex3f(1.0f,1.0f,-1.0f);//四边形的右上//底面glColor3f(1,1,0);glVertex3f(-1.0f,-1.0f,-1.0f);//四边形的右上glVertex3f(1.0f,-1.0f,-1.0f);//四边形的左上glVertex3f(1.0f,-1.0f,1.0f);//四边形的左下glVertex3f(-1.0f,-1.0f,1.0f);//四边形的右下//右面glColor3f(0,1,1);glVertex3f(1.0f,-1.0f,-1.0f);//四边形的右下glVertex3f(1.0f,1.0f,-1.0f);//四边形的右上glVertex3f(1.0f,1.0f,1.0f);//四边形的左上glVertex3f(1.0f,-1.0f,1.0f);//四边形的左下//左面glColor3f(1,0,1);glVertex3f(-1.0f,-1.0f,-1.0f);//四边形的左下glVertex3f(-1.0f,-1.0f,1.0f);//四边形的右下glVertex3f(-1.0f,1.0f,1.0f);//四边形的右上glVertex3f(-1.0f,1.0f,-1.0f);//四边形的左上glEnd();}voiddisplay(){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//清空颜色和深度缓存glMatrixMode(GL_MODELVIEW);glLoadIdentity();//gluLookAt(2.0,2.0,2.0,0.0,0.0,0.0,0.0,1.0,0.0);glTranslatef(0.0f+tx,0.0f+ty,-4.0f);//平移变换glScalef(1+sf,1+sf,1+sf);//缩放变换glRotatef(ratX,1.0f,0.0f,0.0f);//绕X轴的旋转变换glRotatef(ratY,0.0f,1.0f,0.0f);//绕Y轴的旋转变换glRotatef(ratZ,0.0f,0.0f,1.0f);//绕Z轴的旋转变换DrawBox();glFlush();glutSwapBuffers();}voidreshape(intw,inth)//重绘回调函数,在窗口首次创建或用户改变窗口尺寸时被调用{glViewport(0,0,w,h);//指定视口的位置和大小glMatrixMode(GL_PROJECTION);glLoadIdentity();//glFrustum(-1.0,1.0,-1.0,1.0,3.1,10.0);//gluPerspective(45,1,0.1,10.0);glOrtho(-2.0,2.0,-2.0,2.0,2.0,10.0);}voidinit(){glClearColor(1.0,1.0,1.0,1.0);glEnable(GL_DEPTH_TEST);//启动深度测试模式}voidmyKeyboard(unsignedcharkey,intx,inty){if(key=='a'||key=='A')ratX+=2;ratY+=2;ratZ+=2;if(key=='s'||key=='S')ratX-=2;ratY-=2;ratZ-=2;if(key=='j'||key==