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