| 
 | Generated by diff2html.pl © Yves Bailly, MandrakeSoft S.A. 2001, Ryohei Morita 2007 diff2html.pl is licensed under the GNU GPL. | 
| ../07-01/07-01-Planning.c | 07-02-Projection.c | |||
|---|---|---|---|---|
| 224 lines 8716 bytes Last modified : Mon Nov 5 08:00:38 2012 | 30 lines 1162 bytes Last modified : Mon Nov 5 07:58:02 2012 | |||
| 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 | // 2012/11/05a kameda[at]iit.tsukuba.ac.jp | 6 | // 2012/11/05a kameda[at]iit.tsukuba.ac.jp | |
| 7 | // 07.01. プログラムの構造設計 | 7 | // 07.02. ファイル分割 | |
| 8 | ||||
| 9 | // *********************************************************************** | |||
| 10 | // CommonHeaders.h : common header files ********************************* | |||
| 11 | ||||
| 12 | // +++-------------------------------------------------- | |||
| 13 | // どの部分ソースでも利用する可能性が高いライブラリ(関数)に | |||
| 14 | // 相当するヘッダファイル | |||
| 15 | // +++-------------------------------------------------- | |||
| 16 | ||||
| 17 | #include <stdio.h> // printf() | |||
| 18 | #include <GL/glut.h> // gl*(), glut*() | |||
| 19 | ||||
| 20 | // *********************************************************************** | |||
| 21 | // EmbededObjects ******************************************************** | |||
| 22 | ||||
| 23 | // +++-------------------------------------------------- | |||
| 24 | // ソース内部でのCG物体の直接描画 | |||
| 25 | // +++-------------------------------------------------- | |||
| 26 | ||||
| 27 | // +---------------------------------------------------- | |||
| 28 | // OpenGL Logoを描く | |||
| 29 | // +---------------------------------------------------- | |||
| 30 | // s はスケーリングファクタ(s=1.0 で 概ね -1.0 〜 1.0 の大きさ) | |||
| 31 | void ic2_OpenGLLogo (float s) { | |||
| 32 | // 光源によるシェーディング効果を切る(と色の指定が直接色になる) | |||
| 33 | glDisable(GL_LIGHTING); | |||
| 34 | ||||
| 35 | // 線の幅(画素単位) | |||
| 36 | glLineWidth(1.0); | |||
| 37 | ||||
| 38 | // (R, G, B) 値域は 0.0 - 1.0 | |||
| 39 | glColor3f(1.0, 1.0, 1.0); | |||
| 40 | ||||
| 41 | // glBegin(GL_LINES); から glEnd(); までの間は、 | |||
| 42 | // 「始点・終点」ごとに線分が描画される | |||
| 43 | glBegin(GL_LINES); | |||
| 44 | glVertex3f(s * -0.8, s * 0.8, 0.0); glVertex3f(s * -0.8, s * 0.2, 0.0); // O | |||
| 45 | glVertex3f(s * -0.8, s * 0.2, 0.0); glVertex3f(s * -0.4, s * 0.2, 0.0); // O | |||
| 46 | glVertex3f(s * -0.4, s * 0.2, 0.0); glVertex3f(s * -0.4, s * 0.8, 0.0); // O | |||
| 47 | glVertex3f(s * -0.4, s * 0.8, 0.0); glVertex3f(s * -0.8, s * 0.8, 0.0); // O | |||
| 48 | ||||
| 49 | glVertex3f(s * -0.2, s * 0.6, 0.0); glVertex3f(s * -0.2, s * 0.0, 0.0); // p | |||
| 50 | glVertex3f(s * -0.2, s * 0.6, 0.0); glVertex3f(s * 0.0, s * 0.6, 0.0); // p | |||
| 51 | glVertex3f(s * 0.0, s * 0.6, 0.0); glVertex3f(s * 0.0, s * 0.2, 0.0); // p | |||
| 52 | glVertex3f(s * 0.0, s * 0.2, 0.0); glVertex3f(s * -0.2, s * 0.2, 0.0); // p | |||
| 53 | ||||
| 54 | glVertex3f(s * 0.2, s * 0.4, 0.0); glVertex3f(s * 0.4, s * 0.4, 0.0); // e | |||
| 55 | glVertex3f(s * 0.4, s * 0.4, 0.0); glVertex3f(s * 0.4, s * 0.6, 0.0); // e | |||
| 56 | glVertex3f(s * 0.4, s * 0.6, 0.0); glVertex3f(s * 0.2, s * 0.6, 0.0); // e | |||
| 57 | glVertex3f(s * 0.2, s * 0.6, 0.0); glVertex3f(s * 0.2, s * 0.2, 0.0); // e | |||
| 58 | glVertex3f(s * 0.2, s * 0.2, 0.0); glVertex3f(s * 0.4, s * 0.2, 0.0); // e | |||
| 59 | ||||
| 60 | glVertex3f(s * 0.6, s * 0.6, 0.0); glVertex3f(s * 0.6, s * 0.2, 0.0); // n | |||
| 61 | glVertex3f(s * 0.6, s * 0.6, 0.0); glVertex3f(s * 0.8, s * 0.6, 0.0); // n | |||
| 62 | glVertex3f(s * 0.8, s * 0.6, 0.0); glVertex3f(s * 0.8, s * 0.2, 0.0); // n | |||
| 63 | ||||
| 64 | glVertex3f(s * 0.0, s * -0.2, 0.0); glVertex3f(s * -0.6, s * - 0.2, 0.0); // G | |||
| 65 | glVertex3f(s * -0.6, s * -0.2, 0.0); glVertex3f(s * -0.6, s * - 0.8, 0.0); // G | |||
| 66 | glVertex3f(s * -0.6, s * -0.8, 0.0); glVertex3f(s * 0.0, s * - 0.8, 0.0); // G | |||
| 67 | glVertex3f(s * 0.0, s * -0.8, 0.0); glVertex3f(s * 0.0, s * - 0.5, 0.0); // G | |||
| 68 | glVertex3f(s * 0.0, s * -0.5, 0.0); glVertex3f(s * -0.3, s * - 0.5, 0.0); // G | |||
| 69 | ||||
| 70 | glVertex3f(s * 0.2, s * -0.2, 0.0); glVertex3f(s * 0.2, s * - 0.8, 0.0); // L | |||
| 71 | glVertex3f(s * 0.2, s * -0.8, 0.0); glVertex3f(s * 0.8, s * - 0.8, 0.0); // L | |||
| 72 | glEnd(); | |||
| 73 | ||||
| 74 | // glBegin(GL_LINE_LOOP); から glEnd(); までの間は、 | |||
| 75 | // 全ての頂点を使った閉じた線分が描画される | |||
| 76 | ||||
| 77 | // 左上隅 | |||
| 78 | glColor3f(1.0, 1.0, 1.0); | |||
| 79 | glBegin(GL_LINE_LOOP); | |||
| 80 | glVertex3f(s * -1.0, s * 1.0, 0.0); | |||
| 81 | glVertex3f(s * -0.8, s * 1.0, 0.0); | |||
| 82 | glVertex3f(s * -1.0, s * 0.8, 0.0); | |||
| 83 | glEnd(); | |||
| 84 | ||||
| 85 | // 右上隅 | |||
| 86 | glColor3f(0.1, 1.0, 1.0); | |||
| 87 | glBegin(GL_LINE_LOOP); | |||
| 88 | glVertex3f(s * 0.8, s * 1.0, 0.0); | |||
| 89 | glVertex3f(s * 1.0, s * 1.0, 0.0); | |||
| 90 | glVertex3f(s * 1.0, s * 0.8, 0.0); | |||
| 91 | glEnd(); | |||
| 92 | ||||
| 93 | // 左下隅 | |||
| 94 | glColor3f(1.0, 0.1, 1.0); | |||
| 95 | glBegin(GL_LINE_LOOP); | |||
| 96 | glVertex3f(s * -1.0, s * -0.8, 0.0); | |||
| 97 | glVertex3f(s * -0.8, s * -1.0, 0.0); | |||
| 98 | glVertex3f(s * -1.0, s * -1.0, 0.0); | |||
| 99 | glEnd(); | |||
| 100 | ||||
| 101 | // 右下隅 | |||
| 102 | glColor3f(1.0, 1.0, 0.1); | |||
| 103 | glBegin(GL_LINE_LOOP); | |||
| 104 | glVertex3f(s * 0.8, s * -1.0, 0.0); | |||
| 105 | glVertex3f(s * 1.0, s * -0.8, 0.0); | |||
| 106 | glVertex3f(s * 1.0, s * -1.0, 0.0); | |||
| 107 | glEnd(); | |||
| 108 | } | |||
| 109 | 8 | |||
| 110 | // *********************************************************************** | 9 | // *********************************************************************** | |
| 111 | // Projection ************************************************************ | 10 | // Projection ************************************************************ | |
| 112 | 11 | |||
| 113 | // +++-------------------------------------------------- | 12 | // +++-------------------------------------------------- | |
| 114 | // カメラの投影行列を設定 | 13 | // カメラの投影行列を設定 | |
| 115 | // +++-------------------------------------------------- | 14 | // +++-------------------------------------------------- | |
| 116 | 15 | |||
| 117 | // +---------------------------------------------------- | 16 | // +---------------------------------------------------- | |
| 118 | // 直交投影 | 17 | // 直交投影 | |
| 119 | // +---------------------------------------------------- | 18 | // +---------------------------------------------------- | |
| 120 | void ic2_SetUpCamera_Ortho (void) { | 19 | void ic2_SetUpCamera_Ortho (void) { | |
| 121 | // OpenGLのPROJECTION行列スタックにアクセス | 20 | // OpenGLのPROJECTION行列スタックにアクセス | |
| 122 | glMatrixMode(GL_PROJECTION); | 21 | glMatrixMode(GL_PROJECTION); | |
| 123 | 22 | |||
| 124 | // PROJECTION行列スタックトップを単位行列で初期化 | 23 | // PROJECTION行列スタックトップを単位行列で初期化 | |
| 125 | glLoadIdentity(); | 24 | glLoadIdentity(); | |
| 126 | 25 | |||
| 127 | // 直交投影行列を生成するサポート関数 glOrtho を呼び出す | 26 | // 直交投影行列を生成するサポート関数 glOrtho を呼び出す | |
| 128 | // glOrtho(左端, 右端, 下端, 上端, 近接側クリッピング面, 遠方側クリッピング面) | 27 | // glOrtho(左端, 右端, 下端, 上端, 近接側クリッピング面, 遠方側クリッピング面) | |
| 129 | glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); // | 28 | glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); // | |
| 130 | } | 29 | } | |
| 131 | 30 | |||
| 132 | // *********************************************************************** | |||
| 133 | // Rendering ************************************************************* | |||
| 134 | ||||
| 135 | // +++-------------------------------------------------- | |||
| 136 | // スクリーンに描画する | |||
| 137 | // +++-------------------------------------------------- | |||
| 138 | ||||
| 139 | // +---------------------------------------------------- | |||
| 140 | // 1フレーム分の描画 | |||
| 141 | // +---------------------------------------------------- | |||
| 142 | void ic2_DrawFrame (void) { | |||
| 143 | // (1) 描画バッファの初期化 | |||
| 144 | // 以前にglClearColor()で指定した色で塗り潰す | |||
| 145 | glClear(GL_COLOR_BUFFER_BIT); | |||
| 146 | ||||
| 147 | // (2) カメラの設定 | |||
| 148 | ic2_SetUpCamera_Ortho(); | |||
| 149 | ||||
| 150 | // (3) 光源の設置 | |||
| 151 | ||||
| 152 | // (4) 物体の描画 | |||
| 153 | ic2_OpenGLLogo(0.95); | |||
| 154 | ||||
| 155 | // (5) 描画バッファの切替 | |||
| 156 | glutSwapBuffers(); | |||
| 157 | } | |||
| 158 | ||||
| 159 | // *********************************************************************** | |||
| 160 | // Callback ************************************************************** | |||
| 161 | ||||
| 162 | // +++-------------------------------------------------- | |||
| 163 | // GLUTでのイベント駆動型プログラミング | |||
| 164 | // +++-------------------------------------------------- | |||
| 165 | ||||
| 166 | // +---------------------------------------------------- | |||
| 167 | // 「タイマー」で呼出し(繰り返すことで「一定間隔呼出し」化) | |||
| 168 | // +---------------------------------------------------- | |||
| 169 | void ic2_timerhandler(int keynumber){ | |||
| 170 | glutPostRedisplay(); // OpenGLのmainloopに戻ったら再描画を頼む | |||
| 171 | glutTimerFunc(250, ic2_timerhandler, 0); // 250[ms]後にまた呼び出す | |||
| 172 | } | |||
| 173 | ||||
| 174 | // *********************************************************************** | |||
| 175 | // Initialization ******************************************************** | |||
| 176 | ||||
| 177 | // +++-------------------------------------------------- | |||
| 178 | // 初期化(主にGLUT) | |||
| 179 | // +++-------------------------------------------------- | |||
| 180 | ||||
| 181 | // +---------------------------------------------------- | |||
| 182 | // OpenGLとしてのWindowの初期化 | |||
| 183 | // +---------------------------------------------------- | |||
| 184 | void ic2_BootWindow (char winname[]) { | |||
| 185 | ||||
| 186 | // DisplayModeの設定(それぞれを|で繋ぐ) | |||
| 187 | // GLUT_DOUBLE ... ダブルバッファ | |||
| 188 | // GLUT_RGB ... RGB表色モード | |||
| 189 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); | |||
| 190 | ||||
| 191 | // 準備(Initialization)が済んだらウィンドウを開く | |||
| 192 | glutCreateWindow(winname); | |||
| 193 | ||||
| 194 | // Callback関数を設定 (イベント処理) | |||
| 195 | glutDisplayFunc(ic2_DrawFrame); // フレームを(再)描画するために呼び出す関数 | |||
| 196 | glutTimerFunc(250, ic2_timerhandler, 0); // 250[ms]後に呼び出す関数 | |||
| 197 | ||||
| 198 | // ウィンドウ全体を書き直すときの色(R,G,B,0) ここでは黒 | |||
| 199 | glClearColor(0.0, 0.0, 0.0, 0.0); | |||
| 200 | } | |||
| 201 | ||||
| 202 | // *********************************************************************** | |||
| 203 | // MainFunction ********************************************************** | |||
| 204 | ||||
| 205 | // +---------------------------------------------------- | |||
| 206 | // メイン関数など | |||
| 207 | // +---------------------------------------------------- | |||
| 208 | ||||
| 209 | // +---------------------------------------------------- | |||
| 210 | // Main Function | |||
| 211 | // +---------------------------------------------------- | |||
| 212 | int main (int argc, char *argv[]) { | |||
| 213 | ||||
| 214 | // glutライブラリによる引数の解釈 | |||
| 215 | glutInit(&argc, argv); | |||
| 216 | ||||
| 217 | // OpenGL Window の初期化 | |||
| 218 | ic2_BootWindow(argv[0]); | |||
| 219 | ||||
| 220 | // 無限ループの開始 | |||
| 221 | glutMainLoop(); | |||
| 222 | ||||
| 223 | return 0; | |||
| 224 | } |