您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > (完整版)OpenGL中创建一个球体动画-使球体在窗口内做自由落体运动.
计算机图形学实验报告1、实验目的和要求利用第七章所学的知识,试在OpenGL中创建一个球体动画,使球体在窗口内做自由落体运动,并在撞击地面后能够反弹回来。并用相应的代码表示出来。2、实验内容利用glutSolidSphere函数等其它函数,在OpenGL中创建一个球体动画,使球体在窗口内做自由落体运动,并在撞击地面后能够反弹回来3、实验步骤1)相关算法及原理描述我们所使用的glut实用工具中,正好就有一个绘制球体的现成函数:glutSolidSphere,这个函数在“原点”绘制出一个球体。由于坐标是可以通过glTranslate*和glRotate*两个函数进行随意变换的,所以我们就可以在任意位置绘制球体了。2)运行结果如下图,程序调试成功,并且能正常显示4、实验总结通过本次试验,进一步认识,感觉OpenGL的功能很强大,各种各样的物理模拟实验他都不在话下!!不得不说,这软件很好很强大!!由于自己不太擅长编程,所以有些功能还不能完全实现,但我会尽自己最大努力来克服自己的编程不足之处,多加练习。5、附录带注释的源程序#includeglut.h#includestdlib.h#includestdio.h#includetime.h#includemath.h#definePI3.1415926doublemove=20.0;inti=0;intdown=1;intcount=1;doubletimeSpan=0;//下降到底所需时间doublemovey=0.0;doubleduration=0.0;//持续时间doublelength=0.0;clock_tstart,end;voidinit(void){GLfloatmat_specular[]={220.220,220.0,220.0,220.0};GLfloatmat_shininess[]={100.0};GLfloatlight_position[]={0.0,0.0,0.0,-2.0};//r-lu-df-bGLfloatambientLight[]={0.2f,0.2f,0.2f,1.0f};GLfloatdiffuseLight[]={0.6f,0.6f,0.6f,1.0f};GLfloatspecular[]={1.0f,1.0f,1.0f,1.0f};glClearColor(0.2,0.2,1.5,2.0);//bgcglColor3ub(100,100,215);glShadeModel(GL_SMOOTH);glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);glLightfv(GL_LIGHT0,GL_SPECULAR,specular);glLightfv(GL_LIGHT0,GL_POSITION,light_position);glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glEnable(GL_DEPTH_TEST);}voidreshape(intw,inth){glViewport(0,0,(GLsizei)w,(GLsizei)h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if(w=h)glOrtho(-12,12,-12*(GLfloat)(h)/(GLfloat)(w),12*(GLfloat)(h)/(GLfloat)(w),-1.0,1.0);elseglOrtho(-12*(GLfloat)(w)/(GLfloat)(h),12*(GLfloat)(w)/(GLfloat)(h),-12,12,-1.0,1.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}voidinitDisplay(void){down=1;//向下运动glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glLoadIdentity();glTranslatef(0.0,20.0,0.0);glutSolidSphere(0.4,40,50);glutSwapBuffers();}voiddisplay(void){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glLoadIdentity();glTranslatef(0,move,0.0);glutSolidSphere(0.4,40,50);glutSwapBuffers();}voidMoveSphereUp(){end=clock();duration=(double)(end-start-16.0)/CLOCKS_PER_SEC;length=5*(timeSpan-duration)*(timeSpan-duration);move=20-length;if(move19.932){move=20;down=1;printf(%i,down);start=clock();}display();glLoadIdentity();}voidMoveSphereDown(){if(count==1){start=clock();count=0;}end=clock();duration=(double)(end-start)/CLOCKS_PER_SEC;length=5*duration*duration;move=20-length;if(move-20){timeSpan=duration;//记下下降所经历的时间move=-20;start=clock();down=0;//向上运动}display();glLoadIdentity();}voidTimerFunc2(intvalue){if(i==0){//leftGLfloatlight_position[]={2.0,0.0,0.0,0.0};//r-lu-df-bglLightfv(GL_LIGHT0,GL_POSITION,light_position);}if(i==1){//left-upGLfloatlight_position[]={2.0,2.0,0.0,0.0};//r-lu-df-bglLightfv(GL_LIGHT0,GL_POSITION,light_position);}if(i==2){//upGLfloatlight_position[]={0.0,2.0,0.0,0.0};//r-lu-df-bglLightfv(GL_LIGHT0,GL_POSITION,light_position);}if(i==3){//up-rightGLfloatlight_position[]={-2.0,2.0,0.0,0.0};//r-lu-df-bglLightfv(GL_LIGHT0,GL_POSITION,light_position);}if(i==4){//rightGLfloatlight_position[]={-2.0,0.0,0.0,0.0};//r-lu-df-bglLightfv(GL_LIGHT0,GL_POSITION,light_position);}if(i==5){//right-downGLfloatlight_position[]={-2.0,-2.0,0.0,0.0};//r-lu-df-bglLightfv(GL_LIGHT0,GL_POSITION,light_position);}if(i==6){//downGLfloatlight_position[]={0.0,-2.0,0.0,0.0};//r-lu-df-bglLightfv(GL_LIGHT0,GL_POSITION,light_position);}if(i==7){//down-leftGLfloatlight_position[]={2.0,-2.0,0.0,0.0};//r-lu-df-bglLightfv(GL_LIGHT0,GL_POSITION,light_position);}i=(++i)%8;//控制小球旋转的glutTimerFunc(60,TimerFunc2,1);}voidTimerFunc1(intvalue){if(down==1){MoveSphereDown();}if(down==0){MoveSphereUp();}glutTimerFunc(10,TimerFunc1,0);}intmain(intargc,char**argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);glutInitWindowSize(400,740);glutInitWindowPosition(300,20);glutCreateWindow(argv[0]);init();glutDisplayFunc(initDisplay);glutReshapeFunc(reshape);glutTimerFunc(1400,TimerFunc1,0);//毫秒glutTimerFunc(400,TimerFunc2,1);//毫秒glutMainLoop();return0;}
本文标题:(完整版)OpenGL中创建一个球体动画-使球体在窗口内做自由落体运动.
链接地址:https://www.777doc.com/doc-4861782 .html