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