goat3d
view src/goat3d_readxml.cc @ 100:fae6dc0fd462
[goatview] disable animation controls on close_scene
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 21 May 2014 05:09:33 +0300 |
parents | da100bf13f7f |
children |
line source
1 /*
2 goat3d - 3D scene, character, and animation file format library.
3 Copyright (C) 2013-2014 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/>.
17 */
18 #include <stdio.h>
19 #include <map>
20 #include <string>
21 #include <algorithm>
22 #include "goat3d.h"
23 #include "goat3d_impl.h"
24 #include "tinyxml2.h"
25 #include "log.h"
27 using namespace g3dimpl;
28 using namespace tinyxml2;
30 struct Key {
31 long tm;
32 Vector4 val;
33 };
35 static bool read_track(Scene *scn, XMLElement *xml_track, int *anim_idx);
36 static Key read_key(XMLElement *xml_key);
38 static Material *read_material(Scene *scn, XMLElement *xml_mtl);
39 static const char *read_material_attrib(MaterialAttrib *attr, XMLElement *xml_attr);
40 static Mesh *read_mesh(Scene *scn, XMLElement *xml_mesh);
41 static Node *read_node(Scene *scn, XMLElement *xml_node, std::map<Node*, std::string> &linkmap);
42 static std::string get_name(XMLElement *node, int idx, const char *def_prefix);
44 bool Scene::loadxml(goat3d_io *io)
45 {
46 long bytes = io->seek(0, SEEK_END, io->cls);
47 io->seek(0, SEEK_SET, io->cls);
49 char *buf = new char[bytes];
50 if(io->read(buf, bytes, io->cls) < bytes) {
51 logmsg(LOG_ERROR, "failed to read XML scene file\n");
52 delete [] buf;
53 return false;
54 }
56 XMLDocument xml;
57 XMLError err = xml.Parse(buf, bytes);
58 if(err) {
59 logmsg(LOG_ERROR, "failed to parse XML scene file: %s\n%s\n", xml.GetErrorStr1(),
60 xml.GetErrorStr2());
61 delete [] buf;
62 return false;
63 }
65 XMLElement *root = xml.RootElement();
66 if(strcmp(root->Name(), "scene") != 0) {
67 logmsg(LOG_ERROR, "invalid XML file, root node is not <scene>\n");
68 delete [] buf;
69 return false;
70 }
72 XMLElement *elem;
74 // get all materials
75 elem = root->FirstChildElement("mtl");
76 while(elem) {
77 Material *mtl = read_material(this, elem);
78 if(mtl) {
79 add_material(mtl);
80 }
81 elem = elem->NextSiblingElement("mtl");
82 }
84 // get all meshes
85 elem = root->FirstChildElement("mesh");
86 while(elem) {
87 Mesh *mesh = read_mesh(this, elem);
88 if(mesh) {
89 add_mesh(mesh);
90 }
91 elem = elem->NextSiblingElement("mesh");
92 }
94 // get all nodes
95 std::map<Node*, std::string> linkmap;
97 elem = root->FirstChildElement("node");
98 while(elem) {
99 Node *node = read_node(this, elem, linkmap);
100 if(node) {
101 add_node(node);
102 }
103 elem = elem->NextSiblingElement("node");
104 }
106 // link up all the nodes in the hierarchy
107 for(size_t i=0; i<nodes.size(); i++) {
108 std::string parent_name = linkmap[nodes[i]];
109 if(!parent_name.empty()) {
110 Node *parent = get_node(parent_name.c_str());
111 if(parent) {
112 parent->add_child(nodes[i]);
113 }
114 }
115 }
117 delete [] buf;
118 return true;
119 }
121 bool Scene::load_anim_xml(goat3d_io *io)
122 {
123 long bytes = io->seek(0, SEEK_END, io->cls);
124 io->seek(0, SEEK_SET, io->cls);
126 char *buf = new char[bytes];
127 if(io->read(buf, bytes, io->cls) < bytes) {
128 logmsg(LOG_ERROR, "failed to read XML animation file\n");
129 delete [] buf;
130 return false;
131 }
133 XMLDocument xml;
134 XMLError err = xml.Parse(buf, bytes);
135 if(err) {
136 logmsg(LOG_ERROR, "failed to parse XML animation file: %s\n%s\n", xml.GetErrorStr1(),
137 xml.GetErrorStr2());
138 delete [] buf;
139 return false;
140 }
142 XMLElement *root = xml.RootElement();
143 if(strcmp(root->Name(), "anim") != 0) {
144 logmsg(LOG_ERROR, "%s: root node is not <anim>\n", __FUNCTION__);
145 delete [] buf;
146 return false;
147 }
149 int anim_idx = -1;
150 XMLElement *elem = root->FirstChildElement();
151 while(elem) {
152 const char *elem_name = elem->Name();
154 if(strcmp(elem_name, "track") != 0) {
155 logmsg(LOG_ERROR, "%s: only <track>s allowed in <anim>\n", __FUNCTION__);
156 delete [] buf;
157 return false;
158 }
160 if(!read_track(this, elem, &anim_idx)) {
161 delete [] buf;
162 return false;
163 }
164 elem = elem->NextSiblingElement();
165 }
167 if(anim_idx == -1) {
168 logmsg(LOG_INFO, "%s: WARNING animation affected 0 nodes\n", __FUNCTION__);
169 }
171 delete [] buf;
172 return true;
173 }
175 static bool read_track(Scene *scn, XMLElement *xml_track, int *anim_idx)
176 {
177 Node *node = 0;
178 int type = -1;
179 std::vector<Key> keys;
181 XMLElement *elem = xml_track->FirstChildElement();
182 while(elem) {
183 const char *elem_name = elem->Name();
185 if(strcmp(elem_name, "node") == 0) {
186 const char *name = elem->Attribute("string");
187 if(!name || !(node = scn->get_node(name))) {
188 logmsg(LOG_ERROR, "%s: invalid track node: %s\n", __FUNCTION__, name);
189 return false;
190 }
192 } else if(strcmp(elem_name, "attr") == 0) {
193 const char *str = elem->Attribute("string");
194 if(str && strcmp(str, "position") == 0) {
195 type = XFormNode::POSITION_TRACK;
196 } else if(str && strcmp(str, "rotation") == 0) {
197 type = XFormNode::ROTATION_TRACK;
198 } else if(str && strcmp(str, "scaling") == 0) {
199 type = XFormNode::SCALING_TRACK;
200 } else {
201 logmsg(LOG_ERROR, "%s: invalid track attribute specifier: %s\n", __FUNCTION__, str);
202 return false;
203 }
205 } else if(strcmp(elem_name, "key") == 0) {
206 Key key = read_key(elem);
207 if(key.tm == LONG_MIN) {
208 return false; // logging in read_key
209 }
210 keys.push_back(key);
212 } else {
213 logmsg(LOG_ERROR, "%s: unexpected element <%s> in <track>\n", __FUNCTION__, elem_name);
214 return false;
215 }
216 elem = elem->NextSiblingElement();
217 }
219 if(!node) {
220 logmsg(LOG_ERROR, "%s: invalid track, missing node reference\n", __FUNCTION__);
221 return false;
222 }
223 if(type == -1) {
224 logmsg(LOG_ERROR, "%s: invalid track, missing attribute specifier\n", __FUNCTION__);
225 return false;
226 }
228 if(*anim_idx == -1) {
229 // this is the first node we encounter. add a new animation and activate it
230 XFormNode *root = node->get_root();
231 *anim_idx = root->get_animation_count() + 1;
233 char name[64];
234 sprintf(name, "anim%03d\n", *anim_idx);
235 root->add_animation(name);
236 root->use_animation(*anim_idx);
237 } else {
238 // make sure this node hierarchy already has this animation, otherwise add it
239 XFormNode *root = node->get_root();
240 if(root->get_active_animation_index() != *anim_idx) {
241 char name[64];
242 sprintf(name, "anim%03d\n", *anim_idx);
243 root->add_animation(name);
244 root->use_animation(*anim_idx);
245 }
246 }
248 for(size_t i=0; i<keys.size(); i++) {
249 switch(type) {
250 case XFormNode::POSITION_TRACK:
251 node->set_position(Vector3(keys[i].val.x, keys[i].val.y, keys[i].val.z), keys[i].tm);
252 break;
253 case XFormNode::ROTATION_TRACK:
254 node->set_rotation(Quaternion(keys[i].val.w, keys[i].val.x, keys[i].val.y, keys[i].val.z), keys[i].tm);
255 break;
256 case XFormNode::SCALING_TRACK:
257 node->set_scaling(Vector3(keys[i].val.x, keys[i].val.y, keys[i].val.z), keys[i].tm);
258 }
259 }
260 return true;
261 }
263 static Key read_key(XMLElement *xml_key)
264 {
265 Key key;
266 key.tm = LONG_MIN; // initialize to invalid time
268 XMLElement *xml_time = xml_key->FirstChildElement("time");
269 XMLElement *xml_value = xml_key->FirstChildElement("value");
271 if(!xml_time || !xml_value) {
272 logmsg(LOG_ERROR, "%s: invalid key, missing either <time> or <value> elements\n", __FUNCTION__);
273 return key;
274 }
276 int ival;
277 if(xml_time->QueryIntAttribute("int", &ival) != XML_NO_ERROR) {
278 logmsg(LOG_ERROR, "%s: invalid time element in <key>\n", __FUNCTION__);
279 return key;
280 }
282 const char *vstr;
283 if((vstr = xml_value->Attribute("float3"))) {
284 if(sscanf(vstr, "%f %f %f", &key.val.x, &key.val.y, &key.val.z) != 3) {
285 logmsg(LOG_ERROR, "%s: invalid float3 value element in <key>: %s\n", __FUNCTION__, vstr);
286 return key;
287 }
288 } else if((vstr = xml_value->Attribute("float4"))) {
289 if(sscanf(vstr, "%f %f %f %f", &key.val.x, &key.val.y, &key.val.z, &key.val.w) != 4) {
290 logmsg(LOG_ERROR, "%s: invalid float4 value element in <key>: %s\n", __FUNCTION__, vstr);
291 return key;
292 }
293 } else {
294 logmsg(LOG_ERROR, "%s: invalid value element in <key>: missing float3 or float4 attributes\n", __FUNCTION__);
295 return key;
296 }
298 key.tm = ival;
299 return key;
300 }
302 static Material *read_material(Scene *scn, XMLElement *xml_mtl)
303 {
304 Material *mtl = new Material;
305 mtl->name = get_name(xml_mtl, scn->get_material_count(), "material");
307 // get all the material attributes in turn
308 XMLElement *elem = xml_mtl->FirstChildElement("attr");
309 while(elem) {
310 MaterialAttrib attr;
311 const char *name = read_material_attrib(&attr, elem);
312 if(name) {
313 (*mtl)[name] = attr;
314 }
316 elem = elem->NextSiblingElement("attr");
317 }
319 return mtl;
320 }
322 static const char *read_material_attrib(MaterialAttrib *attr, XMLElement *xml_attr)
323 {
324 const char *name = 0;
326 XMLElement *elem;
327 if((elem = xml_attr->FirstChildElement("name"))) {
328 if(!(name = elem->Attribute("string"))) {
329 return 0;
330 }
331 }
333 if((elem = xml_attr->FirstChildElement("val"))) {
334 if(elem->QueryFloatAttribute("float", &attr->value.x) != XML_NO_ERROR) {
335 // try a float3
336 const char *valstr = elem->Attribute("float3");
337 if(!valstr || sscanf(valstr, "%f %f %f", &attr->value.x, &attr->value.y,
338 &attr->value.z) != 3) {
339 // try a float4
340 valstr = elem->Attribute("float4");
341 if(!valstr || sscanf(valstr, "%f %f %f %f", &attr->value.x, &attr->value.y,
342 &attr->value.z, &attr->value.w) != 4) {
343 // no valid val attribute found
344 return 0;
345 }
346 }
347 }
348 }
350 if((elem = xml_attr->FirstChildElement("map"))) {
351 const char *tex = elem->Attribute("string");
352 if(tex) {
353 attr->map = std::string(tex);
354 }
355 }
357 return name;
358 }
360 static Mesh *read_mesh(Scene *scn, XMLElement *xml_mesh)
361 {
362 Mesh *mesh = new Mesh;
363 mesh->name = get_name(xml_mesh, scn->get_mesh_count(), "mesh");
365 XMLElement *elem;
366 if((elem = xml_mesh->FirstChildElement("material"))) {
367 int idx;
368 if(elem->QueryIntAttribute("int", &idx) == XML_NO_ERROR) {
369 mesh->material = scn->get_material(idx);
370 } else {
371 // try string
372 const char *mtlstr = elem->Attribute("string");
373 if(mtlstr) {
374 mesh->material = scn->get_material(mtlstr);
375 }
376 }
377 }
379 /* reading mesh data from XML is not supported, only MESH_FILE can be used to
380 * specify an external mesh file to be loaded
381 */
383 if((elem = xml_mesh->FirstChildElement("file"))) {
384 const char *fname = elem->Attribute("string");
385 if(fname) {
386 char *path = (char*)fname;
387 if(scn->goat->search_path) {
388 path = (char*)alloca(strlen(fname) + strlen(scn->goat->search_path) + 2);
389 sprintf(path, "%s/%s", scn->goat->search_path, fname);
390 }
391 if(!mesh->load(path)) {
392 delete mesh;
393 return 0;
394 }
395 }
396 }
398 return mesh;
399 }
401 static Node *read_node(Scene *scn, XMLElement *xml_node, std::map<Node*, std::string> &linkmap)
402 {
403 Node *node = new Node;
404 node->set_name(get_name(xml_node, scn->get_node_count(), "node").c_str());
406 XMLElement *elem;
407 if((elem = xml_node->FirstChildElement("parent"))) {
408 const char *pname = elem->Attribute("string");
409 if(pname) {
410 linkmap[node] = pname;
411 }
412 }
414 if((elem = xml_node->FirstChildElement("mesh"))) {
415 Mesh *mesh = scn->get_mesh(elem->Attribute("string"));
416 if(mesh) {
417 node->set_object(mesh);
418 }
419 } else if((elem = xml_node->FirstChildElement("light"))) {
420 Light *lt = scn->get_light(elem->Attribute("string"));
421 if(lt) {
422 node->set_object(lt);
423 }
424 } else if((elem = xml_node->FirstChildElement("camera"))) {
425 Camera *cam = scn->get_camera(elem->Attribute("string"));
426 if(cam) {
427 node->set_object(cam);
428 }
429 }
431 float vec[4];
432 if((elem = xml_node->FirstChildElement("pos"))) {
433 const char *val = elem->Attribute("float3");
434 if(val && sscanf(val, "%f %f %f", vec, vec + 1, vec + 2) == 3) {
435 node->set_position(Vector3(vec[0], vec[1], vec[2]));
436 } else {
437 logmsg(LOG_ERROR, "node %s: invalid position tag\n", node->get_name());
438 }
439 }
440 if((elem = xml_node->FirstChildElement("rot"))) {
441 const char *val = elem->Attribute("float4");
442 if(val && sscanf(val, "%f %f %f %f", vec, vec + 1, vec + 2, vec + 3) == 4) {
443 node->set_rotation(Quaternion(vec[3], Vector3(vec[0], vec[1], vec[2])));
444 } else {
445 logmsg(LOG_ERROR, "node %s: invalid rotation tag\n", node->get_name());
446 }
447 }
448 if((elem = xml_node->FirstChildElement("scale"))) {
449 const char *val = elem->Attribute("float3");
450 if(val && sscanf(val, "%f %f %f", vec, vec + 1, vec + 2) == 3) {
451 node->set_scaling(Vector3(vec[0], vec[1], vec[2]));
452 } else {
453 logmsg(LOG_ERROR, "node %s: invalid scaling tag\n", node->get_name());
454 }
455 }
456 if((elem = xml_node->FirstChildElement("pivot"))) {
457 const char *val = elem->Attribute("float3");
458 if(val && sscanf(val, "%f %f %f", vec, vec + 1, vec + 2) == 3) {
459 node->set_pivot(Vector3(vec[0], vec[1], vec[2]));
460 } else {
461 logmsg(LOG_ERROR, "node %s: invalid pivot tag\n", node->get_name());
462 }
463 }
465 return node;
466 }
468 static std::string get_name(XMLElement *node, int idx, const char *def_prefix)
469 {
470 char buf[64];
471 const char *name = 0;
473 XMLElement *elem;
474 if((elem = node->FirstChildElement("name"))) {
475 name = elem->Attribute("string");
476 }
478 if(!name) {
479 sprintf(buf, "%s%04d", def_prefix, idx);
480 name = buf;
481 }
483 return std::string(name);
484 }