rev |
line source |
nuclear@3
|
1 #include <stdlib.h>
|
nuclear@3
|
2 #include <string.h>
|
nuclear@3
|
3 #include <ctype.h>
|
nuclear@3
|
4 #include <limits.h>
|
nuclear@4
|
5 #include <assert.h>
|
nuclear@3
|
6 #include <vector>
|
nuclear@3
|
7 #include <map>
|
nuclear@3
|
8 #include <string>
|
nuclear@3
|
9 #include <sstream>
|
nuclear@3
|
10 #include <iomanip>
|
nuclear@3
|
11 #include "scene.h"
|
nuclear@3
|
12
|
nuclear@3
|
13 using namespace std;
|
nuclear@3
|
14
|
nuclear@3
|
15 #define COMMANDS \
|
nuclear@3
|
16 CMD(V), \
|
nuclear@3
|
17 CMD(VN), \
|
nuclear@3
|
18 CMD(VT), \
|
nuclear@3
|
19 CMD(F), \
|
nuclear@3
|
20 CMD(O), \
|
nuclear@3
|
21 CMD(G), \
|
nuclear@3
|
22 CMD(MTLLIB), \
|
nuclear@3
|
23 CMD(USEMTL), \
|
nuclear@3
|
24 CMD(NEWMTL), \
|
nuclear@6
|
25 CMD(KE), \
|
nuclear@3
|
26 CMD(KA), \
|
nuclear@3
|
27 CMD(KD), \
|
nuclear@3
|
28 CMD(KS), \
|
nuclear@3
|
29 CMD(NS), \
|
nuclear@3
|
30 CMD(NI), \
|
nuclear@3
|
31 CMD(D), \
|
nuclear@3
|
32 CMD(TR), \
|
nuclear@3
|
33 CMD(MAP_KD), \
|
nuclear@6
|
34 CMD(MAP_REFL), \
|
nuclear@6
|
35 CMD(MAP_BUMP), \
|
nuclear@3
|
36 CMD(MAP_KS), \
|
nuclear@3
|
37 CMD(MAP_NS), \
|
nuclear@3
|
38 CMD(MAP_D), \
|
nuclear@3
|
39 CMD(REFL), \
|
nuclear@3
|
40 CMD(BUMP)
|
nuclear@3
|
41
|
nuclear@3
|
42 #define CMD(x) CMD_##x
|
nuclear@3
|
43 enum {
|
nuclear@3
|
44 COMMANDS,
|
nuclear@3
|
45 CMD_UNK
|
nuclear@3
|
46 };
|
nuclear@3
|
47 #undef CMD
|
nuclear@3
|
48
|
nuclear@3
|
49 #define CMD(x) #x
|
nuclear@3
|
50 static const char *cmd_names[] = {
|
nuclear@3
|
51 COMMANDS,
|
nuclear@3
|
52 0
|
nuclear@3
|
53 };
|
nuclear@3
|
54 #undef CMD
|
nuclear@3
|
55
|
nuclear@3
|
56
|
nuclear@3
|
57 struct ObjFace {
|
nuclear@3
|
58 int elem;
|
nuclear@3
|
59 int v[4], n[4], t[4];
|
nuclear@3
|
60 };
|
nuclear@3
|
61
|
nuclear@3
|
62 struct ObjFile {
|
nuclear@3
|
63 string cur_obj, cur_mat;
|
nuclear@3
|
64 vector<Vector3> v, vn;
|
nuclear@3
|
65 vector<Vector2> vt;
|
nuclear@3
|
66 vector<ObjFace> f;
|
nuclear@3
|
67 };
|
nuclear@3
|
68
|
nuclear@3
|
69 typedef Vector3 Color;
|
nuclear@3
|
70
|
nuclear@3
|
71 struct ObjMat {
|
nuclear@3
|
72 string name; // newmtl <name>
|
nuclear@3
|
73 Color ambient, diffuse, specular; // Ka, Kd, Ks
|
nuclear@6
|
74 Color emissive; // Ke
|
nuclear@3
|
75 float shininess; // Ns
|
nuclear@3
|
76 float ior; // Ni
|
nuclear@3
|
77 float alpha; // d, Tr
|
nuclear@3
|
78
|
nuclear@3
|
79 string tex_dif, tex_spec, tex_shin, tex_alpha; // map_Kd, map_Ks, map_Ns, map_d
|
nuclear@3
|
80 string tex_refl; // refl -type sphere|cube file
|
nuclear@3
|
81 string tex_bump; // bump
|
nuclear@3
|
82
|
nuclear@3
|
83 ObjMat() { reset(); }
|
nuclear@3
|
84
|
nuclear@3
|
85 void reset() {
|
nuclear@3
|
86 ambient = diffuse = Color(0.5, 0.5, 0.5);
|
nuclear@3
|
87 specular = Color(0.0, 0.0, 0.0);
|
nuclear@3
|
88 name = tex_dif = tex_spec = tex_shin = tex_alpha = tex_refl = tex_bump = "";
|
nuclear@3
|
89 shininess = 0;
|
nuclear@3
|
90 ior = alpha = 1;
|
nuclear@3
|
91 }
|
nuclear@3
|
92 };
|
nuclear@3
|
93
|
nuclear@3
|
94 static bool read_materials(FILE *fp, vector<ObjMat> *vmtl);
|
nuclear@3
|
95 static Object *cons_object(ObjFile *obj);
|
nuclear@3
|
96
|
nuclear@3
|
97 static int get_cmd(char *str);
|
nuclear@3
|
98 static bool is_int(const char *str);
|
nuclear@3
|
99 static bool is_float(const char *str);
|
nuclear@3
|
100 static bool parse_vec(Vector3 *vec);
|
nuclear@3
|
101 static bool parse_color(Color *col);
|
nuclear@3
|
102 static bool parse_face(ObjFace *face);
|
nuclear@3
|
103 static const char *parse_map();
|
nuclear@3
|
104
|
nuclear@3
|
105
|
nuclear@3
|
106 static map<string, Material> matlib;
|
nuclear@3
|
107
|
nuclear@3
|
108
|
nuclear@3
|
109 #define INVALID_IDX INT_MIN
|
nuclear@3
|
110
|
nuclear@3
|
111 #define SEP " \t\n\r\v"
|
nuclear@3
|
112 #define BUF_SZ 512
|
nuclear@3
|
113 bool Scene::load_obj(FILE *fp)
|
nuclear@3
|
114 {
|
nuclear@3
|
115 static int seq;
|
nuclear@3
|
116 char cur_name[16];
|
nuclear@3
|
117 stringstream sstr;
|
nuclear@3
|
118
|
nuclear@3
|
119 ObjFile obj;
|
nuclear@3
|
120
|
nuclear@3
|
121 sprintf(cur_name, "default%02d.obj", seq++);
|
nuclear@3
|
122 obj.cur_obj = cur_name;
|
nuclear@3
|
123
|
nuclear@3
|
124 int prev_cmd = 0, obj_added = 0;
|
nuclear@3
|
125 for(;;) {
|
nuclear@3
|
126 Vector3 vec;
|
nuclear@3
|
127 ObjFace face;
|
nuclear@3
|
128
|
nuclear@3
|
129 char line[BUF_SZ];
|
nuclear@3
|
130 fgets(line, sizeof line, fp);
|
nuclear@3
|
131 if(feof(fp)) {
|
nuclear@3
|
132 break;
|
nuclear@3
|
133 }
|
nuclear@3
|
134
|
nuclear@3
|
135 char *tok;
|
nuclear@3
|
136 if(!(tok = strtok(line, SEP))) {
|
nuclear@3
|
137 continue; // ignore empty lines
|
nuclear@3
|
138 }
|
nuclear@3
|
139
|
nuclear@3
|
140 int cmd;
|
nuclear@3
|
141 if((cmd = get_cmd(tok)) == -1) {
|
nuclear@3
|
142 continue; // ignore unknown commands ...
|
nuclear@3
|
143 }
|
nuclear@3
|
144
|
nuclear@3
|
145 switch(cmd) {
|
nuclear@3
|
146 case CMD_V:
|
nuclear@3
|
147 if(!parse_vec(&vec)) {
|
nuclear@3
|
148 continue;
|
nuclear@3
|
149 }
|
nuclear@3
|
150 obj.v.push_back(vec);
|
nuclear@3
|
151 break;
|
nuclear@3
|
152
|
nuclear@3
|
153 case CMD_VN:
|
nuclear@3
|
154 if(!parse_vec(&vec)) {
|
nuclear@3
|
155 continue;
|
nuclear@3
|
156 }
|
nuclear@3
|
157 obj.vn.push_back(vec);
|
nuclear@3
|
158 break;
|
nuclear@3
|
159
|
nuclear@3
|
160 case CMD_VT:
|
nuclear@3
|
161 if(!parse_vec(&vec)) {
|
nuclear@3
|
162 continue;
|
nuclear@3
|
163 }
|
nuclear@3
|
164 vec.y = 1.0 - vec.y;
|
nuclear@3
|
165 obj.vt.push_back(Vector2(vec.x, vec.y));
|
nuclear@3
|
166 break;
|
nuclear@3
|
167
|
nuclear@3
|
168 case CMD_O:
|
nuclear@3
|
169 case CMD_G:
|
nuclear@3
|
170 if(prev_cmd == CMD_O || prev_cmd == CMD_G) {
|
nuclear@3
|
171 break; // just in case we've got both of them in a row
|
nuclear@3
|
172 }
|
nuclear@3
|
173 /* if we have any previous data, group them up, add the object
|
nuclear@3
|
174 * and continue with the new one...
|
nuclear@3
|
175 */
|
nuclear@3
|
176 if(!obj.f.empty()) {
|
nuclear@3
|
177 Object *robj = cons_object(&obj);
|
nuclear@3
|
178 robj->mtl = matlib[obj.cur_mat];
|
nuclear@3
|
179 add_object(robj);
|
nuclear@3
|
180 obj_added++;
|
nuclear@3
|
181
|
nuclear@3
|
182 obj.f.clear(); // clean the face list
|
nuclear@3
|
183 }
|
nuclear@3
|
184 if((tok = strtok(0, SEP))) {
|
nuclear@3
|
185 obj.cur_obj = tok;
|
nuclear@3
|
186 } else {
|
nuclear@3
|
187 sprintf(cur_name, "default%02d.obj", seq++);
|
nuclear@3
|
188 obj.cur_obj = cur_name;
|
nuclear@3
|
189 }
|
nuclear@3
|
190 break;
|
nuclear@3
|
191
|
nuclear@3
|
192 case CMD_MTLLIB:
|
nuclear@3
|
193 if((tok = strtok(0, SEP))) {
|
nuclear@3
|
194 FILE *mfile;
|
nuclear@3
|
195 if(!(mfile = fopen(tok, "rb"))) {
|
nuclear@3
|
196 fprintf(stderr, "failed to open material library: %s\n", tok);
|
nuclear@3
|
197 continue;
|
nuclear@3
|
198 }
|
nuclear@3
|
199
|
nuclear@3
|
200 // load all materials of the mtl file into a vector
|
nuclear@3
|
201 vector<ObjMat> vmtl;
|
nuclear@3
|
202 if(!read_materials(mfile, &vmtl)) {
|
nuclear@3
|
203 continue;
|
nuclear@3
|
204 }
|
nuclear@3
|
205 fclose(mfile);
|
nuclear@3
|
206
|
nuclear@3
|
207 // and add them all to the scene
|
nuclear@3
|
208 for(size_t i=0; i<vmtl.size(); i++) {
|
nuclear@3
|
209 Material mat;
|
nuclear@3
|
210 mat.ambient = vmtl[i].ambient;
|
nuclear@3
|
211 mat.diffuse = vmtl[i].diffuse;
|
nuclear@3
|
212 mat.specular = vmtl[i].specular;
|
nuclear@3
|
213 mat.shininess = vmtl[i].shininess;
|
nuclear@6
|
214 mat.emissive = vmtl[i].emissive;
|
nuclear@3
|
215 mat.alpha = vmtl[i].alpha;
|
nuclear@3
|
216
|
nuclear@5
|
217 if(vmtl[i].tex_dif.length()) {
|
nuclear@5
|
218 mat.tex[TEX_DIFFUSE] = load_texture(vmtl[i].tex_dif.c_str());
|
nuclear@5
|
219 }
|
nuclear@5
|
220 if(vmtl[i].tex_refl.length()) {
|
nuclear@5
|
221 mat.tex[TEX_ENVMAP] = load_texture(vmtl[i].tex_refl.c_str());
|
nuclear@5
|
222 }
|
nuclear@3
|
223
|
nuclear@3
|
224 matlib[vmtl[i].name] = mat;
|
nuclear@3
|
225 }
|
nuclear@3
|
226 }
|
nuclear@3
|
227 break;
|
nuclear@3
|
228
|
nuclear@3
|
229 case CMD_USEMTL:
|
nuclear@3
|
230 if((tok = strtok(0, SEP))) {
|
nuclear@3
|
231 obj.cur_mat = tok;
|
nuclear@3
|
232 } else {
|
nuclear@3
|
233 obj.cur_mat = "";
|
nuclear@3
|
234 }
|
nuclear@3
|
235 break;
|
nuclear@3
|
236
|
nuclear@3
|
237 case CMD_F:
|
nuclear@3
|
238 if(!parse_face(&face)) {
|
nuclear@3
|
239 continue;
|
nuclear@3
|
240 }
|
nuclear@3
|
241
|
nuclear@3
|
242 // convert negative indices to regular indices
|
nuclear@3
|
243 for(int i=0; i<4; i++) {
|
nuclear@3
|
244 if(face.v[i] < 0 && face.v[i] != INVALID_IDX) {
|
nuclear@3
|
245 face.v[i] = obj.v.size() + face.v[i];
|
nuclear@3
|
246 }
|
nuclear@3
|
247 if(face.n[i] < 0 && face.n[i] != INVALID_IDX) {
|
nuclear@3
|
248 face.n[i] = obj.vn.size() + face.n[i];
|
nuclear@3
|
249 }
|
nuclear@3
|
250 if(face.t[i] < 0 && face.t[i] != INVALID_IDX) {
|
nuclear@3
|
251 face.t[i] = obj.vt.size() + face.t[i];
|
nuclear@3
|
252 }
|
nuclear@3
|
253 }
|
nuclear@3
|
254
|
nuclear@3
|
255 // break quads into triangles if needed
|
nuclear@3
|
256 obj.f.push_back(face);
|
nuclear@3
|
257 if(face.elem == 4) {
|
nuclear@3
|
258 face.v[1] = face.v[2];
|
nuclear@3
|
259 face.n[1] = face.n[2];
|
nuclear@3
|
260 face.t[1] = face.t[2];
|
nuclear@3
|
261
|
nuclear@3
|
262 face.v[2] = face.v[3];
|
nuclear@3
|
263 face.n[2] = face.n[3];
|
nuclear@3
|
264 face.t[2] = face.t[3];
|
nuclear@3
|
265
|
nuclear@3
|
266 obj.f.push_back(face);
|
nuclear@3
|
267 }
|
nuclear@3
|
268 break;
|
nuclear@3
|
269
|
nuclear@3
|
270 default:
|
nuclear@3
|
271 break; // ignore unknown commands
|
nuclear@3
|
272 }
|
nuclear@3
|
273
|
nuclear@3
|
274 prev_cmd = cmd;
|
nuclear@3
|
275 }
|
nuclear@3
|
276
|
nuclear@3
|
277 // reached end of file...
|
nuclear@3
|
278 if(!obj.f.empty()) {
|
nuclear@3
|
279 Object *robj = cons_object(&obj);
|
nuclear@3
|
280 robj->mtl = matlib[obj.cur_mat];
|
nuclear@3
|
281 add_object(robj);
|
nuclear@3
|
282 obj_added++;
|
nuclear@3
|
283 }
|
nuclear@3
|
284
|
nuclear@3
|
285 return obj_added > 0;
|
nuclear@3
|
286 }
|
nuclear@3
|
287
|
nuclear@3
|
288 static Object *cons_object(ObjFile *obj)
|
nuclear@3
|
289 {
|
nuclear@3
|
290 Object *robj;
|
nuclear@3
|
291 Vector3 *varr, *narr;
|
nuclear@3
|
292 Vector2 *tarr;
|
nuclear@3
|
293
|
nuclear@3
|
294 int nelem = obj->f.size() * 3;
|
nuclear@3
|
295
|
nuclear@4
|
296 assert(sizeof(Vector3) == 3 * sizeof(float));
|
nuclear@4
|
297 assert(sizeof(Vector2) == 2 * sizeof(float));
|
nuclear@4
|
298
|
nuclear@3
|
299 try {
|
nuclear@3
|
300 robj = new Object;
|
nuclear@3
|
301 varr = new Vector3[nelem];
|
nuclear@3
|
302 narr = new Vector3[nelem];
|
nuclear@3
|
303 tarr = new Vector2[nelem];
|
nuclear@3
|
304 }
|
nuclear@3
|
305 catch(...) {
|
nuclear@3
|
306 return 0;
|
nuclear@3
|
307 }
|
nuclear@4
|
308 if(obj->cur_obj.length() > 0) {
|
nuclear@3
|
309 robj->set_name(obj->cur_obj.c_str());
|
nuclear@4
|
310 }
|
nuclear@3
|
311
|
nuclear@3
|
312 // need at least one of each element
|
nuclear@3
|
313 bool added_norm = false, added_tc = false;
|
nuclear@3
|
314 if(obj->vn.empty()) {
|
nuclear@3
|
315 obj->vn.push_back(Vector3(0, 0, 0));
|
nuclear@4
|
316 added_norm = true;
|
nuclear@3
|
317 }
|
nuclear@3
|
318 if(obj->vt.empty()) {
|
nuclear@3
|
319 obj->vt.push_back(Vector2(0, 0));
|
nuclear@3
|
320 added_tc = true;
|
nuclear@3
|
321 }
|
nuclear@3
|
322
|
nuclear@3
|
323 for(size_t i=0; i<obj->f.size(); i++) {
|
nuclear@3
|
324 for(int j=0; j<3; j++) {
|
nuclear@3
|
325 int idx = i * 3 + j;
|
nuclear@3
|
326 ObjFace *f = &obj->f[i];
|
nuclear@3
|
327
|
nuclear@3
|
328 varr[idx] = obj->v[f->v[j]];
|
nuclear@3
|
329 narr[idx] = obj->vn[f->n[j] < 0 ? 0 : f->n[j]];
|
nuclear@3
|
330
|
nuclear@3
|
331 float t = obj->vt[f->t[j] < 0 ? 0 : f->t[j]].x;
|
nuclear@3
|
332 float s = obj->vt[f->t[j] < 0 ? 0 : f->t[j]].y;
|
nuclear@3
|
333 tarr[idx] = Vector2(t, s);
|
nuclear@3
|
334 }
|
nuclear@3
|
335 }
|
nuclear@3
|
336
|
nuclear@3
|
337 if(added_norm) {
|
nuclear@3
|
338 obj->vn.pop_back();
|
nuclear@3
|
339 }
|
nuclear@3
|
340 if(added_tc) {
|
nuclear@3
|
341 obj->vt.pop_back();
|
nuclear@3
|
342 }
|
nuclear@3
|
343
|
nuclear@4
|
344 Mesh *mesh = new Mesh;
|
nuclear@3
|
345 mesh->set_attrib(MESH_ATTR_VERTEX, nelem, 3, &varr->x);
|
nuclear@3
|
346 mesh->set_attrib(MESH_ATTR_NORMAL, nelem, 3, &narr->x);
|
nuclear@3
|
347 mesh->set_attrib(MESH_ATTR_TEXCOORD, nelem, 2, &tarr->x);
|
nuclear@4
|
348 robj->set_mesh(mesh);
|
nuclear@4
|
349
|
nuclear@4
|
350 printf("loaded object %s: %d faces\n", obj->cur_obj.c_str(), nelem / 3);
|
nuclear@3
|
351
|
nuclear@3
|
352 delete [] varr;
|
nuclear@3
|
353 delete [] narr;
|
nuclear@3
|
354 delete [] tarr;
|
nuclear@3
|
355 return robj;
|
nuclear@3
|
356 }
|
nuclear@3
|
357
|
nuclear@3
|
358 static bool read_materials(FILE *fp, vector<ObjMat> *vmtl)
|
nuclear@3
|
359 {
|
nuclear@3
|
360 ObjMat mat;
|
nuclear@3
|
361
|
nuclear@3
|
362 for(;;) {
|
nuclear@3
|
363 char line[BUF_SZ];
|
nuclear@3
|
364 fgets(line, sizeof line, fp);
|
nuclear@3
|
365 if(feof(fp)) {
|
nuclear@3
|
366 break;
|
nuclear@3
|
367 }
|
nuclear@3
|
368
|
nuclear@3
|
369 char *tok;
|
nuclear@3
|
370 if(!(tok = strtok(line, SEP))) {
|
nuclear@3
|
371 continue;
|
nuclear@3
|
372 }
|
nuclear@3
|
373
|
nuclear@3
|
374 int cmd;
|
nuclear@3
|
375 if((cmd = get_cmd(tok)) == -1) {
|
nuclear@3
|
376 continue;
|
nuclear@3
|
377 }
|
nuclear@3
|
378
|
nuclear@3
|
379 switch(cmd) {
|
nuclear@3
|
380 case CMD_NEWMTL:
|
nuclear@3
|
381 // add the previous material, and start a new one
|
nuclear@3
|
382 if(mat.name.length() > 0) {
|
nuclear@3
|
383 vmtl->push_back(mat);
|
nuclear@3
|
384 mat.reset();
|
nuclear@3
|
385 }
|
nuclear@3
|
386 if((tok = strtok(0, SEP))) {
|
nuclear@3
|
387 mat.name = tok;
|
nuclear@3
|
388 }
|
nuclear@3
|
389 break;
|
nuclear@3
|
390
|
nuclear@6
|
391 case CMD_KE:
|
nuclear@6
|
392 parse_color(&mat.emissive);
|
nuclear@6
|
393 break;
|
nuclear@6
|
394
|
nuclear@3
|
395 case CMD_KA:
|
nuclear@3
|
396 parse_color(&mat.ambient);
|
nuclear@3
|
397 break;
|
nuclear@3
|
398
|
nuclear@3
|
399 case CMD_KD:
|
nuclear@3
|
400 parse_color(&mat.diffuse);
|
nuclear@3
|
401 break;
|
nuclear@3
|
402
|
nuclear@3
|
403 case CMD_KS:
|
nuclear@3
|
404 parse_color(&mat.specular);
|
nuclear@3
|
405 break;
|
nuclear@3
|
406
|
nuclear@3
|
407 case CMD_NS:
|
nuclear@3
|
408 if((tok = strtok(0, SEP)) && is_float(tok)) {
|
nuclear@3
|
409 mat.shininess = atof(tok);
|
nuclear@3
|
410 }
|
nuclear@3
|
411 break;
|
nuclear@3
|
412
|
nuclear@3
|
413 case CMD_NI:
|
nuclear@3
|
414 if((tok = strtok(0, SEP)) && is_float(tok)) {
|
nuclear@3
|
415 mat.ior = atof(tok);
|
nuclear@3
|
416 }
|
nuclear@3
|
417 break;
|
nuclear@3
|
418
|
nuclear@3
|
419 case CMD_D:
|
nuclear@3
|
420 case CMD_TR:
|
nuclear@3
|
421 {
|
nuclear@3
|
422 Color c;
|
nuclear@3
|
423 if(parse_color(&c)) {
|
nuclear@3
|
424 mat.alpha = cmd == CMD_D ? c.x : 1.0 - c.x;
|
nuclear@3
|
425 }
|
nuclear@3
|
426 }
|
nuclear@3
|
427 break;
|
nuclear@3
|
428
|
nuclear@3
|
429 case CMD_MAP_KD:
|
nuclear@3
|
430 mat.tex_dif = parse_map();
|
nuclear@3
|
431 break;
|
nuclear@3
|
432
|
nuclear@6
|
433 case CMD_MAP_REFL:
|
nuclear@6
|
434 mat.tex_refl = parse_map();
|
nuclear@6
|
435 break;
|
nuclear@6
|
436
|
nuclear@3
|
437 default:
|
nuclear@3
|
438 break;
|
nuclear@3
|
439 }
|
nuclear@3
|
440 }
|
nuclear@3
|
441
|
nuclear@3
|
442 if(mat.name.length() > 0) {
|
nuclear@3
|
443 vmtl->push_back(mat);
|
nuclear@3
|
444 }
|
nuclear@3
|
445 return true;
|
nuclear@3
|
446 }
|
nuclear@3
|
447
|
nuclear@3
|
448 static int get_cmd(char *str)
|
nuclear@3
|
449 {
|
nuclear@3
|
450 char *s = str;
|
nuclear@3
|
451 while((*s = toupper(*s))) s++;
|
nuclear@3
|
452
|
nuclear@3
|
453 for(int i=0; cmd_names[i]; i++) {
|
nuclear@3
|
454 if(strcmp(str, cmd_names[i]) == 0) {
|
nuclear@3
|
455 return i;
|
nuclear@3
|
456 }
|
nuclear@3
|
457 }
|
nuclear@3
|
458 return CMD_UNK;
|
nuclear@3
|
459 }
|
nuclear@3
|
460
|
nuclear@3
|
461 static bool is_int(const char *str)
|
nuclear@3
|
462 {
|
nuclear@3
|
463 char *tmp;
|
nuclear@3
|
464 strtol(str, &tmp, 10);
|
nuclear@3
|
465 return tmp != str;
|
nuclear@3
|
466 }
|
nuclear@3
|
467
|
nuclear@3
|
468 static bool is_float(const char *str)
|
nuclear@3
|
469 {
|
nuclear@3
|
470 char *tmp;
|
nuclear@3
|
471 strtod(str, &tmp);
|
nuclear@3
|
472 return tmp != str;
|
nuclear@3
|
473 }
|
nuclear@3
|
474
|
nuclear@3
|
475 static bool parse_vec(Vector3 *vec)
|
nuclear@3
|
476 {
|
nuclear@3
|
477 for(int i=0; i<3; i++) {
|
nuclear@3
|
478 char *tok;
|
nuclear@3
|
479
|
nuclear@3
|
480 if(!(tok = strtok(0, SEP)) || !is_float(tok)) {
|
nuclear@3
|
481 if(i < 2) {
|
nuclear@3
|
482 return false;
|
nuclear@3
|
483 }
|
nuclear@3
|
484 vec->z = 0.0;
|
nuclear@3
|
485 } else {
|
nuclear@3
|
486 (*vec)[i] = atof(tok);
|
nuclear@3
|
487 }
|
nuclear@3
|
488 }
|
nuclear@3
|
489 return true;
|
nuclear@3
|
490 }
|
nuclear@3
|
491
|
nuclear@3
|
492 static bool parse_color(Color *col)
|
nuclear@3
|
493 {
|
nuclear@3
|
494 for(int i=0; i<3; i++) {
|
nuclear@3
|
495 char *tok;
|
nuclear@3
|
496
|
nuclear@3
|
497 if(!(tok = strtok(0, SEP)) || !is_float(tok)) {
|
nuclear@3
|
498 col->y = col->z = col->x;
|
nuclear@3
|
499 return i > 0 ? true : false;
|
nuclear@3
|
500 }
|
nuclear@3
|
501 (*col)[i] = atof(tok);
|
nuclear@3
|
502 }
|
nuclear@3
|
503 return true;
|
nuclear@3
|
504 }
|
nuclear@3
|
505
|
nuclear@3
|
506 static bool parse_face(ObjFace *face)
|
nuclear@3
|
507 {
|
nuclear@3
|
508 char *tok[] = {0, 0, 0, 0};
|
nuclear@3
|
509 face->elem = 0;
|
nuclear@3
|
510
|
nuclear@3
|
511 for(int i=0; i<4; i++) {
|
nuclear@3
|
512 if((!(tok[i] = strtok(0, SEP)) || !is_int(tok[i]))) {
|
nuclear@3
|
513 if(i < 3) return false; // less than 3 verts? not a polygon
|
nuclear@3
|
514 } else {
|
nuclear@3
|
515 face->elem++;
|
nuclear@3
|
516 }
|
nuclear@3
|
517 }
|
nuclear@3
|
518
|
nuclear@3
|
519 for(int i=0; i<4; i++) {
|
nuclear@3
|
520 char *subtok = tok[i];
|
nuclear@3
|
521
|
nuclear@3
|
522 if(!subtok || !*subtok || !is_int(subtok)) {
|
nuclear@3
|
523 if(i < 3) {
|
nuclear@3
|
524 return false;
|
nuclear@3
|
525 }
|
nuclear@3
|
526 face->v[i] = INVALID_IDX;
|
nuclear@3
|
527 } else {
|
nuclear@3
|
528 face->v[i] = atoi(subtok);
|
nuclear@3
|
529 if(face->v[i] > 0) face->v[i]--; /* convert to 0-based */
|
nuclear@3
|
530 }
|
nuclear@3
|
531
|
nuclear@3
|
532 while(subtok && *subtok && *subtok != '/') {
|
nuclear@3
|
533 subtok++;
|
nuclear@3
|
534 }
|
nuclear@3
|
535 if(subtok && *subtok && *++subtok && is_int(subtok)) {
|
nuclear@3
|
536 face->t[i] = atoi(subtok);
|
nuclear@3
|
537 if(face->t[i] > 0) face->t[i]--; /* convert to 0-based */
|
nuclear@3
|
538 } else {
|
nuclear@3
|
539 face->t[i] = INVALID_IDX;
|
nuclear@3
|
540 }
|
nuclear@3
|
541
|
nuclear@3
|
542 while(subtok && *subtok && *subtok != '/') {
|
nuclear@3
|
543 subtok++;
|
nuclear@3
|
544 }
|
nuclear@3
|
545 if(subtok && *subtok && *++subtok && is_int(subtok)) {
|
nuclear@3
|
546 face->n[i] = atoi(subtok);
|
nuclear@3
|
547 if(face->n[i] > 0) face->n[i]--; /* convert to 0-based */
|
nuclear@3
|
548 } else {
|
nuclear@3
|
549 face->n[i] = INVALID_IDX;
|
nuclear@3
|
550 }
|
nuclear@3
|
551 }
|
nuclear@3
|
552
|
nuclear@3
|
553 return true;
|
nuclear@3
|
554 }
|
nuclear@3
|
555
|
nuclear@3
|
556 static const char *parse_map()
|
nuclear@3
|
557 {
|
nuclear@3
|
558 char *tok, *prev = 0;
|
nuclear@3
|
559
|
nuclear@3
|
560 while((tok = strtok(0, SEP))) {
|
nuclear@3
|
561 prev = tok;
|
nuclear@3
|
562 }
|
nuclear@3
|
563
|
nuclear@3
|
564 return prev ? prev : "";
|
nuclear@3
|
565 }
|