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;
57 /* attempt to load it as gltf first */
58 if((g3dimpl_loadgltf(g, io)) == 0) {
61 io->seek(0, SEEK_SET, io->cls);
65 tsio.write = io->write;
67 if(!(tsroot = ts_load_io(&tsio))) {
68 goat3d_logmsg(LOG_ERROR, "failed to load scene\n");
71 if(strcmp(tsroot->name, "scene") != 0) {
72 goat3d_logmsg(LOG_ERROR, "invalid scene file, root node is not \"scene\"\n");
77 /* read all materials */
78 c = tsroot->child_list;
80 if(strcmp(c->name, "mtl") == 0) {
81 struct goat3d_material *mtl = read_material(g, c);
83 goat3d_add_mtl(g, mtl);
89 /* create placeholder nodes, only populating the name field, so that bone
90 * references in meshes can be resolved. We'll read the rest of the node
91 * info, including their mesh/light/camera references at the end.
93 c = tsroot->child_list;
95 if(strcmp(c->name, "node") == 0) {
96 struct goat3d_node *node;
98 if(!(node = goat3d_create_node())) {
99 goat3d_logmsg(LOG_ERROR, "failed to allocate node\n");
103 if((str = ts_get_attr_str(c, "name", 0))) {
104 goat3d_set_node_name(node, str);
106 goat3d_add_node(g, node);
111 /* read all meshes, cameras, lights, animations */
112 c = tsroot->child_list;
114 if(strcmp(c->name, "mesh") == 0) {
115 struct goat3d_mesh *mesh = read_mesh(g, c);
117 goat3d_add_mesh(g, mesh);
119 } else if(strcmp(c->name, "anim") == 0) {
126 /* now load the nodes properly */
128 c = tsroot->child_list;
130 if(strcmp(c->name, "node") == 0) {
131 struct goat3d_node *node = goat3d_get_node(g, idx++);
133 read_node(g, node, c);
138 ts_free_tree(tsroot);
142 int g3dimpl_anmload(struct goat3d *g, struct goat3d_io *io)
147 tsio.read = io->read;
148 tsio.write = io->write;
153 static struct goat3d_material *read_material(struct goat3d *g, struct ts_node *tsmtl)
155 struct goat3d_material *mtl;
156 struct material_attrib mattr, *arr;
160 if(!(mtl = malloc(sizeof *mtl)) || g3dimpl_mtl_init(mtl) == -1) {
162 goat3d_logmsg(LOG_ERROR, "read_material: failed to allocate material\n");
166 if(!(str = ts_get_attr_str(tsmtl, "name", 0)) || !*str) {
167 /* XXX wait, we can refer to materials by index, why is the name important? */
168 goat3d_logmsg(LOG_WARNING, "read_material: ignoring material without a name\n");
169 g3dimpl_mtl_destroy(mtl);
172 goat3d_set_mtl_name(mtl, str);
174 /* read all material attributes */
175 c = tsmtl->child_list;
177 if(strcmp(c->name, "attr") == 0) {
178 if(read_material_attrib(&mattr, c)) {
179 if(!(arr = dynarr_push(mtl->attrib, &mattr))) {
180 goat3d_logmsg(LOG_ERROR, "read_material: failed to resize material attribute array\n");
181 g3dimpl_mtl_destroy(mtl);
190 /*if(dynarr_empty(mtl->attrib)) {
191 goat3d_logmsg(LOG_WARNING, "read_material: ignoring empty material: %s\n", mtl->name);
192 g3dimpl_mtl_destroy(mtl);
198 static char *read_material_attrib(struct material_attrib *attr, struct ts_node *tsnode)
201 struct ts_attr *tsattr;
202 const char *name, *map;
204 memset(attr, 0, sizeof *attr);
206 if((tsattr = ts_get_attr(tsnode, "val"))) {
207 attr->value.w = 1.0f; /* default W to 1 if we get less than a float4 */
209 switch(tsattr->val.type) {
211 attr->value.x = tsattr->val.fnum;
214 assert(tsattr->val.vec_size <= 4);
215 for(i=0; i<tsattr->val.vec_size; i++) {
216 (&attr->value.x)[i] = tsattr->val.vec[i];
219 default: /* no valid val attribute found */
224 if(!(name = ts_get_attr_str(tsnode, "name", 0)) || !*name) {
227 if(!(attr->name = malloc(strlen(name) + 1))) {
228 goat3d_logmsg(LOG_ERROR, "read_material_attrib: failed to allocate name\n");
231 strcpy(attr->name, name);
233 if((map = ts_get_attr_str(tsnode, "map", 0)) && *map) {
234 if(!(attr->map = malloc(strlen(map) + 1))) {
235 goat3d_logmsg(LOG_ERROR, "read_material_attrib: failed to allocate map name\n");
239 strcpy(attr->map, map);
244 static struct goat3d_mesh *read_mesh(struct goat3d *g, struct ts_node *tsmesh)
246 struct goat3d_mesh *mesh;
247 struct goat3d_material *mtl;
253 if(!(mesh = malloc(sizeof *mesh)) || g3dimpl_obj_init((struct object*)mesh, OBJTYPE_MESH)) {
254 goat3d_logmsg(LOG_ERROR, "read_mesh: failed to allocate mesh\n");
258 if((str = ts_get_attr_str(tsmesh, "name", 0))) {
259 goat3d_set_mesh_name(mesh, str);
262 /* material reference */
263 if((num = ts_get_attr_num(tsmesh, "material", -1)) >= 0) {
264 if((mtl = goat3d_get_mtl(g, num))) {
265 goat3d_set_mesh_mtl(mesh, mtl);
267 goat3d_logmsg(LOG_WARNING, "read_mesh: mesh %s refers to invalid material: %d\n",
270 } else if((str = ts_get_attr_str(tsmesh, "material", 0))) {
271 if((mtl = goat3d_get_mtl_by_name(g, str))) {
272 goat3d_set_mesh_mtl(mesh, mtl);
274 goat3d_logmsg(LOG_WARNING, "read_mesh: mesh %s refers to invalid material: %s\n",
279 /* external mesh data */
280 if((str = ts_get_attr_str(tsmesh, "file", 0))) {
281 const char *fname = str;
285 pathbuf = alloca(strlen(str) + strlen(g->search_path) + 2);
286 sprintf(pathbuf, "%s/%s", g->search_path, str);
290 if(g3dimpl_loadmesh(mesh, fname) == -1) {
291 goat3d_logmsg(LOG_ERROR, "read_mesh: failed to load external mesh: %s\n", fname);
295 /* done loading, we can't have both an external mesh AND internal data,
296 * if there's anything else hanging under this tsnode, ignore it.
298 if(tsmesh->child_list) {
299 goat3d_logmsg(LOG_WARNING, "read_mesh: external mesh node also has unexpected children; ignoring.\n");
304 c = tsmesh->child_list;
306 if(strcmp(c->name, "vertex_list") == 0) {
307 if(!(tmp = read_veclist(mesh->vertices, 3, "vertex", "pos", c))) {
308 goat3d_logmsg(LOG_ERROR, "read_mesh: failed to read vertex array for mesh %s\n",
312 mesh->vertices = tmp;
314 } else if(strcmp(c->name, "normal_list") == 0) {
315 if(!(tmp = read_veclist(mesh->normals, 3, "normal", "dir", c))) {
316 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read normals array for mesh %s\n",
322 } else if(strcmp(c->name, "tangent_list") == 0) {
323 if(!(tmp = read_veclist(mesh->tangents, 3, "tangent", "dir", c))) {
324 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read tangents array for mesh %s\n",
327 mesh->tangents = tmp;
330 } else if(strcmp(c->name, "texcoord_list") == 0) {
331 if(!(tmp = read_veclist(mesh->texcoords, 2, "texcoord", "uv", c))) {
332 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read texcoord array for mesh %s\n",
335 mesh->texcoords = tmp;
338 } else if(strcmp(c->name, "skinweight_list") == 0) {
339 if(!(tmp = read_veclist(mesh->skin_weights, 4, "skinweight", "weights", c))) {
340 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read skin weights array for mesh %s\n",
343 mesh->skin_weights = tmp;
346 } else if(strcmp(c->name, "skinmatrix_list") == 0) {
347 if(!(tmp = read_intlist(mesh->skin_matrices, 4, "skinmatrix", "idx", c))) {
348 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read skin matrix index array for mesh %s\n",
351 mesh->skin_matrices = tmp;
354 } else if(strcmp(c->name, "color_list") == 0) {
355 if(!(tmp = read_veclist(mesh->colors, 4, "color", "color", c))) {
356 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read color array for mesh %s\n",
362 } else if(strcmp(c->name, "bone_list") == 0) {
363 if(!(tmp = read_bonelist(g, mesh->bones, c))) {
364 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read bones array for mesh %s\n",
370 } else if(strcmp(c->name, "face_list") == 0) {
371 if(!(tmp = read_intlist(mesh->faces, 3, "face", "idx", c))) {
372 goat3d_logmsg(LOG_ERROR, "read_mesh: failed to read faces array for mesh %s\n",
383 g3dimpl_obj_destroy((struct object*)mesh);
387 static void *read_veclist(void *arr, int dim, const char *nodename, const char *attrname, struct ts_node *tslist)
391 struct ts_attr *attr;
395 arr = dynarr_clear(arr);
398 if((size = ts_get_attr_int(tslist, "list_size", -1)) <= 0) {
399 goat3d_logmsg(LOG_WARNING, "read_veclist: list_size attribute missing or invalid\n");
403 if((str = ts_get_attr_str(tslist, "base64", 0))) {
404 if(size == -1) size = calc_b64_size(str);
405 if(!(arr = dynarr_resize(arr, size))) {
406 goat3d_logmsg(LOG_ERROR, "read_veclist: failed to resize %s array\n",
411 bufsz = size * dim * sizeof(float);
412 b64decode(str, arr, &bufsz);
414 goat3d_bswap32(arr, size * dim);
418 c = tslist->child_list;
420 if(strcmp(c->name, nodename) != 0) {
425 if((attr = ts_get_attr(c, attrname)) && attr->val.type == TS_VECTOR) {
426 for(i=0; i<dim; i++) {
427 if(i >= attr->val.vec_size) {
430 vec[i] = attr->val.vec[i];
434 if(!(arr = dynarr_push(arr, vec))) {
435 goat3d_logmsg(LOG_ERROR, "read_veclist: failed to resize %s array\n",
443 if(size > 0 && dynarr_size(arr) != size) {
444 goat3d_logmsg(LOG_WARNING, "read_veclist: expected %d items, read %d\n", size, dynarr_size(arr));
449 static void *read_intlist(void *arr, int dim, const char *nodename, const char *attrname, struct ts_node *tslist)
453 struct ts_attr *attr;
457 arr = dynarr_clear(arr);
460 if((size = ts_get_attr_int(tslist, "list_size", -1)) <= 0) {
461 goat3d_logmsg(LOG_WARNING, "read_intlist: list_size attribute missing or invalid\n");
465 if((str = ts_get_attr_str(tslist, "base64", 0))) {
466 if(size == -1) size = calc_b64_size(str);
467 if(!(arr = dynarr_resize(arr, size))) {
468 goat3d_logmsg(LOG_ERROR, "read_intlist: failed to resize %s array\n",
473 bufsz = size * dim * sizeof(int);
474 b64decode(str, arr, &bufsz);
476 goat3d_bswap32(arr, size * dim);
480 c = tslist->child_list;
482 if(strcmp(c->name, nodename) != 0) {
487 if((attr = ts_get_attr(c, attrname)) && attr->val.type == TS_VECTOR) {
488 for(i=0; i<dim; i++) {
489 if(i >= attr->val.vec_size) {
492 ivec[i] = attr->val.vec[i];
496 if(!(arr = dynarr_push(arr, ivec))) {
497 goat3d_logmsg(LOG_ERROR, "read_intlist: failed to resize %s array\n",
505 if(size > 0 && dynarr_size(arr) != size) {
506 goat3d_logmsg(LOG_WARNING, "read_intlist: expected %d items, read %d\n", size, dynarr_size(arr));
511 static void *read_bonelist(struct goat3d *g, struct goat3d_node **arr, struct ts_node *tslist)
515 struct goat3d_node *bone;
518 arr = dynarr_clear(arr);
521 if((size = ts_get_attr_int(tslist, "list_size", -1)) <= 0) {
522 goat3d_logmsg(LOG_WARNING, "read_bonelist: list_size attribute missing or invalid\n");
526 /* TODO base64 data support */
528 c = tslist->child_list;
530 if(strcmp(c->name, "bone") != 0) {
537 if((idx = ts_get_attr_int(c, "bone", -1)) >= 0) {
538 if(!(bone = goat3d_get_node(g, idx))) {
539 goat3d_logmsg(LOG_ERROR, "read_bonelist: reference to invalid bone: %d\n", idx);
543 } else if((str = ts_get_attr_str(c, "bone", 0))) {
544 if(!(bone = goat3d_get_node_by_name(g, str))) {
545 goat3d_logmsg(LOG_ERROR, "read_bonelist: reference to invalid bone: %s\n", str);
550 if(bone && !(arr = dynarr_push(arr, &bone))) {
551 goat3d_logmsg(LOG_ERROR, "read_bonelist: failed to resize bone array\n");
557 if(size > 0 && dynarr_size(arr) != size) {
558 goat3d_logmsg(LOG_WARNING, "read_bonelist: expected %d items, read %d\n", size, dynarr_size(arr));
563 #define GETREF(ptr, typestr, getname) \
566 if((idx = ts_get_attr_int(tsnode, typestr, -1)) >= 0) { \
567 if(!(ptr = goat3d_get_##getname(g, idx))) { \
568 goat3d_logmsg(LOG_ERROR, "read_node: ignoring reference to invalid " typestr ": %d\n", idx); \
570 } else if((str = ts_get_attr_str(tsnode, typestr, 0))) { \
571 if(!(ptr = goat3d_get_##getname##_by_name(g, str))) { \
572 goat3d_logmsg(LOG_ERROR, "read_node: ignoring reference to invalid " typestr ": %s\n", str); \
577 static int read_node(struct goat3d *g, struct goat3d_node *node, struct ts_node *tsnode)
581 struct goat3d_node *parent;
584 GETREF(parent, "parent", node);
586 goat3d_add_node_child(parent, node);
589 node->type = GOAT3D_NODE_MESH;
590 GETREF(node->obj, "mesh", mesh);
592 node->type = GOAT3D_NODE_LIGHT;
593 GETREF(node->obj, "light", light);
596 node->type = GOAT3D_NODE_CAMERA;
597 GETREF(node->obj, "camera", camera);
600 node->type = GOAT3D_NODE_NULL;
603 if((vec = ts_get_attr_vec(tsnode, "pos", 0))) {
604 goat3d_set_node_position(node, vec[0], vec[1], vec[2]);
606 if((vec = ts_get_attr_vec(tsnode, "rot", 0))) {
607 goat3d_set_node_rotation(node, vec[0], vec[1], vec[2], vec[3]);
609 if((vec = ts_get_attr_vec(tsnode, "scale", 0))) {
610 goat3d_set_node_scaling(node, vec[0], vec[1], vec[2]);
612 if((vec = ts_get_attr_vec(tsnode, "pivot", 0))) {
613 goat3d_set_node_pivot(node, vec[0], vec[1], vec[2]);
619 static int read_anim(struct goat3d *g, struct ts_node *tsanim)
623 struct goat3d_anim *anim;
624 struct goat3d_track *trk;
626 if(!(str = ts_get_attr_str(tsanim, "name", 0))) {
627 goat3d_logmsg(LOG_WARNING, "read_anim: ignoring animation without a name\n");
631 if(!(anim = goat3d_create_anim())) {
632 goat3d_logmsg(LOG_ERROR, "read_anim: failed to initialize animation: %s\n", str);
635 goat3d_set_anim_name(anim, str);
637 c = tsanim->child_list;
639 if(strcmp(c->name, "track") == 0) {
640 if(!(trk = read_track(g, c))) {
644 goat3d_add_anim_track(anim, trk);
649 goat3d_add_anim(g, anim);
653 static int parsetype(const char *str)
655 if(strcmp(str, "pos") == 0) return GOAT3D_TRACK_POS;
656 if(strcmp(str, "rot") == 0) return GOAT3D_TRACK_ROT;
657 if(strcmp(str, "scale") == 0) return GOAT3D_TRACK_SCALE;
658 if(strcmp(str, "val") == 0) return GOAT3D_TRACK_VAL;
659 if(strcmp(str, "vec3") == 0) return GOAT3D_TRACK_VEC3;
660 if(strcmp(str, "vec4") == 0) return GOAT3D_TRACK_VEC4;
661 if(strcmp(str, "quat") == 0) return GOAT3D_TRACK_QUAT;
665 static int parseinterp(const char *str)
667 if(strcmp(str, "step") == 0) return GOAT3D_INTERP_STEP;
668 if(strcmp(str, "linear") == 0) return GOAT3D_INTERP_LINEAR;
669 if(strcmp(str, "cubic") == 0) return GOAT3D_INTERP_CUBIC;
673 static int parseextrap(const char *str)
675 if(strcmp(str, "extend") == 0) return GOAT3D_EXTRAP_EXTEND;
676 if(strcmp(str, "clamp") == 0) return GOAT3D_EXTRAP_CLAMP;
677 if(strcmp(str, "repeat") == 0) return GOAT3D_EXTRAP_REPEAT;
678 if(strcmp(str, "pingpong") == 0) return GOAT3D_EXTRAP_PINGPONG;
682 static struct goat3d_track *read_track(struct goat3d *g, struct ts_node *tstrk)
686 struct goat3d_track *trk;
687 struct goat3d_node *node;
688 struct goat3d_key key;
691 struct ts_attr *tsattr;
693 if(!(str = ts_get_attr_str(tstrk, "type", 0)) || (type = parsetype(str)) == -1) {
694 goat3d_logmsg(LOG_WARNING, "read_track: ignoring track with missing or invalid type attribute\n");
698 if((idx = ts_get_attr_int(tstrk, "node", -1)) >= 0) {
699 if(!(node = goat3d_get_node(g, idx))) {
700 goat3d_logmsg(LOG_WARNING, "read_track: ignoring track with invalid node reference (%d)\n", idx);
703 } else if((str = ts_get_attr_str(tstrk, "node", 0))) {
704 if(!(node = goat3d_get_node_by_name(g, str))) {
705 goat3d_logmsg(LOG_WARNING, "read_track: ignoring track with invalid node reference (%s)\n", str);
709 goat3d_logmsg(LOG_WARNING, "read_track: ignoring track with missing node reference\n");
713 if(!(trk = goat3d_create_track())) {
714 goat3d_logmsg(LOG_ERROR, "read_track: failed to create new keyframe track\n");
717 goat3d_set_track_node(trk, node);
718 goat3d_set_track_type(trk, type);
720 if((str = ts_get_attr_str(tstrk, "name", 0))) {
721 goat3d_set_track_name(trk, str);
724 if((str = ts_get_attr_str(tstrk, "interp", 0))) {
725 if((in = parseinterp(str)) == -1) {
726 goat3d_logmsg(LOG_WARNING, "read_track: ignoring invalid interpolation mode: %s\n", str);
728 goat3d_set_track_interp(trk, in);
731 if((str = ts_get_attr_str(tstrk, "extrap", 0))) {
732 if((ex = parseextrap(str)) == -1) {
733 goat3d_logmsg(LOG_WARNING, "read_track: ignoring invalid extrapolation mode: %s\n", str);
735 goat3d_set_track_extrap(trk, ex);
739 c = tstrk->child_list;
741 if(strcmp(c->name, "key") == 0) {
742 if((key.tm = ts_get_attr_int(c, "time", INT_MIN)) == INT_MIN) {
743 goat3d_logmsg(LOG_WARNING, "read_track: ignoring keyframe with missing or invalid time (%s)\n",
744 ts_get_attr_str(c, "time", ""));
748 if(!(tsattr = ts_get_attr(c, "value")) || (tsattr->val.type != TS_NUMBER &&
749 tsattr->val.type != TS_VECTOR)) {
750 goat3d_logmsg(LOG_WARNING, "read_track: ignoring keyframe with missing or invalid value (%s)\n",
751 ts_get_attr_str(c, "value", ""));
756 if(tsattr->val.type == TS_NUMBER) {
757 key.val[0] = tsattr->val.fnum;
760 if(i < tsattr->val.vec_size) {
761 key.val[i] = tsattr->val.vec[i];
767 goat3d_set_track_key(trk, &key);
772 /* force lazy re-sorting of keyframes if necessary */
773 goat3d_get_track_key(trk, 0, &key);