#include #include #if defined(WIN32) //# pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"") # include "glut.h" #elif defined(__APPLE__) || defined(MACOSX) # include #else # include #endif /* ** 光源 */ static const GLfloat lightpos[] = { 0.0, 0.0, 1.0, 0.0 }; /* 位置    */ static const GLfloat lightcol[] = { 1.0, 1.0, 1.0, 1.0 }; /* 直接光強度 */ static const GLfloat lightamb[] = { 0.1, 0.1, 0.1, 1.0 }; /* 環境光強度 */ /* ** 初期化 */ static void init(void) { /* 初期設定 */ glClearColor(0.3, 0.3, 1.0, 0.0); glEnable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); /* 光源の初期設定 */ glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_DIFFUSE, lightcol); glLightfv(GL_LIGHT0, GL_SPECULAR, lightcol); glLightfv(GL_LIGHT0, GL_AMBIENT, lightamb); } /* ** シーンの描画 */ static void scene(void) { static const GLfloat color[] = { 1.0, 1.0, 1.0, 1.0 }; /* 材質 (色) */ /* 材質の設定 */ glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color); /* 1枚の4角形を描く */ glNormal3d(0.0, 0.0, 1.0); glBegin(GL_QUADS); glVertex3d(-1.0, -1.0, 0.0); glVertex3d( 1.0, -1.0, 0.0); glVertex3d( 1.0, 1.0, 0.0); glVertex3d(-1.0, 1.0, 0.0); glEnd(); } /**************************** ** GLUT のコールバック関数 ** ****************************/ #include "trackball.h" /* トラックボール処理用関数の宣言 */ static void display(void) { /* モデルビュー変換行列の初期化 */ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* 画面クリア */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* 光源の位置を設定 */ glLightfv(GL_LIGHT0, GL_POSITION, lightpos); /* 視点の移動(物体の方を奥に移動)*/ glTranslated(0.0, 0.0, -3.0); /* トラックボール処理による回転 */ glMultMatrixd(trackballRotation()); /* シーンの描画 */ scene(); /* ダブルバッファリング */ glutSwapBuffers(); } static void resize(int w, int h) { /* トラックボールする範囲 */ trackballRegion(w, h); /* ウィンドウ全体をビューポートにする */ glViewport(0, 0, w, h); /* 透視変換行列の指定 */ glMatrixMode(GL_PROJECTION); /* 透視変換行列の初期化 */ glLoadIdentity(); gluPerspective(60.0, (double)w / (double)h, 1.0, 100.0); } static void idle(void) { /* 画面の描き替え */ glutPostRedisplay(); } static void mouse(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: switch (state) { case GLUT_DOWN: /* トラックボール開始 */ trackballStart(x, y); break; case GLUT_UP: /* トラックボール停止 */ trackballStop(x, y); break; default: break; } break; default: break; } } static void motion(int x, int y) { /* トラックボール移動 */ trackballMotion(x, y); } static void keyboard(unsigned char key, int x, int y) { switch (key) { case 'q': case 'Q': case '\033': /* ESC か q か Q をタイプしたら終了 */ exit(0); default: break; } } /* ** メインプログラム */ int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); glutCreateWindow(argv[0]); glutDisplayFunc(display); glutReshapeFunc(resize); glutIdleFunc(idle); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(keyboard); init(); glutMainLoop(); return 0; }