| 
 | Generated by diff2html.pl © Yves Bailly, MandrakeSoft S.A. 2001, Ryohei Morita 2007 diff2html.pl is licensed under the GNU GPL. | 
| ../06-04/06-04-SwapByTimer.c | 06-05-OrthogonalCamera.c | |||
|---|---|---|---|---|
| 84 lines 2803 bytes Last modified : Wed Dec 14 13:14:12 2011 | 101 lines 3406 bytes Last modified : Wed Dec 14 04:33:44 2011 | |||
| 1 | // Keisanki Joron 2 (Introduction to Computing II) | 1 | // Keisanki Joron 2 (Introduction to Computing II) | |
| 2 | // Dept. of Engineering Systems, University of Tsukuba | 2 | // Dept. of Engineering Systems, University of Tsukuba | |
| 3 | // [UTF-8 / Unix] | 3 | // [UTF-8 / Unix] | |
| 4 | // 計算機序論2・実習 (筑波大学工学システム学類) | 4 | // 計算機序論2・実習 (筑波大学工学システム学類) | |
| 5 | 5 | |||
| 6 | // 2011/12/02a kameda[at]iit.tsukuba.ac.jp | 6 | // 2011/12/14a kameda[at]iit.tsukuba.ac.jp | |
| 7 | // 06.04. はじめてのOpenGL - バッファを時間で切り替え - | 7 | // 06.05. はじめてのOpenGL - 直交投影カメラ - | |
| 8 | 8 | |||
| 9 | #include <stdio.h> // printf() | 9 | #include <stdio.h> // printf() | |
| 10 | #include <GL/glut.h> // gl*(), glut*() | 10 | #include <GL/glut.h> // gl*(), glut*() | |
| 11 | 11 | |||
| 12 | // +---------------------------------------------------- | 12 | // +---------------------------------------------------- | |
| 13 | // 直交投影 | |||
| 14 | // +---------------------------------------------------- | |||
| 15 | void ic2_SetUpCamera_Ortho (void) { | |||
| 16 | // OpenGLのPROJECTION行列スタックにアクセス | |||
| 17 | glMatrixMode(GL_PROJECTION); | |||
| 18 | ||||
| 19 | // PROJECTION行列スタックトップを単位行列で初期化 | |||
| 20 | glLoadIdentity(); | |||
| 21 | ||||
| 22 | // 直交投影行列を生成するサポート関数 glOrtho を呼び出す | |||
| 23 | // glOrtho(左端, 右端, 下端, 上端, 近接側クリッピング面, 遠方側クリッピング面) | |||
| 24 | glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); // | |||
| 25 | } | |||
| 26 | ||||
| 27 | // +---------------------------------------------------- | |||
| 13 | // 1フレーム分の描画 | 28 | // 1フレーム分の描画 | |
| 14 | // +---------------------------------------------------- | 29 | // +---------------------------------------------------- | |
| 15 | void ic2_DrawFrame (void) { | 30 | void ic2_DrawFrame (void) { | |
| 16 | static int loopmax = 4; // loopmax回ごとに1回色を変える | 31 | static int loopmax = 4; // loopmax回ごとに1回色を変える | |
| 17 | static int loopcounter = 0; // 何回目かを覚えておく変数 | 32 | static int loopcounter = 0; // 何回目かを覚えておく変数 | |
| 18 | 33 | |||
| 19 | // (0) 指定色の変更 | 34 | // (0) 指定色の変更 | |
| 20 | loopcounter++; | 35 | loopcounter++; | |
| 21 | if (loopcounter == loopmax) { | 36 | if (loopcounter == loopmax) { | |
| 22 | glClearColor(0.0, 1.0, 0.0, 0.0); // 4回に1回は緑 | 37 | glClearColor(0.0, 1.0, 0.0, 0.0); // 4回に1回は緑 | |
| 23 | loopcounter = 0; | 38 | loopcounter = 0; | |
| 24 | } else { | 39 | } else { | |
| 25 | glClearColor(0.0, 0.0, 0.0, 0.0); // 4回に3回は黒 | 40 | glClearColor(0.0, 0.0, 0.0, 0.0); // 4回に3回は黒 | |
| 26 | } | 41 | } | |
| 27 | 42 | |||
| 28 | // (1) 描画バッファの初期化 | 43 | // (1) 描画バッファの初期化 | |
| 29 | // 以前にglClearColor()で指定した色で塗り潰す | 44 | // 以前にglClearColor()で指定した色で塗り潰す | |
| 30 | glClear(GL_COLOR_BUFFER_BIT); | 45 | glClear(GL_COLOR_BUFFER_BIT); | |
| 31 | 46 | |||
| 32 | // (2) カメラの設定 | 47 | // (2) カメラの設定 | |
| 48 | ic2_SetUpCamera_Ortho(); | |||
| 49 | ||||
| 33 | // (3) 光源の設置 | 50 | // (3) 光源の設置 | |
| 34 | // (4) 物体の描画 | 51 | // (4) 物体の描画 | |
| 35 | 52 | |||
| 36 | // (5) 描画バッファの切替 | 53 | // (5) 描画バッファの切替 | |
| 37 | glutSwapBuffers(); | 54 | glutSwapBuffers(); | |
| 38 | } | 55 | } | |
| 39 | 56 | |||
| 40 | // +---------------------------------------------------- | 57 | // +---------------------------------------------------- | |
| 41 | // タイマーで呼出し(繰り返すことで「一定間隔呼出し」化) | 58 | // タイマーで呼出し(繰り返すことで「一定間隔呼出し」化) | |
| 42 | // +---------------------------------------------------- | 59 | // +---------------------------------------------------- | |
| 43 | void ic2_timerhandler(int keynumber){ | 60 | void ic2_timerhandler(int keynumber){ | |
| 44 | glutPostRedisplay(); // OpenGLのmainloopに戻ったら再描画を頼む | 61 | glutPostRedisplay(); // OpenGLのmainloopに戻ったら再描画を頼む | |
| 45 | glutTimerFunc(250, ic2_timerhandler, 0); // 250[ms]後にまた呼び出す | 62 | glutTimerFunc(250, ic2_timerhandler, 0); // 250[ms]後にまた呼び出す | |
| 46 | } | 63 | } | |
| 47 | 64 | |||
| 48 | // +---------------------------------------------------- | 65 | // +---------------------------------------------------- | |
| 49 | // OpenGLとしてのWindowの初期化 | 66 | // OpenGLとしてのWindowの初期化 | |
| 50 | // +---------------------------------------------------- | 67 | // +---------------------------------------------------- | |
| 51 | void ic2_BootWindow (char winname[]) { | 68 | void ic2_BootWindow (char winname[]) { | |
| 52 | 69 | |||
| 53 | // DisplayModeの設定(それぞれを|で繋ぐ) | 70 | // DisplayModeの設定(それぞれを|で繋ぐ) | |
| 54 | // GLUT_DOUBLE ... ダブルバッファ | 71 | // GLUT_DOUBLE ... ダブルバッファ | |
| 55 | // GLUT_RGB ... RGB表色モード | 72 | // GLUT_RGB ... RGB表色モード | |
| 56 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); | 73 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); | |
| 57 | 74 | |||
| 58 | // 準備(Initialization)が済んだらウィンドウを開く | 75 | // 準備(Initialization)が済んだらウィンドウを開く | |
| 59 | glutCreateWindow(winname); | 76 | glutCreateWindow(winname); | |
| 60 | 77 | |||
| 61 | // Callback関数を設定 (イベント処理) | 78 | // Callback関数を設定 (イベント処理) | |
| 62 | glutDisplayFunc(ic2_DrawFrame); // フレームを(再)描画するために呼び出す関数 | 79 | glutDisplayFunc(ic2_DrawFrame); // フレームを(再)描画するために呼び出す関数 | |
| 63 | glutTimerFunc(250, ic2_timerhandler, 0); // 250[ms]後に呼び出す関数 | 80 | glutTimerFunc(250, ic2_timerhandler, 0); // 250[ms]後に呼び出す関数 | |
| 64 | 81 | |||
| 65 | // ウィンドウ全体を書き直すときの色(R,G,B,0) ここでは黒 | 82 | // ウィンドウ全体を書き直すときの色(R,G,B,0) ここでは黒 | |
| 66 | glClearColor(0.0, 0.0, 0.0, 0.0); | 83 | glClearColor(0.0, 0.0, 0.0, 0.0); | |
| 67 | } | 84 | } | |
| 68 | 85 | |||
| 69 | // +---------------------------------------------------- | 86 | // +---------------------------------------------------- | |
| 70 | // Main Function | 87 | // Main Function | |
| 71 | // +---------------------------------------------------- | 88 | // +---------------------------------------------------- | |
| 72 | int main (int argc, char *argv[]) { | 89 | int main (int argc, char *argv[]) { | |
| 73 | 90 | |||
| 74 | // glutライブラリによる引数の解釈 | 91 | // glutライブラリによる引数の解釈 | |
| 75 | glutInit(&argc, argv); | 92 | glutInit(&argc, argv); | |
| 76 | 93 | |||
| 77 | // OpenGL Window の初期化 | 94 | // OpenGL Window の初期化 | |
| 78 | ic2_BootWindow(argv[0]); | 95 | ic2_BootWindow(argv[0]); | |
| 79 | 96 | |||
| 80 | // 無限ループの開始 | 97 | // 無限ループの開始 | |
| 81 | glutMainLoop(); | 98 | glutMainLoop(); | |
| 82 | 99 | |||
| 83 | return 0; | 100 | return 0; | |
| 84 | } | 101 | } |