实验三观察三维物体_第1页
实验三观察三维物体_第2页
实验三观察三维物体_第3页
实验三观察三维物体_第4页
实验三观察三维物体_第5页
已阅读5页,还剩34页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验三观察三维物体姓名 叶传军 学号E11414103得分阅读arraycube.c,掌握彩色立方体的建模方法,为程序加注释。修改arraycube.c,实现交互式地移动照相机来观察已经建模好的彩色立方体。即用鼠标 或键盘来改变 gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upziS数的 9 个参数, 以此来观察立方体。要求:交互时采用的鼠标和键盘按键自定。分别在正投影和透视投影下实现题目中的功能。可增加菜单功能,将正投影和透视投影下的观察功能融合到一个程序中。可参考交互 式教程Projection.c的功能。在arraycube.

2、c的基础上编写一个交互式程序,实现立方体的旋转。具体要求如下:(1)立方体的旋转方式由鼠标和键盘按键来控制:按下鼠标左键,立方体绕x轴连续旋转; 按下鼠标左键+ctrl键,立方体绕y轴连续旋转;按下鼠标右键,立方体绕z轴连续旋转。(注意:旋转的不动点在原点,正好是立方体的中心。)(2 )如果旋转的不动点不在原点,而改为点P (1,1,1),如何实现立方体绕3个坐标轴轴 的旋转?(3 )如果要求每按下一次鼠标按键或键盘按键,立方体旋转的角度增加5度,应如何修改 程序?自学教材4.2.7节。编写一个交互式程序,使其可以通过鼠标(或键盘)和菜单实现一些glu和glut对象的旋转,平移和比例缩放。(加

3、自己的创意)二设计思想本次试验主要是用户交互与三维物体观察的结合,主要用到的函数有gluPerspective(fovy, aspect, near, far);glOrtho(left, right, bottom, top, near, far);gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz);通过这些函数的调用可以分别在正投影和透视投影下从不同的角度来观察三维物体。三程序清单1. #includestdafx.h#include #include GLdouble vertices83= -1.0, -1.0, 1.0,

4、?-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 a, int b, int c , int d)/根据索引列表绘制多边形/* draw a polygon v

5、ia 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,1);polygon(2,3

6、,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(1.0, 1.0, 1.0, 0.0, 0.

7、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 = h)glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);elseglOrtho(-2.0 * (GLfloat) w / (GL

8、float) h,2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);glMatrixMode(GL_MODELVIEW);void main(int argc, char *argv)glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);/设置窗口的显示模式glutInitWindowSize(500, 500);/指定窗口大小glutCreateWindow(E11414103 叶传军);/创建一个名为E11414103 叶传

9、军的 窗口glClearColor(0.0,0.0,0.0,0.0);/指 定窗口的背景色为黑色/glShadeModel(GL_SMOOTH);glShadeModel(GL_FLAT);glutReshapeFunc(myReshape);glutDisplayFunc(display);/设置当前窗口的显示回调函数/glEnable(GL_DEPTH_TEST); /* Enable hidden-surface-removal */glutMainLoop();/启动主GLUT事件处理循环2.(1)#includestdafx.h#include #include GLdouble v

10、ertices83 =(-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;/定义顶点和颜色的全局数组int x1=1,y1=1,z1=1,x2=0,y2=0,z2=0,x3=0,y3 = 1

11、,z3=0;void polygon(int 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();vo

12、id colorcube()/利用表面绘制立方体/* map vertices 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_B

13、IT);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(x1, y1, 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 = h)glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,2.0 * (GL

14、float) h / (GLfloat) w, -10.0, 10.0);elseglOrtho(-2.0 * (GLfloat) w / (GLfloat) h,2.0 * (GLfloat) w / (GLfloat) 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

15、= = w)y1+;glutPostRedisplay();else if(key= = W)y1-;glutPostRedisplay();else if(key= = e)z1+;glutPostRedisplay();else 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-;glu

16、tPostRedisplay();else if(key= = d)z2+;glutPostRedisplay();else if(key= = D)z2-;glutPostRedisplay();else if(key= = z)x3 + +;glutPostRedisplay();else if(key= = Z)z3-;glutPostRedisplay();else if(key= = x)y3+;glutPostRedisplay();else if(key= = X)y3-;glutPostRedisplay();else if(key= = c)z3 + +;glutPostRe

17、display();else if(key= = C)z3-; glutPostRedisplay();void main(int argc, char *argv)glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);/设置窗口的 显示模式glutInitWindowSize(500, 500);/指定窗口大小glutCreateWindow(E11414103 叶传军);/创建一个名为E11414103 叶传 军的窗口glClearColor(0.0,0.0,0.0,0.0);/指 定窗口的背景

18、色为黑色/glShadeModel(GL_SMOOTH);glShadeModel(GL_FLAT);glutReshapeFunc(myReshape);glutDisplayFunc(display);/设置当前窗口的显示回调函数glutKeyboardFunc(keyboard);/glEnable(GL_DEPTH_TEST); /* Enable hidden-surface-removal */g_uiMa5LOOP()7、mH+GLUTJHHiE11414103叶传军 g 回(2)#includestdafx.h#include #include GLdouble vertice

19、s83 =-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;/定义顶点和颜色的全局数组int x1=1,y1=1,z1=1,x2=0,y2=0,z2=0,x3=0,y3 = 1,z3=0;vo

20、id polygon(int 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 color

21、cube()/利用表面绘制立方体/* map vertices 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);glMa

22、trixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(x1, y1, 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 = h)glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,2.0 * (GLfloat

23、) h / (GLfloat) w, -10.0, 10.0);elseglFrustum(-2.0 * (GLfloat) w / (GLfloat) h,2.0 * (GLfloat) w / (GLfloat) 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= =

24、w)y1+;glutPostRedisplay();else if(key= = W)y1-;glutPostRedisplay();else if(key= = e)z1+;glutPostRedisplay();else 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= =

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论