1 | // created: 2013/08/02
|
---|
2 | // filename: Parser.cpp
|
---|
3 | //
|
---|
4 | // author: César Richard
|
---|
5 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
6 | //
|
---|
7 | // version: $Id: $
|
---|
8 | //
|
---|
9 | // purpose: classe chargeant un XML decrivant une map
|
---|
10 | //
|
---|
11 | /*********************************************************************/
|
---|
12 | #ifdef GL
|
---|
13 |
|
---|
14 | #include "Parser.h"
|
---|
15 | #include "Simulator.h"
|
---|
16 | #include "GenericObject.h"
|
---|
17 | #include <vector3d.h>
|
---|
18 | #include <irrlicht.h>
|
---|
19 | #include "GenericObject.h"
|
---|
20 |
|
---|
21 |
|
---|
22 | using namespace irr;
|
---|
23 | using namespace irr::core;
|
---|
24 | using namespace irr::scene;
|
---|
25 | using namespace irr::video;
|
---|
26 | using namespace flair::core;
|
---|
27 | using namespace flair::simulator;
|
---|
28 |
|
---|
29 | //todo: getMeshVect doit plutot donner point3D et euler
|
---|
30 | //adaptr le xml et la dtd
|
---|
31 |
|
---|
32 | namespace flair
|
---|
33 | {
|
---|
34 | namespace simulator
|
---|
35 | {
|
---|
36 | Parser::Parser(Simulator* parent,int app_width, int app_height,int scene_width, int scene_height,std::string media_path, std::string xmlFile): Gui(parent,"Parser",app_width,app_height,scene_width,scene_height, media_path)
|
---|
37 | {
|
---|
38 | this->media_path = media_path;
|
---|
39 | this->parent = parent;
|
---|
40 | xmlNode *root_element = NULL;
|
---|
41 | doc = xmlReadFile(xmlFile.c_str(), NULL, 0);
|
---|
42 | xmlDtdPtr dtd = NULL;
|
---|
43 | dtd = xmlParseDTD(NULL, (const xmlChar *)(media_path.append("/scene.dtd").c_str()));
|
---|
44 |
|
---|
45 | if (dtd == NULL)
|
---|
46 | {
|
---|
47 | //xmlGenericError(xmlGenericErrorContext,
|
---|
48 | Err("Could not parse DTD scene.dtd\n");
|
---|
49 | }
|
---|
50 | else
|
---|
51 | {
|
---|
52 | xmlValidCtxt cvp;
|
---|
53 | cvp.userData = (void *) stderr;
|
---|
54 | cvp.error = (xmlValidityErrorFunc) fprintf;
|
---|
55 | cvp.warning = (xmlValidityWarningFunc) fprintf;
|
---|
56 | if (!xmlValidateDtd(&cvp, doc, dtd))
|
---|
57 | {
|
---|
58 | //xmlGenericError(xmlGenericErrorContext,
|
---|
59 | Err("Document does not validate against scene.dtd\n");
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (doc == NULL)
|
---|
64 | {
|
---|
65 | Err("error: could not parse file %s\n", xmlFile.c_str());
|
---|
66 | }
|
---|
67 | else
|
---|
68 | {
|
---|
69 | /*
|
---|
70 | * Get the root element node
|
---|
71 | */
|
---|
72 | root_element = xmlDocGetRootElement(doc);
|
---|
73 | processElements(root_element);
|
---|
74 | }
|
---|
75 |
|
---|
76 | }
|
---|
77 |
|
---|
78 | Parser::~Parser()
|
---|
79 | {
|
---|
80 | if(doc!=NULL)
|
---|
81 | {
|
---|
82 | /*
|
---|
83 | * free the document
|
---|
84 | */
|
---|
85 | xmlFreeDoc(doc);;
|
---|
86 | }
|
---|
87 | /*
|
---|
88 | *Free the global variables that may
|
---|
89 | *have been allocated by the parser.
|
---|
90 | */
|
---|
91 | xmlCleanupParser();
|
---|
92 | }
|
---|
93 |
|
---|
94 | /* Recursive function that prints the XML structure */
|
---|
95 | void Parser::processElements(xmlNode * a_node)
|
---|
96 | {
|
---|
97 | xmlNode *cur_node = NULL;
|
---|
98 | for (cur_node = a_node; cur_node; cur_node = cur_node->next)
|
---|
99 | {
|
---|
100 | if(cur_node->type == XML_ELEMENT_NODE)
|
---|
101 | {
|
---|
102 | if(xmlStrEqual(cur_node->name, (xmlChar*) "params"))
|
---|
103 | {
|
---|
104 | processParams(cur_node->children);
|
---|
105 | }
|
---|
106 | else if(xmlStrEqual(cur_node->name, (xmlChar*) "objects"))
|
---|
107 | {
|
---|
108 | processObjects(cur_node->children);
|
---|
109 | }
|
---|
110 | else
|
---|
111 | {
|
---|
112 | processElements(cur_node->children);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | void Parser::processObjects(xmlNode * a_node)
|
---|
119 | {
|
---|
120 | FILE* fp;
|
---|
121 | std::string fileName;
|
---|
122 | xmlNode *cur_node = NULL;
|
---|
123 |
|
---|
124 | const IGeometryCreator *geo;
|
---|
125 |
|
---|
126 | geo=getGui()->getSceneManager()->getGeometryCreator();
|
---|
127 |
|
---|
128 | for (cur_node = a_node; cur_node; cur_node = cur_node->next)
|
---|
129 | {
|
---|
130 | if(xmlStrEqual(cur_node->name, (xmlChar*) "mesh"))
|
---|
131 | {
|
---|
132 | fileName=this->media_path;
|
---|
133 | fp = NULL;
|
---|
134 | fp = fopen( fileName.append((char*)xmlGetProp(cur_node,(xmlChar*)"model")).c_str(), "rb" );
|
---|
135 | if(fp!=NULL){
|
---|
136 | GenericObject* object = new GenericObject(parent,"Object", getSceneManager());
|
---|
137 | object->setMesh(getGui()->getMesh((char*)xmlGetProp(cur_node,(xmlChar*)"model")));
|
---|
138 | object->setPosition(getMeshVect(cur_node->children,(xmlChar*)"position"));
|
---|
139 | object->setRotation(getMeshVect(cur_node->children,(xmlChar*)"rotation"));
|
---|
140 | object->setScale(getMeshVect(cur_node->children,(xmlChar*)"scale"));
|
---|
141 | object->render();
|
---|
142 | }else{
|
---|
143 | Err("FATAL ERROR : File %s doesn't exist !\r\n",(char*)xmlGetProp(cur_node,(xmlChar*)"model"));
|
---|
144 | }
|
---|
145 | }else if(xmlStrEqual(cur_node->name, (xmlChar*) "cylinder")){
|
---|
146 | GenericObject* object = new GenericObject(parent,"Object", getSceneManager());
|
---|
147 | object->setMesh(geo->createCylinderMesh(ToIrrlichtScale(atof((char*)xmlGetProp(cur_node,(xmlChar*)"radius"))),
|
---|
148 | ToIrrlichtScale(atof((char*)xmlGetProp(cur_node,(xmlChar*)"length"))),
|
---|
149 | atof((char*)xmlGetProp(cur_node,(xmlChar*)"tesselation")),
|
---|
150 | SColor(100, 255, 100, 100)));
|
---|
151 | object->setPosition(getMeshVect(cur_node->children,(xmlChar*)"position"));
|
---|
152 | object->setRotation(getMeshVect(cur_node->children,(xmlChar*)"rotation"));
|
---|
153 | object->setScale(getMeshVect(cur_node->children,(xmlChar*)"scale"));
|
---|
154 | //object->setMaterialTexture(0,getTexture("/home/apeiron/igep/uav_dev_svn/trunk/media/nskinbl.jpg"));
|
---|
155 | object->setMaterialType( video::EMT_SOLID );
|
---|
156 | object->render();
|
---|
157 | }else if(xmlStrEqual(cur_node->name, (xmlChar*) "eight")){
|
---|
158 | GenericObject* object = new GenericObject(parent,"Object", getSceneManager());
|
---|
159 | object->setMesh(geo->createCubeMesh(vector3df(atof((char*)xmlGetProp(cur_node,(xmlChar*)"length")),atof((char*)xmlGetProp(cur_node,(xmlChar*)"width")),atof((char*)xmlGetProp(cur_node,(xmlChar*)"eight")))));
|
---|
160 | object->setPosition(getMeshVect(cur_node->children,(xmlChar*)"position"));
|
---|
161 | object->setRotation(getMeshVect(cur_node->children,(xmlChar*)"rotation"));
|
---|
162 | object->setScale(getMeshVect(cur_node->children,(xmlChar*)"scale"));
|
---|
163 | object->setMaterialType( video::EMT_TRANSPARENT_ALPHA_CHANNEL );
|
---|
164 | //object->getMaterial(0).Textures[0] = getTexture("/home/apeiron/igep/uav_dev_svn/trunk/media/nskinbl.jpg");
|
---|
165 | object->setMaterialFlag(EMF_LIGHTING, false);
|
---|
166 | object->render();
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | void Parser::processParams(xmlNode * a_node)
|
---|
172 | {
|
---|
173 |
|
---|
174 | xmlNode *cur_node = NULL;
|
---|
175 | for (cur_node = a_node; cur_node; cur_node = cur_node->next)
|
---|
176 | {
|
---|
177 | if(xmlStrEqual(cur_node->name, (xmlChar*) "archive"))
|
---|
178 | {
|
---|
179 | std::string file=media_path + "/" + (char*)xmlGetProp(cur_node,(xmlChar*)"path");
|
---|
180 | getDevice()->getFileSystem()->addFileArchive(file.c_str());
|
---|
181 | }
|
---|
182 | else if(xmlStrEqual(cur_node->name, (xmlChar*) "mesh"))
|
---|
183 | {
|
---|
184 | setMesh((char*)xmlGetProp(cur_node,(xmlChar*)"model"),getSceneVect(cur_node->children,(xmlChar*)"position"),getSceneVect(cur_node->children,(xmlChar*)"rotation"),getSceneVect(cur_node->children,(xmlChar*)"scale",true));
|
---|
185 |
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 | //todo rendre un vector3D framework
|
---|
190 | //retirer irr::core::vector3df ToIrrlichtCoordinates(irr::core::vector3df vect);
|
---|
191 | vector3df Parser::getMeshVect(xmlNode * mesh_node, xmlChar * param)
|
---|
192 | {
|
---|
193 | xmlNode *cur_node = NULL;
|
---|
194 | for (cur_node = mesh_node; cur_node; cur_node = cur_node->next)
|
---|
195 | {
|
---|
196 | if(xmlStrEqual(cur_node->name, param)){
|
---|
197 | return vector3df(atof((char*)xmlGetProp(cur_node,(xmlChar*)"x")),atof((char*)xmlGetProp(cur_node,(xmlChar*)"y")),atof((char*)xmlGetProp(cur_node,(xmlChar*)"z")));
|
---|
198 | }
|
---|
199 | }
|
---|
200 | return vector3df(0,0,0);
|
---|
201 | }
|
---|
202 |
|
---|
203 | vector3df Parser::getSceneVect(xmlNode * mesh_node, xmlChar * param, bool isScale)
|
---|
204 | {
|
---|
205 | xmlNode *cur_node = NULL;
|
---|
206 | for (cur_node = mesh_node; cur_node; cur_node = cur_node->next)
|
---|
207 | {
|
---|
208 | if(xmlStrEqual(cur_node->name, param)){
|
---|
209 | if(isScale){
|
---|
210 | return vector3df(atof((char*)xmlGetProp(cur_node,(xmlChar*)"x")),atof((char*)xmlGetProp(cur_node,(xmlChar*)"z")),atof((char*)xmlGetProp(cur_node,(xmlChar*)"y")));
|
---|
211 | }
|
---|
212 | return vector3df(atof((char*)xmlGetProp(cur_node,(xmlChar*)"x")),-atof((char*)xmlGetProp(cur_node,(xmlChar*)"z")),atof((char*)xmlGetProp(cur_node,(xmlChar*)"y")));
|
---|
213 | }
|
---|
214 | }
|
---|
215 | return vector3df(0,0,0);
|
---|
216 | }
|
---|
217 |
|
---|
218 | } // end namespace simulator
|
---|
219 | } // end namespace flair
|
---|
220 | #endif //GL
|
---|