#include #include #include #define R 0.5 #define W 6 #define D 6 GLfloat light0pos[] = { -150.0, -10.0, -150.0, 1.0 }; GLfloat light1pos[] = { 150.0, 0.0, 10.0, 1.0 }; GLfloat param_d[] = { 0.7, 0.7, 0.7, 1.0 }; GLfloat param_s[] = { 1.0, 1.0, 1.0, 1.0 }; static double a = 0.0, b = 0.0; void idle(void) { glutPostRedisplay(); } void display(void) { int i; int j; const static GLfloat bcolor[] = { 0.8, 0.3, 0.2, 1.0 }; /* 球の色 */ static int r = 0; /* 回転角 */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); /* 光源の位置設定 */ glLightfv(GL_LIGHT0, GL_POSITION, light0pos); glLightfv(GL_LIGHT1, GL_POSITION, light1pos); /* 図形の回転 */ glTranslated(a,0.0,b); glRotated((double)r, 0.0, 1.0, 0.0); /* 図形の描画 */ glPushMatrix(); glMaterialfv(GL_FRONT, GL_DIFFUSE, bcolor); glutSolidSphere(R, 32, 16); glPopMatrix(); glutSwapBuffers(); /* 一周回ったら回転角を 0 に戻す */ if (++r >= 360) r = 0; } void resize(int w, int h) { glViewport(0, 0, w, h); /* 透視変換行列の設定 */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0); /* モデルビュー変換行列の設定 */ glMatrixMode(GL_MODELVIEW); } 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); case 'i': /* 前 */ a = a; b = b + 0.03; glutPostRedisplay(); break; case 'k': /* 後 */ a = a; b = b - 0.03; glutPostRedisplay(); break; case 'l': /* 左 */ a = a - 0.03; b = b; glutPostRedisplay(); break; case ';': /* 右 */ a = a + 0.03; b = b; glutPostRedisplay(); break; default: break; } } void init(void) { glClearColor(1.0, 1.0, 1.0, 1.0); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glLightfv(GL_LIGHT1, GL_DIFFUSE, param_d); glLightfv(GL_LIGHT1, GL_SPECULAR, param_s); } int main(int argc, char *argv[]) { glutInitWindowPosition(100, 100); glutInitWindowSize(320, 240); 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; }