// 3D空間内を移動出来るプログラム // // WASDで平行移動 ←↑↓→で視点移動 // R/D 上下移動 // // 数字とobjectの対応 // 1 Redcube 2 GreenSphere 3 BlueTetrahedron 4 YellowTorus // 5 PinkDodecahedron 6 whiteTeapot // 各数字を押した後、 // ←↑↓→で回転 // // r/f 上下移動 WASD平行移動 // 右クリック:拡大、左クリック:縮小、真ん中クリック:初期サイズに戻る #include #include #include #include #include #define W 15 #define D 15 static double ex = 0.0, ez = 0.0, ey = 0.0; /* 視点の位置 */ static double the = 0.0, phi = 0.0; /* 視点の向き */ static GLfloat lit_amb[4]={0.2, 0.2, 0.2, 1.0}; /* 環境光 */ static GLfloat lit_dif[4]={0.5, 0.5, 0.5, 1.0}; /* 拡散光 */ static GLfloat lit_spc[4]={0.5, 0.5, 0.5, 1.0}; /* 鏡面光 */ static GLfloat lit_pos[4]={0.0, 0.5, 0.0, 1.0}; /* 光源の位置 */ static GLfloat lit_shi[1]={30.0}; /* ハイライト */ static GLfloat lit_emi[4]={0.0,0.0,0.0,0.0}; /* 発光係数 */ static GLfloat spotDirrection[]={0.0, 0.0, -1.0}; /* 光源の向き */ static double sca1 = 1.0, sca2 = 1.0, sca3 = 1.0, sca4 = 1.0, sca5 = 1.0, sca6 = 1.0; /* 拡大係数 */ static double the1 = 0.0, the2 = 0.0, the3 = 0.0, the4 = 60.0, the5 = 0.0, the6 = 0.0; /*横回転*/ static double phi1 = 0.0, phi2 = 0.0, phi3 = 0.0, phi4 = 0.0, phi5 = 0.0, phi6 = 0.0;/*縦回転*/ static double obj1[3]={0.0,0.0,0.0},obj2[3]={0.0,0.0,0.0},obj3[3]={0.0,0.0,0.0},obj4[3]={0.0,0.0,0.0},obj5[3]={0.0,0.0,0.0}, obj6[3]={0.0,0.0,0.0}; /* object移動用 (x, y, z) */ static int flag = 0; /* オブジェクト指定用 */ void scene(void) { /* 物体の色 */ static GLfloat white[] = { 0.5, 1.0, 1.0, 1.0 }; static GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 }; static GLfloat red[] = { 0.8, 0.2, 0.2, 1.0 }; static GLfloat pink[] = { 1.0, 0.4, 0.7, 1.0 }; static GLfloat yellow[] = { 0.8, 0.8, 0.2, 1.0 }; static GLfloat orange[] = { 1.0, 0.5, 0.0, 1.0 }; static GLfloat green[] = { 0.2, 0.8, 0.2, 1.0 }; static GLfloat lightblue[] = { 0.0, 0.9, 1.0, 1.0 }; static GLfloat blue[] = { 0.2, 0.2, 0.8, 1.0 }; static GLfloat purple[] = { 0.5, 0.0, 0.5, 1.0 }; static GLfloat ground[][4] = { // 地面の色の設定 { 0.0, 0.0, 0.0, 1.0 }, { 1.0, 1.0, 1.0, 1.0 } }; int i, j; /* それぞれを4つの整数値、または浮動小数点で指定する */ glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, lit_amb); // 環境光 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, lit_dif); // 拡散光 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, lit_spc); // 鏡面光 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, lit_shi); // ハイライト glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, lit_emi); // 発光係数 /* 赤い箱(キー:1) */ glPushMatrix(); // モデルビュー変換行列のプッシュ&保存 glTranslated(2.0+obj1[0], 1.5+obj1[1], 3.0+obj1[2]); // 右側から平行移動 glRotated( the1, 0.0, 1.0, 0.0); // ベクトル (0.0, 1.0, 0.0)を回転軸としてthe1だけ回転 glRotated( phi1, cos(M_PI*the/180.0) ,0.0,sin(M_PI*the/180.0) ); glScalef( sca1, sca1, sca1); // それぞれをsca1,sca1,sca1倍する行列を右側からかける glMaterialfv(GL_FRONT, GL_DIFFUSE, red); // 色を赤に設定 glutSolidCube(1.0); // BOX glPopMatrix(); /* 緑の玉(キー:2) */ glPushMatrix(); glTranslated(-3.0+obj2[0], 0.5+obj2[1], 3.0+obj2[2]); glRotated( the2, 0.0, 1.0, 0.0); glRotated( phi2, cos(M_PI*the/180.0) , 0.0, sin(M_PI*the/180.0)); glScalef( sca2, sca2, sca2); glMaterialfv(GL_FRONT, GL_DIFFUSE, green); glutSolidSphere(1.0,30,30); glPopMatrix(); /* 青い六面体(キー:3) */ glPushMatrix(); glTranslated(-3.0+obj3[0], 2.5+obj3[1], 0.0+obj3[2]); glRotated( the3, 0.0, 1.0, 0.0); glRotated( phi3, cos(M_PI*the/180.0) ,0.0,sin(M_PI*the/180.0) ); glScalef( sca3, sca3, sca3); glMaterialfv(GL_FRONT, GL_DIFFUSE, blue); glutSolidTetrahedron(); glPopMatrix(); /* 黄色いトーラス(キー:4) */ glPushMatrix(); glTranslated(0.0+obj4[0], 0.0+obj4[1], 0.0+obj4[2]); glRotated( the4, 0.0, 1.0, 0.0); glRotated( phi4, cos(M_PI*the/180.0) ,0.0,sin(M_PI*the/180.0) ); glScalef(sca4, sca4, sca4); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, yellow); glutSolidTorus(0.5,7,40,40); glPopMatrix(); /* 正十二面体(キー:5) */ glPushMatrix(); glTranslated(3.0+obj5[0], 1.5+obj5[1], -1.0+obj5[2]); glRotated( the5, 0.0, 1.0, 0.0); glRotated( phi5, cos(M_PI*the/180.0) ,0.0,sin(M_PI*the/180.0) ); glScalef(sca5, sca5, sca5); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, pink); glutSolidDodecahedron(); glPopMatrix(); /* ティーポット(キー:6) */ glPushMatrix(); glTranslated(0.0+obj6[0], 0.3+obj6[1], 0.0+obj6[2]); glRotated( the6, 0.0, 1.0, 0.0); glRotated( phi6, cos(M_PI*the/180.0) ,0.0,sin(M_PI*the/180.0) ); glScalef(sca6, sca6, sca6); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, red); glutSolidTeapot(1.0); glPopMatrix(); /* 地面 */ 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, -0.5, (GLdouble)j); glVertex3d((GLdouble)i, -0.5, (GLdouble)(j + 1)); glVertex3d((GLdouble)(i + 1), -0.5, (GLdouble)(j + 1)); glVertex3d((GLdouble)(i + 1), -0.5, (GLdouble)j); } } glEnd(); } void display(void) { /* 画面クリア */ 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_AMBIENT, lit_amb); glLightfv(GL_LIGHT0, GL_DIFFUSE, lit_dif); glLightfv(GL_LIGHT0, GL_SPECULAR, lit_spc); glLightfv(GL_LIGHT0, GL_POSITION, lit_pos); glLightfv(GL_LIGHT0, GL_SHININESS, lit_shi); glLightfv(GL_LIGHT0, GL_EMISSION, lit_emi); glLightf( GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.01); glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION, spotDirrection ); //スポットライトの向ける方向(デフォルト (0,0,-1.0)) glLightf( GL_LIGHT0, GL_SPOT_CUTOFF, 25.0 );// スポットライトの絞り(デフォルト 180.0) glLightf( GL_LIGHT0, GL_SPOT_EXPONENT, 0.1 );// スポットライトの中心からの減衰の度合い(デフォルト 0) glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); /* シーンの描画 */ scene(); glutSwapBuffers(); } void idle(void) { /* 再描画(display()関数を呼び出す) */ glutPostRedisplay(); } void movedisplay(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glPushMatrix(); glRotated(the, 0.0, 1.0, 0.0); glRotated(phi,cos(M_PI*the/180.0) ,0.0,sin(M_PI*the/180.0)); glTranslated(ex, ey, ez); scene(); glPopMatrix(); glutSwapBuffers(); } 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); glLoadIdentity(); } void max(void) { switch(flag){ case 0: if(ez > D) ez = D; if(ez < -D) ez = -D; if(ex > W) ex = W; if(ex < -W) ex = -W; break; case 1: if(obj1[0] > 10.0) obj1[0]=10.0; if(obj1[0] < -10.0) obj1[0]=-10.0; if(obj1[2] > 13.0) obj1[2]=13.0; if(obj1[2] < -7.0)obj1[2]=-7.0; break; case 2: if(obj2[0] > 10.0) obj2[0]=10.0; if(obj2[0] < -10.0) obj2[0]=-10.0; if(obj2[2] > 7.0) obj2[2]=7.0; if(obj2[2] < -13.0) obj2[2]=-13.0; break; case 3: if(obj3[0] > 13.0) obj3[0]=13.0; if(obj3[0] < -7.0) obj3[0]=-7.0; if(obj3[2] > 10.0) obj3[2]=10.0; if(obj3[2] < -10.0) obj3[2]=-10.0; break; case 4: if(obj4[0] > 7.0) obj4[0]=7.0; if(obj4[0] < -13.0) obj4[0]=-13.0; if(obj4[2] > 10.0) obj4[2]=10.0; if(obj4[2] < -10.0) obj4[2]=-10.0; break; case 5: if(obj5[0] > 7.0) obj5[0]=7.0; if(obj5[0] < -13.0) obj5[0]=-13.0; if(obj5[2] > 10.0) obj5[2]=10.0; if(obj5[2] < -10.0) obj5[2]=-10.0; break; case 6: if(obj6[0] > 7.0) obj6[0]=7.0; if(obj6[0] < -13.0) obj6[0]=-13.0; if(obj6[2] > 10.0) obj6[2]=10.0; if(obj6[2] < -10.0) obj6[2]=-10.0; break; default: break; } return; } void mouse(int button, int state, int x, int y) { if(button == GLUT_LEFT_BUTTON || button == GLUT_RIGHT_BUTTON || button == GLUT_MIDDLE_BUTTON ){ // マウスがクリックされたら if(state == GLUT_DOWN){ /* 左をクリックすると拡大、右をクリックすると縮小、真ん中をクリックすると初期サイズに戻る */ switch (flag) { case 0: break; case 1: glutIdleFunc(idle); if(button == GLUT_LEFT_BUTTON) sca1 += 0.1; else if(button == GLUT_RIGHT_BUTTON) sca1 -= 0.1; else if(button == GLUT_MIDDLE_BUTTON) sca1 = 1.0; if( sca1 > 2.0 ) sca1 = 2.0; if( sca1 < 0.3 ) sca1 = 0.3; movedisplay(); glutIdleFunc(0); break; case 2: glutIdleFunc(idle); if(button == GLUT_LEFT_BUTTON) sca2 += 0.1; else if(button == GLUT_RIGHT_BUTTON) sca2 -= 0.1; else if(button == GLUT_MIDDLE_BUTTON) sca2 = 1.0; if( sca2 > 1.5 ) sca2 = 1.5; if( sca2 < 0.3 ) sca2 = 0.3; movedisplay(); glutIdleFunc(0); break; case 3: glutIdleFunc(idle); if(button == GLUT_LEFT_BUTTON) sca3 += 0.1; else if(button == GLUT_RIGHT_BUTTON) sca3 -= 0.1; else if(button == GLUT_MIDDLE_BUTTON) sca3 = 1.0; if( sca3 > 1.5 ) sca3 = 1.5; if( sca3 < 0.3 ) sca3 = 0.3; movedisplay(); glutIdleFunc(0); break; case 4: glutIdleFunc(idle); if(button == GLUT_LEFT_BUTTON) sca4 += 0.1; else if(button == GLUT_RIGHT_BUTTON) sca4 -= 0.1; else if(button == GLUT_MIDDLE_BUTTON) sca4 = 1.0; if( sca4 > 2.0 ) sca4 = 2.0; if( sca4 < 0.3 ) sca4 = 0.3; movedisplay(); glutIdleFunc(0); break; case 5: glutIdleFunc(idle); if(button == GLUT_LEFT_BUTTON) sca5 += 0.1; else if(button == GLUT_RIGHT_BUTTON) sca5 -= 0.1; else if(button == GLUT_MIDDLE_BUTTON) sca5 = 1.0; if( sca5 > 2.0 ) sca5 = 2.0; if( sca5 < 0.5 ) sca5 = 0.5; movedisplay(); glutIdleFunc(0); break; case 6: glutIdleFunc(idle); if(button == GLUT_LEFT_BUTTON) sca6 += 0.1; else if(button == GLUT_RIGHT_BUTTON) sca6 -= 0.1; else if(button == GLUT_MIDDLE_BUTTON) sca6 = 1.0; if( sca6 > 2.0 ) sca6 = 2.0; if( sca6 < 0.3 ) sca6 = 0.3; movedisplay(); glutIdleFunc(0); break; default: break; } } else { glutIdleFunc(0); } } } void key(int key, int x, int y) { switch ( key ) { /*************視点移動/物体回転**************/ case GLUT_KEY_DOWN: //上 switch (flag) { case 0: glutIdleFunc(idle); phi+=5.0; if(phi > 75.0) phi=75.0; movedisplay(); glutIdleFunc(0); break; case 1: glutIdleFunc(idle); phi1 += 5.0; if(phi1 > 360.0) phi1 = 0.0; movedisplay(); glutIdleFunc(0); break; case 2: glutIdleFunc(idle); phi2 += 5.0; if(phi2 > 360.0) phi2 = 0.0; movedisplay(); glutIdleFunc(0); break; case 3: glutIdleFunc(idle); phi3 += 5.0; if(phi3 > 360.0) phi3 = 0.0; movedisplay(); glutIdleFunc(0); break; case 4: glutIdleFunc(idle); phi4 += 5.0; if(phi4 > 360.0) phi4 = 0.0; movedisplay(); glutIdleFunc(0); break; case 5: glutIdleFunc(idle); phi5 += 5.0; if(phi5 > 360.0) phi5 = 0.0; movedisplay(); glutIdleFunc(0); break; case 6: glutIdleFunc(idle); phi6 += 5.0; if(phi6 > 360.0) phi6 = 0.0; movedisplay(); glutIdleFunc(0); break; default: break; } break; case GLUT_KEY_UP: //下 switch (flag) { case 0: glutIdleFunc(idle); phi-=5.0; if(phi < -75.0) phi= -75.0; movedisplay(); glutIdleFunc(0); break; case 1: glutIdleFunc(idle); phi1 -= 5.0; if(phi1 < -360.0) phi1 = 0.0; movedisplay(); glutIdleFunc(0); break; case 2: glutIdleFunc(idle); phi2 -= 5.0; if(phi2 < -360.0) phi2 = 0.0; movedisplay(); glutIdleFunc(0); break; case 3: glutIdleFunc(idle); phi3 -= 5.0; if(phi3 < -360.0) phi3 = 0.0; movedisplay(); glutIdleFunc(0); break; case 4: glutIdleFunc(idle); phi4 -= 5.0; if(phi4 < -360.0) phi4 = 0.0; movedisplay(); glutIdleFunc(0); break; case 5: glutIdleFunc(idle); phi5 -= 5.0; if(phi5 < -360.0) phi5 = 0.0; movedisplay(); glutIdleFunc(0); break; case 6: glutIdleFunc(idle); phi6 -= 5.0; if(phi6 < -360.0) phi6 = 0.0; movedisplay(); glutIdleFunc(0); break; default: break; } break; case GLUT_KEY_LEFT: //左 switch (flag) { case 0: glutIdleFunc(idle); the-=5.0; if(the < -360.0) the=0.0; movedisplay(); glutIdleFunc(0); break; case 1: glutIdleFunc(idle); the1 -= 5.0; if(the1 < -360.0) the1 = 0.0; movedisplay(); glutIdleFunc(0); break; case 2: glutIdleFunc(idle); the2 -= 5.0; if(the2 < -360.0) the2 = 0.0; movedisplay(); glutIdleFunc(0); break; case 3: glutIdleFunc(idle); the3 -= 5.0; if(the3 < -360.0) the3 = 0.0; movedisplay(); glutIdleFunc(0); break; case 4: glutIdleFunc(idle); the4 -= 5.0; if(the4 < -360.0) the4 = 0.0; movedisplay(); glutIdleFunc(0); break; case 5: glutIdleFunc(idle); the5 -= 5.0; if(the5 < -360.0) the5 = 0.0; movedisplay(); glutIdleFunc(0); break; case 6: glutIdleFunc(idle); the6 -= 5.0; if(the6 < -360.0) the6 = 0.0; movedisplay(); glutIdleFunc(0); break; default: break; } break; case GLUT_KEY_RIGHT: //右 switch (flag) { case 0: glutIdleFunc(idle); the+=5.0; if(the > 360.0) the=0.0; movedisplay(); glutIdleFunc(0); break; case 1: glutIdleFunc(idle); the1 += 5.0; if(the1 > 360.0) the1 = 0.0; movedisplay(); glutIdleFunc(0); break; case 2: glutIdleFunc(idle); the2 += 5.0; if(the2 > 360.0) the2 = 0.0; movedisplay(); glutIdleFunc(0); break; case 3: glutIdleFunc(idle); the3 += 5.0; if(the3 > 360.0) the3 = 0.0; movedisplay(); glutIdleFunc(0); break; case 4: glutIdleFunc(idle); the4 += 5.0; if(the4 > 360.0) the4 = 0.0; movedisplay(); glutIdleFunc(0); break; case 5: glutIdleFunc(idle); the5 += 5.0; if(the5 > 360.0) the5 = 0.0; movedisplay(); glutIdleFunc(0); break; case 6: glutIdleFunc(idle); the6 += 5.0; if(the6 > 360.0) the6 = 0.0; movedisplay(); glutIdleFunc(0); break; default: break; } break; /************************************/ default: break; } } void keyboard(unsigned char key, int x, int y) { switch (key) { case '\033': // ESC 終了 exit(0); break; case 'd': //右移動 switch(flag){ case 0: glutIdleFunc(idle); ez-=0.2*sin(M_PI*the/180.0); ex-=0.2*cos(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 1: glutIdleFunc(idle); obj1[0]+=0.2*cos(M_PI*the/180.0); obj1[2]+=0.2*sin(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 2: glutIdleFunc(idle); obj2[0]+=0.2*cos(M_PI*the/180.0); obj2[2]+=0.2*sin(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 3: glutIdleFunc(idle); obj3[0]+=0.2*cos(M_PI*the/180.0); obj3[2]+=0.2*sin(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 4: glutIdleFunc(idle); obj4[0]+=0.2*cos(M_PI*the/180.0); obj4[2]+=0.2*sin(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 5: glutIdleFunc(idle); obj5[0]+=0.2*cos(M_PI*the/180.0); obj5[2]+=0.2*sin(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 6: glutIdleFunc(idle); obj6[0]+=0.2*cos(M_PI*the/180.0); obj6[2]+=0.2*sin(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; default: break; } break; case 'a': //左移動 switch(flag){ case 0: glutIdleFunc(idle); ez+=0.2*sin(M_PI*the/180.0); ex+=0.2*cos(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 1: glutIdleFunc(idle); obj1[0]-=0.2*cos(M_PI*the/180.0); obj1[2]-=0.2*sin(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 2: glutIdleFunc(idle); obj2[0]-=0.2*cos(M_PI*the/180.0); obj2[2]-=0.2*sin(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 3: glutIdleFunc(idle); obj3[0]-=0.2*cos(M_PI*the/180.0); obj3[2]-=0.2*sin(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 4: glutIdleFunc(idle); obj4[0]-=0.2*cos(M_PI*the/180.0); obj4[2]-=0.2*sin(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 5: glutIdleFunc(idle); obj5[0]-=0.2*cos(M_PI*the/180.0); obj5[2]-=0.2*sin(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 6: glutIdleFunc(idle); obj6[0]-=0.2*cos(M_PI*the/180.0); obj6[2]-=0.2*sin(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; default: break; } break; case 'w': //前方移動 switch(flag){ case 0: glutIdleFunc(idle); ex-=0.2*sin(M_PI*the/180.0); ez+=0.2*cos(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 1: glutIdleFunc(idle); obj1[0]+=0.2*sin(M_PI*the/180.0); obj1[2]-=0.2*cos(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 2: glutIdleFunc(idle); obj2[0]+=0.2*sin(M_PI*the/180.0); obj2[2]-=0.2*cos(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 3: glutIdleFunc(idle); obj3[0]+=0.2*sin(M_PI*the/180.0); obj3[2]-=0.2*cos(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 4: glutIdleFunc(idle); obj4[0]+=0.2*sin(M_PI*the/180.0); obj4[2]-=0.2*cos(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 5: glutIdleFunc(idle); obj5[0]+=0.2*sin(M_PI*the/180.0); obj5[2]-=0.2*cos(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 6: glutIdleFunc(idle); obj6[0]+=0.2*sin(M_PI*the/180.0); obj6[2]-=0.2*cos(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; default: break; } break; case 's': //後方移動 switch(flag){ case 0: glutIdleFunc(idle); ex+=0.2*sin(M_PI*the/180.0); ez-=0.2*cos(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 1: glutIdleFunc(idle); obj1[0]-=0.2*sin(M_PI*the/180.0); obj1[2]+=0.2*cos(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 2: glutIdleFunc(idle); obj2[0]-=0.2*sin(M_PI*the/180.0); obj2[2]+=0.2*cos(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 3: glutIdleFunc(idle); obj3[0]-=0.2*sin(M_PI*the/180.0); obj3[2]+=0.2*cos(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 4: glutIdleFunc(idle); obj4[0]-=0.2*sin(M_PI*the/180.0); obj4[2]+=0.2*cos(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 5: glutIdleFunc(idle); obj5[0]-=0.2*sin(M_PI*the/180.0); obj5[2]+=0.2*cos(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; case 6: glutIdleFunc(idle); obj6[0]-=0.2*sin(M_PI*the/180.0); obj6[2]+=0.2*cos(M_PI*the/180.0); max(); movedisplay(); glutIdleFunc(0); break; default: break; } break; case 'f': //下へ switch(flag){ case 0: glutIdleFunc(idle); ey += 0.5; movedisplay(); glutIdleFunc(0); break; case 1: glutIdleFunc(idle); obj1[1]-=0.1; if(obj1[1] < 0.0) obj1[1]=0.0; movedisplay(); glutIdleFunc(0); break; case 2: glutIdleFunc(idle); obj2[1]-=0.1; if(obj2[1] < 0.0) obj2[1]=0.0; movedisplay(); glutIdleFunc(0); break; case 3: glutIdleFunc(idle); obj3[1]-=0.1; if(obj3[1] < 0.0) obj3[1]=0.0; movedisplay(); glutIdleFunc(0); break; case 4: glutIdleFunc(idle); obj4[1]-=0.1; if(obj4[1] < 0.0) obj4[1]=0.0; movedisplay(); glutIdleFunc(0); break; case 5: glutIdleFunc(idle); obj5[1]-=0.1; if(obj5[1] < 0.0) obj5[1]=0.0; movedisplay(); glutIdleFunc(0); break; case 6: glutIdleFunc(idle); obj6[1]-=0.1; if(obj6[1] < 0.0) obj6[1]=0.0; movedisplay(); glutIdleFunc(0); break; default: break; } break; case 'r': //上へ switch(flag){ case 0: glutIdleFunc(idle); ey -= 0.5; movedisplay(); glutIdleFunc(0); break; case 1: glutIdleFunc(idle); obj1[1]+=0.1; movedisplay(); glutIdleFunc(0); break; case 2: glutIdleFunc(idle); obj2[1]+=0.1; movedisplay(); glutIdleFunc(0); break; case 3: glutIdleFunc(idle); obj3[1]+=0.1; movedisplay(); glutIdleFunc(0); break; case 4: glutIdleFunc(idle); obj4[1]+=0.1; movedisplay(); glutIdleFunc(0); break; case 5: glutIdleFunc(idle); obj5[1]+=0.1; movedisplay(); glutIdleFunc(0); break; case 6: glutIdleFunc(idle); obj6[1]+=0.1; movedisplay(); glutIdleFunc(0); break; default: break; } break; case '0': flag = 0; break; case '1': flag = 1; break; case '2': flag = 2; break; case '3': flag = 3; break; case '4': flag = 4; break; case '5': flag = 5; break; case '6': flag = 6; break; default: break; } } void init(void) { /* 初期設定 */ glClearColor(1.0, 1.0, 1.0, 0.0); // ウィンドウを塗りつぶす際の色を指定する、(1,1,1)で白色、0で透明 glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); } int main(int argc, char *argv[]) { glutInit(&argc, argv); // OpenGL環境の初期化 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE |GLUT_DEPTH); // ディスプレイの表示モードを設定(色の指定) glutCreateWindow(argv[0]); // 文字列argv[0]でウィンドウを開く glutDisplayFunc(display); // 開いたウィンドウ内に描画する関数へのポインタ glutReshapeFunc(resize); glutKeyboardFunc(keyboard); glutSpecialFunc(key); glutMouseFunc(mouse); init(); glutMainLoop(); // 無限ループで、プログラムはイベントの待ち受け状態になる return 0; }