#include < windows.h > #include < gl / gl.h > #include < gl / glu.h > #include < gl / glut.h > #include < math.h > #define glRGB(x,y,z)glColor3ub((GLubyte)x,(GLubyte)y,(GLubyte)z) #define SUN1 #define MERCURY2 #define VENUS3 #define EARTH4 #define MARS5 GLfloatfAspect; int n; // Lightingvalues GLfloatwhiteLight[] = {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} ; // Calledtodrawscene void RenderScene( 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/sunwasselectedanddisplayed void ProcessPlanet(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). #define BUFFER_LENGTH64 // 重点段 void ProcessSelection( int xPos, int yPos) {//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;} // Processthemouseclick void MouseCallback( int button, int state, int x, int y) {if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)ProcessSelection(x,y);} // Thisfunctiondoesanyneededinitializationontherendering // context. void SetupRC() {//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);} void ChangeSize( int w, int h) {//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();} int main( int argc, 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;}