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