//cc 3.c -lglut -lGLU -lGL -lm #include #include #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,p = 0,q = 0; static int r1 = 0,r2 = 0,r3 = 0,a = 0; static double b = 0.0,c = 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 }; /* light2's color */ 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.5, 1.0 }; GLfloat yellow[] = { 0.8, 0.8, 0.2, 1.0 }; void cube(void){ int i; int j; glPushMatrix(); glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,yellow); if(push == 0){ if(b >= -4.0 && p == 0)b = b - 0.01; if(c >= -4.0 && p == 1)c = c - 0.01; if(b >= -4.0 && c >= -4.0 && p == 2){b = b - 0.01; c = c - 0.01;} if(b >= -4.0 && c <= 4.0 && p == 3){b = b - 0.01; c = c + 0.01;} if(++r1 >= 360) r1 = 0; if(a == 1){ if(++r3 >= 360) r3 = 0; /* haneru */ } }else if(push == 1){ if(++r2 >= 360) r2 = 0; if(b <= 4.0 && p == 0)b = b + 0.01; if(c <= 4.0 && p == 1)c = c + 0.01; if(b <= 4.0 && c <= 4.0 && p == 2){b = b + 0.01; c = c + 0.01;} if(b <= 4.0 && c >= -4.0 && p == 3){b = b + 0.01; c = c - 0.01;} if(a == 1){ if(++r3 >= 360) r3 = 0; /* haneru */ } }else{ } /* haneru */ glTranslated(0,fabs(3*sin(r3*PAI/72))-3,0); /* korogaru */ glTranslated(b,0.0,c); /* kaiten */ if(p == 0){ glRotated((double)r1,0.0,0.0,1.0); glRotated((double)r2,0.0,0.0,-1.0); } else if(p == 1){ glRotated((double)r1,-1.0,0.0,0.0); glRotated((double)r2,1.0,0.0,0.0); } else if(p == 2){ glRotated((double)r1,-1.0,0.0,1.0); glRotated((double)r2,1.0,0.0,-1.0); } else if(p == 3){ glRotated((double)r1,1.0,0.0,1.0); glRotated((double)r2,-1.0,0.0,-1.0); } else if(p == 4){ glRotated((double)r1,0.0,1.0,0.0); glRotated((double)r2,0.0,-1.0,0.0); } glBegin(GL_QUADS); glutSolidSphere(1.0,30,30); glPopMatrix(); glEnd(); } void idle(void) { glutPostRedisplay(); } /* display */ static void display(void) { /* light's position */ const static GLfloat lightpos[] = {3.0,4.0,-5.0,1.0}; int i; /* display clear */ 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); glLightfv(GL_LIGHT1, GL_POSITION, light1pos); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red); myGround(-(WR + GR + GR)); cube(); glutSwapBuffers(); glFlush(); } 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; if(++r1 >= 360) r1 = 0; glutIdleFunc(idle); } else { /* アニメーション停止 */ r3 = 0; glTranslated(b,0.0,0.0); glutIdleFunc(0); } break; case GLUT_MIDDLE_BUTTON: if (state == GLUT_DOWN) { /* アニメーション開始 */ push = 2; glutIdleFunc(idle); //if(++r3 >= 360) r3 = 0; if(a == 0) a = 1; else if(a == 1) a = 0; glTranslated(b,0.0,0.0); } else { /* アニメーション停止 */ r3 = 0; glTranslated(b,0.0,0.0); glutIdleFunc(0); } break; case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) { /* アニメーション開始 */ push = 1; glutIdleFunc(idle); if(++r2 >= 360) r2 = 0; } else { /* アニメーション停止 */ glutIdleFunc(0); r3 = 0; glTranslated(b,0.0,0.0); } break; default: break; } } void keyboard(unsigned char key, int x, int y) { /* ESC か q をタイプしたら終了 */ if(key == '\033' || key == 'q'){ exit(0); } switch (key) { case '1': p = 0; break; case '2': p = 1; break; case '3': p = 2; break; case '4': p = 3; break; case '5': p = 4; break; case '6': p = 5; break; default: break; } } 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; }