#include #include #include #include #include #define W 8 /* 地面の幅        */ #define D 10 /* 地面の長さ       */ #define QX 1.0 /* 球の初期位置のx座標値 */ #define QY 1.0 /* 球の初期位置のy座標値 */ #define QZ (-5.0) /* 球の初期位置のz座標値 */ #define R 1.0 /* 物体の半径   */ #define PAI 3.141592 static GLfloat white[] = { 0.8, 0.8, 0.8, 1.0 }; /* 球の色 */ static GLfloat lightpos[] = { 3.0, 4.0, 5.0, 1.0 }; /* 光源の位置 */ static int r1 = 0, r2 = 0, r3 = 0; static double a = 0.0,b=0.0; int mouse_button = 0; int key_button = 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(); } void cube(void){ int i; int j; glPushMatrix(); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, white); if(mouse_button == 0){ if(++r1 >= 360) r1 = 0; if(++r3 >= 360) r3 = 0; }else if(mouse_button == 1){ if(++r2 >= 360) r2 = 0; }else if(mouse_button == 2){ if(++r3 >= 360) r3 = 0; } else if(mouse_button == 3) { a = a + 0.01; b = b; } else if(mouse_button == 4) { a = a - 0.01; b = b; } else if(mouse_button == 5) { b = b - 0.01; a=a; } else if(mouse_button == 6) { b = b + 0.01; a = a; } glTranslated(0.0, 0.0, -D); 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); /*球体の表示*/ if(key_button == 0){ glBegin(GL_POLYGON); glutSolidSphere(R, 16, 8); } /*立方体の表示*/ else if(key_button == 1){ glBegin(GL_QUADS); glutSolidCube(R+R); } glPopMatrix(); glEnd(); } void idle(void) { glutPostRedisplay(); } /* 画面表示*/ static void display(void) { /* 画面クリア */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* モデルビュー変換行列の初期化 */ glLoadIdentity(); /* 光源の位置を設定 */ glLightfv(GL_LIGHT0, GL_POSITION, lightpos); /* 視点の移動(物体の方を奥に移す)*/ glTranslated(0.0, -QY, -D); /* 地面の描画 */ myGround(0.0); /* 色の指定 */ glMaterialfv(GL_FRONT, GL_DIFFUSE, white); /* 図形の回転 */ cube(); glutSwapBuffers(); glFlush(); } static 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: key_button = 0; break; case GLUT_RIGHT_BUTTON: key_button = 1; break; default: break; } } static void keyboard(unsigned char key, int x, int y) { /* ESC か q をタイプしたら終了 */ switch(key){ case '\033': exit(0); break; case 'q': exit(0); break; case '1'://回転 mouse_button = 1; glutIdleFunc(idle); break; case '2'://上下 mouse_button = 2; glutIdleFunc(idle); break; case 'd'://右 mouse_button = 3; glutIdleFunc(idle); break; case 'a'://左 mouse_button = 4; glutIdleFunc(idle); break; case 'w'://前 mouse_button = 5; glutIdleFunc(idle); break; case 's'://後ろ mouse_button = 6; glutIdleFunc(idle); break; default: break; } } static void init(void) { /* 初期設定 */ glClearColor(1.0, 1.0, 1.0, 0.0); // glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); } int main(int argc, char *argv[]) { glutInitWindowPosition(100,100); glutInitWindowSize(640,640); glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutCreateWindow(argv[0]); glutDisplayFunc(display); glutReshapeFunc(resize); glutMouseFunc(mouse); glutKeyboardFunc(keyboard); init(); glutMainLoop(); return 0; }