计算机图形学实验报告实验报告文件命名方式:学号姓名_实验序号_实验名称.doc姓名学号专业任课教师实验教师评阅教师实验地点书写日期2013-8-7实验课时间实验得分实验序号与名称:实验十交互的3D漫游世界实验目的:1)进一步掌握3D编程概念:2)主要掌握视点和目标的改变对场景生成的影响3)掌握3D漫游场景的基本技巧实验开发环境:硬件要求:PC机,主流配置,最好为独立显卡,显存512M以上。软件环境:操作系统:WindowsXP。语言开发工具:MicrosoftVisualstudio2008,VisualC++。并安装OpenGL图形函数库实验要求:1.当场检查,计现场分2.实验所有步骤所生成的效果截图拷贝到实验报告文档里备查,并附上相应的代码。WORD文档命名方式:学号姓名-实验序号-实验名称。实验内容与步骤小结、主要截图、核心代码实现:比例:50%#includestdafx.h#include3Dshijie.h#includemath.h#includeglut.h#definePI3.14159floattheta=-90.0;//rotatingangle小球旋转角intinner=10,outer=80;//torus'sinner&outerradius内径外径floats=outer+4*inner+50;floateyex=0,eyey=0,eyez=s;//eyepointinitialpositionfloatatx=0,aty=0,atz=0;//atpointinitialpositionintww,hh;//variableforwindows'swidth//boolflag=true;doubleangle=0;//漫游移动旋转角floatstep=0.1*s;//步长为视点到目标点的距离voidDisplay(void);voidReshape(intw,inth);voidmytime(intvalue);voiddrawground();//地板天花voiddrawsphere();//环+球voiddrawwall();//墙voidinit();voidmykeyboard(unsignedcharkey,intx,inty);intAPIENTRY_tWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,LPTSTRlpCmdLine,intnCmdShow){UNREFERENCED_PARAMETER(hPrevInstance);UNREFERENCED_PARAMETER(lpCmdLine);char*argv[]={hello,};intargc=2;//must/shouldmatchthenumberofstringsinargvglutInit(&argc,argv);//初始化GLUT库;glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);//设置显示模式;(缓冲,颜色类型)glutInitWindowSize(500,500);glutInitWindowPosition(1024/2-250,768/2-250);glutCreateWindow(Rotating3DWorld);//创建窗口,标题为“Rotating3DWorld”;glutReshapeFunc(Reshape);init();glutDisplayFunc(Display);//用于绘制当前窗口;glutKeyboardFunc(mykeyboard);glutTimerFunc(100,mytime,10);glutMainLoop();//表示开始运行程序,用于程序的结尾;return0;}voidinit(){glClearColor(1,1,1,1);glPixelStorei(GL_PACK_ALIGNMENT,1);glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);}voidmykeyboard(unsignedcharkey,intx,inty){switch(key){case'W':case'w'://向前直走旋转角angle不变,平移量为步长*sin/cos(angle)//yourcodeHereeyex=eyex-step*sin(angle);eyez=eyez-step*cos(angle);atx=atx-step*sin(angle);atz=atz-step*cos(angle);break;case'S':case's'://向后退//yourcodeHereeyex=eyex+step*sin(angle);eyez=eyez+step*cos(angle);atx=atx+step*sin(angle);atz=atz+step*cos(angle);break;case'A':case'a'://左看//yourcodeHere视点不变,目标点绕视点旋转angle+=0.1;atx=eyex-x*sin(angle);atz=eyex-x*cos(angle);break;case'D':case'd'://右看//yourcodeHereangle-=0.1;atx=eyex-x*sin(angle);atz=eyex-x*cos(angle);break;}glutPostRedisplay();//参数修改后调用重画函数,屏幕图形将发生改变}voidDisplay(void){//glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glClear(GL_COLOR_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(eyex,eyey,eyez,atx,aty,atz,0,1,0);glPushMatrix();//旋转茶壶glRotated(theta,0,1,0);glColor3f(0,0,1);glutWireTeapot(30);glPopMatrix();glPushMatrix();glColor3f(0.0,0.0,1.0);drawwall();glColor3f(1.0,0,0);drawground();drawsphere();//画球+环glPopMatrix();glutSwapBuffers();}voiddrawsphere(){floattr;tr=(outer+3*inner);glRotatef(theta,0,1,0);glPushMatrix();glPushMatrix();glColor3f(1.0,0,1.0);glutWireTorus(inner,outer,30,50);glPopMatrix();glPushMatrix();glTranslatef(outer,0,0);glRotatef(theta,0,1,0);glTranslatef(-outer,0,0);glPushMatrix();glTranslatef(tr,0,0);glRotatef(-45,1,0,0);glColor3f(0.0,1.0,0);glutWireSphere(inner,20,20);glPopMatrix();glPopMatrix();glPopMatrix();}voiddrawground(){//groundfor(inti=-outer-4*inner;iouter+4*inner;i+=2*inner)for(intj=-outer-4*inner;jouter+4*inner;j+=2*inner){glBegin(GL_QUADS);glVertex3d(j,-outer-4*inner,i);glVertex3d(j,-outer-4*inner,i+2*inner);glVertex3d(j+2*inner,-outer-4*inner,i+2*inner);glVertex3d(j+2*inner,-outer-4*inner,i);glEnd();}//topfor(inti=-outer-4*inner;iouter+4*inner;i+=2*inner)for(intj=-outer-4*inner;jouter+4*inner;j+=2*inner){glBegin(GL_QUADS);glVertex3d(j,outer+4*inner,i);glVertex3d(j,outer+4*inner,i+2*inner);glVertex3d(j+2*inner,outer+4*inner,i+2*inner);glVertex3d(j+2*inner,outer+4*inner,i);glEnd();}}voiddrawwall(){inti,j;glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);//leftfor(i=-outer-4*inner;iouter+4*inner;i+=2*inner)for(j=-outer-4*inner;jouter+4*inner;j+=2*inner){glBegin(GL_QUADS);glVertex3d(-outer-4*inner,j,i);glVertex3d(-outer-4*inner,j+2*inner,i);glVertex3d(-outer-4*inner,j+2*inner,i+2*inner);glVertex3d(-outer-4*inner,j,i+2*inner);glEnd();}//rightfor(i=-outer-4*inner;i=outer+4*inner-2*inner;i+=2*inner)//forzfor(j=-outer-4*inner;j=outer+4*inner-2*inner;j+=2*inner)//fory{glBegin(GL_QUADS);glVertex3f(outer+4*inner,j,i);glVertex3f(outer+4*inner,j+2*inner,i);glVertex3f(outer+4*inner,j+2*inner,i+2*inner);glVertex3f(outer+4*inner,j,i+2*inner);glEnd();}glColor3f(1.0,1.0,0.0);//frontfor(i=-outer-4*inner;i=outer+4*inner-2*inner;i+=2*inner)//forzfor(j=-outer-4*inner;j=outer+4*inner-2*inner;j+=2*inner)//fory{glBegin(GL_QUADS);glVertex3f(j,i,-outer-4*inner);glVertex3f(j+2*inner,i,-outer-4*inner);glVertex3f(j+2*inner,i+2*inner,-outer-4*inner);glVertex3f(j,i+2*inner,-outer-4*inner);glEnd();}}voidmytime(intvalue){theta+=0.5;if(theta=360.0)theta-=360.0;glutPostRedisplay();glutTimerFunc(100,mytime,10);}voidReshape(GLsizeiw,GLsizeih){glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(90,w/h,10,2*outer+8*inner+250);glViewport(0,0,w,h);glMatrixMode(GL