source: flair-src/trunk/lib/FlairSimulator/src/Parser.cpp@ 64

Last change on this file since 64 was 15, checked in by Bayard Gildas, 9 years ago

sources reformatted with flair-format-dir script

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