#include<windows.h>#include<gl/gl.h>#include<gl/glu.h>#include<gl/glut.h>#include<math.h>#defineglRGB(x,y,z)glColor3ub((GLubyte)x,(GLubyte)y,(GLubyte)z)#defineSUN1#defineMERCURY2#defineVENUS3#defineEARTH4#defineMARS5GLfloatfAspect;intn;//LightingvaluesGLfloatwhiteLight[]={0.35f,0.35f,0.35f,1.0f};GLfloatsourceLight[]={0.65f,0.65f,0.65f,1.0f};GLfloatlightPos[]={0.0f,0.0f,0.0f,1.0f};//CalledtodrawscenevoidRenderScene(void){//ClearthewindowwithcurrentclearingcolorglClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//SavethematrixstateanddotherotationsglMatrixMode(GL_MODELVIEW);glPushMatrix();//TranslatethewholesceneoutandintoviewglTranslatef(0.0f,0.0f,-300.0f);//InitializethenamesstackglInitNames();glPushName(0);//Setmaterialcolor,Yellow//SunglRGB(255,255,255);glLoadName(SUN);glutSolidSphere(15.0f,15,15);//DrawMercuryglRGB(128,0,0);glPushMatrix();glTranslatef(24.0f,0.0f,0.0f);glLoadName(MERCURY);glutSolidSphere(2.0f,15,15);glPopMatrix();//DrawVenusglPushMatrix();glRGB(128,128,255);glTranslatef(60.0f,0.0f,0.0f);glLoadName(VENUS);glutSolidSphere(4.0f,15,15);glPopMatrix();//DrawtheEarthglPushMatrix();glRGB(0,0,255);glTranslatef(100.0f,0.0f,0.0f);glLoadName(EARTH);glutSolidSphere(8.0f,15,15);glPopMatrix();//DrawMarsglRGB(255,0,0);glPushMatrix();glTranslatef(150.0f,0.0f,0.0f);glLoadName(MARS);glutSolidSphere(4.0f,15,15);glPopMatrix();//RestorethematrixstateglPopMatrix();//ModelviewmatrixglutSwapBuffers();}//Presenttheinformationonwhichplanet/sunwasselectedanddisplayedvoidProcessPlanet(GLuintid){switch(id){caseSUN:MessageBox(NULL,"YouclickedontheSun!","Info",MB_OK|MB_ICONEXCLAMATION);break;caseMERCURY:MessageBox(NULL,"YouclickedonMercury!","Info",MB_OK|MB_ICONEXCLAMATION);break;caseVENUS:MessageBox(NULL,"YouclickedonVenus!","Info",MB_OK|MB_ICONEXCLAMATION);break;caseEARTH:MessageBox(NULL,"YouclickedonEarth!","Info",MB_OK|MB_ICONEXCLAMATION);break;caseMARS:MessageBox(NULL,"YouclickedonMars!","Info",MB_OK|MB_ICONEXCLAMATION);break;default:MessageBox(NULL,"Nothingwasclickedon!","Error",MB_OK|MB_ICONEXCLAMATION);break;}}//Processtheselection,whichistriggeredbyarightmouse//clickat(xPos,yPos).#defineBUFFER_LENGTH64//重点段voidProcessSelection(intxPos,intyPos){//SpaceforselectionbufferGLuintselectBuff[BUFFER_LENGTH];//HitcounterandviewportstoreageGLinthits,viewport[4];//SetupselectionbufferglSelectBuffer(BUFFER_LENGTH,selectBuff);//GettheviewportglGetIntegerv(GL_VIEWPORT,viewport);//SwitchtoprojectionandsavethematrixglMatrixMode(GL_PROJECTION);glPushMatrix();//ChangerendermodeglRenderMode(GL_SELECT);//Establishnewclippingvolumetobeunitcubearound//mousecursorpoint(xPos,yPos)andextendingtwopixels//intheverticalandhorzontaldirectionglLoadIdentity();gluPickMatrix(xPos,viewport[3]-yPos,2,2,viewport);//ApplyperspectivematrixgluPerspective(45.0f,fAspect,1.0,425.0);//DrawthesceneRenderScene();//Collectthehitshits=glRenderMode(GL_RENDER);//Ifasinglehitoccured,displaytheinfo.if(hits==1)ProcessPlanet(selectBuff[3]);//RestoretheprojectionmatrixglMatrixMode(GL_PROJECTION);glPopMatrix();//GobacktomodelviewfornormalrenderingglMatrixMode(GL_MODELVIEW);//n=selectBuff[0];//printfn;}//ProcessthemouseclickvoidMouseCallback(intbutton,intstate,intx,inty){if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)ProcessSelection(x,y);}//Thisfunctiondoesanyneededinitializationontherendering//context.voidSetupRC(){//LightvaluesandcoordinatesglEnable(GL_DEPTH_TEST);//HiddensurfaceremovalglFrontFace(GL_CCW);//Counterclock-wisepolygonsfaceoutglEnable(GL_CULL_FACE);//Donotcalculateinsides//EnablelightingglEnable(GL_LIGHTING);//Setupandenablelight0glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);glLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);glLightfv(GL_LIGHT0,GL_POSITION,lightPos);glEnable(GL_LIGHT0);//EnablecolortrackingglEnable(GL_COLOR_MATERIAL);//SetMaterialpropertiestofollowglColorvaluesglColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);//BlackbluebackgroundglClearColor(0.60f,0.60f,0.60f,1.0f);}voidChangeSize(intw,inth){//Preventadividebyzeroif(h==0)h=1;//SetViewporttowindowdimensionsglViewport(0,0,w,h);//CalculateaspectratioofthewindowfAspect=(GLfloat)w/(GLfloat)h;//SettheperspectivecoordinatesystemglMatrixMode(GL_PROJECTION);glLoadIdentity();//Fieldofviewof45degrees,nearandfarplanes1.0and425gluPerspective(45.0f,fAspect,1.0,425.0);//ModelviewmatrixresetglMatrixMode(GL_MODELVIEW);glLoadIdentity();}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);glutInitWindowSize(600,300);glutCreateWindow("PickaPlanet");glutReshapeFunc(ChangeSize);glutMouseFunc(MouseCallback);glutDisplayFunc(RenderScene);SetupRC();glutMainLoop();return0;}