/* * 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(); } #define S { glutSolidTeapot(1.0); } // #define S { glutSolidSphere(1.0, 6, 6); } /* 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 */ GLfloat a[] = { 1.0, 1.0, 0.0 }; glPushMatrix(); //glColor3f(1.0,1.0,0.0); // yellow //glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,a); glScalef(0.1,0.1,0.1); S; glPopMatrix(); } void earth() { /* smallish and blue */ GLfloat a[] = { 0.4, 0.4, 0.4 }; glPushMatrix(); //glColor3f(0.4,0.4,1.0); // medium blue //glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,a); glScalef(0.02,0.02,0.02); S; glPopMatrix(); } void moon() { /* tiny & white */ GLfloat a[] = { 1.0, 1.0, 1.0 }; glPushMatrix(); //glColor3f(1.0,1.0,1.0); // white //glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,a); glScalef(0.005,0.005,0.005); S; glPopMatrix(); } /* a moon is a moon is a moon. */ void phobos() { moon(); } void deimos() { moon(); } void comet() { GLfloat a[] = { 1.0, 1.0, 1.0 }; glPushMatrix(); //glColor3f(1.0,1.0,1.0); // white //glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,a); glScalef(0.005,0.005,0.005); S; glPopMatrix(); } void venus() { /* small & gray */ GLfloat a[] = { 0.3, 0.3, 0.3 }; glPushMatrix(); //glColor3f(0.3,0.3,0.3); // dark gray //glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,a); glScalef(0.01,0.01,0.01); S; glPopMatrix(); } void mars(){ /* earthsized and red */ GLfloat a[] = { 1.0, 0.2, 0.2 }; glPushMatrix(); //glColor3f(1.0,0.2,0.2); // bright red //glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,a); glScalef(0.02,0.02,0.02); S; 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); glPushMatrix(); 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 inlined relative to orbit of earth glRotatef(20.0,0.0,1.0,0.0); // orbit of moon //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(); glPopMatrix(); glFlush(); } /* keeps a timer variable; used to calculate orbit positions */ int timer(int value) { t++; glutPostRedisplay(); glutTimerFunc(10,timer,0); } void init() { GLfloat pos0[] = {0.3,-0.3,-18.0 }; GLfloat diffuse0[] = { 0.2, 0.2, 0.2, 1.0 }; GLfloat specular0[] = { 1., 1., 1., 1.0 }; GLfloat ambient0[] = {0.2, 0.2, 0.2, 1.0 }; GLfloat mdif[]= {0.2, 0.2, 0.2, 1.0}; GLfloat mspec[] = {0.8, 0.8, 0.8, 1.0}; GLfloat mamb[]= {0.0, 0.0, 0.0, 1.0}; GLfloat mshin = 40.0; // enable lighting and position the light glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0,0.0,2.0, //eye 0.0,0.0,0.0, // look-at point (center of Sun) 0.0,1.0,0.0); // view up vector glLightfv(GL_LIGHT0,GL_POSITION,pos0); glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse0); glLightfv(GL_LIGHT0,GL_SPECULAR,specular0); glLightfv(GL_LIGHT0,GL_AMBIENT,ambient0); glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mamb); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mdif); glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mspec); glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,mshin); glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // choose orthographic or perspective projection glOrtho(-1.1,1.1,-1.1,1.1,-2.5,2.5); //gluPerspective(120,1.0,0.1,2.2); glClearColor(0.0,0.0,0.0,0.0); glShadeModel(GL_FLAT); } 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(); }