#include #include #include #include void cube(double r,double g,double b,double R) { int i; int j; glBegin(GL_POLYGON); glColor3d(r, g, b); glTranslated(0, 0, 0); glutSolidSphere(R, 16, 8); glRotated((double)r, 0.0, 1.0, 0.0); glEnd(); } void idle(void) { int i,I; for(i=0;i<=10000;i++){ for(I=0;I<=1000;I++){ } } glutPostRedisplay(); } void en(double hankei){ double th1,th2,th1_rad,th2_rad,x1,y1,x2,y2; for (th1 = 0.0; th1 <= 360.0; th1 = th1 + 10.0) { th2 = th1 + 10.0; th1_rad = th1 / 180.0 * 3.1415926; th2_rad = th2 / 180.0 * 3.1415926; x1 = hankei * cos(th1_rad); y1 = hankei * sin(th1_rad); x2 = hankei * cos(th2_rad); y2 = hankei * sin(th2_rad); glColor3d(0.7, 0.7, 0.7); glBegin(GL_LINES); glVertex3f( x1,0.0, y1 ); glVertex3f( x2,0.0, y2 ); glEnd(); } } void display(void) { static int r = 0; /* 回転角 */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); double hankei; /* モデルビュー変換行列の保存 */ glPushMatrix(); /* 太陽の描画 */ cube(1.0,0.0,0.0,3); glRotated((double)r, 0.0, 1.0, 0.0); /* 海王星の図形の描画 */ hankei = 18.0; glPushMatrix(); en(hankei); glTranslated(hankei, 0.0, 0.0); cube(0.1,0.0,1.0,0.5); glPopMatrix(); r=r+0.1; if (r >= 360) r = 0; glRotated((double)r, 0.0, 1.0, 0.0); /* 天皇星の図形の描画 */ hankei = 16; glPushMatrix(); en(hankei); glTranslated(hankei, 0.0, 0.0); cube(0.3,1.0,0.4,0.5); glPopMatrix(); r=r+0.1; if (r >= 360) r= 0; glRotated((double)r, 0.0, 1.0, 0.0); /* 土星の図形の描画 */ hankei = 13.0; glPushMatrix(); en(hankei); glTranslated(hankei, 0.0, 0.0); en(1.3); en(1.32); en(1.34); en(1.36); cube(0.8,0.8,0.4,1.0); glPopMatrix(); r=r+0.12; if (r >= 360) r = 0; glRotated((double)r, 0.0, 1.0, 0.0); /* 木星の図形の描画 */ hankei=9.0; glPushMatrix(); en(hankei); glTranslated(hankei, 0.0, 0.0); cube(0.6,0.3,0.1,1.2); glPopMatrix(); r=r+0.3; if (r >= 360) r = 0; glRotated((double)r, 0.0, 1.0, 0.0); /* 火星の図形の描画 */ hankei = 7.0; glPushMatrix(); en(hankei); glTranslated(hankei, 0.0, 0.0); cube(0.9,0.2,0.1,0.1); glPopMatrix(); r=r+1.0; if (r >= 360) r = 0; glRotated((double)r, 0.0, 1.0, 0.0); /* 地球の図形の描画 */ hankei = 6.0; glPushMatrix(); en(hankei); glTranslated(hankei, 0.0, 0.0); cube(0.2,0.2,1.0,0.2); glPopMatrix(); r=r+0.1; if (r >= 360) r = 0; glRotated((double)r, 0.0, 1.0, 0.0); /* 金星の図形の描画 */ hankei = 5.0; glPushMatrix(); en(hankei); glTranslated(hankei, 0.0,0.0); cube(0.9,0.9,0.2,0.2); glPopMatrix(); r=r+0.3; if (r >= 360) r = 0; glRotated((double)r, 0.0, 1.0, 0.0); /* 水星の図形の描画 */ hankei = 4.0; glPushMatrix(); en(hankei); glTranslated(hankei, 0.0, 0.0); cube(0.3,0.9,0.9,0.2); glPopMatrix(); r=r+0.2; if (r >= 360) r = 0; /* モデルビュー変換行列の復帰 */ glPopMatrix(); glutSwapBuffers(); /* 一周回ったら回転角を 0 に戻す */ if (++r >= 360) r = 0; } void resize(int w, int h) { glViewport(0, 0, w, h); /* 透視変換行列の設定 */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(90.0, (double)w / (double)h, 1.0, 200.0); /* モデルビュー変換行列の設定 */ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(10.0, 11.0, 16.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); } void mouse(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { /* アニメーション開始 */ glutIdleFunc(idle); } else { /* アニメーション停止 */ glutIdleFunc(0); } break; case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) { /* コマ送り (1ステップだけ進める) */ glutPostRedisplay(); } break; default: break; } } void keyboard(unsigned char key, int x, int y) { switch (key) { case 'q': case 'Q': case '\033': /* '\033' は ESC の ASCII コード */ exit(0); default: break; } } void init(void) { glClearColor(0.0, 0.0, 0.0, 1.0); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE| GLUT_DEPTH); glutCreateWindow(argv[0]); glutDisplayFunc(display); glutReshapeFunc(resize); glutMouseFunc(mouse); glutKeyboardFunc(keyboard); init(); glutMainLoop(); return 0; }