goat3d

view src/goat3d_readxml.cc @ 47:498ca7ac7047

- placed all the implementation stuff in the g3dimpl namespace - added animation stuff to the public API - started writing animation saving/loading
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 28 Dec 2013 06:47:39 +0200
parents 8da36540e2e9
children 0be413ac2e0a fa5c52ea9d59
line source
1 #include <stdio.h>
2 #include "goat3d.h"
3 #include "goat3d_impl.h"
4 #include "tinyxml2.h"
5 #include "log.h"
7 using namespace g3dimpl;
8 using namespace tinyxml2;
10 static Material *read_material(Scene *scn, XMLElement *xml_mtl);
11 static const char *read_material_attrib(MaterialAttrib *attr, XMLElement *xml_attr);
12 static Mesh *read_mesh(Scene *scn, XMLElement *xml_mesh);
13 static std::string get_name(XMLElement *node, int idx);
15 bool Scene::loadxml(goat3d_io *io)
16 {
17 long bytes = io->seek(0, SEEK_END, io->cls);
18 io->seek(0, SEEK_SET, io->cls);
20 char *buf = new char[bytes];
21 if(io->read(buf, bytes, io->cls) < bytes) {
22 logmsg(LOG_ERROR, "failed to read XML scene file\n");
23 delete [] buf;
24 return false;
25 }
27 XMLDocument xml;
28 XMLError err = xml.Parse(buf, bytes);
29 if(err) {
30 logmsg(LOG_ERROR, "failed to parse XML scene file: %s\n%s\n", xml.GetErrorStr1(),
31 xml.GetErrorStr2());
32 delete [] buf;
33 return false;
34 }
36 XMLElement *root = xml.RootElement();
37 if(strcmp(root->Name(), "scene") != 0) {
38 logmsg(LOG_ERROR, "invalid XML file, root node is not <scene>\n");
39 delete [] buf;
40 return false;
41 }
43 XMLElement *elem;
45 // get all materials
46 elem = root->FirstChildElement("mtl");
47 while(elem) {
48 Material *mtl = read_material(this, elem);
49 if(mtl) {
50 add_material(mtl);
51 }
52 elem = elem->NextSiblingElement("mtl");
53 }
55 // get all meshes
56 elem = root->FirstChildElement("mesh");
57 while(elem) {
58 Mesh *mesh = read_mesh(this, elem);
59 if(mesh) {
60 add_mesh(mesh);
61 }
62 elem = elem->NextSiblingElement("mesh");
63 }
65 delete [] buf;
66 return true;
67 }
70 static Material *read_material(Scene *scn, XMLElement *xml_mtl)
71 {
72 Material *mtl = new Material;
73 mtl->name = get_name(xml_mtl, scn->get_material_count());
75 // get all the material attributes in turn
76 XMLElement *elem = xml_mtl->FirstChildElement("attr");
77 while(elem) {
78 MaterialAttrib attr;
79 const char *name = read_material_attrib(&attr, elem);
80 if(name) {
81 (*mtl)[name] = attr;
82 }
84 elem = elem->NextSiblingElement("attr");
85 }
87 return mtl;
88 }
90 static const char *read_material_attrib(MaterialAttrib *attr, XMLElement *xml_attr)
91 {
92 const char *name;
94 XMLElement *elem;
95 if((elem = xml_attr->FirstChildElement("name"))) {
96 if(!(name = elem->Attribute("string"))) {
97 return 0;
98 }
99 }
101 if((elem = xml_attr->FirstChildElement("val"))) {
102 if(elem->QueryFloatAttribute("float", &attr->value.x) != XML_NO_ERROR) {
103 // try a float3
104 const char *valstr = elem->Attribute("float3");
105 if(!valstr || sscanf(valstr, "%f %f %f", &attr->value.x, &attr->value.y,
106 &attr->value.z) != 3) {
107 // try a float4
108 valstr = elem->Attribute("float4");
109 if(!valstr || sscanf(valstr, "%f %f %f %f", &attr->value.x, &attr->value.y,
110 &attr->value.z, &attr->value.w) != 4) {
111 // no valid val attribute found
112 return 0;
113 }
114 }
115 }
116 }
118 if((elem = xml_attr->FirstChildElement("map"))) {
119 const char *tex = elem->Attribute("string");
120 if(tex) {
121 attr->map = std::string(tex);
122 }
123 }
125 return name;
126 }
128 static Mesh *read_mesh(Scene *scn, XMLElement *xml_mesh)
129 {
130 Mesh *mesh = new Mesh;
131 mesh->name = get_name(xml_mesh, scn->get_mesh_count());
133 XMLElement *elem;
134 if((elem = xml_mesh->FirstChildElement("material"))) {
135 int idx;
136 if(elem->QueryIntAttribute("int", &idx) == XML_NO_ERROR) {
137 mesh->material = scn->get_material(idx);
138 } else {
139 // try string
140 const char *mtlstr = elem->Attribute("string");
141 if(mtlstr) {
142 mesh->material = scn->get_material(mtlstr);
143 }
144 }
145 }
147 /* reading mesh data from XML is not supported, only MESH_FILE can be used to
148 * specify an external mesh file to be loaded
149 */
151 if((elem = xml_mesh->FirstChildElement("file"))) {
152 const char *fname = elem->Attribute("string");
153 if(fname) {
154 if(!mesh->load(fname)) {
155 delete mesh;
156 return 0;
157 }
158 }
159 }
161 return mesh;
162 }
164 static std::string get_name(XMLElement *node, int idx)
165 {
166 char buf[64];
167 const char *name = 0;
169 XMLElement *elem;
170 if((elem = node->FirstChildElement("name"))) {
171 name = elem->Attribute("string");
172 }
174 if(!name) {
175 sprintf(buf, "mesh%04d", idx);
176 name = buf;
177 }
179 return std::string(name);
180 }