#includestdio.h#ifdef__APPLE__#includeGLUT/glut.h#else#includeGL/glut.h#endif#includestdlib.h#includestdio.h#includemath.h#includewindows.h#includetime.hstaticdoublea=0;staticdoubleb=0.2;staticdoublet=0;staticdoublet1=0;staticdoublev=0;staticdoublei=0;staticdoublem=0;staticdoublen=0;staticdoublep,q,r,r1=1;staticvoidresize(intwidth,intheight)//重绘回调函数{constfloatar=(float)width/(float)height;glViewport(0,0,width,height);//利用函数glViewport定义视区glMatrixMode(GL_PROJECTION);//投影矩阵glLoadIdentity();//重置坐标系统,使投影变换复位glFrustum(-ar,ar,-1.0,1.0,2.0,100.0);//利用函数glFrustum定义修剪区glMatrixMode(GL_MODELVIEW);glLoadIdentity();}staticvoiddisplay(void)//显示回调{glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//清除颜色缓冲区和深度缓冲区t=t+t1*0.0001;glColor3d(1,0.6,0.6);//画图颜色灰红//绘制一个实心球,默认原点在屏幕中心与照相机重合glPushMatrix();glTranslated(-3+0.5*cos(a),-0.6+0.5*sin(a),-5);glRotated(0,0,0,0);glutSolidSphere(b,16,16);glPopMatrix();glColor3d(1,0,0);//画图颜色红//绘制一个实心球,默认原点在屏幕中心与照相机重合glPushMatrix();glTranslated(-3+t*cos(a)*v*t1,-0.6+(t*sin(a)*v-45*t*t)*t1,-5);glRotated(0,0,0,0);glutSolidSphere(0.2*r1,16,16);glPopMatrix();glColor3d(1,1,1);//画图颜色灰//绘制一个线框圆环glPushMatrix();glTranslated(m+2.3,n-0.6,-5);glRotated(100,1,0,0);glRotated(7.5,0,1,0);glutWireTorus(0.1*r1,0.6,16,16);glPopMatrix();if(pow(3-t*cos(a)*v*t1+m+2.3,2)+pow(0.6-(t*sin(a)*v-45*t*t)*t1+n-0.6,2)0.024){t=t-t1*0.0001;r1=3.5;}elser1=1;for(p=-3.5,q=0.5;p3.6;p=p+0.5){if(pow(3-t*cos(a)*v*t1+p,2)+pow(0.6-(t*sin(a)*v-45*t*t)*t1+q,2)0.2)r=0.4;elser=0.05;glColor3d(0,0,0);//画图颜色黑//绘制一个线框圆环glPushMatrix();glTranslated(p,q,-5);glRotated(t*5000,0,1,0);glutWireTorus(r,0.15,16,16);glPopMatrix();}for(p=-3.5,q=0;p3.6;p=p+0.5){if(pow(3-t*cos(a)*v*t1+p,2)+pow(0.6-(t*sin(a)*v-45*t*t)*t1+q,2)0.2)r=0.4;elser=0.05;glColor3d(0,0,0);//画图颜色黑//绘制一个线框圆环glPushMatrix();glTranslated(p,q,-5);glRotated(t*5000,0,1,0);glutWireTorus(r,0.15,16,16);glPopMatrix();}for(p=-3.5,q=1;p3.6;p=p+0.5){if(pow(3-t*cos(a)*v*t1+p,2)+pow(0.6-(t*sin(a)*v-45*t*t)*t1+q,2)0.2)r=0.4;elser=0.05;glColor3d(0,0,0);//画图颜色黑//绘制一个线框圆环glPushMatrix();glTranslated(p,q,-5);glRotated(t*5000,0,1,0);glutWireTorus(r,0.15,16,16);glPopMatrix();}glutSwapBuffers();//交换缓冲区}staticvoidkey(unsignedcharkey,intx,inty)//键盘回调函数{switch(key){case27://Esc键的ASCII码为27case13://回车的ASCII码为13exit(0);break;case43://加号的ASCII码为43if(a1.4){a=a+0.1;}break;case45://减号的ASCII码为45if(a0){a=a-0.1;}break;case49://1的ASCII码为49v=4;break;case50://2的ASCII码为50v=8;break;case51://3的ASCII码为51v=12;break;case52://4的ASCII码为52v=16;break;case53://5的ASCII码为53v=20;break;case54://6的ASCII码为54v=24;break;case55://7的ASCII码为55v=28;break;case56://8的ASCII码为56v=32;break;case57://9的ASCII码为57v=36;break;case32://空格的ASCII码为32if(t0.1){t1=1;b=0.001;}else{b=0.2;v=0;t1=0;t=0;m=0;n=0;}break;case97://a的ASCII码为97m=m-0.2;break;case119://w的ASCII码为119n=n+0.2;break;case100://d的ASCII码为100m=m+0.2;break;case115://s的ASCII码为115n=n-0.2;break;}glutPostRedisplay();//标记当前窗口需要重新绘制}staticvoididle(void)//空闲回调函数{glutPostRedisplay();//标记当前窗口需要重新绘制}//定义一个默认的光源的属性值,使用数组来存放光源的颜色值constGLfloatlight_ambient[]={0.0f,0.0f,0.0f,1.0f};//光源中环境光的颜色,黑色,第四个分量1.0表示不透明。constGLfloatlight_diffuse[]={1.0f,1.0f,1.0f,1.0f};//光源中漫射光的颜色,白色constGLfloatlight_specular[]={1.0f,1.0f,1.0f,1.0f};//光源中镜面反射光的颜色constGLfloatlight_position[]={2.0f,5.0f,5.0f,0.0f};//定向光源,指定了光源的方向//定义材质属性值,使用数组来存放材质的颜色值constGLfloatmat_ambient[]={0.7f,0.7f,0.7f,1.0f};//材质的环境颜色constGLfloatmat_diffuse[]={0.8f,0.8f,0.8f,1.0f};//材质的漫射光颜色constGLfloatmat_specular[]={1.0f,1.0f,1.0f,1.0f};//材质镜面反射光的颜色constGLfloathigh_shininess[]={100.0f};//材质的镜面反射指数intmain(intargc,char*argv[]){MessageBox(NULL,\+、-\控制角度\n\1~9\控制速度\n\wasd\控制框移动\n按空格发射,介绍,MB_OK);glutInit(&argc,argv);//对GLUT进行初始化glutInitWindowPosition(15,15);//窗口位置glutInitWindowSize(960,640);//窗口大小glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);//使用双缓冲区窗口,窗口使用深度缓存glutCreateWindow(GLUTShapes);//参数将被作为窗口的标题glutReshapeFunc(resize);glutDisplayFunc(display);glutKeyboardFunc(key);glutIdleFunc(idle);//注册回调函数glClearColor(0.1f,0.1f,1.0f,1);//参数可以在0、1改动glEnable(GL_CULL_FACE);glCullFace(GL_BACK);glEnable(GL_DEPTH_TEST);//开启剔除操作效果glDepthFunc(GL_LESS);//指定深度缓冲比较值,深度值小于参考值,则通过glEnable(GL_LIGHT0);//启用0号光源glEnable(GL_NORMALIZE);//启用自动单位化法向量glEnable(GL_COLOR_MATERIAL);//启用材质颜色glEnable(GL_LIGHTING);//启用光照//设置0号光源的特性值glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);glLightfv(GL_LIGHT0,GL_POSITION,light_position);//设置材质的特性值,本题只设置了物体前面的材质值glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);glMaterialfv(GL_FRONT,GL_SHININESS,high_shininess);glutMainLoop();//启动glut程序进入事件循环returnEXIT_SUCCESS;}