#include #include #define texWidth 256 #define texHeight 256 unsigned char texImage[texHeight][texWidth][4]; GLdouble vertex[][3] = { { 0.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0 }, { 1.0, 1.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 }, { 1.0, 0.0, 1.0 }, { 1.0, 1.0, 1.0 }, { 0.0, 1.0, 1.0 } }; GLfloat vertex2[][3] = { { 0.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f, 1.0f } }; 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 } }; GLfloat light0pos[] = { 0.0, 3.0, 5.0, 1.0 }; GLfloat light1pos[] = { 5.0, 3.0, 0.0, 1.0 }; //GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 }; //GLfloat red[] = { 0.8, 0.2, 0.2, 1.0 }; void makeCheckImage(int numCheck) { int i, j, ii, jj, col; double ww = texWidth / (double)numCheck; double hh = texHeight / (double)numCheck; for(j = 0 ; j < texHeight ; j++) { for(i = 0 ; i < texWidth ; i++) { ii = (int)((double)i / ww); jj = (int)((double)j / hh); col = ((ii + jj) & 1) * 255; texImage[j][i][0] = (unsigned char)col; //red texImage[j][i][1] = (unsigned char)0; //green texImage[j][i][2] = (unsigned char)col; //blue } } } void displayTexPolygon(void) { glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3fv(vertex2[4]); glTexCoord2f(0.0f, 1.0f); glVertex3fv(vertex2[7]); glTexCoord2f(1.0f, 1.0f); glVertex3fv(vertex2[6]); glTexCoord2f(1.0f, 0.0f); glVertex3fv(vertex2[5]); glEnd(); glDisable(GL_TEXTURE_2D); } void setTexture() { glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); } void idle(void) { glutPostRedisplay(); } void display(void) { int i; int j; static int r = 0; /* 回転角 */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); /* 視点位置と視線方向 */ gluLookAt(3.0, 4.0, 5.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); /* 図形の回転 */ glPushMatrix(); { //glTranslatef(0.0, 0.0,-20.0); glRotated((double)r, 0.0, 1.0, 0.0); glColor3f(1.0, 1.0, 1.0); displayTexPolygon(); } /* 図形の色 (赤) */ // glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red); /* 図形の描画 */ glColor3d(0.0, 0.0, 0.0); glBegin(GL_QUADS); for (j = 0; j < 6; ++j) { glNormal3dv(normal[j]); for (i = 0; i < 4; ++i) { if(j != 2) glVertex3dv(vertex[face[j][i]]); } } glEnd(); glutSwapBuffers(); /* 一周回ったら回転角を 0 に戻す */ if (++r >= 360) r = 0; } 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: 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': case 'Q': case '\033': /* '\033' は ESC の ASCII コード */ exit(0); default: break; } } void init(void) { glClearColor(1.0, 1.0, 1.0, 1.0); /* デプスバッファ */ 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, green); // glLightfv(GL_LIGHT1, GL_SPECULAR, green); makeCheckImage(6); setTexture(); } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow(argv[0]); glutDisplayFunc(display); glutReshapeFunc(resize); glutMouseFunc(mouse); glutKeyboardFunc(keyboard); init(); glutMainLoop(); return 0; }