#include #include #include #include #include /* declare functions */ void init(int width, int height); void idle(void); void reshape(int width, int height); void define_earth_list(void); void define_moon_list(void); void display(void); /* declare globals */ GLint Earth; GLint Moon; GLfloat EARTH_ORBIT_ANGLE = 0.0; GLfloat EARTH_ROTATION_ANGLE = 0.0; GLfloat MOON_ORBIT_ANGLE = 0.0; GLfloat MOON_ROTATION_ANGLE = 0.0; /* * main function - set up window * set handlers * and run main loop */ int main(int argc, char **argv) { /* Initialize glut */ glutInit (&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (600, 600); glutCreateWindow ("Earth and Moon"); glutIdleFunc (idle); glutReshapeFunc(reshape); glutDisplayFunc (display); init(600,600); glutMainLoop(); return 1; } /* INIT * Use this function to set global variables and states. * Open GL function that are only done once can also be done here; * Setting the initial projection matrix, * Object creation and definition * Display list creation * Light creation and setup * Material definition and creation */ void init(int width, int height) { glClearColor (0.0, 0.0, 0.0, 0.0); glMatrixMode (GL_PROJECTION); glShadeModel (GL_FLAT); glLoadIdentity (); gluPerspective (45.0f, (GLfloat) width/(GLfloat) height, 0.1f, 300.0f); gluLookAt (150.0,50.0,50.0, 0.0,0.0,0.0, 0.0,1.0,0.0); glEnable (GL_DEPTH_TEST); /* initialize lists */ Earth = glGenLists(1); define_earth_list(); Moon = glGenLists(1); define_moon_list(); glMatrixMode (GL_MODELVIEW); glLoadIdentity(); } /* IDLE * these events are done every frame but aren't drawn * put pre-frame computations here * useful for changing animation variables between frames * also a good place to change over-all machine states */ void idle(void) { EARTH_ROTATION_ANGLE += 1.0; EARTH_ORBIT_ANGLE = (1.0/365.0)*EARTH_ROTATION_ANGLE; MOON_ORBIT_ANGLE = (1.0/30.0)*EARTH_ROTATION_ANGLE; MOON_ROTATION_ANGLE = MOON_ORBIT_ANGLE; } /* RESHAPE * width and height are generated by glut * These commands are done everytime the window is moved or resized. */ void reshape(int width, int height) { glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective (45.0f, (GLfloat) width/(GLfloat) height, 0.1f, 300.0f); gluLookAt (150.0,50.0,50.0, 0.0,0.0,0.0, 0.0,1.0,0.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* DISPLAY * call your command and function calls that handle drawing here */ void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix();\ glLoadIdentity(); glRotatef(EARTH_ORBIT_ANGLE,0.0,1.0,0.0); glTranslatef(30.0,0.0,0.0); glColor3f(0.0,0.0,1.0); glRotatef(EARTH_ROTATION_ANGLE,0.0,1.0,0.0); glCallList(Earth); glPushMatrix(); glRotatef(MOON_ORBIT_ANGLE,0.0,1.0,0.0); glTranslatef(20.0,0.0,0.0); glColor3f(1.0,0.0,1.0); glRotatef(MOON_ROTATION_ANGLE,0.0,1.0,0.0); glCallList(Moon); glPopMatrix(); glPopMatrix(); glutSwapBuffers(); glutPostRedisplay(); } /* * * glList definitions * */ /* * Earth List */ void define_earth_list(void) { glNewList(Earth,GL_COMPILE); glutWireSphere(10.0,16,16); glEndList(); } /* * Moon List */ void define_moon_list(void) { glNewList(Moon,GL_COMPILE); glutWireSphere(2.0,16,16); glEndList(); }