/* * orrery3: A 3D orrery intended to demonstrate transformation stacks * and 3D viewing * * * PJF 11/03/04 */ #include #include #include int t=0; // yuck void circle() { float angle; glLineWidth(2.0); glBegin(GL_LINE_LOOP); for(angle=0.0;angle<2.0*M_PI;angle += M_PI/64) glVertex2f(cos(angle),sin(angle)); glEnd(); } /* each of these functions draws a colored and scaled circle, still at the origin. The effects of the scale are limited to he function since the scale happens inside a push/pop of the matrix stack. */ void sun() { /* big & bright */ glPushMatrix(); glColor3f(1.0,1.0,0.0); // yellow glScalef(0.1,0.1,0.1); glutSolidSphere(1.0,8,8); glPopMatrix(); } void earth() { /* smallish and blue */ glPushMatrix(); glColor3f(0.4,0.4,1.0); // medium blue glScalef(0.02,0.02,0.02); glutSolidSphere(1.0,8,8); glPopMatrix(); } void moon() { /* tiny & white */ glPushMatrix(); glColor3f(1.0,1.0,1.0); // white glScalef(0.005,0.005,0.005); glutSolidSphere(1.0,8,8); glPopMatrix(); } /* a moon is a moon is a moon. */ void phobos() { moon(); } void deimos() { moon(); } void comet() { glPushMatrix(); glColor3f(1.0,1.0,1.0); // white glScalef(0.005,0.005,0.005); glutSolidSphere(1.0,8,8); glPopMatrix(); } void venus() { /* small & gray */ glPushMatrix(); glColor3f(0.3,0.3,0.3); // dark gray glScalef(0.01,0.01,0.01); glutSolidSphere(1.0,8,8); glPopMatrix(); } void mars(){ /* earthsized and red */ glPushMatrix(); glColor3f(1.0,0.2,0.2); // bright red glScalef(0.02,0.02,0.02); glutSolidSphere(1.0,8,8); glPopMatrix(); } void display() { float eangle = t; float mangle = 3*t; float vangle = 2*t; float maangle = t*0.75; float mpangle = t*4, mdangle=t*5; float cangle = t*0.01; glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.5,0.5,0.5, //eye 0.0,0.0,0.0, // look-at point (center of Sun) 0.0,1.0,0.0); // view up vector sun(); // earth & moon glPushMatrix(); // incline orbit of earth relative to others glRotatef(30.0,1.0,0.0,0.0); // orbit of earth glPushMatrix(); glScalef(0.5,0.5,1.0); glColor3f(0.3,0.3,0.6); circle(); glPopMatrix(); // earth glPushMatrix(); glRotatef(eangle,0.0,0.0,1.0); glTranslatef(0.5,0.0,0.0); earth(); // orbit of moon glRotatef(70.0,0.0,1.0,0.0); glPushMatrix(); glScalef(0.07,0.07,1.0); glColor3f(0.3,0.3,0.3); circle(); glPopMatrix(); // moon glRotatef(mangle,0.0,0.0,1.0); glTranslatef(0.07,0.0,0.0); moon(); glPopMatrix(); glPopMatrix(); // orbit of venus glPushMatrix(); glScalef(0.3,0.3,1.0); glColor3f(0.4,0.2,0.4); circle(); glPopMatrix(); // venus glPushMatrix(); glRotatef(vangle,0.0,0.0,1.0); glTranslatef(0.3,0.0,0.0); venus(); glPopMatrix(); // orbit of mars glPushMatrix(); glScalef(0.7,0.7,1.0); glColor3f(0.6,0.2,0.2); circle(); glPopMatrix(); // mars, phobos,diemos glPushMatrix(); glRotatef(maangle,0.0,0.0,1.0); glTranslatef(0.7,0.0,0.0); mars(); glPushMatrix(); // orbit of phobos glPushMatrix(); glScalef(0.1,0.1,1.0); glColor3f(0.2,0.6,0.2); circle(); glPopMatrix(); // phobos glRotatef(mpangle,0.0,0.0,1.0); glTranslatef(0.1,0.0,0.0); phobos(); glPopMatrix(); // orbit of diemos glPushMatrix(); glScalef(0.1,0.1,1.0); glColor3f(0.2,0.2,0.6); circle(); glPopMatrix(); glPushMatrix(); glRotatef(mdangle,0.0,0.0,1.0); glTranslatef(0.07,0.0,0.0); phobos(); // diemos, phobos same glPopMatrix(); glPopMatrix(); // finally, the comet, on an offset circular orbit glPushMatrix(); glRotatef(45.0,0.0,0.0,1.0); glTranslatef(1.0*cos(cangle)+ 0.8,0.5*sin(cangle),0); comet(); glPopMatrix(); glFlush(); } /* keeps a timer variable; used to calculate orbit positions */ int timer(int value) { t++; glutPostRedisplay(); glutTimerFunc(10,timer,0); } void init() { glClearColor(0.0,0.0,0.0,0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // choose orthographic or perspective projection glOrtho(-1.3,1.3,-1.3,1.3,-1.5,1.5); //gluPerspective(120,1.0,0.1,2.2); } int main(int argc,char *argv[]) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("orrery"); glutDisplayFunc(display); glutTimerFunc(10,timer,0); init(); glutMainLoop(); }