#include #include #include #include #define W 6 #define D 6 #define CYCLE 600 #define GR (D * 0.1) #define GD (D * 0.05) #define WR (W * 0.6) #define SR (W * 0.01) #define NG 8 #define PAI 3.1415926536 int push = 0; static int r1 = 0, r2 = 0, r3 = 0; static double a = 0.0,b=0.0; /* * 床を描く */ static void myGround(double height){ const static GLfloat ground[][4] = { /* 台の色    */ { 0.6, 0.6, 0.6, 1.0 }, { 0.3, 0.3, 0.3, 1.0 } }; int i,j; glBegin(GL_QUADS); glNormal3d(0.0, 1.0, 0.0); for (j = -D; j < D; ++j) { for (i = -W; i < W; ++i) { glMaterialfv(GL_FRONT, GL_DIFFUSE, ground[(i + j) & 1]); glVertex3d((GLdouble)i, height, (GLdouble)j); glVertex3d((GLdouble)i, height, (GLdouble)(j + 1)); glVertex3d((GLdouble)(i + 1), height, (GLdouble)(j + 1)); glVertex3d((GLdouble)(i + 1), height, (GLdouble)j); } } glEnd(); } /*光源設定*/ /* light1 */ GLfloat light0pos[] = { 0.0, 3.0, 5.0, 1.0 }; /* light2 */ GLfloat light1pos[] = { 5.0, 3.0, 0.0, 1.0 }; /*光源の色設定*/ GLfloat green[] = { 0.2, 0.8, 0.2, 1.0 }; GLfloat red[] = { 0.9, 0.6, 0.6, 1.0 }; GLfloat blue[] = { 0.2, 0.2, 0.4, 1.0 }; void cube(void){ int i; int j; glPushMatrix(); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue); if(push == 0){ if(++r1 >= 360) r1 = 0; if(++r3 >= 360) r3 = 0; }else if(push == 1){ if(++r2 >= 360) r2 = 0; }else if(push == 2){ if(++r3 >= 360) r3 = 0; } else if(push == 3) { if(a <= 3.0) a = a + 0.01; b = b; } else if(push == 4) { if(a >= -3.0) a = a - 0.01; b = b; } else if(push == 5) { if(b >= -3.0) b = b - 0.01; a=a; } else if(push == 6) { if(b <= 3.0) b = b + 0.01; a = a; } glTranslated(0, fabs(3*sin(r3*PAI/72))-3, 0); glTranslated(a,0.0,b); glRotated((double)r1, 0.0, 0.0, 1.0); glRotated((double)r2, 0.0, 1.0, 0.0); glBegin(GL_QUADS); glutSolidCube(1.0); glEnd(); glPopMatrix(); } void idle(void) { glutPostRedisplay(); } /* * 画面表示 */ static void display(void) { /* 光源の位置    */ const static GLfloat lightpos[] = {3.0,4.0,-5.0,1.0}; int i; /* 画面クリア */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* モデルビュー変換行列の初期化 */ glLoadIdentity(); /* 視点位置と視線方向 */ gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); /* 光源の位置を設定 */ glLightfv(GL_LIGHT0, GL_POSITION, light0pos); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red); /* 地面の描画 */ myGround(-(WR + GR + GR)); /* 図形の回転 */ cube(); glutSwapBuffers(); glFlush(); } static void resize(int w, int h) { /* ウィンドウ全体をビューポートにする */ glViewport(0, 0, w, h); /* 透視変換行列の指定 */ glMatrixMode(GL_PROJECTION); /* 透視変換行列の初期化 */ glLoadIdentity(); gluPerspective(60.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) { /* アニメーション開始 */ push = 0; glutIdleFunc(idle); if(++r1 >= 360) r1 = 0; } else { /* アニメーション停止 */ glutIdleFunc(0); } break; case GLUT_MIDDLE_BUTTON: if (state == GLUT_DOWN) { /* アニメーション開始 */ glutIdleFunc(idle); push = 2; if(++r3 >= 360) r3 = 0; } else { /* アニメーション停止 */ glutIdleFunc(0); } break; case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) { /* アニメーション開始 */ push = 1; glutIdleFunc(idle); if(++r2 >= 360) r2 = 0; } else { /* アニメーション停止 */ glutIdleFunc(0); } break; default: break; } } static void keyboard(unsigned char key, int x, int y) { switch(key){ case '\033': exit(0); break; case 'q': exit(0); break; case 'f'://右 push = 3; glutIdleFunc(idle); break; case 'a'://左 push = 4; glutIdleFunc(idle); break; case 'e'://前 push = 5; glutIdleFunc(idle); break; case 'x'://後ろ push = 6; glutIdleFunc(idle); break; default: break; } } static void init(void) { /* 初期設定 */ glClearColor(0.2, 0.3, 0.4, 1.0); } int main(int argc, char *argv[]) { glutInitWindowPosition(100, 100); glutInitWindowSize(640, 640); glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow(argv[0]); glutDisplayFunc(display); glutReshapeFunc(resize); glutMouseFunc(mouse); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glutKeyboardFunc(keyboard); init(); glutMainLoop(); return 0; }