nuclear@23: #include nuclear@23: #include nuclear@23: #include nuclear@23: #include nuclear@23: #include nuclear@23: #include nuclear@23: #include nuclear@23: #include nuclear@23: #include nuclear@23: #include "scene.h" nuclear@23: #include "vector.h" nuclear@23: nuclear@23: #ifndef PATH_MAX nuclear@23: #define PATH_MAX 512 nuclear@23: #endif nuclear@23: nuclear@23: #define COMMANDS \ nuclear@23: CMD(V), \ nuclear@23: CMD(VN), \ nuclear@23: CMD(VT), \ nuclear@23: CMD(F), \ nuclear@23: CMD(O), \ nuclear@23: CMD(G), \ nuclear@23: CMD(MTLLIB), \ nuclear@23: CMD(USEMTL), \ nuclear@23: CMD(NEWMTL), \ nuclear@23: CMD(KA), \ nuclear@23: CMD(KD), \ nuclear@23: CMD(KS), \ nuclear@23: CMD(KR), \ nuclear@23: CMD(NS), \ nuclear@23: CMD(NI), \ nuclear@23: CMD(D), \ nuclear@23: CMD(TR), \ nuclear@23: CMD(MAP_KD), \ nuclear@23: CMD(MAP_KS), \ nuclear@23: CMD(MAP_NS), \ nuclear@23: CMD(MAP_D), \ nuclear@23: CMD(REFL), \ nuclear@23: CMD(BUMP) nuclear@23: nuclear@23: #define CMD(x) CMD_##x nuclear@23: enum { nuclear@23: COMMANDS, nuclear@23: CMD_UNK nuclear@23: }; nuclear@23: #undef CMD nuclear@23: nuclear@23: #define CMD(x) #x nuclear@23: static const char *cmd_names[] = { nuclear@23: COMMANDS, nuclear@23: 0 nuclear@23: }; nuclear@23: #undef CMD nuclear@23: nuclear@23: struct obj_face { nuclear@23: int elem; nuclear@23: int v[4], n[4], t[4]; nuclear@23: }; nuclear@23: nuclear@23: struct obj_file { nuclear@23: std::string cur_obj, cur_mat; nuclear@23: std::vector v, vn, vt; nuclear@23: std::vector f; nuclear@23: }; nuclear@23: nuclear@23: struct obj_mat { nuclear@23: std::string name; // newmtl nuclear@23: Vector3 ambient, diffuse, specular; // Ka, Kd, Ks nuclear@23: float shininess; // Ns nuclear@23: float ior; // Ni nuclear@23: float alpha; // d, Tr nuclear@23: float refl; // Kr (my extesnsion) nuclear@23: nuclear@23: std::string tex_dif, tex_spec, tex_shin, tex_alpha; // map_Kd, map_Ks, map_Ns, map_d nuclear@23: std::string tex_refl; // refl -type sphere|cube file nuclear@23: std::string tex_bump; // bump nuclear@23: nuclear@23: obj_mat() { reset(); } nuclear@23: nuclear@23: void reset() { nuclear@23: ambient = diffuse = Vector3(0.5, 0.5, 0.5); nuclear@23: specular = Vector3(0.0, 0.0, 0.0); nuclear@23: name = tex_dif = tex_spec = tex_shin = tex_alpha = tex_refl = tex_bump = ""; nuclear@23: shininess = 0; nuclear@23: ior = alpha = 1; nuclear@23: refl = 0.0; nuclear@23: } nuclear@23: }; nuclear@23: nuclear@23: static bool read_materials(FILE *fp, std::vector *vmtl); nuclear@23: static Mesh *cons_mesh(obj_file *obj); nuclear@23: nuclear@23: static int get_cmd(char *str); nuclear@23: static bool is_int(const char *str); nuclear@23: static bool is_float(const char *str); nuclear@23: static bool parse_vec(Vector3 *vec); nuclear@23: static bool parse_color(Vector3 *col); nuclear@23: static bool parse_face(obj_face *face); nuclear@23: static const char *parse_map(); nuclear@23: nuclear@23: static bool find_file(char *res, int sz, const char *fname, const char *path = ".", const char *mode = "rb"); nuclear@23: static const char *dirname(const char *str); nuclear@23: nuclear@23: static std::map matnames; nuclear@23: nuclear@23: #define INVALID_IDX INT_MIN nuclear@23: nuclear@23: #define SEP " \t\n\r\v" nuclear@23: #define BUF_SZ 512 nuclear@23: nuclear@23: nuclear@23: bool Scene::load(const char *fname) nuclear@23: { nuclear@23: FILE *fp; nuclear@23: nuclear@23: if(!(fp = fopen(fname, "rb"))) { nuclear@23: fprintf(stderr, "failed to open %s: %s\n", fname, strerror(errno)); nuclear@23: return false; nuclear@23: } nuclear@23: nuclear@23: bool res = load(fp); nuclear@23: fclose(fp); nuclear@23: return res; nuclear@23: } nuclear@23: nuclear@23: bool Scene::load(FILE *fp) nuclear@23: { nuclear@23: static int seq; nuclear@23: char cur_name[16]; nuclear@23: nuclear@23: obj_file obj; nuclear@23: nuclear@23: sprintf(cur_name, "default%02d.obj", seq++); nuclear@23: obj.cur_obj = cur_name; nuclear@23: nuclear@23: int prev_cmd = 0, obj_added = 0; nuclear@23: for(;;) { nuclear@23: Vector3 vec; nuclear@23: obj_face face; nuclear@23: nuclear@23: char line[BUF_SZ]; nuclear@23: fgets(line, sizeof line, fp); nuclear@23: if(feof(fp)) { nuclear@23: break; nuclear@23: } nuclear@23: nuclear@23: char *tok; nuclear@23: if(!(tok = strtok(line, SEP))) { nuclear@23: continue; // ignore empty lines nuclear@23: } nuclear@23: nuclear@23: int cmd; nuclear@23: if((cmd = get_cmd(tok)) == -1) { nuclear@23: continue; // ignore unknown commands ... nuclear@23: } nuclear@23: nuclear@23: switch(cmd) { nuclear@23: case CMD_V: nuclear@23: if(!parse_vec(&vec)) { nuclear@23: continue; nuclear@23: } nuclear@23: obj.v.push_back(vec); nuclear@23: break; nuclear@23: nuclear@23: case CMD_VN: nuclear@23: if(!parse_vec(&vec)) { nuclear@23: continue; nuclear@23: } nuclear@23: obj.vn.push_back(vec); nuclear@23: break; nuclear@23: nuclear@23: case CMD_VT: nuclear@23: if(!parse_vec(&vec)) { nuclear@23: continue; nuclear@23: } nuclear@23: vec.y = 1.0 - vec.y; nuclear@23: obj.vt.push_back(vec); nuclear@23: break; nuclear@23: nuclear@23: case CMD_O: nuclear@23: case CMD_G: nuclear@23: if(prev_cmd == CMD_O || prev_cmd == CMD_G) { nuclear@23: break; // just in case we've got both of them in a row nuclear@23: } nuclear@23: /* if we have any previous data, group them up, add the object nuclear@23: * and continue with the new one... nuclear@23: */ nuclear@23: if(!obj.f.empty()) { nuclear@23: Mesh *mesh = cons_mesh(&obj); nuclear@23: mesh->matid = matnames[obj.cur_mat]; nuclear@23: add_mesh(mesh); nuclear@23: obj_added++; nuclear@23: nuclear@23: obj.f.clear(); // clean the face list nuclear@23: } nuclear@23: if((tok = strtok(0, SEP))) { nuclear@23: obj.cur_obj = tok; nuclear@23: } else { nuclear@23: sprintf(cur_name, "default%02d.obj", seq++); nuclear@23: obj.cur_obj = cur_name; nuclear@23: } nuclear@23: break; nuclear@23: nuclear@23: case CMD_MTLLIB: nuclear@23: if((tok = strtok(0, SEP))) { nuclear@23: char path[PATH_MAX]; nuclear@23: nuclear@23: sprintf(path, ".:%s", dirname(tok)); nuclear@23: if(!find_file(path, PATH_MAX, tok, path)) { nuclear@23: fprintf(stderr, "material library not found: %s\n", tok); nuclear@23: continue; nuclear@23: } nuclear@23: nuclear@23: FILE *mfile; nuclear@23: if(!(mfile = fopen(path, "rb"))) { nuclear@23: fprintf(stderr, "failed to open material library: %s\n", path); nuclear@23: continue; nuclear@23: } nuclear@23: nuclear@23: // load all materials of the mtl file into a vector nuclear@23: std::vector vmtl; nuclear@23: if(!read_materials(mfile, &vmtl)) { nuclear@23: continue; nuclear@23: } nuclear@23: fclose(mfile); nuclear@23: nuclear@23: // and add them all to the scene nuclear@23: for(size_t i=0; imatid = matnames[obj.cur_mat]; nuclear@23: add_mesh(mesh); nuclear@23: obj_added++; nuclear@23: } nuclear@23: nuclear@23: return obj_added > 0; nuclear@23: } nuclear@23: nuclear@23: static Mesh *cons_mesh(obj_file *obj) nuclear@23: { nuclear@23: Mesh *mesh; nuclear@23: nuclear@23: // need at least one of each element nuclear@23: bool added_norm = false, added_tc = false; nuclear@23: if(obj->vn.empty()) { nuclear@23: obj->vn.push_back(Vector3(0, 0, 0)); nuclear@23: added_norm = true; nuclear@23: } nuclear@23: if(obj->vt.empty()) { nuclear@23: obj->vt.push_back(Vector3(0, 0, 0)); nuclear@23: added_tc = true; nuclear@23: } nuclear@23: nuclear@23: mesh = new Mesh; nuclear@23: nuclear@23: for(size_t i=0; if.size(); i++) { nuclear@23: Face face; nuclear@23: Vector3 v[3]; nuclear@23: nuclear@23: for(int j=0; j<3; j++) { nuclear@23: obj_face *f = &obj->f[i]; nuclear@23: nuclear@23: face.v[j].pos[0] = v[j].x = obj->v[f->v[j]].x; nuclear@23: face.v[j].pos[1] = v[j].y = obj->v[f->v[j]].y; nuclear@23: face.v[j].pos[2] = v[j].z = obj->v[f->v[j]].z; nuclear@23: face.v[j].pos[3] = 0.0; nuclear@23: nuclear@23: int nidx = f->n[j] < 0 ? 0 : f->n[j]; nuclear@23: face.v[j].normal[0] = obj->vn[nidx].x; nuclear@23: face.v[j].normal[1] = obj->vn[nidx].y; nuclear@23: face.v[j].normal[2] = obj->vn[nidx].z; nuclear@23: face.v[j].normal[3] = 0.0; nuclear@23: nuclear@23: int tidx = f->t[j] < 0 ? 0 : f->t[j]; nuclear@23: face.v[j].tex[0] = obj->vt[tidx].x; nuclear@23: face.v[j].tex[1] = obj->vt[tidx].y; nuclear@23: } nuclear@23: nuclear@23: Vector3 a = v[1] - v[0]; nuclear@23: Vector3 b = v[2] - v[0]; nuclear@23: Vector3 n = cross(a, b); nuclear@23: n.normalize(); nuclear@23: nuclear@23: face.normal[0] = n.x; nuclear@23: face.normal[1] = n.y; nuclear@23: face.normal[2] = n.z; nuclear@23: face.normal[3] = 0.0; nuclear@23: nuclear@23: mesh->faces.push_back(face); nuclear@23: } nuclear@23: nuclear@23: if(added_norm) { nuclear@23: obj->vn.pop_back(); nuclear@23: } nuclear@23: if(added_tc) { nuclear@23: obj->vt.pop_back(); nuclear@23: } nuclear@23: nuclear@23: return mesh; nuclear@23: } nuclear@23: nuclear@23: static bool read_materials(FILE *fp, std::vector *vmtl) nuclear@23: { nuclear@23: obj_mat mat; nuclear@23: nuclear@23: for(;;) { nuclear@23: char line[BUF_SZ]; nuclear@23: fgets(line, sizeof line, fp); nuclear@23: if(feof(fp)) { nuclear@23: break; nuclear@23: } nuclear@23: nuclear@23: char *tok; nuclear@23: if(!(tok = strtok(line, SEP))) { nuclear@23: continue; nuclear@23: } nuclear@23: nuclear@23: int cmd; nuclear@23: if((cmd = get_cmd(tok)) == -1) { nuclear@23: continue; nuclear@23: } nuclear@23: nuclear@23: switch(cmd) { nuclear@23: case CMD_NEWMTL: nuclear@23: // add the previous material, and start a new one nuclear@23: if(mat.name.length() > 0) { nuclear@23: printf("Adding material: %s\n", mat.name.c_str()); nuclear@23: vmtl->push_back(mat); nuclear@23: mat.reset(); nuclear@23: } nuclear@23: if((tok = strtok(0, SEP))) { nuclear@23: mat.name = tok; nuclear@23: } nuclear@23: break; nuclear@23: nuclear@23: case CMD_KA: nuclear@23: parse_color(&mat.ambient); nuclear@23: break; nuclear@23: nuclear@23: case CMD_KD: nuclear@23: parse_color(&mat.diffuse); nuclear@23: break; nuclear@23: nuclear@23: case CMD_KS: nuclear@23: parse_color(&mat.specular); nuclear@23: break; nuclear@23: nuclear@23: case CMD_KR: nuclear@23: if((tok = strtok(0, SEP)) && is_float(tok)) { nuclear@23: mat.refl = atof(tok); nuclear@23: } nuclear@23: break; nuclear@23: nuclear@23: case CMD_NS: nuclear@23: if((tok = strtok(0, SEP)) && is_float(tok)) { nuclear@23: mat.shininess = atof(tok); nuclear@23: } nuclear@23: break; nuclear@23: nuclear@23: case CMD_NI: nuclear@23: if((tok = strtok(0, SEP)) && is_float(tok)) { nuclear@23: mat.ior = atof(tok); nuclear@23: } nuclear@23: break; nuclear@23: nuclear@23: case CMD_D: nuclear@23: case CMD_TR: nuclear@23: { nuclear@23: Vector3 c; nuclear@23: if(parse_color(&c)) { nuclear@23: mat.alpha = cmd == CMD_D ? c.x : 1.0 - c.x; nuclear@23: } nuclear@23: } nuclear@23: break; nuclear@23: nuclear@23: case CMD_MAP_KD: nuclear@23: mat.tex_dif = parse_map(); nuclear@23: break; nuclear@23: nuclear@23: default: nuclear@23: break; nuclear@23: } nuclear@23: } nuclear@23: nuclear@23: if(mat.name.length() > 0) { nuclear@23: printf("Adding material: %s\n", mat.name.c_str()); nuclear@23: vmtl->push_back(mat); nuclear@23: } nuclear@23: return true; nuclear@23: } nuclear@23: nuclear@23: static int get_cmd(char *str) nuclear@23: { nuclear@23: char *s = str; nuclear@23: while((*s = toupper(*s))) s++; nuclear@23: nuclear@23: for(int i=0; cmd_names[i]; i++) { nuclear@23: if(strcmp(str, cmd_names[i]) == 0) { nuclear@23: return i; nuclear@23: } nuclear@23: } nuclear@23: return CMD_UNK; nuclear@23: } nuclear@23: nuclear@23: static bool is_int(const char *str) nuclear@23: { nuclear@23: char *tmp; nuclear@23: strtol(str, &tmp, 10); nuclear@23: return tmp != str; nuclear@23: } nuclear@23: nuclear@23: static bool is_float(const char *str) nuclear@23: { nuclear@23: char *tmp; nuclear@23: strtod(str, &tmp); nuclear@23: return tmp != str; nuclear@23: } nuclear@23: nuclear@23: static bool parse_vec(Vector3 *vec) nuclear@23: { nuclear@23: for(int i=0; i<3; i++) { nuclear@23: char *tok; nuclear@23: nuclear@23: if(!(tok = strtok(0, SEP)) || !is_float(tok)) { nuclear@23: if(i < 2) { nuclear@23: return false; nuclear@23: } nuclear@23: vec->z = 0.0; nuclear@23: } else { nuclear@23: float v = atof(tok); nuclear@23: nuclear@23: switch(i) { nuclear@23: case 0: nuclear@23: vec->x = v; nuclear@23: break; nuclear@23: case 1: nuclear@23: vec->y = v; nuclear@23: break; nuclear@23: case 2: nuclear@23: vec->z = v; nuclear@23: break; nuclear@23: } nuclear@23: } nuclear@23: } nuclear@23: return true; nuclear@23: } nuclear@23: nuclear@23: static bool parse_color(Vector3 *col) nuclear@23: { nuclear@23: for(int i=0; i<3; i++) { nuclear@23: char *tok; nuclear@23: nuclear@23: if(!(tok = strtok(0, SEP)) || !is_float(tok)) { nuclear@23: col->y = col->z = col->x; nuclear@23: return i > 0 ? true : false; nuclear@23: } nuclear@23: nuclear@23: float v = atof(tok); nuclear@23: switch(i) { nuclear@23: case 0: nuclear@23: col->x = v; nuclear@23: break; nuclear@23: case 1: nuclear@23: col->y = v; nuclear@23: break; nuclear@23: case 2: nuclear@23: col->z = v; nuclear@23: break; nuclear@23: } nuclear@23: } nuclear@23: return true; nuclear@23: } nuclear@23: nuclear@23: static bool parse_face(obj_face *face) nuclear@23: { nuclear@23: char *tok[] = {0, 0, 0, 0}; nuclear@23: face->elem = 0; nuclear@23: nuclear@23: for(int i=0; i<4; i++) { nuclear@23: if((!(tok[i] = strtok(0, SEP)) || !is_int(tok[i]))) { nuclear@23: if(i < 3) return false; // less than 3 verts? not a polygon nuclear@23: } else { nuclear@23: face->elem++; nuclear@23: } nuclear@23: } nuclear@23: nuclear@23: for(int i=0; i<4; i++) { nuclear@23: char *subtok = tok[i]; nuclear@23: nuclear@23: if(!subtok || !*subtok || !is_int(subtok)) { nuclear@23: if(i < 3) { nuclear@23: return false; nuclear@23: } nuclear@23: face->v[i] = INVALID_IDX; nuclear@23: } else { nuclear@23: face->v[i] = atoi(subtok); nuclear@23: if(face->v[i] > 0) face->v[i]--; /* convert to 0-based */ nuclear@23: } nuclear@23: nuclear@23: while(subtok && *subtok && *subtok != '/') { nuclear@23: subtok++; nuclear@23: } nuclear@23: if(subtok && *subtok && *++subtok && is_int(subtok)) { nuclear@23: face->t[i] = atoi(subtok); nuclear@23: if(face->t[i] > 0) face->t[i]--; /* convert to 0-based */ nuclear@23: } else { nuclear@23: face->t[i] = INVALID_IDX; nuclear@23: } nuclear@23: nuclear@23: while(subtok && *subtok && *subtok != '/') { nuclear@23: subtok++; nuclear@23: } nuclear@23: if(subtok && *subtok && *++subtok && is_int(subtok)) { nuclear@23: face->n[i] = atoi(subtok); nuclear@23: if(face->n[i] > 0) face->n[i]--; /* convert to 0-based */ nuclear@23: } else { nuclear@23: face->n[i] = INVALID_IDX; nuclear@23: } nuclear@23: } nuclear@23: nuclear@23: return true; nuclear@23: } nuclear@23: nuclear@23: static const char *parse_map() nuclear@23: { nuclear@23: char *tok, *prev = 0; nuclear@23: nuclear@23: while((tok = strtok(0, SEP))) { nuclear@23: prev = tok; nuclear@23: } nuclear@23: nuclear@23: return prev ? prev : ""; nuclear@23: } nuclear@23: nuclear@23: static bool find_file(char *res, int sz, const char *fname, const char *path, const char *mode) nuclear@23: { nuclear@23: FILE *fp; nuclear@23: const char *beg, *end; nuclear@23: int fnamelen = strlen(fname); nuclear@23: nuclear@23: beg = path; nuclear@23: while(beg && *beg) { nuclear@23: end = beg; nuclear@23: while(*end && *end != ':') { nuclear@23: end++; nuclear@23: } nuclear@23: nuclear@23: int res_len = end - beg; nuclear@23: char *pathname = (char*)alloca(res_len + fnamelen + 2); nuclear@23: memcpy(pathname, beg, res_len); nuclear@23: pathname[res_len] = 0; nuclear@23: if(res_len) { nuclear@23: strcat(pathname, "/"); nuclear@23: } nuclear@23: strcat(pathname, fname); nuclear@23: nuclear@23: if((fp = fopen(pathname, mode))) { nuclear@23: fclose(fp); nuclear@23: strncpy(res, pathname, sz); nuclear@23: return true; nuclear@23: } nuclear@23: nuclear@23: beg += res_len; nuclear@23: if(*beg == ':') beg++; nuclear@23: } nuclear@23: return false; nuclear@23: } nuclear@23: nuclear@23: static const char *dirname(const char *str) nuclear@23: { nuclear@23: static char buf[PATH_MAX]; nuclear@23: nuclear@23: if(!str || !*str) { nuclear@23: strcpy(buf, "."); nuclear@23: } else { nuclear@23: strncpy(buf, str, PATH_MAX); nuclear@23: char *ptr = strrchr(buf, '/'); nuclear@23: nuclear@23: if(ptr && *ptr) { nuclear@23: *ptr = 0; nuclear@23: } else { nuclear@23: strcpy(buf, "."); nuclear@23: } nuclear@23: } nuclear@23: return buf; nuclear@23: }