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

Last change on this file since 122 was 87, checked in by Sanahuja Guillaume, 8 years ago

camera

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