资源描述:
《opengl跳动的小球》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、/**创建一个球体动画,使球体在窗口内做自由落体运动,并在撞击地面后能够弹回原来的高度。*/#include#include#include#include#include#definePI3.1415926doublemove=20.0;doublemovex=9.0;doublestepx=-0.03;inti=0;intdown=1;intcount=1;doubletimeSpan=0;//下降到底所需时间doublemovey=0.0;doubl
2、eduration=0.0;//持续时间doublelength=0.0;clock_tstart,end;voidinit(void){printf("init");GLfloatmat_specular[]={220.220,220.0,220.0,220.0};GLfloatmat_shininess[]={70.0};GLfloatlight_position[]={0.0,0.0,0.0,-2.0};//r-lu-df-bGLfloatambientLight[]={0.2f,0.2f,0.2f,1.0f};GLfloatdiffuse
3、Light[]={0.6f,0.6f,0.6f,1.0f};GLfloatspecular[]={1.0f,1.0f,1.0f,1.0f};glClearColor(0.3,0.8,0.8,0.0);//bgcglColor3ub(23,17,215);glShadeModel(GL_SMOOTH);glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);glLightfv(GL_LIGHT0,GL_AMBI
4、ENT,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){printf("reshape");glViewport(0,0,
5、(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);g
6、lLoadIdentity();}voidinitDisplay(void){down=1;//向下运动glClear(GL_COLOR_BUFFER_BIT
7、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
8、GL_DEPTH_BUFFER_BIT);glLoadIdentity();g
9、lTranslatef(movex,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);//printf("%f",length);move=20-length;//printf("%f",move);if(move>19.932){
10、move=20;down=1;printf("%i",down);start=clock();}display();glLoadIden