| ../109-4/109-4-ReadModel.c | 110-1-MergedSource.c | |||
|---|---|---|---|---|
| 191 lines 6221 bytes Last modified : Mon Nov 7 22:21:22 2011 | 482 lines 17874 bytes Last modified : Sat Nov 12 18:55:55 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 | 4 | |||
| 5 | // 2011/11/07 kameda[at]iit.tsukuba.ac.jp | 5 | // 2011/11/07 kameda[at]iit.tsukuba.ac.jp | |
| 6 | // 9.4 モデルの読み込み | 6 | // 10.1 モデル読み込みとCG表示の統合 | |
| 7 | 7 | |||
| 8 | #include <stdio.h> | 8 | #include <stdio.h> | |
| 9 | #include <stdlib.h> // calloc() | 9 | #include <stdlib.h> // exit(), calloc() | |
| 10 | #include <GL/glut.h> | |||
| 11 | ||||
| 12 | // *********************************************************************** | |||
| 13 | // global variables ****************************************************** | |||
| 14 | ||||
| 15 | // +---------------------------------------------------- | |||
| 16 | // Global Variables | |||
| 17 | // +---------------------------------------------------- | |||
| 18 | ||||
| 19 | // Windowサイズ | |||
| 20 | int window_w = 400; | |||
| 21 | int window_h = 400; | |||
| 22 | ||||
| 23 | // 直交投影時のスケールファクタ [pixel / unit_of_imager] | |||
| 24 | // ここでは正規化カメラの撮像面での 1.0 単位を 200画素に相当させる | |||
| 25 | float ortho_unit = 200.0; | |||
| 26 | ||||
| 27 | // WindowのID(描画管理用) | |||
| 28 | int window_id = -1; | |||
| 29 | ||||
| 30 | // *********************************************************************** | |||
| 31 | // read model ************************************************************ | |||
| 10 | 32 | |||
| 11 | // +---------------------------------------------------- | 33 | // +---------------------------------------------------- | |
| 12 | // 1つの点のための構造体 | 34 | // 1つの点のための構造体 | |
| 13 | // +---------------------------------------------------- | 35 | // +---------------------------------------------------- | |
| 14 | struct ic2POINT { | 36 | struct ic2POINT { | |
| 15 | float x; | 37 | float x; | |
| 16 | float y; | 38 | float y; | |
| 17 | float z; | 39 | float z; | |
| 18 | }; | 40 | }; | |
| 19 | 41 | |||
| 20 | // +---------------------------------------------------- | 42 | // +---------------------------------------------------- | |
| 21 | // 1つの色のための構造体 | 43 | // 1つの色のための構造体 | |
| 22 | // +---------------------------------------------------- | 44 | // +---------------------------------------------------- | |
| 23 | struct ic2COLOR { | 45 | struct ic2COLOR { | |
| 24 | float r; | 46 | float r; | |
| 25 | float g; | 47 | float g; | |
| 26 | float b; | 48 | float b; | |
| 27 | }; | 49 | }; | |
| 28 | 50 | |||
| 29 | // +---------------------------------------------------- | 51 | // +---------------------------------------------------- | |
| 30 | // 1つの三角形パッチのための構造体 | 52 | // 1つの三角形パッチのための構造体 | |
| 31 | // 次の三角形パッチへのポインタを持つ。 | 53 | // 次の三角形パッチへのポインタを持つ。 | |
| 32 | // v(st) × v(su) [外積]がこの面の法線ベクトルを成す(右ネジ式) | 54 | // v(st) × v(su) [外積]がこの面の法線ベクトルを成す(右ネジ式) | |
| 33 | // +---------------------------------------------------- | 55 | // +---------------------------------------------------- | |
| 34 | struct ic2PATCH { | 56 | struct ic2PATCH { | |
| 35 | struct ic2POINT s; // 頂点s | 57 | struct ic2POINT s; // 頂点s | |
| 36 | struct ic2POINT t; // 頂点t | 58 | struct ic2POINT t; // 頂点t | |
| 37 | struct ic2POINT u; // 頂点u | 59 | struct ic2POINT u; // 頂点u | |
| 38 | struct ic2COLOR c; // 色の強度 (通常は0.0 - 1.0) | 60 | struct ic2COLOR c; // 色の強度 (通常は0.0 - 1.0) | |
| 39 | struct ic2PATCH *next; // 次の三角形パッチへのポインタ | 61 | struct ic2PATCH *next; // 次の三角形パッチへのポインタ | |
| 40 | }; | 62 | }; | |
| 41 | 63 | |||
| 42 | // +++-------------------------------------------------- | 64 | // +++-------------------------------------------------- | |
| 43 | // 新しい三角形パッチ構造体(ic2PATCH)のメモリ確保と初期化 | 65 | // 新しい三角形パッチ構造体(ic2PATCH)のメモリ確保と初期化 | |
| 44 | // +++-------------------------------------------------- | 66 | // +++-------------------------------------------------- | |
| 45 | static struct ic2PATCH *ic2_NewPATCH (void) { | 67 | static struct ic2PATCH *ic2_NewPATCH (void) { | |
| 46 | struct ic2PATCH *newpatch = NULL; | 68 | struct ic2PATCH *newpatch = NULL; | |
| 47 | 69 | |||
| 48 | newpatch = (struct ic2PATCH *)calloc(1, sizeof(struct ic2PATCH)); | 70 | newpatch = (struct ic2PATCH *)calloc(1, sizeof(struct ic2PATCH)); | |
| 49 | return (newpatch); | 71 | return (newpatch); | |
| 50 | } | 72 | } | |
| 51 | 73 | |||
| 52 | // +++-------------------------------------------------- | 74 | // +++-------------------------------------------------- | |
| 53 | // 三角形パッチ構造体(ic2PATCH)1つ分の読み込み | 75 | // 三角形パッチ構造体(ic2PATCH)1つ分の読み込み | |
| 54 | // +++-------------------------------------------------- | 76 | // +++-------------------------------------------------- | |
| 55 | static int ic2_InsertPATCH (struct ic2PATCH **firstpatchptr, char *onelinedata) { | 77 | static int ic2_InsertPATCH (struct ic2PATCH **firstpatchptr, char *onelinedata) { | |
| 56 | // (1) 文字列へのポインタは存在するか | 78 | // (1) 文字列へのポインタは存在するか | |
| 57 | // (2) メモリ確保/下請け | 79 | // (2) メモリ確保/下請け | |
| 58 | // (3) 値の読み込み | 80 | // (3) 値の読み込み | |
| 59 | // (4) ic2PATCHリスト構造への組み込み | 81 | // (4) ic2PATCHリスト構造への組み込み | |
| 60 | struct ic2PATCH *newpatch = NULL; // 三角形パッチ構造体ヘのポインタ | 82 | struct ic2PATCH *newpatch = NULL; // 三角形パッチ構造体ヘのポインタ | |
| 61 | int number_of_element = 0; // 読み込めた項目数 | 83 | int number_of_element = 0; // 読み込めた項目数 | |
| 62 | 84 | |||
| 63 | // (1) 文字列へのポインタは存在するか | 85 | // (1) 文字列へのポインタは存在するか | |
| 64 | if (onelinedata == NULL) return 1; | 86 | if (onelinedata == NULL) return 1; | |
| 65 | 87 | |||
| 66 | // (2) メモリ確保/下請け | 88 | // (2) メモリ確保/下請け | |
| 67 | if ((newpatch = ic2_NewPATCH()) == NULL) { | 89 | if ((newpatch = ic2_NewPATCH()) == NULL) { | |
| 68 | printf("ic2_InsertPATCH: Memory allocation failed.\n"); | 90 | printf("ic2_InsertPATCH: Memory allocation failed.\n"); | |
| 69 | return 2; | 91 | return 2; | |
| 70 | } | 92 | } | |
| 71 | 93 | |||
| 72 | // (3) 値の読み込み | 94 | // (3) 値の読み込み | |
| 73 | number_of_element = | 95 | number_of_element = | |
| 74 | sscanf(onelinedata, "%f %f %f %f %f %f %f %f %f %f %f %f", | 96 | sscanf(onelinedata, "%f %f %f %f %f %f %f %f %f %f %f %f", | |
| 75 | &newpatch->s.x, &newpatch->s.y, &newpatch->s.z, | 97 | &newpatch->s.x, &newpatch->s.y, &newpatch->s.z, | |
| 76 | &newpatch->t.x, &newpatch->t.y, &newpatch->t.z, | 98 | &newpatch->t.x, &newpatch->t.y, &newpatch->t.z, | |
| 77 | &newpatch->u.x, &newpatch->u.y, &newpatch->u.z, | 99 | &newpatch->u.x, &newpatch->u.y, &newpatch->u.z, | |
| 78 | &newpatch->c.r, &newpatch->c.g, &newpatch->c.b); | 100 | &newpatch->c.r, &newpatch->c.g, &newpatch->c.b); | |
| 79 | if (number_of_element != 12) { | 101 | if (number_of_element != 12) { | |
| 80 | printf("ic2_InsertPATCH: format error (%d elements found)\n", number_of_element); | 102 | printf("ic2_InsertPATCH: format error (%d elements found)\n", number_of_element); | |
| 81 | printf("ic2_InsertPATCH: \"%s\"\n", onelinedata); | 103 | printf("ic2_InsertPATCH: \"%s\"\n", onelinedata); | |
| 82 | free(newpatch); | 104 | free(newpatch); | |
| 83 | return 3; | 105 | return 3; | |
| 84 | } | 106 | } | |
| 85 | 107 | |||
| 86 | // (4) ic2PATCHリスト構造への組み込み | 108 | // (4) ic2PATCHリスト構造への組み込み | |
| 87 | // *newpatch を 三角形パッチ集合の先頭に挿入 | 109 | // *newpatch を 三角形パッチ集合の先頭に挿入 | |
| 88 | newpatch->next = *firstpatchptr; | 110 | newpatch->next = *firstpatchptr; | |
| 89 | *firstpatchptr = newpatch; | 111 | *firstpatchptr = newpatch; | |
| 90 | return 0; | 112 | return 0; | |
| 91 | } | 113 | } | |
| 92 | 114 | |||
| 93 | // +---------------------------------------------------- | 115 | // +---------------------------------------------------- | |
| 94 | // モデルのファイルからの読込 | 116 | // モデルのファイルからの読込 | |
| 95 | // +---------------------------------------------------- | 117 | // +---------------------------------------------------- | |
| 96 | // 返値:負 ... エラー | 118 | // 返値:負 ... エラー | |
| 97 | // 返値:0ないし正値 ... 読み込みに成功したパッチ数 | 119 | // 返値:0ないし正値 ... 読み込みに成功したパッチ数 | |
| 98 | int ic2_ReadModel(char *filename, struct ic2PATCH **firstpatchptr) { | 120 | int ic2_ReadModel(char *filename, struct ic2PATCH **firstpatchptr) { | |
| 99 | FILE *filetoopen = NULL; // A pointer to FILE structure | 121 | FILE *filetoopen = NULL; // A pointer to FILE structure | |
| 100 | char oneline[256]; // 1行分のバッファ,固定長にしておくとsizeof()が利用可能 | 122 | char oneline[256]; // 1行分のバッファ,固定長にしておくとsizeof()が利用可能 | |
| 101 | char firstword[256]; // コメント行かどうかの判定用 | 123 | char firstword[256]; // コメント行かどうかの判定用 | |
| 102 | int linenumber = 0; // ファイル中の行番号 | 124 | int linenumber = 0; // ファイル中の行番号 | |
| 103 | int patchnumber = 0; // パッチ数 | 125 | int patchnumber = 0; // パッチ数 | |
| 104 | 126 | |||
| 105 | // We need at least one option to indicate a file to open | 127 | // We need at least one option to indicate a file to open | |
| 106 | if (filename == NULL) { | 128 | if (filename == NULL) { | |
| 107 | printf("Error: You need to specify a one file to open.\n"); | 129 | printf("Error: You need to specify a one file to open.\n"); | |
| 108 | return -1; | 130 | return -1; | |
| 109 | } | 131 | } | |
| 110 | 132 | |||
| 111 | // Try to open it | 133 | // Try to open it | |
| 112 | filetoopen = fopen(filename, "r"); | 134 | filetoopen = fopen(filename, "r"); | |
| 113 | if (filetoopen == NULL) { | 135 | if (filetoopen == NULL) { | |
| 114 | printf("Error: Failed to open/read \"%s\".\n", filename); | 136 | printf("Error: Failed to open/read \"%s\".\n", filename); | |
| 115 | return -2; | 137 | return -2; | |
| 116 | } | 138 | } | |
| 117 | printf("Reading model from \"%s\"\n", filename); | 139 | printf("Reading model from \"%s\"\n", filename); | |
| 118 | 140 | |||
| 119 | // 1行ずつ読込 | 141 | // 1行ずつ読込 | |
| 120 | while (fgets(oneline, sizeof(oneline), filetoopen) != NULL) { | 142 | while (fgets(oneline, sizeof(oneline), filetoopen) != NULL) { | |
| 121 | linenumber++; | 143 | linenumber++; | |
| 122 | 144 | |||
| 123 | // もし行内に1文字もなければ(1単語もなければ)次行へ | 145 | // もし行内に1文字もなければ(1単語もなければ)次行へ | |
| 124 | if (sscanf(oneline, "%256s", firstword) < 1) | 146 | if (sscanf(oneline, "%256s", firstword) < 1) | |
| 125 | continue; | 147 | continue; | |
| 126 | // もし先頭が#で始まっていれば次行へ | 148 | // もし先頭が#で始まっていれば次行へ | |
| 127 | if (firstword[0] == '#') | 149 | if (firstword[0] == '#') | |
| 128 | continue; | 150 | continue; | |
| 129 | // 他のエラートラップ | 151 | // 他のエラートラップ | |
| 130 | if (0) { | 152 | if (0) { | |
| 131 | printf("Skip(line=%d): %s\n", linenumber, oneline); | 153 | printf("Skip(line=%d): %s\n", linenumber, oneline); | |
| 132 | continue; | 154 | continue; | |
| 133 | } | 155 | } | |
| 134 | 156 | |||
| 135 | if (ic2_InsertPATCH(firstpatchptr, oneline)) { | 157 | if (ic2_InsertPATCH(firstpatchptr, oneline)) { | |
| 136 | printf("Model reading is interrupted.\n"); | 158 | printf("Model reading is interrupted.\n"); | |
| 137 | break; | 159 | break; | |
| 138 | } | 160 | } | |
| 139 | patchnumber++; | 161 | patchnumber++; | |
| 140 | } | 162 | } | |
| 141 | 163 | |||
| 142 | // And close it | 164 | // And close it | |
| 143 | if (fclose(filetoopen) != 0) { | 165 | if (fclose(filetoopen) != 0) { | |
| 144 | printf("Error: Failed to close \"%s\".\n", filename); | 166 | printf("Error: Failed to close \"%s\".\n", filename); | |
| 145 | // error, but we get data anyway... | 167 | // error, but we get data anyway... | |
| 146 | } | 168 | } | |
| 147 | 169 | |||
| 148 | printf("Finish reading the model (%d patches).\n", patchnumber); | 170 | printf("Finish reading the model (%d patches).\n", patchnumber); | |
| 149 | return patchnumber; | 171 | return patchnumber; | |
| 150 | } | 172 | } | |
| 151 | 173 | |||
| 152 | // ----------------------------------------------------- | 174 | // ----------------------------------------------------- | |
| 153 | // 三角形パッチ構造体(ic2PATCH)リストの表示 | 175 | // 三角形パッチ構造体(ic2PATCH)リストの表示 | |
| 154 | // ----------------------------------------------------- | 176 | // ----------------------------------------------------- | |
| 155 | int ic2_PrintPATCHList (struct ic2PATCH *firstpatchptr) { | 177 | int ic2_PrintPATCHList (struct ic2PATCH *firstpatchptr) { | |
| 156 | struct ic2PATCH *p; | 178 | struct ic2PATCH *p; | |
| 157 | int np = 0; | 179 | int np = 0; | |
| 158 | 180 | |||
| 159 | for (p = firstpatchptr; p != NULL; p = p->next) { | 181 | for (p = firstpatchptr; p != NULL; p = p->next) { | |
| 160 | np++; | 182 | np++; | |
| 161 | printf("PATCH: (%g, %g, %g), (%g, %g, %g), (%g, %g, %g), rgb=[%g, %g, %g]\n", | 183 | printf("PATCH: (%g, %g, %g), (%g, %g, %g), (%g, %g, %g), rgb=[%g, %g, %g]\n", | |
| 162 | p->s.x, p->s.y, p->s.z, | 184 | p->s.x, p->s.y, p->s.z, | |
| 163 | p->t.x, p->t.y, p->t.z, | 185 | p->t.x, p->t.y, p->t.z, | |
| 164 | p->u.x, p->u.y, p->u.z, | 186 | p->u.x, p->u.y, p->u.z, | |
| 165 | p->c.r, p->c.g, p->c.b); | 187 | p->c.r, p->c.g, p->c.b); | |
| 166 | } | 188 | } | |
| 167 | 189 | |||
| 168 | return np; | 190 | return np; | |
| 169 | } | 191 | } | |
| 170 | 192 | |||
| 171 | // -------------------------------------------------------------- | 193 | // *********************************************************************** | |
| 194 | // gl utilitiess ********************************************************* | |||
| 195 | ||||
| 196 | // +---------------------------------------------------- | |||
| 197 | // MODELVIEW Matrix と PROJECTION を表示する | |||
| 198 | // +---------------------------------------------------- | |||
| 199 | void ic2_ShowMATRIX (char *str) { | |||
| 200 | GLfloat m[16]; // GL_MODELVIEW matrix | |||
| 201 | GLfloat p[16]; // GL_PROJECTION matrix | |||
| 202 | ||||
| 203 | glGetFloatv(GL_MODELVIEW_MATRIX , m); // MODELVIEWのスタックトップmatrixをmにコピー | |||
| 204 | glGetFloatv(GL_PROJECTION_MATRIX, p); // PROJECTIONのスタックトップmatrixをpにコピー | |||
| 205 | if (str != NULL) printf("<< %s >>\n", str); | |||
| 206 | printf("MODELVIEW Matrix PROJECTION Matrix\n"); | |||
| 207 | printf("%7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f\n", | |||
| 208 | m[ 0], m[ 4], m[ 8], m[12], p[ 0], p[ 4], p[ 8], p[12]); | |||
| 209 | printf("%7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f\n", | |||
| 210 | m[ 1], m[ 5], m[ 9], m[13], p[ 1], p[ 5], p[ 9], p[13]); | |||
| 211 | printf("%7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f\n", | |||
| 212 | m[ 2], m[ 6], m[10], m[14], p[ 2], p[ 6], p[10], p[14]); | |||
| 213 | printf("%7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f\n", | |||
| 214 | m[ 3], m[ 7], m[11], m[15], p[ 3], p[ 7], p[11], p[15]); | |||
| 215 | } | |||
| 216 | ||||
| 217 | // *********************************************************************** | |||
| 218 | // objects_embeded ******************************************************* | |||
| 219 | ||||
| 220 | // +---------------------------------------------------- | |||
| 221 | // 正方形を描く | |||
| 222 | // +---------------------------------------------------- | |||
| 223 | void ic2_FigSquare (float s) { | |||
| 224 | glDisable(GL_LIGHTING); // 光源によるシェーディングを一旦切る | |||
| 225 | ||||
| 226 | // 正方形(Z=0の平面内、+/- 0.9) | |||
| 227 | glBegin(GL_LINE_LOOP); { | |||
| 228 | glColor3f(1.0, 1.0, 1.0); | |||
| 229 | glVertex3f(s * -1, s * -1, 0.0); | |||
| 230 | glVertex3f(s * +1, s * -1, 0.0); | |||
| 231 | glVertex3f(s * +1, s * +1, 0.0); | |||
| 232 | glVertex3f(s * -1, s * +1, 0.0); | |||
| 233 | } glEnd(); | |||
| 234 | ||||
| 235 | // 3軸 | |||
| 236 | glBegin(GL_LINES); { | |||
| 237 | glColor3f(1.0, 0.5, 0.5); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.85, 0.0, 0.0); // X (red) | |||
| 238 | glColor3f(0.5, 1.0, 0.5); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.70, 0.0); // Y (green) | |||
| 239 | glColor3f(0.5, 0.5, 1.0); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 1.0); // Z (blue) | |||
| 240 | } glEnd(); | |||
| 241 | ||||
| 242 | glEnable(GL_LIGHTING); // 光源によるシェーディングを開始する | |||
| 243 | } | |||
| 244 | ||||
| 245 | // +---------------------------------------------------- | |||
| 246 | // ティーポットを描く (glutの作り付け関数の1つ) | |||
| 247 | // +---------------------------------------------------- | |||
| 248 | void ic2_FigSolidTeapot (float s) { | |||
| 249 | GLfloat obj_ref[] = {1.0, 1.0, 0.3, 1.0}; // teapotの色情報 (DIFFUSE用) | |||
| 250 | GLfloat obj_shn[] = {10.0}; // teapotの色情報 (SHININESS用) | |||
| 251 | ||||
| 252 | // 色の設定 | |||
| 253 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, obj_ref); | |||
| 254 | glMaterialfv(GL_FRONT, GL_SHININESS, obj_shn); | |||
| 255 | ||||
| 256 | glutSolidTeapot(s); | |||
| 257 | } | |||
| 258 | ||||
| 259 | // *********************************************************************** | |||
| 260 | // lighting ************************************************************** | |||
| 261 | ||||
| 262 | // +---------------------------------------------------- | |||
| 263 | // 光源を用意 | |||
| 264 | // +---------------------------------------------------- | |||
| 265 | // X Y Z Diff(R,G,B) Spec(R,G,B) | |||
| 266 | // 1.0 2.0 3.0 0.2 0.2 0.2 0.4 0.4 0.4 | |||
| 267 | // -1.0 2.0 3.0 0.4 0.4 0.4 0.4 0.4 0.4 | |||
| 268 | // 0.0 4.0 0.0 1.0 1.0 1.0 1.0 1.0 1.0 | |||
| 269 | void ic2_LightSetA (void) { | |||
| 270 | static int initflag = 0; | |||
| 271 | ||||
| 272 | if (initflag == 0) { | |||
| 273 | glEnable(GL_DEPTH_TEST); // デプスバッファによる描画を行う | |||
| 274 | glEnable(GL_NORMALIZE); // 法線ベクトルを常に正規化して解釈させる | |||
| 275 | glEnable(GL_LIGHTING); // 光源によるシェーディングを開始する | |||
| 276 | glEnable(GL_LIGHT0); // LIGHT0 を利用 | |||
| 277 | glEnable(GL_LIGHT1); // LIGHT1 を利用 | |||
| 278 | glEnable(GL_LIGHT2); // LIGHT2 を利用 | |||
| 279 | initflag = 1; | |||
| 280 | } | |||
| 281 | ||||
| 282 | GLfloat val[4]; | |||
| 283 | ||||
| 284 | val[0] = 1.0; val[1] = 2.0; val[2] = 3.0; val[3] = 1.0; glLightfv(GL_LIGHT0, GL_POSITION, val); | |||
| 285 | val[0] = 0.2; val[1] = 0.2; val[2] = 0.2; val[3] = 1.0; glLightfv(GL_LIGHT0, GL_DIFFUSE, val); | |||
| 286 | val[0] = 0.4; val[1] = 0.4; val[2] = 0.4; val[3] = 1.0; glLightfv(GL_LIGHT0, GL_SPECULAR, val); | |||
| 287 | ||||
| 288 | val[0] = -1.0; val[1] = 2.0; val[2] = 3.0; val[3] = 1.0; glLightfv(GL_LIGHT1, GL_POSITION, val); | |||
| 289 | val[0] = 0.4; val[1] = 0.4; val[2] = 0.4; val[3] = 1.0; glLightfv(GL_LIGHT1, GL_DIFFUSE, val); | |||
| 290 | val[0] = 0.4; val[1] = 0.4; val[2] = 0.4; val[3] = 1.0; glLightfv(GL_LIGHT1, GL_SPECULAR, val); | |||
| 291 | ||||
| 292 | val[0] = 0.0; val[1] = 4.0; val[2] = 0.0; val[3] = 1.0; glLightfv(GL_LIGHT2, GL_POSITION, val); | |||
| 293 | val[0] = 1.0; val[1] = 1.0; val[2] = 1.0; val[3] = 1.0; glLightfv(GL_LIGHT2, GL_DIFFUSE, val); | |||
| 294 | val[0] = 1.0; val[1] = 1.0; val[2] = 1.0; val[3] = 1.0; glLightfv(GL_LIGHT2, GL_SPECULAR, val); | |||
| 295 | } | |||
| 296 | ||||
| 297 | // *********************************************************************** | |||
| 298 | // camera work *********************************************************** | |||
| 299 | ||||
| 300 | // +---------------------------------------------------- | |||
| 301 | // カメラの投影行列を設定 | |||
| 302 | // +---------------------------------------------------- | |||
| 303 | // 利用する大域変数: window_w, window_h | |||
| 304 | // window_w/window_hの変化に対して、物体の見かけの大きさが変わらないように描画 | |||
| 305 | // → ortho_unit が重要! | |||
| 306 | // | |||
| 307 | void ic2_SetUpCamera_Ortho (void) { | |||
| 308 | float wlimit, hlimit; | |||
| 309 | wlimit = (window_w/2) / ortho_unit; | |||
| 310 | hlimit = (window_h/2) / ortho_unit; | |||
| 311 | ||||
| 312 | // glOrtho(左端, 右端, 下端, 上端, 近接側クリッピング面, 遠方側クリッピング面) | |||
| 313 | glMatrixMode(GL_PROJECTION); | |||
| 314 | glLoadIdentity(); // 毎フレーム再設定するのでPROJECTION行列スタックトップの初期化が必要に | |||
| 315 | glOrtho(-wlimit, wlimit, -hlimit, hlimit, -1.0, 1.0); | |||
| 316 | glMatrixMode(GL_MODELVIEW); | |||
| 317 | } | |||
| 318 | ||||
| 319 | // *********************************************************************** | |||
| 320 | // rendering ************************************************************* | |||
| 321 | ||||
| 322 | // +---------------------------------------------------- | |||
| 323 | // スクリーンに描画する | |||
| 324 | // +---------------------------------------------------- | |||
| 325 | void ic2_DrawFrame (void) { | |||
| 326 | ||||
| 327 | // (前処理) 以前にglClearColor()で指定した色で塗り潰す | |||
| 328 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |||
| 329 | // (前処理) 今回CGを描画する範囲 | |||
| 330 | glViewport(0, 0, window_w, window_h); | |||
| 331 | ||||
| 332 | // (1) カメラの設置 | |||
| 333 | ic2_SetUpCamera_Ortho(); | |||
| 334 | ||||
| 335 | // (2) 光源の設置 | |||
| 336 | ic2_LightSetA(); | |||
| 337 | ||||
| 338 | // (3) 物体の設置 | |||
| 339 | // 物体描画:正方形 | |||
| 340 | ic2_FigSquare(0.9); | |||
| 341 | // 物体描画:ティーポット | |||
| 342 | ic2_FigSolidTeapot(0.5); | |||
| 343 | ||||
| 344 | // (後処理) スクリーンの切り替え | |||
| 345 | glutSwapBuffers(); | |||
| 346 | ||||
| 347 | } | |||
| 348 | ||||
| 349 | // *********************************************************************** | |||
| 350 | // callbacks ************************************************************* | |||
| 351 | ||||
| 352 | // +---------------------------------------------------- | |||
| 353 | // キーが何か押されたときの対策用関数 | |||
| 354 | // +---------------------------------------------------- | |||
| 355 | // glutKeyboardFunc()にて登録予定 | |||
| 356 | // 引数 : key ... 入力文字 | |||
| 357 | // 引数 : x ... 文字が押されたときのマウスカーソルのX位置 | |||
| 358 | // 引数 : y ... 文字が押されたときのマウスカーソルのY位置 | |||
| 359 | void ic2_NormalKeyInput (unsigned char key, int x, int y) { | |||
| 360 | float delta_t = 0.1; // [unit] | |||
| 361 | float delta_r = 1.0; // [degree] | |||
| 362 | ||||
| 363 | switch (key) { | |||
| 364 | case 'q' : | |||
| 365 | case 'Q' : | |||
| 366 | case 27 : // ESCキーのこと | |||
| 367 | exit (0); | |||
| 368 | break; | |||
| 369 | ||||
| 370 | // Translation -_+ : [X]h_l [Y]n_u [Z]j_k | |||
| 371 | case 'h': glTranslatef(delta_t * -1, 0, 0); break; | |||
| 372 | case 'l': glTranslatef(delta_t * +1, 0, 0); break; | |||
| 373 | case 'n': glTranslatef(0, delta_t * -1, 0); break; | |||
| 374 | case 'u': glTranslatef(0, delta_t * +1, 0); break; | |||
| 375 | case 'j': glTranslatef(0, 0, delta_t * -1); break; | |||
| 376 | case 'k': glTranslatef(0, 0, delta_t * +1); break; | |||
| 377 | ||||
| 378 | // Rotation -_+ : [Y]a_f [Z]s_d [X]x_w | |||
| 379 | case 'x': glRotatef(delta_r * -1, 1, 0, 0); break; | |||
| 380 | case 'w': glRotatef(delta_r * +1, 1, 0, 0); break; | |||
| 381 | case 'a': glRotatef(delta_r * -1, 0, 1, 0); break; | |||
| 382 | case 'f': glRotatef(delta_r * +1, 0, 1, 0); break; | |||
| 383 | case 's': glRotatef(delta_r * -1, 0, 0, 1); break; | |||
| 384 | case 'd': glRotatef(delta_r * +1, 0, 0, 1); break; | |||
| 385 | ||||
| 386 | // [Scale] v_b | |||
| 387 | case 'v': glScalef(0.95, 0.95, 0.95); break; | |||
| 388 | case 'b': glScalef(1.05, 1.05, 1.05); break; | |||
| 389 | ||||
| 390 | // [Reset] | |||
| 391 | case 'R': | |||
| 392 | glMatrixMode(GL_MODELVIEW); | |||
| 393 | glPopMatrix(); // 保護されてた第1階層に降りる | |||
| 394 | glPushMatrix(); // 保護されてた第1階層からコピーしてスタックトップを1つ上げる | |||
| 395 | break; | |||
| 396 | ||||
| 397 | // [Show Stacktop MODELVIEW Matrix] | |||
| 398 | case 'p': | |||
| 399 | ic2_ShowMATRIX("Current status"); | |||
| 400 | break; | |||
| 401 | ||||
| 402 | } | |||
| 403 | ||||
| 404 | // 次のメインループ(glutMainLoop)での繰り返し時に描画を要求 | |||
| 405 | glutPostWindowRedisplay(window_id); | |||
| 406 | } | |||
| 407 | ||||
| 408 | // +---------------------------------------------------- | |||
| 409 | // ウィンドウサイズの変更が生じたときの対策用関数 | |||
| 410 | // +---------------------------------------------------- | |||
| 411 | // glutReshapeFunc()にて登録 | |||
| 412 | void ic2_ReshapeWindow (int w, int h) { | |||
| 413 | ||||
| 414 | // 新しいウィンドウサイズを大域変数にセット | |||
| 415 | window_w = w; window_h = h; | |||
| 416 | ||||
| 417 | // 次のメインループ(glutMainLoop)での繰り返し時に描画を要求 | |||
| 418 | glutPostWindowRedisplay(window_id); | |||
| 419 | } | |||
| 420 | ||||
| 421 | // +---------------------------------------------------- | |||
| 422 | // OpenGLとしてのWindowの初期化 | |||
| 423 | // +---------------------------------------------------- | |||
| 424 | void ic2_BootWindow (char winname[]) { | |||
| 425 | ||||
| 426 | // ダブルバッファ,RGB表色モード,デプスバッファ を利用 | |||
| 427 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); | |||
| 428 | ||||
| 429 | // ウィンドウの大きさ | |||
| 430 | glutInitWindowSize(window_w, window_h); | |||
| 431 | ||||
| 432 | // ウィンドウを開く | |||
| 433 | window_id = glutCreateWindow(winname); | |||
| 434 | ||||
| 435 | // レンダリングにはSmooth Shadingを採用 | |||
| 436 | glShadeModel(GL_SMOOTH); | |||
| 437 | ||||
| 438 | // ウィンドウ全体を書き直すときの色(ここでは黒) | |||
| 439 | glClearColor(0.0, 0.0, 0.0, 0.0); | |||
| 440 | ||||
| 441 | // 初期MODELVIEW matrixの保護 | |||
| 442 | glMatrixMode(GL_MODELVIEW); | |||
| 443 | glPushMatrix(); // 以後本プログラムでは GL_MODELVIEW スタックの2層目以上で作業 | |||
| 444 | ||||
| 445 | // Callback関数を設定 (イベント処理) | |||
| 446 | glutIdleFunc(ic2_DrawFrame); // 暇だったらフレームを描く(よい実装ではない) | |||
| 447 | glutKeyboardFunc(ic2_NormalKeyInput); // キーが押されたときの対策 | |||
| 448 | glutReshapeFunc(ic2_ReshapeWindow); // ウィンドウサイズ変更が検知されたときの対策 | |||
| 449 | } | |||
| 450 | ||||
| 451 | // *********************************************************************** | |||
| 452 | // main ***************************************************************** | |||
| 453 | // +---------------------------------------------------- | |||
| 454 | // Main Function | |||
| 455 | // +---------------------------------------------------- | |||
| 172 | int main (int argc, char *argv[]) { | 456 | int main (int argc, char *argv[]) { | |
| 173 | struct ic2PATCH *firstpatchptr = NULL; | 457 | struct ic2PATCH *firstpatchptr = NULL; | |
| 174 | int numberpatches = 0; | 458 | int numberpatches = 0; | |
| 175 | 459 | |||
| 176 | // model ファイルの読み込み | 460 | // model ファイルの読み込み | |
| 177 | if (argc <= 1) { | 461 | if (argc <= 1) { | |
| 178 | printf("Error: no model file is specified.\n"); | 462 | printf("Error: no model file is specified.\n"); | |
| 179 | return 1; | 463 | return 1; | |
| 180 | } | 464 | } | |
| 181 | numberpatches = ic2_ReadModel(argv[1], &firstpatchptr); | 465 | numberpatches = ic2_ReadModel(argv[1], &firstpatchptr); | |
| 182 | if (numberpatches < 0) { | 466 | if (numberpatches < 0) { | |
| 183 | printf("Error: invalid model \"%s\", reading failed.\n", argv[1]); | 467 | printf("Error: invalid model \"%s\", reading failed.\n", argv[1]); | |
| 184 | return 1; | 468 | return 1; | |
| 185 | } | 469 | } | |
| 186 | printf("Number of Patches in the model = %d \n", numberpatches); | 470 | printf("Number of Patches in the model = %d \n", numberpatches); | |
| 187 | ic2_PrintPATCHList(firstpatchptr); | |||
| 188 | 471 | |||
| 472 | // glutライブラリによる引数の解釈 | |||
| 473 | glutInit(&argc, argv); | |||
| 474 | ||||
| 475 | // OpenGL Window の初期化 | |||
| 476 | ic2_BootWindow(argv[0]); | |||
| 477 | ||||
| 478 | // 無限ループの開始 | |||
| 479 | glutMainLoop(); | |||
| 480 | ||||
| 189 | return 0; | 481 | return 0; | |
| 190 | } | 482 | } | |
| 191 |