goat3d

view src/goat3d_readxml.cc @ 50:0be413ac2e0a

ass2goat compiles...
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 16 Jan 2014 19:27: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 }
70 bool Scene::load_anim_xml(goat3d_io *io)
71 {
72 return false;
73 }
76 static Material *read_material(Scene *scn, XMLElement *xml_mtl)
77 {
78 Material *mtl = new Material;
79 mtl->name = get_name(xml_mtl, scn->get_material_count());
81 // get all the material attributes in turn
82 XMLElement *elem = xml_mtl->FirstChildElement("attr");
83 while(elem) {
84 MaterialAttrib attr;
85 const char *name = read_material_attrib(&attr, elem);
86 if(name) {
87 (*mtl)[name] = attr;
88 }
90 elem = elem->NextSiblingElement("attr");
91 }
93 return mtl;
94 }
96 static const char *read_material_attrib(MaterialAttrib *attr, XMLElement *xml_attr)
97 {
98 const char *name;
100 XMLElement *elem;
101 if((elem = xml_attr->FirstChildElement("name"))) {
102 if(!(name = elem->Attribute("string"))) {
103 return 0;
104 }
105 }
107 if((elem = xml_attr->FirstChildElement("val"))) {
108 if(elem->QueryFloatAttribute("float", &attr->value.x) != XML_NO_ERROR) {
109 // try a float3
110 const char *valstr = elem->Attribute("float3");
111 if(!valstr || sscanf(valstr, "%f %f %f", &attr->value.x, &attr->value.y,
112 &attr->value.z) != 3) {
113 // try a float4
114 valstr = elem->Attribute("float4");
115 if(!valstr || sscanf(valstr, "%f %f %f %f", &attr->value.x, &attr->value.y,
116 &attr->value.z, &attr->value.w) != 4) {
117 // no valid val attribute found
118 return 0;
119 }
120 }
121 }
122 }
124 if((elem = xml_attr->FirstChildElement("map"))) {
125 const char *tex = elem->Attribute("string");
126 if(tex) {
127 attr->map = std::string(tex);
128 }
129 }
131 return name;
132 }
134 static Mesh *read_mesh(Scene *scn, XMLElement *xml_mesh)
135 {
136 Mesh *mesh = new Mesh;
137 mesh->name = get_name(xml_mesh, scn->get_mesh_count());
139 XMLElement *elem;
140 if((elem = xml_mesh->FirstChildElement("material"))) {
141 int idx;
142 if(elem->QueryIntAttribute("int", &idx) == XML_NO_ERROR) {
143 mesh->material = scn->get_material(idx);
144 } else {
145 // try string
146 const char *mtlstr = elem->Attribute("string");
147 if(mtlstr) {
148 mesh->material = scn->get_material(mtlstr);
149 }
150 }
151 }
153 /* reading mesh data from XML is not supported, only MESH_FILE can be used to
154 * specify an external mesh file to be loaded
155 */
157 if((elem = xml_mesh->FirstChildElement("file"))) {
158 const char *fname = elem->Attribute("string");
159 if(fname) {
160 if(!mesh->load(fname)) {
161 delete mesh;
162 return 0;
163 }
164 }
165 }
167 return mesh;
168 }
170 static std::string get_name(XMLElement *node, int idx)
171 {
172 char buf[64];
173 const char *name = 0;
175 XMLElement *elem;
176 if((elem = node->FirstChildElement("name"))) {
177 name = elem->Attribute("string");
178 }
180 if(!name) {
181 sprintf(buf, "mesh%04d", idx);
182 name = buf;
183 }
185 return std::string(name);
186 }