goat3d

view src/goat3d_readxml.cc @ 75:76dea247f75c

in progress
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 08 May 2014 00:50:16 +0300
parents ab66cdabf6f2
children 57e745dd13c2
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 "goat3d.h"
22 #include "goat3d_impl.h"
23 #include "tinyxml2.h"
24 #include "log.h"
26 using namespace g3dimpl;
27 using namespace tinyxml2;
29 static Material *read_material(Scene *scn, XMLElement *xml_mtl);
30 static const char *read_material_attrib(MaterialAttrib *attr, XMLElement *xml_attr);
31 static Mesh *read_mesh(Scene *scn, XMLElement *xml_mesh);
32 static Node *read_node(Scene *scn, XMLElement *xml_node, std::map<Node*, std::string> &linkmap);
33 static std::string get_name(XMLElement *node, int idx, const char *def_prefix);
35 bool Scene::loadxml(goat3d_io *io)
36 {
37 long bytes = io->seek(0, SEEK_END, io->cls);
38 io->seek(0, SEEK_SET, io->cls);
40 char *buf = new char[bytes];
41 if(io->read(buf, bytes, io->cls) < bytes) {
42 logmsg(LOG_ERROR, "failed to read XML scene file\n");
43 delete [] buf;
44 return false;
45 }
47 XMLDocument xml;
48 XMLError err = xml.Parse(buf, bytes);
49 if(err) {
50 logmsg(LOG_ERROR, "failed to parse XML scene file: %s\n%s\n", xml.GetErrorStr1(),
51 xml.GetErrorStr2());
52 delete [] buf;
53 return false;
54 }
56 XMLElement *root = xml.RootElement();
57 if(strcmp(root->Name(), "scene") != 0) {
58 logmsg(LOG_ERROR, "invalid XML file, root node is not <scene>\n");
59 delete [] buf;
60 return false;
61 }
63 XMLElement *elem;
65 // get all materials
66 elem = root->FirstChildElement("mtl");
67 while(elem) {
68 Material *mtl = read_material(this, elem);
69 if(mtl) {
70 add_material(mtl);
71 }
72 elem = elem->NextSiblingElement("mtl");
73 }
75 // get all meshes
76 elem = root->FirstChildElement("mesh");
77 while(elem) {
78 Mesh *mesh = read_mesh(this, elem);
79 if(mesh) {
80 add_mesh(mesh);
81 }
82 elem = elem->NextSiblingElement("mesh");
83 }
85 // get all nodes
86 std::map<Node*, std::string> linkmap;
88 elem = root->FirstChildElement("node");
89 while(elem) {
90 Node *node = read_node(this, elem, linkmap);
91 if(node) {
92 add_node(node);
93 }
94 elem = elem->NextSiblingElement("node");
95 }
97 // link up all the nodes in the hierarchy
98 for(size_t i=0; i<nodes.size(); i++) {
99 std::string parent_name = linkmap[nodes[i]];
100 if(!parent_name.empty()) {
101 Node *parent = get_node(parent_name.c_str());
102 if(parent) {
103 parent->add_child(nodes[i]);
104 }
105 }
106 }
108 delete [] buf;
109 return true;
110 }
112 bool Scene::load_anim_xml(goat3d_io *io)
113 {
114 long bytes = io->seek(0, SEEK_END, io->cls);
115 io->seek(0, SEEK_SET, io->cls);
117 char *buf = new char[bytes];
118 if(io->read(buf, bytes, io->cls) < bytes) {
119 logmsg(LOG_ERROR, "failed to read XML animation file\n");
120 delete [] buf;
121 return false;
122 }
124 XMLDocument xml;
125 XMLError err = xml.Parse(buf, bytes);
126 if(err) {
127 logmsg(LOG_ERROR, "failed to parse XML animation file: %s\n%s\n", xml.GetErrorStr1(),
128 xml.GetErrorStr2());
129 delete [] buf;
130 return false;
131 }
133 XMLElement *root = xml.RootElement();
134 if(strcmp(root->Name(), "anim") != 0) {
135 logmsg(LOG_ERROR, "invalid XML file, root node is not <anim>\n");
136 delete [] buf;
137 return false;
138 }
140 XMLElement *elem;
142 elem = root->FirstChildElement();
143 while(elem) {
144 const char *elem_name = elem->Name();
146 if(strcmp(elem_name, "name") == 0) {
147 } else if(strcmp(elem_name, "attr") == 0) {
148 }
149 elem = elem->NextSiblingElement();
150 }
152 delete [] buf;
153 return true;
154 }
156 static Material *read_material(Scene *scn, XMLElement *xml_mtl)
157 {
158 Material *mtl = new Material;
159 mtl->name = get_name(xml_mtl, scn->get_material_count(), "material");
161 // get all the material attributes in turn
162 XMLElement *elem = xml_mtl->FirstChildElement("attr");
163 while(elem) {
164 MaterialAttrib attr;
165 const char *name = read_material_attrib(&attr, elem);
166 if(name) {
167 (*mtl)[name] = attr;
168 }
170 elem = elem->NextSiblingElement("attr");
171 }
173 return mtl;
174 }
176 static const char *read_material_attrib(MaterialAttrib *attr, XMLElement *xml_attr)
177 {
178 const char *name;
180 XMLElement *elem;
181 if((elem = xml_attr->FirstChildElement("name"))) {
182 if(!(name = elem->Attribute("string"))) {
183 return 0;
184 }
185 }
187 if((elem = xml_attr->FirstChildElement("val"))) {
188 if(elem->QueryFloatAttribute("float", &attr->value.x) != XML_NO_ERROR) {
189 // try a float3
190 const char *valstr = elem->Attribute("float3");
191 if(!valstr || sscanf(valstr, "%f %f %f", &attr->value.x, &attr->value.y,
192 &attr->value.z) != 3) {
193 // try a float4
194 valstr = elem->Attribute("float4");
195 if(!valstr || sscanf(valstr, "%f %f %f %f", &attr->value.x, &attr->value.y,
196 &attr->value.z, &attr->value.w) != 4) {
197 // no valid val attribute found
198 return 0;
199 }
200 }
201 }
202 }
204 if((elem = xml_attr->FirstChildElement("map"))) {
205 const char *tex = elem->Attribute("string");
206 if(tex) {
207 attr->map = std::string(tex);
208 }
209 }
211 return name;
212 }
214 static Mesh *read_mesh(Scene *scn, XMLElement *xml_mesh)
215 {
216 Mesh *mesh = new Mesh;
217 mesh->name = get_name(xml_mesh, scn->get_mesh_count(), "mesh");
219 XMLElement *elem;
220 if((elem = xml_mesh->FirstChildElement("material"))) {
221 int idx;
222 if(elem->QueryIntAttribute("int", &idx) == XML_NO_ERROR) {
223 mesh->material = scn->get_material(idx);
224 } else {
225 // try string
226 const char *mtlstr = elem->Attribute("string");
227 if(mtlstr) {
228 mesh->material = scn->get_material(mtlstr);
229 }
230 }
231 }
233 /* reading mesh data from XML is not supported, only MESH_FILE can be used to
234 * specify an external mesh file to be loaded
235 */
237 if((elem = xml_mesh->FirstChildElement("file"))) {
238 const char *fname = elem->Attribute("string");
239 if(fname) {
240 char *path = (char*)fname;
241 if(scn->goat->search_path) {
242 path = (char*)alloca(strlen(fname) + strlen(scn->goat->search_path) + 2);
243 sprintf(path, "%s/%s", scn->goat->search_path, fname);
244 }
245 if(!mesh->load(path)) {
246 delete mesh;
247 return 0;
248 }
249 }
250 }
252 return mesh;
253 }
255 static Node *read_node(Scene *scn, XMLElement *xml_node, std::map<Node*, std::string> &linkmap)
256 {
257 Node *node = new Node;
258 node->set_name(get_name(xml_node, scn->get_node_count(), "node").c_str());
260 XMLElement *elem;
261 if((elem = xml_node->FirstChildElement("parent"))) {
262 const char *pname = elem->Attribute("string");
263 if(pname) {
264 linkmap[node] = pname;
265 }
266 }
268 if((elem = xml_node->FirstChildElement("mesh"))) {
269 Mesh *mesh = scn->get_mesh(elem->Attribute("string"));
270 if(mesh) {
271 node->set_object(mesh);
272 }
273 } else if((elem = xml_node->FirstChildElement("light"))) {
274 Light *lt = scn->get_light(elem->Attribute("string"));
275 if(lt) {
276 node->set_object(lt);
277 }
278 } else if((elem = xml_node->FirstChildElement("camera"))) {
279 Camera *cam = scn->get_camera(elem->Attribute("string"));
280 if(cam) {
281 node->set_object(cam);
282 }
283 }
285 float vec[4];
286 if((elem = xml_node->FirstChildElement("pos"))) {
287 const char *val = elem->Attribute("float3");
288 if(val && sscanf(val, "%f %f %f", vec, vec + 1, vec + 2) == 3) {
289 node->set_position(Vector3(val[0], val[1], val[2]));
290 } else {
291 logmsg(LOG_ERROR, "node %s: invalid position tag\n", node->get_name());
292 }
293 }
294 if((elem = xml_node->FirstChildElement("rot"))) {
295 const char *val = elem->Attribute("float4");
296 if(val && sscanf(val, "%f %f %f %f", vec, vec + 1, vec + 2, vec + 3) == 4) {
297 node->set_rotation(Quaternion(vec[3], Vector3(vec[0], vec[1], vec[2])));
298 } else {
299 logmsg(LOG_ERROR, "node %s: invalid rotation tag\n", node->get_name());
300 }
301 }
302 if((elem = xml_node->FirstChildElement("scale"))) {
303 const char *val = elem->Attribute("float3");
304 if(val && sscanf(val, "%f %f %f", vec, vec + 1, vec + 2) == 3) {
305 node->set_scaling(Vector3(vec[0], vec[1], vec[2]));
306 } else {
307 logmsg(LOG_ERROR, "node %s: invalid scaling tag\n", node->get_name());
308 }
309 }
310 if((elem = xml_node->FirstChildElement("pivot"))) {
311 const char *val = elem->Attribute("float3");
312 if(val && sscanf(val, "%f %f %f", vec, vec + 1, vec + 2) == 3) {
313 node->set_pivot(Vector3(vec[0], vec[1], vec[2]));
314 } else {
315 logmsg(LOG_ERROR, "node %s: invalid pivot tag\n", node->get_name());
316 }
317 }
319 return node;
320 }
322 static std::string get_name(XMLElement *node, int idx, const char *def_prefix)
323 {
324 char buf[64];
325 const char *name = 0;
327 XMLElement *elem;
328 if((elem = node->FirstChildElement("name"))) {
329 name = elem->Attribute("string");
330 }
332 if(!name) {
333 sprintf(buf, "%s%04d", def_prefix, idx);
334 name = buf;
335 }
337 return std::string(name);
338 }