5 #include "cgmath/cgmath.h"
15 cgm_vec3 ka, kd, ks, ke;
19 char *map_kd, *map_ke, *map_alpha;
23 static int proc_facevert(struct mesh *mesh, struct facevertex *fv,
24 cgm_vec3 *varr, cgm_vec3 *narr, cgm_vec2 *tarr, struct rbtree *rbtree);
26 static char *cleanline(char *s);
27 static char *parse_idx(char *ptr, int *idx, int arrsz);
28 static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn);
30 static int load_mtllib(struct scenefile *scn, const char *path_prefix, const char *mtlfname);
31 static void free_mtllist(struct material *mtl);
32 static void conv_mtl(struct material *mm, struct objmtl *om, const char *path_prefix);
34 static int cmp_facevert(const void *ap, const void *bp);
35 static void free_rbnode_key(struct rbnode *n, void *cls);
37 #define GROW_ARRAY(arr, sz) \
39 int newsz = (sz) ? (sz) * 2 : 16; \
40 void *tmp = realloc(arr, newsz * sizeof *(arr)); \
42 fprintf(stderr, "failed to grow array to %d\n", newsz); \
50 int load_scenefile(struct scenefile *scn, const char *fname)
52 int i, nlines, res = -1;
54 char buf[256], *line, *ptr, *path_prefix;
55 int varr_size, varr_max, narr_size, narr_max, tarr_size, tarr_max;
56 cgm_vec3 v, *varr = 0, *narr = 0;
58 struct facevertex fv[4];
60 struct material *mtl = 0;
62 struct rbtree *rbtree = 0;
64 memset(scn, 0, sizeof *scn);
66 varr_size = varr_max = narr_size = narr_max = tarr_size = tarr_max = 0;
70 if(!(fp = fopen(fname, "rb"))) {
71 fprintf(stderr, "load_scenefile: failed to open %s\n", fname);
75 if(!(rbtree = rb_create(cmp_facevert))) {
76 fprintf(stderr, "load_scenefile: failed to create facevertex search tree\n");
79 rb_set_delete_func(rbtree, free_rbnode_key, 0);
82 if((sep = strrchr(buf, '/'))) {
87 path_prefix = alloca(strlen(buf) + 1);
88 strcpy(path_prefix, buf);
91 sep = (char*)fname + (sep - buf);
93 if(!(scn->fname = strdup(sep ? sep + 1 : fname))) {
94 fprintf(stderr, "failed to allocate scenefile name buffer\n");
98 if(!(mesh = malloc(sizeof *mesh))) {
99 fprintf(stderr, "failed to allocate mesh\n");
109 while(fgets(buf, sizeof buf, fp)) {
111 if(!(line = cleanline(buf))) {
117 v.x = v.y = v.z = 0.0f;
118 if(sscanf(line + 2, "%f %f %f", &v.x, &v.y, &v.z) < 2) {
121 if(isspace(line[1])) {
122 if(varr_size >= varr_max) {
123 GROW_ARRAY(varr, varr_max);
125 varr[varr_size++] = v;
126 } else if(line[1] == 't' && isspace(line[2])) {
127 if(tarr_size >= tarr_max) {
128 GROW_ARRAY(tarr, tarr_max);
130 tarr[tarr_size++] = *(cgm_vec2*)&v;
131 } else if(line[1] == 'n' && isspace(line[2])) {
132 if(narr_size >= narr_max) {
133 GROW_ARRAY(narr, narr_max);
135 narr[narr_size++] = v;
140 if(!isspace(line[1])) break;
145 if(!(ptr = parse_face_vert(ptr, fv + i, varr_size, tarr_size, narr_size))) {
148 if(proc_facevert(mesh, fv + i, varr, narr, tarr, rbtree) == -1) {
153 if(parse_face_vert(ptr, fv + 3, varr_size, tarr_size, narr_size)) {
154 proc_facevert(mesh, fv, varr, narr, tarr, rbtree);
155 proc_facevert(mesh, fv + 2, varr, narr, tarr, rbtree);
156 proc_facevert(mesh, fv + 3, varr, narr, tarr, rbtree);
162 if(mesh->num_verts) {
164 mesh->next = scn->meshlist;
165 scn->meshlist = mesh;
168 if(!(mesh = malloc(sizeof *mesh))) {
169 fprintf(stderr, "failed to allocate mesh\n");
177 if(memcmp(line, "mtllib", 6) == 0 && (line = cleanline(line + 6))) {
178 free_mtllist(scn->mtllist);
179 load_mtllib(scn, path_prefix, line);
184 if(memcmp(line, "usemtl", 6) == 0 && (line = cleanline(line + 6))) {
187 if(strcmp(mtl->name, line) == 0) {
200 if(mesh->num_verts) {
202 mesh->next = scn->meshlist;
203 scn->meshlist = mesh;
210 printf("load_scenefile %s: loaded %d meshes, %d vertices\n", scn->fname,
211 scn->num_meshes, varr_size);
229 static int proc_facevert(struct mesh *mesh, struct facevertex *fv,
230 cgm_vec3 *varr, cgm_vec3 *narr, cgm_vec2 *tarr, struct rbtree *rbtree)
233 unsigned int idx, newidx;
234 struct facevertex *newfv;
237 if((node = rb_find(rbtree, &fv))) {
238 idx = (unsigned int)node->data;
239 assert(idx < mesh->num_verts);
241 newidx = mesh->num_verts;
243 v.pos = varr[fv->vidx];
245 v.norm = narr[fv->nidx];
248 v.tex = tarr[fv->tidx];
250 add_mesh_vertex(mesh, &v);
251 add_mesh_index(mesh, newidx);
254 if((newfv = malloc(sizeof *newfv))) {
257 if(!newfv || rb_insert(rbtree, newfv, (void*)newidx) == -1) {
258 fprintf(stderr, "load_scenefile: failed to insert facevertex to rbtree\n");
265 void destroy_scenefile(struct scenefile *scn)
268 while(scn->meshlist) {
270 scn->meshlist = scn->meshlist->next;
275 static char *cleanline(char *s)
279 if((ptr = strchr(s, '#'))) *ptr = 0;
281 while(*s && isspace(*s)) s++;
282 ptr = s + strlen(s) - 1;
283 while(ptr >= s && isspace(*ptr)) *ptr-- = 0;
288 static char *parse_idx(char *ptr, int *idx, int arrsz)
291 int val = strtol(ptr, &endp, 10);
292 if(endp == ptr) return 0;
294 if(val < 0) { /* convert negative indices */
297 *idx = val - 1; /* indices in obj are 1-based */
302 /* possible face-vertex definitions:
306 * 4. vertex/texcoord/normal
308 static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn)
310 fv->tidx = fv->nidx = -1;
312 if(!(ptr = parse_idx(ptr, &fv->vidx, numv)))
314 if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
316 if(*++ptr == '/') { /* no texcoord */
319 if(!(ptr = parse_idx(ptr, &fv->tidx, numt)))
321 if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
325 if(!(ptr = parse_idx(ptr, &fv->nidx, numn)))
327 return (!*ptr || isspace(*ptr)) ? ptr : 0;
330 static int load_mtllib(struct scenefile *scn, const char *path_prefix, const char *mtlfname)
333 char buf[256], *line;
335 struct material *mtl = 0;
337 if(path_prefix && *path_prefix) {
338 sprintf(buf, "%s/%s", path_prefix, mtlfname);
340 strcpy(buf, mtlfname);
343 if(!(fp = fopen(buf, "rb"))) {
347 while(fgets(buf, sizeof buf, fp)) {
348 if(!(line = cleanline(buf))) {
352 if(memcmp(line, "newmtl", 6) == 0) {
354 conv_mtl(mtl, &om, path_prefix);
355 mtl->next = scn->mtllist;
358 mtl = calloc(1, sizeof *mtl);
360 memset(&om, 0, sizeof om);
362 if((line = cleanline(line + 6))) {
363 om.name = strdup(line);
366 } else if(memcmp(line, "Kd", 2) == 0) {
367 sscanf(line + 3, "%f %f %f", &om.kd.x, &om.kd.y, &om.kd.z);
368 } else if(memcmp(line, "Ks", 2) == 0) {
369 sscanf(line + 3, "%f %f %f", &om.ks.x, &om.ks.y, &om.ks.z);
370 } else if(memcmp(line, "Ke", 2) == 0) {
371 sscanf(line + 3, "%f %f %f", &om.ke.x, &om.ke.y, &om.ke.z);
372 } else if(memcmp(line, "Ni", 2) == 0) {
373 om.ior = atof(line + 3);
374 } else if(line[0] == 'd' && isspace(line[1])) {
375 om.alpha = atof(line + 2);
376 } else if(memcmp(line, "map_Kd", 6) == 0) {
377 if((line = cleanline(line + 6))) {
378 om.map_kd = strdup(line);
380 } else if(memcmp(line, "map_Ke", 6) == 0) {
381 if((line = cleanline(line + 6))) {
382 om.map_ke = strdup(line);
384 } else if(memcmp(line, "map_d", 5) == 0) {
385 if((line = cleanline(line + 5))) {
386 om.map_alpha = strdup(line);
392 conv_mtl(mtl, &om, path_prefix);
393 mtl->next = scn->mtllist;
401 static void free_mtllist(struct material *mtl)
410 static void conv_mtl(struct material *mm, struct objmtl *om, const char *path_prefix)
412 char *fname = 0, *suffix = 0;
413 int len, prefix_len, maxlen = 0;
415 memset(mm, 0, sizeof *mm);
419 mm->shininess = om->shin;
421 if(om->map_kd && (len = strlen(om->map_kd)) > maxlen) maxlen = len;
422 if(om->map_ke && (len = strlen(om->map_ke)) > maxlen) maxlen = len;
423 if(om->map_alpha && (len = strlen(om->map_alpha)) > maxlen) maxlen = len;
426 prefix_len = strlen(path_prefix);
427 fname = alloca(maxlen + prefix_len + 2);
428 suffix = fname + prefix_len;
429 strcpy(fname, path_prefix);
434 strcpy(suffix, om->map_kd);
435 mm->tex[TEX_DIFFUSE] = get_image(fname);
440 static int cmp_facevert(const void *ap, const void *bp)
442 const struct facevertex *a = ap;
443 const struct facevertex *b = bp;
445 if(a->vidx == b->vidx) {
446 if(a->tidx == b->tidx) {
447 return a->nidx - b->nidx;
449 return a->tidx - b->tidx;
451 return a->vidx - b->vidx;
454 static void free_rbnode_key(struct rbnode *n, void *cls)