#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 )
{
//Clearthewindowwithcurrentclearingcolor
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//Savethematrixstateanddotherotations
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
//Translatethewholesceneoutandintoview
glTranslatef(0.0f,0.0f,-300.0f);
//Initializethenamesstack
glInitNames();
glPushName(0);
//Setmaterialcolor,Yellow
//Sun
glRGB(255,255,255);
glLoadName(SUN);
glutSolidSphere(15.0f,15,15);
//DrawMercury
glRGB(128,0,0);
glPushMatrix();
glTranslatef(24.0f,0.0f,0.0f);
glLoadName(MERCURY);
glutSolidSphere(2.0f,15,15);
glPopMatrix();
//DrawVenus
glPushMatrix();
glRGB(128,128,255);
glTranslatef(60.0f,0.0f,0.0f);
glLoadName(VENUS);
glutSolidSphere(4.0f,15,15);
glPopMatrix();
//DrawtheEarth
glPushMatrix();
glRGB(0,0,255);
glTranslatef(100.0f,0.0f,0.0f);
glLoadName(EARTH);
glutSolidSphere(8.0f,15,15);
glPopMatrix();
//DrawMars
glRGB(255,0,0);
glPushMatrix();
glTranslatef(150.0f,0.0f,0.0f);
glLoadName(MARS);
glutSolidSphere(4.0f,15,15);
glPopMatrix();
//Restorethematrixstate
glPopMatrix();//Modelviewmatrix
glutSwapBuffers();
}
// 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)
{
//Spaceforselectionbuffer
GLuintselectBuff[BUFFER_LENGTH];
//Hitcounterandviewportstoreage
GLinthits,viewport[4];
//Setupselectionbuffer
glSelectBuffer(BUFFER_LENGTH,selectBuff);
//Gettheviewport
glGetIntegerv(GL_VIEWPORT,viewport);
//Switchtoprojectionandsavethematrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();
//Changerendermode
glRenderMode(GL_SELECT);
//Establishnewclippingvolumetobeunitcubearound
//mousecursorpoint(xPos,yPos)andextendingtwopixels
//intheverticalandhorzontaldirection
glLoadIdentity();
gluPickMatrix(xPos,viewport[3]-yPos,2,2,viewport);
//Applyperspectivematrix
gluPerspective(45.0f,fAspect,1.0,425.0);
//Drawthescene
RenderScene();
//Collectthehits
hits=glRenderMode(GL_RENDER);
//Ifasinglehitoccured,displaytheinfo.
if(hits==1)
ProcessPlanet(selectBuff[3]);
//Restoretheprojectionmatrix
glMatrixMode(GL_PROJECTION);
glPopMatrix();
//Gobacktomodelviewfornormalrendering
glMatrixMode(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()
{
//Lightvaluesandcoordinates
glEnable(GL_DEPTH_TEST);//Hiddensurfaceremoval
glFrontFace(GL_CCW);//Counterclock-wisepolygonsfaceout
glEnable(GL_CULL_FACE);//Donotcalculateinsides
//Enablelighting
glEnable(GL_LIGHTING);
//Setupandenablelight0
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);
//Enablecolortracking
glEnable(GL_COLOR_MATERIAL);
//SetMaterialpropertiestofollowglColorvalues
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
//Blackbluebackground
glClearColor(0.60f,0.60f,0.60f,1.0f);
}
void ChangeSize( int w, int h)
{
//Preventadividebyzero
if(h==0)
h=1;
//SetViewporttowindowdimensions
glViewport(0,0,w,h);
//Calculateaspectratioofthewindow
fAspect=(GLfloat)w/(GLfloat)h;
//Settheperspectivecoordinatesystem
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Fieldofviewof45degrees,nearandfarplanes1.0and425
gluPerspective(45.0f,fAspect,1.0,425.0);
//Modelviewmatrixreset
glMatrixMode(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;
}
#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 )
{
//Clearthewindowwithcurrentclearingcolor
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//Savethematrixstateanddotherotations
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
//Translatethewholesceneoutandintoview
glTranslatef(0.0f,0.0f,-300.0f);
//Initializethenamesstack
glInitNames();
glPushName(0);
//Setmaterialcolor,Yellow
//Sun
glRGB(255,255,255);
glLoadName(SUN);
glutSolidSphere(15.0f,15,15);
//DrawMercury
glRGB(128,0,0);
glPushMatrix();
glTranslatef(24.0f,0.0f,0.0f);
glLoadName(MERCURY);
glutSolidSphere(2.0f,15,15);
glPopMatrix();
//DrawVenus
glPushMatrix();
glRGB(128,128,255);
glTranslatef(60.0f,0.0f,0.0f);
glLoadName(VENUS);
glutSolidSphere(4.0f,15,15);
glPopMatrix();
//DrawtheEarth
glPushMatrix();
glRGB(0,0,255);
glTranslatef(100.0f,0.0f,0.0f);
glLoadName(EARTH);
glutSolidSphere(8.0f,15,15);
glPopMatrix();
//DrawMars
glRGB(255,0,0);
glPushMatrix();
glTranslatef(150.0f,0.0f,0.0f);
glLoadName(MARS);
glutSolidSphere(4.0f,15,15);
glPopMatrix();
//Restorethematrixstate
glPopMatrix();//Modelviewmatrix
glutSwapBuffers();
}
// 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)
{
//Spaceforselectionbuffer
GLuintselectBuff[BUFFER_LENGTH];
//Hitcounterandviewportstoreage
GLinthits,viewport[4];
//Setupselectionbuffer
glSelectBuffer(BUFFER_LENGTH,selectBuff);
//Gettheviewport
glGetIntegerv(GL_VIEWPORT,viewport);
//Switchtoprojectionandsavethematrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();
//Changerendermode
glRenderMode(GL_SELECT);
//Establishnewclippingvolumetobeunitcubearound
//mousecursorpoint(xPos,yPos)andextendingtwopixels
//intheverticalandhorzontaldirection
glLoadIdentity();
gluPickMatrix(xPos,viewport[3]-yPos,2,2,viewport);
//Applyperspectivematrix
gluPerspective(45.0f,fAspect,1.0,425.0);
//Drawthescene
RenderScene();
//Collectthehits
hits=glRenderMode(GL_RENDER);
//Ifasinglehitoccured,displaytheinfo.
if(hits==1)
ProcessPlanet(selectBuff[3]);
//Restoretheprojectionmatrix
glMatrixMode(GL_PROJECTION);
glPopMatrix();
//Gobacktomodelviewfornormalrendering
glMatrixMode(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()
{
//Lightvaluesandcoordinates
glEnable(GL_DEPTH_TEST);//Hiddensurfaceremoval
glFrontFace(GL_CCW);//Counterclock-wisepolygonsfaceout
glEnable(GL_CULL_FACE);//Donotcalculateinsides
//Enablelighting
glEnable(GL_LIGHTING);
//Setupandenablelight0
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);
//Enablecolortracking
glEnable(GL_COLOR_MATERIAL);
//SetMaterialpropertiestofollowglColorvalues
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
//Blackbluebackground
glClearColor(0.60f,0.60f,0.60f,1.0f);
}
void ChangeSize( int w, int h)
{
//Preventadividebyzero
if(h==0)
h=1;
//SetViewporttowindowdimensions
glViewport(0,0,w,h);
//Calculateaspectratioofthewindow
fAspect=(GLfloat)w/(GLfloat)h;
//Settheperspectivecoordinatesystem
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Fieldofviewof45degrees,nearandfarplanes1.0and425
gluPerspective(45.0f,fAspect,1.0,425.0);
//Modelviewmatrixreset
glMatrixMode(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;
}