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) {
64 tsio.write = io->write;
66 if(!(tsroot = ts_load_io(&tsio))) {
67 goat3d_logmsg(LOG_ERROR, "failed to load scene\n");
70 if(strcmp(tsroot->name, "scene") != 0) {
71 goat3d_logmsg(LOG_ERROR, "invalid scene file, root node is not \"scene\"\n");
76 /* read all materials */
77 c = tsroot->child_list;
79 if(strcmp(c->name, "mtl") == 0) {
80 struct goat3d_material *mtl = read_material(g, c);
82 goat3d_add_mtl(g, mtl);
88 /* create placeholder nodes, only populating the name field, so that bone
89 * references in meshes can be resolved. We'll read the rest of the node
90 * info, including their mesh/light/camera references at the end.
92 c = tsroot->child_list;
94 if(strcmp(c->name, "node") == 0) {
95 struct goat3d_node *node;
97 if(!(node = goat3d_create_node())) {
98 goat3d_logmsg(LOG_ERROR, "failed to allocate node\n");
102 if((str = ts_get_attr_str(c, "name", 0))) {
103 goat3d_set_node_name(node, str);
105 goat3d_add_node(g, node);
110 /* read all meshes, cameras, lights, animations */
111 c = tsroot->child_list;
113 if(strcmp(c->name, "mesh") == 0) {
114 struct goat3d_mesh *mesh = read_mesh(g, c);
116 goat3d_add_mesh(g, mesh);
118 } else if(strcmp(c->name, "anim") == 0) {
125 /* now load the nodes properly */
127 c = tsroot->child_list;
129 if(strcmp(c->name, "node") == 0) {
130 struct goat3d_node *node = goat3d_get_node(g, idx++);
132 read_node(g, node, c);
137 ts_free_tree(tsroot);
141 int g3dimpl_anmload(struct goat3d *g, struct goat3d_io *io)
146 tsio.read = io->read;
147 tsio.write = io->write;
152 static struct goat3d_material *read_material(struct goat3d *g, struct ts_node *tsmtl)
154 struct goat3d_material *mtl;
155 struct material_attrib mattr, *arr;
159 if(!(mtl = malloc(sizeof *mtl)) || g3dimpl_mtl_init(mtl) == -1) {
161 goat3d_logmsg(LOG_ERROR, "read_material: failed to allocate material\n");
165 if(!(str = ts_get_attr_str(tsmtl, "name", 0)) || !*str) {
166 /* XXX wait, we can refer to materials by index, why is the name important? */
167 goat3d_logmsg(LOG_WARNING, "read_material: ignoring material without a name\n");
168 g3dimpl_mtl_destroy(mtl);
171 goat3d_set_mtl_name(mtl, str);
173 /* read all material attributes */
174 c = tsmtl->child_list;
176 if(strcmp(c->name, "attr") == 0) {
177 if(read_material_attrib(&mattr, c)) {
178 if(!(arr = dynarr_push(mtl->attrib, &mattr))) {
179 goat3d_logmsg(LOG_ERROR, "read_material: failed to resize material attribute array\n");
180 g3dimpl_mtl_destroy(mtl);
189 /*if(dynarr_empty(mtl->attrib)) {
190 goat3d_logmsg(LOG_WARNING, "read_material: ignoring empty material: %s\n", mtl->name);
191 g3dimpl_mtl_destroy(mtl);
197 static char *read_material_attrib(struct material_attrib *attr, struct ts_node *tsnode)
200 struct ts_attr *tsattr;
201 const char *name, *map;
203 memset(attr, 0, sizeof *attr);
205 if((tsattr = ts_get_attr(tsnode, "val"))) {
206 attr->value.w = 1.0f; /* default W to 1 if we get less than a float4 */
208 switch(tsattr->val.type) {
210 attr->value.x = tsattr->val.fnum;
213 assert(tsattr->val.vec_size <= 4);
214 for(i=0; i<tsattr->val.vec_size; i++) {
215 (&attr->value.x)[i] = tsattr->val.vec[i];
218 default: /* no valid val attribute found */
223 if(!(name = ts_get_attr_str(tsnode, "name", 0)) || !*name) {
226 if(!(attr->name = malloc(strlen(name) + 1))) {
227 goat3d_logmsg(LOG_ERROR, "read_material_attrib: failed to allocate name\n");
230 strcpy(attr->name, name);
232 if((map = ts_get_attr_str(tsnode, "map", 0)) && *map) {
233 if(!(attr->map = malloc(strlen(map) + 1))) {
234 goat3d_logmsg(LOG_ERROR, "read_material_attrib: failed to allocate map name\n");
238 strcpy(attr->map, map);
243 static struct goat3d_mesh *read_mesh(struct goat3d *g, struct ts_node *tsmesh)
245 struct goat3d_mesh *mesh;
246 struct goat3d_material *mtl;
252 if(!(mesh = malloc(sizeof *mesh)) || g3dimpl_obj_init((struct object*)mesh, OBJTYPE_MESH)) {
253 goat3d_logmsg(LOG_ERROR, "read_mesh: failed to allocate mesh\n");
257 if((str = ts_get_attr_str(tsmesh, "name", 0))) {
258 goat3d_set_mesh_name(mesh, str);
261 /* material reference */
262 if((num = ts_get_attr_num(tsmesh, "material", -1)) >= 0) {
263 if((mtl = goat3d_get_mtl(g, num))) {
264 goat3d_set_mesh_mtl(mesh, mtl);
266 goat3d_logmsg(LOG_WARNING, "read_mesh: mesh %s refers to invalid material: %d\n",
269 } else if((str = ts_get_attr_str(tsmesh, "material", 0))) {
270 if((mtl = goat3d_get_mtl_by_name(g, str))) {
271 goat3d_set_mesh_mtl(mesh, mtl);
273 goat3d_logmsg(LOG_WARNING, "read_mesh: mesh %s refers to invalid material: %s\n",
278 /* external mesh data */
279 if((str = ts_get_attr_str(tsmesh, "file", 0))) {
280 const char *fname = str;
284 pathbuf = alloca(strlen(str) + strlen(g->search_path) + 2);
285 sprintf(pathbuf, "%s/%s", g->search_path, str);
289 if(g3dimpl_loadmesh(mesh, fname) == -1) {
290 goat3d_logmsg(LOG_ERROR, "read_mesh: failed to load external mesh: %s\n", fname);
294 /* done loading, we can't have both an external mesh AND internal data,
295 * if there's anything else hanging under this tsnode, ignore it.
297 if(tsmesh->child_list) {
298 goat3d_logmsg(LOG_WARNING, "read_mesh: external mesh node also has unexpected children; ignoring.\n");
303 c = tsmesh->child_list;
305 if(strcmp(c->name, "vertex_list") == 0) {
306 if(!(tmp = read_veclist(mesh->vertices, 3, "vertex", "pos", c))) {
307 goat3d_logmsg(LOG_ERROR, "read_mesh: failed to read vertex array for mesh %s\n",
311 mesh->vertices = tmp;
313 } else if(strcmp(c->name, "normal_list") == 0) {
314 if(!(tmp = read_veclist(mesh->normals, 3, "normal", "dir", c))) {
315 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read normals array for mesh %s\n",
321 } else if(strcmp(c->name, "tangent_list") == 0) {
322 if(!(tmp = read_veclist(mesh->tangents, 3, "tangent", "dir", c))) {
323 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read tangents array for mesh %s\n",
326 mesh->tangents = tmp;
329 } else if(strcmp(c->name, "texcoord_list") == 0) {
330 if(!(tmp = read_veclist(mesh->texcoords, 2, "texcoord", "uv", c))) {
331 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read texcoord array for mesh %s\n",
334 mesh->texcoords = tmp;
337 } else if(strcmp(c->name, "skinweight_list") == 0) {
338 if(!(tmp = read_veclist(mesh->skin_weights, 4, "skinweight", "weights", c))) {
339 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read skin weights array for mesh %s\n",
342 mesh->skin_weights = tmp;
345 } else if(strcmp(c->name, "skinmatrix_list") == 0) {
346 if(!(tmp = read_intlist(mesh->skin_matrices, 4, "skinmatrix", "idx", c))) {
347 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read skin matrix index array for mesh %s\n",
350 mesh->skin_matrices = tmp;
353 } else if(strcmp(c->name, "color_list") == 0) {
354 if(!(tmp = read_veclist(mesh->colors, 4, "color", "color", c))) {
355 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read color array for mesh %s\n",
361 } else if(strcmp(c->name, "bone_list") == 0) {
362 if(!(tmp = read_bonelist(g, mesh->bones, c))) {
363 goat3d_logmsg(LOG_WARNING, "read_mesh: failed to read bones array for mesh %s\n",
369 } else if(strcmp(c->name, "face_list") == 0) {
370 if(!(tmp = read_intlist(mesh->faces, 3, "face", "idx", c))) {
371 goat3d_logmsg(LOG_ERROR, "read_mesh: failed to read faces array for mesh %s\n",
382 g3dimpl_obj_destroy((struct object*)mesh);
386 static void *read_veclist(void *arr, int dim, const char *nodename, const char *attrname, struct ts_node *tslist)
390 struct ts_attr *attr;
394 arr = dynarr_clear(arr);
397 if((size = ts_get_attr_int(tslist, "list_size", -1)) <= 0) {
398 goat3d_logmsg(LOG_WARNING, "read_veclist: list_size attribute missing or invalid\n");
402 if((str = ts_get_attr_str(tslist, "base64", 0))) {
403 if(size == -1) size = calc_b64_size(str);
404 if(!(arr = dynarr_resize(arr, size))) {
405 goat3d_logmsg(LOG_ERROR, "read_veclist: failed to resize %s array\n",
410 bufsz = size * dim * sizeof(float);
411 b64decode(str, arr, &bufsz);
413 goat3d_bswap32(arr, size * dim);
417 c = tslist->child_list;
419 if(strcmp(c->name, nodename) != 0) {
424 if((attr = ts_get_attr(c, attrname)) && attr->val.type == TS_VECTOR) {
425 for(i=0; i<dim; i++) {
426 if(i >= attr->val.vec_size) {
429 vec[i] = attr->val.vec[i];
433 if(!(arr = dynarr_push(arr, vec))) {
434 goat3d_logmsg(LOG_ERROR, "read_veclist: failed to resize %s array\n",
442 if(size > 0 && dynarr_size(arr) != size) {
443 goat3d_logmsg(LOG_WARNING, "read_veclist: expected %d items, read %d\n", size, dynarr_size(arr));
448 static void *read_intlist(void *arr, int dim, const char *nodename, const char *attrname, struct ts_node *tslist)
452 struct ts_attr *attr;
456 arr = dynarr_clear(arr);
459 if((size = ts_get_attr_int(tslist, "list_size", -1)) <= 0) {
460 goat3d_logmsg(LOG_WARNING, "read_intlist: list_size attribute missing or invalid\n");
464 if((str = ts_get_attr_str(tslist, "base64", 0))) {
465 if(size == -1) size = calc_b64_size(str);
466 if(!(arr = dynarr_resize(arr, size))) {
467 goat3d_logmsg(LOG_ERROR, "read_intlist: failed to resize %s array\n",
472 bufsz = size * dim * sizeof(int);
473 b64decode(str, arr, &bufsz);
475 goat3d_bswap32(arr, size * dim);
479 c = tslist->child_list;
481 if(strcmp(c->name, nodename) != 0) {
486 if((attr = ts_get_attr(c, attrname)) && attr->val.type == TS_VECTOR) {
487 for(i=0; i<dim; i++) {
488 if(i >= attr->val.vec_size) {
491 ivec[i] = attr->val.vec[i];
495 if(!(arr = dynarr_push(arr, ivec))) {
496 goat3d_logmsg(LOG_ERROR, "read_intlist: failed to resize %s array\n",
504 if(size > 0 && dynarr_size(arr) != size) {
505 goat3d_logmsg(LOG_WARNING, "read_intlist: expected %d items, read %d\n", size, dynarr_size(arr));
510 static void *read_bonelist(struct goat3d *g, struct goat3d_node **arr, struct ts_node *tslist)
514 struct goat3d_node *bone;
517 arr = dynarr_clear(arr);
520 if((size = ts_get_attr_int(tslist, "list_size", -1)) <= 0) {
521 goat3d_logmsg(LOG_WARNING, "read_bonelist: list_size attribute missing or invalid\n");
525 /* TODO base64 data support */
527 c = tslist->child_list;
529 if(strcmp(c->name, "bone") != 0) {
536 if((idx = ts_get_attr_int(c, "bone", -1)) >= 0) {
537 if(!(bone = goat3d_get_node(g, idx))) {
538 goat3d_logmsg(LOG_ERROR, "read_bonelist: reference to invalid bone: %d\n", idx);
542 } else if((str = ts_get_attr_str(c, "bone", 0))) {
543 if(!(bone = goat3d_get_node_by_name(g, str))) {
544 goat3d_logmsg(LOG_ERROR, "read_bonelist: reference to invalid bone: %s\n", str);
549 if(bone && !(arr = dynarr_push(arr, &bone))) {
550 goat3d_logmsg(LOG_ERROR, "read_bonelist: failed to resize bone array\n");
556 if(size > 0 && dynarr_size(arr) != size) {
557 goat3d_logmsg(LOG_WARNING, "read_bonelist: expected %d items, read %d\n", size, dynarr_size(arr));
562 #define GETREF(ptr, typestr, getname) \
565 if((idx = ts_get_attr_int(tsnode, typestr, -1)) >= 0) { \
566 if(!(ptr = goat3d_get_##getname(g, idx))) { \
567 goat3d_logmsg(LOG_ERROR, "read_node: ignoring reference to invalid " typestr ": %d\n", idx); \
569 } else if((str = ts_get_attr_str(tsnode, typestr, 0))) { \
570 if(!(ptr = goat3d_get_##getname##_by_name(g, str))) { \
571 goat3d_logmsg(LOG_ERROR, "read_node: ignoring reference to invalid " typestr ": %s\n", str); \
576 static int read_node(struct goat3d *g, struct goat3d_node *node, struct ts_node *tsnode)
580 struct goat3d_node *parent;
583 GETREF(parent, "parent", node);
585 goat3d_add_node_child(parent, node);
588 node->type = GOAT3D_NODE_MESH;
589 GETREF(node->obj, "mesh", mesh);
591 node->type = GOAT3D_NODE_LIGHT;
592 GETREF(node->obj, "light", light);
595 node->type = GOAT3D_NODE_CAMERA;
596 GETREF(node->obj, "camera", camera);
599 node->type = GOAT3D_NODE_NULL;
602 if((vec = ts_get_attr_vec(tsnode, "pos", 0))) {
603 goat3d_set_node_position(node, vec[0], vec[1], vec[2]);
605 if((vec = ts_get_attr_vec(tsnode, "rot", 0))) {
606 goat3d_set_node_rotation(node, vec[0], vec[1], vec[2], vec[3]);
608 if((vec = ts_get_attr_vec(tsnode, "scale", 0))) {
609 goat3d_set_node_scaling(node, vec[0], vec[1], vec[2]);
611 if((vec = ts_get_attr_vec(tsnode, "pivot", 0))) {
612 goat3d_set_node_pivot(node, vec[0], vec[1], vec[2]);
618 static int read_anim(struct goat3d *g, struct ts_node *tsanim)
622 struct goat3d_anim *anim;
623 struct goat3d_track *trk;
625 if(!(str = ts_get_attr_str(tsanim, "name", 0))) {
626 goat3d_logmsg(LOG_WARNING, "read_anim: ignoring animation without a name\n");
630 if(!(anim = goat3d_create_anim())) {
631 goat3d_logmsg(LOG_ERROR, "read_anim: failed to initialize animation: %s\n", str);
634 goat3d_set_anim_name(anim, str);
636 c = tsanim->child_list;
638 if(strcmp(c->name, "track") == 0) {
639 if(!(trk = read_track(g, c))) {
643 goat3d_add_anim_track(anim, trk);
648 goat3d_add_anim(g, anim);
652 static int parsetype(const char *str)
654 if(strcmp(str, "pos") == 0) return GOAT3D_TRACK_POS;
655 if(strcmp(str, "rot") == 0) return GOAT3D_TRACK_ROT;
656 if(strcmp(str, "scale") == 0) return GOAT3D_TRACK_SCALE;
657 if(strcmp(str, "val") == 0) return GOAT3D_TRACK_VAL;
658 if(strcmp(str, "vec3") == 0) return GOAT3D_TRACK_VEC3;
659 if(strcmp(str, "vec4") == 0) return GOAT3D_TRACK_VEC4;
660 if(strcmp(str, "quat") == 0) return GOAT3D_TRACK_QUAT;
664 static int parseinterp(const char *str)
666 if(strcmp(str, "step") == 0) return GOAT3D_INTERP_STEP;
667 if(strcmp(str, "linear") == 0) return GOAT3D_INTERP_LINEAR;
668 if(strcmp(str, "cubic") == 0) return GOAT3D_INTERP_CUBIC;
672 static int parseextrap(const char *str)
674 if(strcmp(str, "extend") == 0) return GOAT3D_EXTRAP_EXTEND;
675 if(strcmp(str, "clamp") == 0) return GOAT3D_EXTRAP_CLAMP;
676 if(strcmp(str, "repeat") == 0) return GOAT3D_EXTRAP_REPEAT;
677 if(strcmp(str, "pingpong") == 0) return GOAT3D_EXTRAP_PINGPONG;
681 static struct goat3d_track *read_track(struct goat3d *g, struct ts_node *tstrk)
685 struct goat3d_track *trk;
686 struct goat3d_node *node;
687 struct goat3d_key key;
690 struct ts_attr *tsattr;
692 if(!(str = ts_get_attr_str(tstrk, "type", 0)) || (type = parsetype(str)) == -1) {
693 goat3d_logmsg(LOG_WARNING, "read_track: ignoring track with missing or invalid type attribute\n");
697 if((idx = ts_get_attr_int(tstrk, "node", -1)) >= 0) {
698 if(!(node = goat3d_get_node(g, idx))) {
699 goat3d_logmsg(LOG_WARNING, "read_track: ignoring track with invalid node reference (%d)\n", idx);
702 } else if((str = ts_get_attr_str(tstrk, "node", 0))) {
703 if(!(node = goat3d_get_node_by_name(g, str))) {
704 goat3d_logmsg(LOG_WARNING, "read_track: ignoring track with invalid node reference (%s)\n", str);
708 goat3d_logmsg(LOG_WARNING, "read_track: ignoring track with missing node reference\n");
712 if(!(trk = goat3d_create_track())) {
713 goat3d_logmsg(LOG_ERROR, "read_track: failed to create new keyframe track\n");
716 goat3d_set_track_node(trk, node);
717 goat3d_set_track_type(trk, type);
719 if((str = ts_get_attr_str(tstrk, "name", 0))) {
720 goat3d_set_track_name(trk, str);
723 if((str = ts_get_attr_str(tstrk, "interp", 0))) {
724 if((in = parseinterp(str)) == -1) {
725 goat3d_logmsg(LOG_WARNING, "read_track: ignoring invalid interpolation mode: %s\n", str);
727 goat3d_set_track_interp(trk, in);
730 if((str = ts_get_attr_str(tstrk, "extrap", 0))) {
731 if((ex = parseextrap(str)) == -1) {
732 goat3d_logmsg(LOG_WARNING, "read_track: ignoring invalid extrapolation mode: %s\n", str);
734 goat3d_set_track_extrap(trk, ex);
738 c = tstrk->child_list;
740 if(strcmp(c->name, "key") == 0) {
741 if((key.tm = ts_get_attr_int(c, "time", INT_MIN)) == INT_MIN) {
742 goat3d_logmsg(LOG_WARNING, "read_track: ignoring keyframe with missing or invalid time (%s)\n",
743 ts_get_attr_str(c, "time", ""));
747 if(!(tsattr = ts_get_attr(c, "value")) || (tsattr->val.type != TS_NUMBER &&
748 tsattr->val.type != TS_VECTOR)) {
749 goat3d_logmsg(LOG_WARNING, "read_track: ignoring keyframe with missing or invalid value (%s)\n",
750 ts_get_attr_str(c, "value", ""));
755 if(tsattr->val.type == TS_NUMBER) {
756 key.val[0] = tsattr->val.fnum;
759 if(i < tsattr->val.vec_size) {
760 key.val[i] = tsattr->val.vec[i];
766 goat3d_set_track_key(trk, &key);
771 /* force lazy re-sorting of keyframes if necessary */
772 goat3d_get_track_key(trk, 0, &key);