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