2 goat3d - 3D scene, and animation file format library.
3 Copyright (C) 2013-2023 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #if defined(__WATCOMC__) || defined(_WIN32) || defined(__DJGPP__)
39 static struct goat3d_material *read_material(struct goat3d *g, struct ts_node *tsmtl);
40 static char *read_material_attrib(struct material_attrib *attr, struct ts_node *tsmattr);
41 static struct goat3d_mesh *read_mesh(struct goat3d *g, struct ts_node *tsmesh);
42 static void *read_veclist(void *arr, int dim, const char *nodename, const char *attrname, struct ts_node *tsnode);
43 static void *read_intlist(void *arr, int dim, const char *nodename, const char *attrname, struct ts_node *tsnode);
44 static void *read_bonelist(struct goat3d *g, struct goat3d_node **arr, struct ts_node *tsnode);
45 static int read_node(struct goat3d *g, struct goat3d_node *node, struct ts_node *tsnode);
46 static int read_anim(struct goat3d *g, struct ts_node *tsanim);
47 static struct goat3d_track *read_track(struct goat3d *g, struct ts_node *tstrk);
50 int g3dimpl_scnload(struct goat3d *g, struct goat3d_io *io)
54 struct ts_node *tsroot, *c;
59 tsio.write = io->write;
61 if(!(tsroot = ts_load_io(&tsio))) {
62 goat3d_logmsg(LOG_ERROR, "failed to load scene\n");
65 if(strcmp(tsroot->name, "scene") != 0) {
66 goat3d_logmsg(LOG_ERROR, "invalid scene file, root node is not \"scene\"\n");
71 /* read all materials */
72 c = tsroot->child_list;
74 if(strcmp(c->name, "mtl") == 0) {
75 struct goat3d_material *mtl = read_material(g, c);
77 goat3d_add_mtl(g, mtl);
83 /* create placeholder nodes, only populating the name field, so that bone
84 * references in meshes can be resolved. We'll read the rest of the node
85 * info, including their mesh/light/camera references at the end.
87 c = tsroot->child_list;
89 if(strcmp(c->name, "node") == 0) {
90 struct goat3d_node *node;
92 if(!(node = goat3d_create_node())) {
93 goat3d_logmsg(LOG_ERROR, "failed to allocate node\n");
97 if((str = ts_get_attr_str(c, "name", 0))) {
98 goat3d_set_node_name(node, str);
100 goat3d_add_node(g, node);
105 /* read all meshes, cameras, lights, animations */
106 c = tsroot->child_list;
108 if(strcmp(c->name, "mesh") == 0) {
109 struct goat3d_mesh *mesh = read_mesh(g, c);
111 goat3d_add_mesh(g, mesh);
113 } else if(strcmp(c->name, "anim") == 0) {
120 /* now load the nodes properly */
122 c = tsroot->child_list;
124 if(strcmp(c->name, "node") == 0) {
125 struct goat3d_node *node = goat3d_get_node(g, idx++);
127 read_node(g, node, c);
132 ts_free_tree(tsroot);
136 int g3dimpl_anmload(struct goat3d *g, struct goat3d_io *io)
141 tsio.read = io->read;
142 tsio.write = io->write;
147 static struct goat3d_material *read_material(struct goat3d *g, struct ts_node *tsmtl)
149 struct goat3d_material *mtl;
150 struct material_attrib mattr, *arr;
154 if(!(mtl = malloc(sizeof *mtl)) || g3dimpl_mtl_init(mtl) == -1) {
156 goat3d_logmsg(LOG_ERROR, "read_material: failed to allocate material\n");
160 if(!(str = ts_get_attr_str(tsmtl, "name", 0)) || !*str) {
161 /* XXX wait, we can refer to materials by index, why is the name important? */
162 goat3d_logmsg(LOG_WARNING, "read_material: ignoring material without a name\n");
163 g3dimpl_mtl_destroy(mtl);
166 goat3d_set_mtl_name(mtl, str);
168 /* read all material attributes */
169 c = tsmtl->child_list;
171 if(strcmp(c->name, "attr") == 0) {
172 if(read_material_attrib(&mattr, c)) {
173 if(!(arr = dynarr_push(mtl->attrib, &mattr))) {
174 goat3d_logmsg(LOG_ERROR, "read_material: failed to resize material attribute array\n");
175 g3dimpl_mtl_destroy(mtl);
184 /*if(dynarr_empty(mtl->attrib)) {
185 goat3d_logmsg(LOG_WARNING, "read_material: ignoring empty material: %s\n", mtl->name);
186 g3dimpl_mtl_destroy(mtl);
192 static char *read_material_attrib(struct material_attrib *attr, struct ts_node *tsnode)
195 struct ts_attr *tsattr;
196 const char *name, *map;
198 memset(attr, 0, sizeof *attr);
200 if((tsattr = ts_get_attr(tsnode, "val"))) {
201 attr->value.w = 1.0f; /* default W to 1 if we get less than a float4 */
203 switch(tsattr->val.type) {
205 attr->value.x = tsattr->val.fnum;
208 assert(tsattr->val.vec_size <= 4);
209 for(i=0; i<tsattr->val.vec_size; i++) {
210 (&attr->value.x)[i] = tsattr->val.vec[i];
213 default: /* no valid val attribute found */
218 if(!(name = ts_get_attr_str(tsnode, "name", 0)) || !*name) {
221 if(!(attr->name = malloc(strlen(name) + 1))) {
222 goat3d_logmsg(LOG_ERROR, "read_material_attrib: failed to allocate name\n");
225 strcpy(attr->name, name);
227 if((map = ts_get_attr_str(tsnode, "map", 0)) && *map) {
228 if(!(attr->map = malloc(strlen(map) + 1))) {
229 goat3d_logmsg(LOG_ERROR, "read_material_attrib: failed to allocate map name\n");
233 strcpy(attr->map, map);
238 static struct goat3d_mesh *read_mesh(struct goat3d *g, struct ts_node *tsmesh)
240 struct goat3d_mesh *mesh;
241 struct goat3d_material *mtl;
247 if(!(mesh = malloc(sizeof *mesh)) || g3dimpl_obj_init((struct object*)mesh, OBJTYPE_MESH)) {
248 goat3d_logmsg(LOG_ERROR, "read_mesh: failed to allocate mesh\n");
252 if((str = ts_get_attr_str(tsmesh, "name", 0))) {
253 goat3d_set_mesh_name(mesh, str);
256 /* material reference */
257 if((num = ts_get_attr_num(tsmesh, "material", -1)) >= 0) {
258 if((mtl = goat3d_get_mtl(g, num))) {
259 goat3d_set_mesh_mtl(mesh, mtl);
261 goat3d_logmsg(LOG_WARNING, "read_mesh: mesh %s refers to invalid material: %d\n",
264 } else if((str = ts_get_attr_str(tsmesh, "material", 0))) {
265 if((mtl = goat3d_get_mtl_by_name(g, str))) {
266 goat3d_set_mesh_mtl(mesh, mtl);
268 goat3d_logmsg(LOG_WARNING, "read_mesh: mesh %s refers to invalid material: %s\n",
273 /* external mesh data */
274 if((str = ts_get_attr_str(tsmesh, "file", 0))) {
275 const char *fname = str;
279 pathbuf = alloca(strlen(str) + strlen(g->search_path) + 2);
280 sprintf(pathbuf, "%s/%s", g->search_path, str);
284 if(g3dimpl_loadmesh(mesh, fname) == -1) {
285 goat3d_logmsg(LOG_ERROR, "read_mesh: failed to load external mesh: %s\n", fname);
289 /* done loading, we can't have both an external mesh AND internal data,
290 * if there's anything else hanging under this tsnode, ignore it.
292 if(tsmesh->child_list) {
293 goat3d_logmsg(LOG_WARNING, "read_mesh: external mesh node also has unexpected children; ignoring.\n");
298 c = tsmesh->child_list;
300 if(strcmp(c->name, "vertex_list") == 0) {
301 if(!(tmp = read_veclist(mesh->vertices, 3, "vertex", "pos", c))) {
302 goat3d_logmsg(LOG_ERROR, "read_mesh: failed to read vertex array for mesh %s\n",
306 mesh->vertices = tmp;
308 } else if(strcmp(c->name, "normal_list") == 0) {
309 if(!(tmp = read_veclist(mesh->normals, 3, "normal", "dir", c))) {
310 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read normals array for mesh %s\n",
316 } else if(strcmp(c->name, "tangent_list") == 0) {
317 if(!(tmp = read_veclist(mesh->tangents, 3, "tangent", "dir", c))) {
318 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read tangents array for mesh %s\n",
321 mesh->tangents = tmp;
324 } else if(strcmp(c->name, "texcoord_list") == 0) {
325 if(!(tmp = read_veclist(mesh->texcoords, 2, "texcoord", "uv", c))) {
326 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read texcoord array for mesh %s\n",
329 mesh->texcoords = tmp;
332 } else if(strcmp(c->name, "skinweight_list") == 0) {
333 if(!(tmp = read_veclist(mesh->skin_weights, 4, "skinweight", "weights", c))) {
334 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read skin weights array for mesh %s\n",
337 mesh->skin_weights = tmp;
340 } else if(strcmp(c->name, "skinmatrix_list") == 0) {
341 if(!(tmp = read_intlist(mesh->skin_matrices, 4, "skinmatrix", "idx", c))) {
342 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read skin matrix index array for mesh %s\n",
345 mesh->skin_matrices = tmp;
348 } else if(strcmp(c->name, "color_list") == 0) {
349 if(!(tmp = read_veclist(mesh->colors, 4, "color", "color", c))) {
350 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read color array for mesh %s\n",
356 } else if(strcmp(c->name, "bone_list") == 0) {
357 if(!(tmp = read_bonelist(g, mesh->bones, c))) {
358 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read bones array for mesh %s\n",
364 } else if(strcmp(c->name, "face_list") == 0) {
365 if(!(tmp = read_intlist(mesh->faces, 3, "face", "idx", c))) {
366 goat3d_logmsg(LOG_ERROR, "read_mesh: failed to read faces array for mesh %s\n",
377 g3dimpl_obj_destroy((struct object*)mesh);
381 static void *read_veclist(void *arr, int dim, const char *nodename, const char *attrname, struct ts_node *tslist)
385 struct ts_attr *attr;
389 arr = dynarr_clear(arr);
392 if((size = ts_get_attr_int(tslist, "list_size", -1)) <= 0) {
393 goat3d_logmsg(LOG_WARNING, "read_veclist: list_size attribute missing or invalid\n");
397 if((str = ts_get_attr_str(tslist, "base64", 0))) {
398 if(size == -1) size = calc_b64_size(str);
399 if(!(arr = dynarr_resize(arr, size))) {
400 goat3d_logmsg(LOG_ERROR, "read_veclist: failed to resize %s array\n",
405 bufsz = size * dim * sizeof(float);
406 b64decode(str, arr, &bufsz);
408 goat3d_bswap32(arr, size * dim);
412 c = tslist->child_list;
414 if(strcmp(c->name, nodename) != 0) {
419 if((attr = ts_get_attr(c, attrname)) && attr->val.type == TS_VECTOR) {
420 for(i=0; i<dim; i++) {
421 if(i >= attr->val.vec_size) {
424 vec[i] = attr->val.vec[i];
428 if(!(arr = dynarr_push(arr, vec))) {
429 goat3d_logmsg(LOG_ERROR, "read_veclist: failed to resize %s array\n",
437 if(size > 0 && dynarr_size(arr) != size) {
438 goat3d_logmsg(LOG_WARNING, "read_veclist: expected %d items, read %d\n", size, dynarr_size(arr));
443 static void *read_intlist(void *arr, int dim, const char *nodename, const char *attrname, struct ts_node *tslist)
447 struct ts_attr *attr;
451 arr = dynarr_clear(arr);
454 if((size = ts_get_attr_int(tslist, "list_size", -1)) <= 0) {
455 goat3d_logmsg(LOG_WARNING, "read_intlist: list_size attribute missing or invalid\n");
459 if((str = ts_get_attr_str(tslist, "base64", 0))) {
460 if(size == -1) size = calc_b64_size(str);
461 if(!(arr = dynarr_resize(arr, size))) {
462 goat3d_logmsg(LOG_ERROR, "read_intlist: failed to resize %s array\n",
467 bufsz = size * dim * sizeof(int);
468 b64decode(str, arr, &bufsz);
470 goat3d_bswap32(arr, size * dim);
474 c = tslist->child_list;
476 if(strcmp(c->name, nodename) != 0) {
481 if((attr = ts_get_attr(c, attrname)) && attr->val.type == TS_VECTOR) {
482 for(i=0; i<dim; i++) {
483 if(i >= attr->val.vec_size) {
486 ivec[i] = attr->val.vec[i];
490 if(!(arr = dynarr_push(arr, ivec))) {
491 goat3d_logmsg(LOG_ERROR, "read_intlist: failed to resize %s array\n",
499 if(size > 0 && dynarr_size(arr) != size) {
500 goat3d_logmsg(LOG_WARNING, "read_intlist: expected %d items, read %d\n", size, dynarr_size(arr));
505 static void *read_bonelist(struct goat3d *g, struct goat3d_node **arr, struct ts_node *tslist)
509 struct goat3d_node *bone;
512 arr = dynarr_clear(arr);
515 if((size = ts_get_attr_int(tslist, "list_size", -1)) <= 0) {
516 goat3d_logmsg(LOG_WARNING, "read_bonelist: list_size attribute missing or invalid\n");
520 /* TODO base64 data support */
522 c = tslist->child_list;
524 if(strcmp(c->name, "bone") != 0) {
531 if((idx = ts_get_attr_int(c, "bone", -1)) >= 0) {
532 if(!(bone = goat3d_get_node(g, idx))) {
533 goat3d_logmsg(LOG_ERROR, "read_bonelist: reference to invalid bone: %d\n", idx);
537 } else if((str = ts_get_attr_str(c, "bone", 0))) {
538 if(!(bone = goat3d_get_node_by_name(g, str))) {
539 goat3d_logmsg(LOG_ERROR, "read_bonelist: reference to invalid bone: %s\n", str);
544 if(bone && !(arr = dynarr_push(arr, &bone))) {
545 goat3d_logmsg(LOG_ERROR, "read_bonelist: failed to resize bone array\n");
551 if(size > 0 && dynarr_size(arr) != size) {
552 goat3d_logmsg(LOG_WARNING, "read_bonelist: expected %d items, read %d\n", size, dynarr_size(arr));
557 #define GETREF(ptr, typestr, getname) \
560 if((idx = ts_get_attr_int(tsnode, typestr, -1)) >= 0) { \
561 if(!(ptr = goat3d_get_##getname(g, idx))) { \
562 goat3d_logmsg(LOG_ERROR, "read_node: ignoring reference to invalid " typestr ": %d\n", idx); \
564 } else if((str = ts_get_attr_str(tsnode, typestr, 0))) { \
565 if(!(ptr = goat3d_get_##getname##_by_name(g, str))) { \
566 goat3d_logmsg(LOG_ERROR, "read_node: ignoring reference to invalid " typestr ": %s\n", str); \
571 static int read_node(struct goat3d *g, struct goat3d_node *node, struct ts_node *tsnode)
575 struct goat3d_node *parent;
578 GETREF(parent, "parent", node);
580 goat3d_add_node_child(parent, node);
583 node->type = GOAT3D_NODE_MESH;
584 GETREF(node->obj, "mesh", mesh);
586 node->type = GOAT3D_NODE_LIGHT;
587 GETREF(node->obj, "light", light);
590 node->type = GOAT3D_NODE_CAMERA;
591 GETREF(node->obj, "camera", camera);
594 node->type = GOAT3D_NODE_NULL;
597 if((vec = ts_get_attr_vec(tsnode, "pos", 0))) {
598 goat3d_set_node_position(node, vec[0], vec[1], vec[2]);
600 if((vec = ts_get_attr_vec(tsnode, "rot", 0))) {
601 goat3d_set_node_rotation(node, vec[0], vec[1], vec[2], vec[3]);
603 if((vec = ts_get_attr_vec(tsnode, "scale", 0))) {
604 goat3d_set_node_scaling(node, vec[0], vec[1], vec[2]);
606 if((vec = ts_get_attr_vec(tsnode, "pivot", 0))) {
607 goat3d_set_node_pivot(node, vec[0], vec[1], vec[2]);
613 static int read_anim(struct goat3d *g, struct ts_node *tsanim)
617 struct goat3d_anim *anim;
618 struct goat3d_track *trk;
620 if(!(str = ts_get_attr_str(tsanim, "name", 0))) {
621 goat3d_logmsg(LOG_WARNING, "read_anim: ignoring animation without a name\n");
625 if(!(anim = goat3d_create_anim())) {
626 goat3d_logmsg(LOG_ERROR, "read_anim: failed to initialize animation: %s\n", str);
629 goat3d_set_anim_name(anim, str);
631 c = tsanim->child_list;
633 if(strcmp(c->name, "track") == 0) {
634 if(!(trk = read_track(g, c))) {
638 goat3d_add_anim_track(anim, trk);
643 goat3d_add_anim(g, anim);
647 static int parsetype(const char *str)
649 if(strcmp(str, "pos") == 0) return GOAT3D_TRACK_POS;
650 if(strcmp(str, "rot") == 0) return GOAT3D_TRACK_ROT;
651 if(strcmp(str, "scale") == 0) return GOAT3D_TRACK_SCALE;
652 if(strcmp(str, "val") == 0) return GOAT3D_TRACK_VAL;
653 if(strcmp(str, "vec3") == 0) return GOAT3D_TRACK_VEC3;
654 if(strcmp(str, "vec4") == 0) return GOAT3D_TRACK_VEC4;
655 if(strcmp(str, "quat") == 0) return GOAT3D_TRACK_QUAT;
659 static int parseinterp(const char *str)
661 if(strcmp(str, "step") == 0) return GOAT3D_INTERP_STEP;
662 if(strcmp(str, "linear") == 0) return GOAT3D_INTERP_LINEAR;
663 if(strcmp(str, "cubic") == 0) return GOAT3D_INTERP_CUBIC;
667 static int parseextrap(const char *str)
669 if(strcmp(str, "extend") == 0) return GOAT3D_EXTRAP_EXTEND;
670 if(strcmp(str, "clamp") == 0) return GOAT3D_EXTRAP_CLAMP;
671 if(strcmp(str, "repeat") == 0) return GOAT3D_EXTRAP_REPEAT;
672 if(strcmp(str, "pingpong") == 0) return GOAT3D_EXTRAP_PINGPONG;
676 static struct goat3d_track *read_track(struct goat3d *g, struct ts_node *tstrk)
680 struct goat3d_track *trk;
681 struct goat3d_node *node;
682 struct goat3d_key key;
685 struct ts_attr *tsattr;
687 if(!(str = ts_get_attr_str(tstrk, "type", 0)) || (type = parsetype(str)) == -1) {
688 goat3d_logmsg(LOG_WARNING, "read_track: ignoring track with missing or invalid type attribute\n");
692 if((idx = ts_get_attr_int(tstrk, "node", -1)) >= 0) {
693 if(!(node = goat3d_get_node(g, idx))) {
694 goat3d_logmsg(LOG_WARNING, "read_track: ignoring track with invalid node reference (%d)\n", idx);
697 } else if((str = ts_get_attr_str(tstrk, "node", 0))) {
698 if(!(node = goat3d_get_node_by_name(g, str))) {
699 goat3d_logmsg(LOG_WARNING, "read_track: ignoring track with invalid node reference (%s)\n", str);
703 goat3d_logmsg(LOG_WARNING, "read_track: ignoring track with missing node reference\n");
707 if(!(trk = goat3d_create_track())) {
708 goat3d_logmsg(LOG_ERROR, "read_track: failed to create new keyframe track\n");
711 goat3d_set_track_node(trk, node);
712 goat3d_set_track_type(trk, type);
714 if((str = ts_get_attr_str(tstrk, "name", 0))) {
715 goat3d_set_track_name(trk, str);
718 if((str = ts_get_attr_str(tstrk, "interp", 0))) {
719 if((in = parseinterp(str)) == -1) {
720 goat3d_logmsg(LOG_WARNING, "read_track: ignoring invalid interpolation mode: %s\n", str);
722 goat3d_set_track_interp(trk, in);
725 if((str = ts_get_attr_str(tstrk, "extrap", 0))) {
726 if((ex = parseextrap(str)) == -1) {
727 goat3d_logmsg(LOG_WARNING, "read_track: ignoring invalid extrapolation mode: %s\n", str);
729 goat3d_set_track_extrap(trk, ex);
733 c = tstrk->child_list;
735 if(strcmp(c->name, "key") == 0) {
736 if((key.tm = ts_get_attr_int(c, "time", INT_MIN)) == INT_MIN) {
737 goat3d_logmsg(LOG_WARNING, "read_track: ignoring keyframe with missing or invalid time (%s)\n",
738 ts_get_attr_str(c, "time", ""));
742 if(!(tsattr = ts_get_attr(c, "value")) || (tsattr->val.type != TS_NUMBER &&
743 tsattr->val.type != TS_VECTOR)) {
744 goat3d_logmsg(LOG_WARNING, "read_track: ignoring keyframe with missing or invalid value (%s)\n",
745 ts_get_attr_str(c, "value", ""));
750 if(tsattr->val.type == TS_NUMBER) {
751 key.val[0] = tsattr->val.fnum;
754 if(i < tsattr->val.vec_size) {
755 key.val[i] = tsattr->val.vec[i];
761 goat3d_set_track_key(trk, &key);
766 /* force lazy re-sorting of keyframes if necessary */
767 goat3d_get_track_key(trk, 0, &key);