//cc gl.c -lglut -lGLU -lGL -lm #include #include #include #include #define W 6 #define D 4 #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,r = 0,g = 0,b = 0; static double x = 0.0,y = 0.0; /* * 床を描く */ static void myGround(double height){ const static GLfloat ground[][4] = { /* 台の色    */ { 1.0, 1.0, 1.0, 1.0 }, { -0.1, -0.1, -0.1, 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[] = { 1.0, 0.0, 0.0, 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(x <= 3.0) x = x + 0.05; y = y; push = -1; } else if(push == 4) { if(x >= -3.0) x = x - 0.05; y = y; push = -1; } else if(push == 5) { if(y >= -3.0) y = y - 0.05; x = x; push = -1; } else if(push == 6) { if(y <= 3.0) y = y + 0.05; x = x; push = -1; } else if(push == 7) { blue[0] = ((float)rand()+1.0)/((float)RAND_MAX+2.0); blue[1] = ((float)rand()+1.0)/((float)RAND_MAX+2.0); blue[2] = ((float)rand()+1.0)/((float)RAND_MAX+2.0); push = -1; } //中クリック、左クリック上下移動 glTranslated(0, fabs(3*sin(r3*PAI/72))-3, 0); //キーボード移動 glTranslated(x,0.0,y); //左クリック回転 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(); /* 視点位置と視線方向 */ // 視点の位置x,y,z;視界の中心位置の参照点座標x,y,z//視界の上方向のベクトルx,y,z 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 'd'://右 push = 3; glutIdleFunc(idle); break; case 'a'://左 push = 4; glutIdleFunc(idle); break; case 'w'://前 push = 5; glutIdleFunc(idle); break; case 's'://後ろ push = 6; glutIdleFunc(idle); break; case 32://色をランダムに変更(スペース) push = 7; glutIdleFunc(idle); break; default: break; } } static void init(void) { /* 初期設定 */ glClearColor(0.2, 0.3, 0.6, 1.0); glEnable(GL_DEPTH_TEST); } int main(int argc, char *argv[]) { srand((unsigned)time(NULL)); 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_LIGHTING); glEnable(GL_LIGHT0); glutKeyboardFunc(keyboard); init(); glutMainLoop(); return 0; }