clray

annotate src/mesh.cc @ 6:b06518bb16e9

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