1、实验三 观察三维物体姓名 叶传军 学号 E11414103 得分 一.实验题目1. 阅读 arraycube.c,掌握彩色立方体的建模方法,为程序加注释。2. 修改 arraycube.c,实现交互式地移动照相机来观察已经建模好的彩色立方体。即用鼠标或键盘来改变 gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz)函数的 9 个参数,以此来观察立方体。要求:(1)交互时采用的鼠标和键盘按键自定。(2)分别在正投影和透视投影下实现题目中的功能。(3)可增加菜单功能,将正投影和透视投影下的观察功能融合到一个程序中。可参考交互式教程 Pr
2、ojection.c 的功能。3. 在 arraycube.c 的基础上编写一个交互式程序,实现立方体的旋转。具体要求如下:(1)立方体的旋转方式由鼠标和键盘按键来控制:按下鼠标左键,立方体绕 x 轴连续旋转;按下鼠标左键+ctrl 键,立方体绕 y 轴连续旋转;按下鼠标右键,立方体绕 z 轴连续旋转。(注意:旋转的不动点在原点,正好是立方体的中心。 )(2) 如果旋转的不动点不在原点,而改为点 P(1,1,1) ,如何实现立方体绕 3 个坐标轴轴的旋转?(3)如果要求每按下一次鼠标按键或键盘按键,立方体旋转的角度增加 5 度,应如何修改程序?4. 自学教材 4.2.7 节。编写一个交互式程序
3、,使其可以通过鼠标(或键盘)和菜单实现一些 glu 和 glut 对象的旋转,平移和比例缩放。 (加自己的创意)二.设计思想本次试验主要是用户交互与三维物体观察的结合,主要用到的函数有gluPerspective(fovy, aspect, near, far);glOrtho(left, right, bottom, top, near, far);gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz);通过这些函数的调用可以分别在正投影和透视投影下从不同的角度来观察三维物体。三.程序清单1. #include“stdafx.h“#
4、include #include GLdouble vertices83= -1.0, -1.0, 1.0,?-1.0,1.0,1.0,1.0,1.0,1.0,?1.0,-1.0,1.0,-1.0,-1.0,-1.0, -1.0,1.0,-1.0, 1.0,1.0,-1.0, 1.0,-1.0,-1.0;GLdouble colors83=0.0,0.0,0.0, 1.0,0.0,0.0, 1.0,1.0,0.0,0.0,1.0,0.0, 0.0,0.0,1.0,1.0,0.0,1.0, 1.0,1.0,1.0,0.0,1.0,1.0;/定义顶点和颜色的全局数组void polygon(int
5、 a, int b, int c , int d)/根据索引列表绘制多边形/* draw a polygon via list of vertices */glBegin(GL_QUADS);glColor3dv(colorsa);glVertex3dv(verticesa);glColor3dv(colorsb);glVertex3dv(verticesb);glColor3dv(colorsc);glVertex3dv(verticesc);glColor3dv(colorsd);glVertex3dv(verticesd);glEnd();void colorcube()/* map v
6、ertices to faces */利用表面绘制立方体polygon(0,3,2,1);polygon(2,3,7,6);polygon(0,4,7,3);polygon(1,2,6,5);polygon(4,5,6,7);polygon(0,1,5,4);void display()/* display callback, clear frame buffer and z buffer,/and draw, swap buffers */显示回调函数glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODE
7、LVIEW);glLoadIdentity();gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);colorcube();glutSwapBuffers();void myReshape(int w, int h)/设置投影方式glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if (w #include GLdouble vertices83= -1.0, -1.0, 1.0,-1.0,1.0,1.0,1.0,1.0,1.0,1.0,-1.0,1.
8、0,-1.0,-1.0,-1.0, -1.0,1.0,-1.0, 1.0,1.0,-1.0, 1.0,-1.0,-1.0;GLdouble colors83=0.0,0.0,0.0, 1.0,0.0,0.0, 1.0,1.0,0.0,0.0,1.0,0.0, 0.0,0.0,1.0,1.0,0.0,1.0, 1.0,1.0,1.0,0.0,1.0,1.0;/定义顶点和颜色的全局数组int x1=1,y1=1,z1=1,x2=0,y2=0,z2=0,x3=0,y3=1,z3=0;void polygon(int a, int b, int c , int d)/根据索引列表绘制多边形/* dra
9、w a polygon via list of vertices */glBegin(GL_QUADS);glColor3dv(colorsa);glVertex3dv(verticesa);glColor3dv(colorsb);glVertex3dv(verticesb);glColor3dv(colorsc);glVertex3dv(verticesc);glColor3dv(colorsd);glVertex3dv(verticesd);glEnd();void colorcube()/利用表面绘制立方体/* map vertices to faces */polygon(0,3,2,
10、1);polygon(2,3,7,6);polygon(0,4,7,3);polygon(1,2,6,5);polygon(4,5,6,7);polygon(0,1,5,4);void display()/显示回调函数/* display callback, clear frame buffer and z buffer,/and draw, swap buffers */glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(x1, y1,
11、 z1, x2, y2, z2, x3, y3, z3);colorcube();glutSwapBuffers();void myReshape(int w, int h)/设置投影方式正投影glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if (w #include GLdouble vertices83= -1.0, -1.0, 1.0,-1.0,1.0,1.0,1.0,1.0,1.0,1.0,-1.0,1.0,-1.0,-1.0,-1.0, -1.0,1.0,-1.0, 1.0,1.0,-1.0,
12、1.0,-1.0,-1.0;GLdouble colors83=0.0,0.0,0.0, 1.0,0.0,0.0, 1.0,1.0,0.0,0.0,1.0,0.0, 0.0,0.0,1.0,1.0,0.0,1.0, 1.0,1.0,1.0,0.0,1.0,1.0;/定义顶点和颜色的全局数组int x1=1,y1=1,z1=1,x2=0,y2=0,z2=0,x3=0,y3=1,z3=0;void polygon(int a, int b, int c , int d)/根据索引列表绘制多边形/* draw a polygon via list of vertices */glBegin(GL_Q
13、UADS);glColor3dv(colorsa);glVertex3dv(verticesa);glColor3dv(colorsb);glVertex3dv(verticesb);glColor3dv(colorsc);glVertex3dv(verticesc);glColor3dv(colorsd);glVertex3dv(verticesd);glEnd();void colorcube()/利用表面绘制立方体/* map vertices to faces */polygon(0,3,2,1);polygon(2,3,7,6);polygon(0,4,7,3);polygon(1,
14、2,6,5);polygon(4,5,6,7);polygon(0,1,5,4);void display()/显示回调函数/* display callback, clear frame buffer and z buffer,/and draw, swap buffers */glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(x1, y1, z1, x2, y2, z2, x3, y3, z3);colorcube();glutSw
15、apBuffers();void myReshape(int w, int h)/设置投影方式透视投影glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if (w = h)glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);elseglFrustum(-2.0 * (GLfloat) w / (GLfloat) h,2.0 * (GLfloat) w / (GLf
16、loat) h, -2.0, 2.0, -10.0, 10.0); glMatrixMode(GL_MODELVIEW);void keyboard(unsigned char key,int x,int y)if(key=q) x1+;glutPostRedisplay();else if(key=Q) x1-;glutPostRedisplay();else if(key=w) y1+;glutPostRedisplay();else if(key=W) y1-;glutPostRedisplay();else if(key=e) z1+;glutPostRedisplay();else
17、if(key=E) z1-;glutPostRedisplay();else if(key=a) x2+;glutPostRedisplay();else if(key=A) x2-;glutPostRedisplay();else if(key=s) y2+;glutPostRedisplay();else if(key=S) y2-;glutPostRedisplay();else if(key=d) z2+;glutPostRedisplay();else if(key=D) z2-;glutPostRedisplay();else if(key=z) x3+;glutPostRedis
18、play();else if(key=Z) z3-;glutPostRedisplay();else if(key=x) y3+;glutPostRedisplay();else if(key=X) y3-;glutPostRedisplay();else if(key=c) z3+;glutPostRedisplay();else if(key=C) z3-;glutPostRedisplay();void main(int argc, char *argv)glutInit(glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);/设置窗口
19、的显示模式glutInitWindowSize(500, 500);/指定窗口大小glutCreateWindow(“E11414103叶传军“);/创建一个名为E11414103叶传军的窗口glClearColor(0.0,0.0,0.0,0.0);/指定窗口的背景色为黑色/glShadeModel(GL_SMOOTH);glShadeModel(GL_FLAT);glutReshapeFunc(myReshape);glutDisplayFunc(display);/设置当前窗口的显示回调函数glutKeyboardFunc(keyboard);/glEnable(GL_DEPTH_TEST); /* Enable hidden-surface-removal */glutMainLoop();/启动主GLUT事件处理循环(3)四结果分析掌握了三维物体的观察方法,以及对视角的一些调整函数。