goat3d
view src/goat3d_readxml.cc @ 51:fa5c52ea9d59
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 17 Jan 2014 18:16:09 +0200 |
parents | 498ca7ac7047 |
children | cb5414f406eb |
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 }
69 bool Scene::load_anim_xml(goat3d_io *io)
70 {
71 long bytes = io->seek(0, SEEK_END, io->cls);
72 io->seek(0, SEEK_SET, io->cls);
74 char *buf = new char[bytes];
75 if(io->read(buf, bytes, io->cls) < bytes) {
76 logmsg(LOG_ERROR, "failed to read XML animation file\n");
77 delete [] buf;
78 return false;
79 }
81 XMLDocument xml;
82 XMLError err = xml.Parse(buf, bytes);
83 if(err) {
84 logmsg(LOG_ERROR, "failed to parse XML animation file: %s\n%s\n", xml.GetErrorStr1(),
85 xml.GetErrorStr2());
86 delete [] buf;
87 return false;
88 }
90 XMLElement *root = xml.RootElement();
91 if(strcmp(root->Name(), "anim") != 0) {
92 logmsg(LOG_ERROR, "invalid XML file, root node is not <anim>\n");
93 delete [] buf;
94 return false;
95 }
97 XMLElement *elem;
99 elem = root->FirstChildElement();
100 while(elem) {
101 const char *elem_name = elem->Name();
103 if(strcmp(elem_name, "name") == 0) {
104 } else if(strcmp(elem_name, "attr") == 0) {
105 }
106 elem = elem->NextSiblingElement();
107 }
109 delete [] buf;
110 return true;
111 }
113 static Material *read_material(Scene *scn, XMLElement *xml_mtl)
114 {
115 Material *mtl = new Material;
116 mtl->name = get_name(xml_mtl, scn->get_material_count());
118 // get all the material attributes in turn
119 XMLElement *elem = xml_mtl->FirstChildElement("attr");
120 while(elem) {
121 MaterialAttrib attr;
122 const char *name = read_material_attrib(&attr, elem);
123 if(name) {
124 (*mtl)[name] = attr;
125 }
127 elem = elem->NextSiblingElement("attr");
128 }
130 return mtl;
131 }
133 static const char *read_material_attrib(MaterialAttrib *attr, XMLElement *xml_attr)
134 {
135 const char *name;
137 XMLElement *elem;
138 if((elem = xml_attr->FirstChildElement("name"))) {
139 if(!(name = elem->Attribute("string"))) {
140 return 0;
141 }
142 }
144 if((elem = xml_attr->FirstChildElement("val"))) {
145 if(elem->QueryFloatAttribute("float", &attr->value.x) != XML_NO_ERROR) {
146 // try a float3
147 const char *valstr = elem->Attribute("float3");
148 if(!valstr || sscanf(valstr, "%f %f %f", &attr->value.x, &attr->value.y,
149 &attr->value.z) != 3) {
150 // try a float4
151 valstr = elem->Attribute("float4");
152 if(!valstr || sscanf(valstr, "%f %f %f %f", &attr->value.x, &attr->value.y,
153 &attr->value.z, &attr->value.w) != 4) {
154 // no valid val attribute found
155 return 0;
156 }
157 }
158 }
159 }
161 if((elem = xml_attr->FirstChildElement("map"))) {
162 const char *tex = elem->Attribute("string");
163 if(tex) {
164 attr->map = std::string(tex);
165 }
166 }
168 return name;
169 }
171 static Mesh *read_mesh(Scene *scn, XMLElement *xml_mesh)
172 {
173 Mesh *mesh = new Mesh;
174 mesh->name = get_name(xml_mesh, scn->get_mesh_count());
176 XMLElement *elem;
177 if((elem = xml_mesh->FirstChildElement("material"))) {
178 int idx;
179 if(elem->QueryIntAttribute("int", &idx) == XML_NO_ERROR) {
180 mesh->material = scn->get_material(idx);
181 } else {
182 // try string
183 const char *mtlstr = elem->Attribute("string");
184 if(mtlstr) {
185 mesh->material = scn->get_material(mtlstr);
186 }
187 }
188 }
190 /* reading mesh data from XML is not supported, only MESH_FILE can be used to
191 * specify an external mesh file to be loaded
192 */
194 if((elem = xml_mesh->FirstChildElement("file"))) {
195 const char *fname = elem->Attribute("string");
196 if(fname) {
197 if(!mesh->load(fname)) {
198 delete mesh;
199 return 0;
200 }
201 }
202 }
204 return mesh;
205 }
207 static std::string get_name(XMLElement *node, int idx)
208 {
209 char buf[64];
210 const char *name = 0;
212 XMLElement *elem;
213 if((elem = node->FirstChildElement("name"))) {
214 name = elem->Attribute("string");
215 }
217 if(!name) {
218 sprintf(buf, "mesh%04d", idx);
219 name = buf;
220 }
222 return std::string(name);
223 }