#include #include #include #include #define TEX_HEIGHT 180 #define TEX_WIDTH 180 static GLubyte image[TEX_HEIGHT][TEX_WIDTH][3]; //各立方体の回転flag(true->回転On , false->回転Off) bool f1 = true; bool f2 = true; bool f3 = true; bool f4 = true; //視点の座標 float lookx = 0.0, looky = 0.0, lookz = 3.0; //各立方体の回転角度 int r1 = 0, r2 = 0, r3 = 0, r4 = 0; //座標軸 struct vector3f{ float x; float y; float z; }; int face[][4] = { { 0, 1, 2, 3 }, { 1, 5, 6, 2 }, { 5, 4, 7, 6 }, { 4, 0, 3, 7 }, { 4, 5, 1, 0 }, { 3, 2, 6, 7 } }; GLdouble normal[][3] = { { 0.0, 0.0,-1.0 }, { 1.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0 }, {-1.0, 0.0, 0.0 }, { 0.0,-1.0, 0.0 }, { 0.0, 1.0, 0.0 } }; GLdouble vertex[][3] = { { -0.5, -0.5, -0.5 }, //A { 0.5, -0.5, -0.5 }, //B { 0.5, 0.5, -0.5 }, //C { -0.5, 0.5, -0.5 }, //D { -0.5, -0.5, 0.5 }, //E { 0.5, -0.5, 0.5 }, //F { 0.5, 0.5, 0.5 }, //G { -0.5, 0.5, 0.5 } //H }; //光源の位置1と2 GLfloat light0pos[] = { 0.0, 3.0, 5.0, 1.0 }; GLfloat light1pos[] = { 5.0, 3.0, 0.0, 1.0 }; GLfloat white[] = {1.0, 1.0, 1.0, 1.0}; GLfloat black[] = {0.0, 0.0, 0.0, 1.0}; void initTexture(void){ FILE *fp; int x, z; //texture file open if((fp = fopen("Himawari.bmp", "rb")) == NULL){ fprintf(stderr, "texture file cannot open\n"); return; } fseek(fp, 54L, SEEK_SET); for(x = 0; x < TEX_HEIGHT; x++){ for(z = 0; z < TEX_WIDTH; z++){ image[x][z][2] = fgetc(fp);//R image[x][z][1] = fgetc(fp);//G image[x][z][0] = fgetc(fp);//B //image[x][z][3] = 0;//alpha } } fclose(fp); } void cube(GLfloat color[]) { int i; int j; //テクスチャマッピング開始 glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, white); glNormal3dv(normal[2]); glTexCoord2f(1.0f, 0.0f); glVertex3dv(vertex[face[2][0]]); glTexCoord2f(0.0f, 0.0f); glVertex3dv(vertex[face[2][1]]); glTexCoord2f(0.0f, 1.0f); glVertex3dv(vertex[face[2][2]]); glTexCoord2f(1.0f, 1.0f); glVertex3dv(vertex[face[2][3]]); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color); for (j = 0; j < 6; ++j) { if(j != 2){ glNormal3dv(normal[j]); for (i = 0; i < 4; ++i) { glVertex3dv(vertex[face[j][i]]); } } } glEnd(); glDisable(GL_TEXTURE_2D); } void creatcoordinates(float sx, float sy, float sz, float ex, float ey, float ez){ GLfloat red[] = { 0.8, 0.2, 0.2, 1.0 }; glBegin(GL_LINES); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, black); //x軸 glVertex3f(sx, 0.0, 0.0); glVertex3f(ex, 0.0, 0.0); //y軸 glVertex3f(0.0, sy, 0.0); glVertex3f(0.0, ey, 0.0); //z軸 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red); glVertex3f(0.0, 0.0, sz); glVertex3f(0.0, 0.0, ez); glEnd(); } void idle(void) { glutPostRedisplay(); } 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(); gluLookAt(lookx, looky, lookz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); } void mouse(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { /* アニメーション開始 */ glutIdleFunc(idle); } else { /* アニメーション停止 */ glutIdleFunc(0); } break; case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) { /* コマ送り (1ステップだけ進める) */ glutPostRedisplay(); } break; default: break; } } void keyboard(unsigned char key, int x, int y) { switch (key) { case 'q': //1個目の図形だけ回転 f1 = true; f2 = false; f3 = false; f4 = false; //glutIdleFunc(idle); break; case 'w': // 2個目の図形だけ回転 f1 = false; f2 = true; f3 = false; f4 = false; //glutIdleFunc(idle); break; case 'e': // 3個目の図形だけ回転 f1 = false; f2 = false; f3 = true; f4 = false; //glutIdleFunc(idle); break; case 'r': // 4個目の図形だけ回転 f1 = false; f2 = false; f3 = false; f4 = true; //glutIdleFunc(idle); break; case 's': f1 = true; f2 = true; f3 = true; f4 = true; //glutIdleFunc(idle); break; case 'l': lookx = 0.0; looky = 0.0; lookz = 3.0; glutReshapeFunc(resize); break; case '\033': /* '\033' は ESC の ASCII コード */ exit(0); default: glutIdleFunc(0); break; } } void display(void){ struct vector3f start = {-1.5, -2.0, -1.5}; struct vector3f end = {1.5, 2.0, 1.5}; GLfloat pink[] = { 0.8118, 0.2275, 0.7765, 1.0 }; GLfloat red[] = { 0.8, 0.2, 0.2, 1.0 }; GLfloat blue[] = { 0.2, 0.2, 0.8, 1.0 }; GLfloat orange[] = {0.9961, 0.7922, 0.1647, 1.0}; lookx++; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* 光源の位置設定 */ glLightfv(GL_LIGHT0, GL_POSITION, light0pos); glLightfv(GL_LIGHT1, GL_POSITION, light1pos); /* モデルビュー変換行列の保存 */ glPushMatrix(); /* 図形の回転 */ glRotated((double)r1, 0.0, 1.0, 0.0); if(f1){ r1++; } glScalef(0.5f, 0.5f, 0.5f); /* 図形の描画 */ cube(red); //座標軸表示 creatcoordinates(start.x, start.y, start.z, end.x, end.y, end.z); glPopMatrix(); /* 2つ目の図形の描画 */ glPushMatrix(); glTranslatef(1.5, 0.0, 0.0); glRotated((double)r2, 0.0, 1.0, 0.0); if(f2){ r2++; } glScalef(0.4f, 0.4f, 0.4f); cube(blue); //座標軸表示 creatcoordinates(start.x, start.y, start.z, end.x, end.y, end.z); glPopMatrix(); /* 3つ目の図形の描画 */ glPushMatrix(); glTranslatef(2.6, 0.0, 0.0); glRotated((double)r3, 0.0, 1.0, 0.0); if(f3){ r3++; } glScalef(0.25f, 0.25f, 0.25f); cube(pink); //座標軸表示 creatcoordinates(start.x, start.y, start.z, end.x, end.y, end.z); glPopMatrix(); /* 4つ目の図形の描画 */ glPushMatrix(); glTranslatef(3.4, 0.0, 0.0); glRotated((double)r4, 0.0, 1.0, 0.0); if(f4){ r4++; } glScalef(0.15f, 0.15f, 0.15f); cube(orange); //座標軸表示 creatcoordinates(start.x, start.y, start.z, end.x, end.y, end.z); glPopMatrix(); glutSwapBuffers(); /* 一周回ったら回転角を 0 に戻す */ if (r1 >= 360) r1 = 0; if (r2 >= 360) r2 = 0; if (r3 >= 360) r3 = 0; if (r4 >= 360) r4 = 0; //printf("r1:%d, r2:%d, r3:%d, r4:%d\n",r1, r2, r3, r4); } void init(void){ glClearColor(1.0, 1.0, 1.0, 1.0); initTexture(); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEX_WIDTH, TEX_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, image); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glLightfv(GL_LIGHT1, GL_DIFFUSE, black); glLightfv(GL_LIGHT1, GL_SPECULAR, black); } int main(int argc, char *argv[]) { glutInitWindowPosition(100,100); glutInitWindowSize(720, 480); glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("Creat!"); glutDisplayFunc(display); glutReshapeFunc(resize); glutMouseFunc(mouse); glutKeyboardFunc(keyboard); init(); glutMainLoop(); return 0; }