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