[187] | 1 | #include "IVReader.h"
|
---|
[183] | 2 | #include <string>
|
---|
| 3 | #include <stdlib.h>
|
---|
| 4 | #include <sstream>
|
---|
| 5 |
|
---|
| 6 | IVMeshListNode *IVReader::meshList = NULL;
|
---|
| 7 | int IVReader::instanceCnt = 0;
|
---|
| 8 | Ogre::Log *IVReader::IVLog = NULL;
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | IVMeshListNode::IVMeshListNode(std::string name, IVManualMeshLoader *loader)
|
---|
| 13 | {
|
---|
| 14 | this->name = name;
|
---|
| 15 | this->loader = loader;
|
---|
| 16 | next = NULL;
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | IVMeshListNode::~IVMeshListNode()
|
---|
| 20 | {
|
---|
| 21 | if (loader != NULL) delete loader;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | void IVMeshListNode::attachNewNode(std::string name, IVManualMeshLoader *loader)
|
---|
| 25 | {
|
---|
| 26 | IVMeshListNode *help = this;
|
---|
| 27 |
|
---|
| 28 | while (help->next != NULL) help = help->next;
|
---|
| 29 | help->next = new IVMeshListNode(name, loader);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | IVManualMeshLoader *IVMeshListNode::getManualMeshLoader(std::string name)
|
---|
| 33 | {
|
---|
| 34 | IVMeshListNode *help = this;
|
---|
| 35 | if (help->name == name) return help->loader;
|
---|
| 36 | while (help->next != NULL)
|
---|
| 37 | {
|
---|
| 38 | help = help->next;
|
---|
| 39 | if (help->name == name) return help->loader;
|
---|
| 40 | }
|
---|
| 41 | return NULL;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | IVDefListNode::IVDefListNode(std::string name, IVNode *node)
|
---|
| 45 | {
|
---|
| 46 | this->name = name;
|
---|
| 47 | this->node = node;
|
---|
| 48 | next = NULL;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | IVDefListNode::~IVDefListNode()
|
---|
| 52 | {
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | void IVDefListNode::attachNewNode(std::string name, IVNode *node)
|
---|
| 56 | {
|
---|
| 57 | IVDefListNode *help = this;
|
---|
| 58 |
|
---|
| 59 | while (help->next != NULL) help = help->next;
|
---|
| 60 | help->next = new IVDefListNode(name, node);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | IVNode *IVDefListNode::getDef(std::string name)
|
---|
| 64 | {
|
---|
| 65 | IVDefListNode *help = this;
|
---|
| 66 | if (help->name == name) return help->node;
|
---|
| 67 | while (help->next != NULL)
|
---|
| 68 | {
|
---|
| 69 | help = help->next;
|
---|
| 70 | if (help->name == name) return help->node;
|
---|
| 71 | }
|
---|
| 72 | return NULL;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | IVReader::IVReader()
|
---|
| 76 | {
|
---|
| 77 | root = NULL; meshList = NULL; defList = NULL; fileName = NULL;
|
---|
| 78 | instanceCnt++;
|
---|
| 79 | IVLog = NULL;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | IVReader::~IVReader()
|
---|
| 83 | {
|
---|
| 84 | collapse();
|
---|
| 85 |
|
---|
| 86 | instanceCnt--;
|
---|
| 87 | if (instanceCnt == 0) if (meshList != NULL)
|
---|
| 88 | {
|
---|
| 89 | IVMeshListNode *help = meshList, *delme = NULL;
|
---|
| 90 | while (help->next != NULL)
|
---|
| 91 | {
|
---|
| 92 | delme = help; help = help->next; delete delme;
|
---|
| 93 | }
|
---|
| 94 | delete help;
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | void IVReader::collapse()
|
---|
| 99 | {
|
---|
| 100 | if (root != NULL) { delete root; root = NULL; }
|
---|
| 101 | if (defList != NULL)
|
---|
| 102 | {
|
---|
| 103 | IVDefListNode *help = defList, *delme = NULL;
|
---|
| 104 | while (help->next != NULL)
|
---|
| 105 | {
|
---|
| 106 | delme = help; help = help->next; delete delme;
|
---|
| 107 | }
|
---|
| 108 | delete help;
|
---|
| 109 | defList = NULL;
|
---|
| 110 | }
|
---|
| 111 | if (this->fileName != NULL) { delete [] this->fileName; this->fileName = NULL; }
|
---|
| 112 |
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | void IVReader::setLog(Ogre::Log *IVLog)
|
---|
| 116 | {
|
---|
| 117 | this->IVLog = IVLog;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | std::string IVReader::intToStr(int num)
|
---|
| 121 | {
|
---|
| 122 | std::ostringstream myStream;
|
---|
| 123 | myStream << num;
|
---|
| 124 | myStream.flush();
|
---|
| 125 |
|
---|
| 126 | return(myStream.str());
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | std::string IVReader::realToStr(Ogre::Real num)
|
---|
| 130 | {
|
---|
| 131 | std::ostringstream myStream;
|
---|
| 132 | myStream << num;
|
---|
| 133 | myStream.flush();
|
---|
| 134 |
|
---|
| 135 | return(myStream.str());
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | bool IVReader::loadFile(const char* fileName)
|
---|
| 139 | {
|
---|
| 140 | std::string message = "trying to load "; message = message + fileName + "...";
|
---|
| 141 |
|
---|
| 142 | if (IVLog != NULL)
|
---|
| 143 | IVLog->logMessage(message);
|
---|
| 144 |
|
---|
| 145 | collapse();
|
---|
| 146 | this->fileName = new char[strlen(fileName)+1]; strcpy(this->fileName, fileName);
|
---|
| 147 | treeCnt = 0;
|
---|
| 148 |
|
---|
| 149 | infile = fopen(fileName, "r"); root = NULL;
|
---|
| 150 |
|
---|
| 151 | if (infile == NULL)
|
---|
| 152 | {
|
---|
| 153 | message = "! Error loading File "; message = message + fileName + " !";
|
---|
| 154 | if (IVLog != NULL) IVLog->logMessage(message);
|
---|
| 155 | return false;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | if ((fgets(buf, BUFFERSIZE, infile) == NULL) && !feof(infile))
|
---|
| 159 | {
|
---|
| 160 | message = "! Error loading File "; message = message + fileName + " !";
|
---|
| 161 | if (IVLog != NULL) IVLog->logMessage(message);
|
---|
| 162 | fclose(infile);
|
---|
| 163 | return false;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | if (!checkHeader(buf))
|
---|
| 167 | {
|
---|
| 168 | message = "! Invalid Header !";
|
---|
| 169 | if (IVLog != NULL) IVLog->logMessage(message);
|
---|
| 170 | fclose(infile);
|
---|
| 171 | return false;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | IVTokenType tmptyp = IV_TOKEN_INVALID;
|
---|
| 175 | eof = false;
|
---|
| 176 |
|
---|
| 177 | bufOffset = 0;
|
---|
| 178 | char token[BUFFERSIZE] = "\0", oldtoken[BUFFERSIZE] = "\0", fieldtoken[BUFFERSIZE] = "\0";
|
---|
| 179 | int level = 0;
|
---|
| 180 |
|
---|
| 181 | while (getNextElement(token, &tmptyp))
|
---|
| 182 | {
|
---|
| 183 | if (tmptyp == IV_TOKEN_BRACEOPEN)
|
---|
| 184 | {
|
---|
| 185 | root = new IVNode(oldtoken);
|
---|
| 186 | level++;
|
---|
| 187 | break;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | strcpy(oldtoken, token);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | IVNode *actualNode = root, *parent = root, *fieldNode = root, *newNode = NULL;
|
---|
| 194 | IVTokenType oldtmptyp = tmptyp;
|
---|
| 195 | bool textfield = false, realfield = false; IVType fieldtyp = IV_INVALID;
|
---|
| 196 | bool def = false; std::string defLabel;
|
---|
| 197 | Ogre::Real rblock[BLOCKSIZE];
|
---|
| 198 | float *rvalues;
|
---|
| 199 | int cnt = 0; int blockcnt = 0;
|
---|
| 200 | while (getNextElement(token, &tmptyp))
|
---|
| 201 | {
|
---|
| 202 | if ((tmptyp == IV_TOKEN_BRACKETOPEN))
|
---|
| 203 | if (!getNextElement(token, &tmptyp)) break;
|
---|
| 204 | if ((tmptyp == IV_TOKEN_INVALID))
|
---|
| 205 | if (!getNextElement(token, &tmptyp)) break;
|
---|
| 206 | if (tmptyp == IV_TOKEN_BRACEOPEN)
|
---|
| 207 | {
|
---|
| 208 | actualNode = new IVNode(oldtoken);
|
---|
| 209 | if (parent == NULL)
|
---|
| 210 | {
|
---|
| 211 | message = "Error loading file "; message = message + fileName + "! Invalid tree structure.";
|
---|
| 212 | if (IVLog != NULL) IVLog->logMessage(message);
|
---|
| 213 | fclose(infile);
|
---|
| 214 | return false;
|
---|
| 215 | }
|
---|
| 216 | parent->attachNode(actualNode);
|
---|
| 217 | parent = actualNode;
|
---|
| 218 | level++;
|
---|
| 219 |
|
---|
| 220 | if (def)
|
---|
| 221 | {
|
---|
| 222 | addDefToList(defLabel, actualNode);
|
---|
| 223 | def = false;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | } else
|
---|
| 227 | if (tmptyp == IV_TOKEN_BRACECLOSE)
|
---|
| 228 | {
|
---|
| 229 | if (realfield)
|
---|
| 230 | {
|
---|
| 231 | if (blockcnt == 0) rvalues = (Ogre::Real *)malloc((cnt)*sizeof(Ogre::Real));
|
---|
| 232 | else rvalues = (Ogre::Real *)realloc(rvalues, (BLOCKSIZE*(blockcnt) + cnt)*sizeof(Ogre::Real));
|
---|
| 233 | memcpy((void *)&rvalues[BLOCKSIZE*blockcnt], rblock, cnt*sizeof(Ogre::Real));
|
---|
| 234 | fieldNode->addField(fieldtoken, rvalues, BLOCKSIZE*blockcnt+cnt, fieldtyp);
|
---|
| 235 | realfield = false;
|
---|
| 236 | // free(rvalues);
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | if (level > 0)
|
---|
| 240 | {
|
---|
| 241 | actualNode = parent = actualNode->getParent();
|
---|
| 242 | level--;
|
---|
| 243 | }
|
---|
| 244 | } else
|
---|
| 245 | {
|
---|
| 246 | if (oldtmptyp == IV_TOKEN_DEF)
|
---|
| 247 | {
|
---|
| 248 | def = true; defLabel = ""; defLabel = defLabel + token;
|
---|
| 249 | } else
|
---|
| 250 | if (oldtmptyp == IV_TOKEN_USE)
|
---|
| 251 | {
|
---|
| 252 | newNode = new IVNode("USE");
|
---|
| 253 | if (parent == NULL)
|
---|
| 254 | {
|
---|
| 255 | message = "Error loading file "; message = message + fileName + "! Invalid tree structure.";
|
---|
| 256 | if (IVLog != NULL) IVLog->logMessage(message);
|
---|
| 257 | fclose(infile);
|
---|
| 258 | return false;
|
---|
| 259 | }
|
---|
| 260 | char *s = (char *)malloc(sizeof(char)*(strlen(token)+1));
|
---|
| 261 | memcpy(s, token, strlen(token));
|
---|
| 262 | s[strlen(token)] = '\0';
|
---|
| 263 | newNode->addField("label", s);
|
---|
| 264 | actualNode->attachNode(newNode);
|
---|
| 265 | } else
|
---|
| 266 | if (!def)
|
---|
| 267 | if (oldtmptyp == IV_TOKEN_STRING)
|
---|
| 268 | {
|
---|
| 269 | if (!textfield)
|
---|
| 270 | {
|
---|
| 271 | if (tmptyp == IV_TOKEN_STRING)
|
---|
| 272 | {
|
---|
| 273 | /* char *s = new char[strlen(token)+1];
|
---|
| 274 | strcpy(s, token);
|
---|
| 275 | */
|
---|
| 276 | char *s = (char *)malloc(sizeof(char)*(strlen(token)+1));
|
---|
| 277 | memcpy(s, token, strlen(token));
|
---|
| 278 | s[strlen(token)] = '\0';
|
---|
| 279 | actualNode->addField(oldtoken, s);
|
---|
| 280 | // delete [] s;
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 | if (!realfield)
|
---|
| 284 | {
|
---|
| 285 | if ((tmptyp == IV_TOKEN_REAL) || (tmptyp == IV_TOKEN_INT))
|
---|
| 286 | {
|
---|
| 287 | cnt = 0; blockcnt = 0;
|
---|
| 288 | char *stopstring;
|
---|
| 289 | rblock[0] = strtod(token, &stopstring);
|
---|
| 290 | cnt++;
|
---|
| 291 | strcpy(fieldtoken, oldtoken);
|
---|
| 292 | fieldNode = actualNode;
|
---|
| 293 | if (tmptyp == IV_TOKEN_REAL) fieldtyp = IV_REAL;
|
---|
| 294 | if (tmptyp == IV_TOKEN_INT) fieldtyp = IV_INT;
|
---|
| 295 | realfield = true;
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 | textfield = !textfield;
|
---|
| 299 | }
|
---|
| 300 | else
|
---|
| 301 | {
|
---|
| 302 | if (realfield)
|
---|
| 303 | {
|
---|
| 304 | if ((tmptyp == IV_TOKEN_REAL) || (tmptyp == IV_TOKEN_INT))
|
---|
| 305 | {
|
---|
| 306 | char *stopstring;
|
---|
| 307 | if (cnt >= BLOCKSIZE)
|
---|
| 308 | {
|
---|
| 309 | if (blockcnt == 0) rvalues = (Ogre::Real *)malloc((BLOCKSIZE)*sizeof(Ogre::Real));
|
---|
| 310 | else rvalues = (Ogre::Real *)realloc(rvalues, (BLOCKSIZE*(blockcnt+1))*sizeof(Ogre::Real));
|
---|
| 311 | memcpy((void *)&rvalues[BLOCKSIZE*blockcnt], rblock, BLOCKSIZE*sizeof(Ogre::Real));
|
---|
| 312 | cnt = 0;
|
---|
| 313 | blockcnt++;
|
---|
| 314 |
|
---|
| 315 | }
|
---|
| 316 | rblock[cnt] = strtod(token, &stopstring);
|
---|
| 317 | cnt++;
|
---|
| 318 | if (tmptyp == IV_TOKEN_REAL) fieldtyp = IV_REAL;
|
---|
| 319 | }
|
---|
| 320 | else
|
---|
| 321 | {
|
---|
| 322 | if (blockcnt == 0) rvalues = (Ogre::Real *)malloc((cnt)*sizeof(Ogre::Real));
|
---|
| 323 | else rvalues = (Ogre::Real *)realloc(rvalues, (BLOCKSIZE*(blockcnt) + cnt)*sizeof(Ogre::Real));
|
---|
| 324 | memcpy((void *)&rvalues[BLOCKSIZE*blockcnt], rblock, cnt*sizeof(Ogre::Real));
|
---|
| 325 | fieldNode->addField(fieldtoken, rvalues, BLOCKSIZE*blockcnt+cnt, fieldtyp);
|
---|
| 326 | realfield = false;
|
---|
| 327 | // free(rvalues);
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | textfield = false;
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 |
|
---|
| 336 | strcpy(oldtoken, token);
|
---|
| 337 | oldtmptyp = tmptyp;
|
---|
| 338 | }
|
---|
| 339 | message = ""; ; message = message + fileName + " loaded.";
|
---|
| 340 | if (IVLog != NULL) IVLog->logMessage(message);
|
---|
| 341 | fclose(infile);
|
---|
| 342 |
|
---|
| 343 | return true;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | void IVReader::print()
|
---|
| 347 | {
|
---|
| 348 | if (root != NULL) root->print();
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | bool IVReader::checkHeader(const char *string)
|
---|
| 352 | {
|
---|
| 353 | for (int i=0; i < validIVHeaderCnt; i++)
|
---|
| 354 | if (strcmp(validIVHeader[i], string) == 0) return true;
|
---|
| 355 | return false;
|
---|
| 356 | }
|
---|
| 357 | bool IVReader::isSpace(char c)
|
---|
| 358 | {
|
---|
| 359 | if ((c==' ') || (c=='\t')) return true;
|
---|
| 360 | return false;
|
---|
| 361 | }
|
---|
| 362 | bool IVReader::isNewline(char c)
|
---|
| 363 | {
|
---|
| 364 | if (c=='\n') return true;
|
---|
| 365 | return false;
|
---|
| 366 | }
|
---|
| 367 | bool IVReader::isDigit(char c)
|
---|
| 368 | {
|
---|
| 369 | if ((c>=48) && (c<=57)) return true;
|
---|
| 370 | return false;
|
---|
| 371 | }
|
---|
| 372 | bool IVReader::isHexDigit(char c)
|
---|
| 373 | {
|
---|
| 374 | if ( ((c>=48) && (c<=57)) || ((c>=65) && (c<=70)) || ((c>=97) && (c<=102)) ) return true;
|
---|
| 375 | return false;
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 |
|
---|
| 379 | bool IVReader::getNextElement(char *token, IVTokenType *type)
|
---|
| 380 | {
|
---|
| 381 | if (eof) return false;
|
---|
| 382 | bool quoted = false;
|
---|
| 383 | while (isSpace(buf[bufOffset])) bufOffset++;
|
---|
| 384 | int i=0;
|
---|
| 385 | while ((!isSpace(buf[bufOffset])) || (quoted))
|
---|
| 386 | {
|
---|
| 387 | if (!quoted)
|
---|
| 388 | {
|
---|
| 389 | if (isNewline(buf[bufOffset]) || (buf[bufOffset] == COMMENTCHAR))
|
---|
| 390 | {
|
---|
| 391 | token[i] = '\0';
|
---|
| 392 | classify(token, type);
|
---|
| 393 | bufOffset = 0;
|
---|
| 394 | if (fgets(buf, BUFFERSIZE, infile) == NULL)
|
---|
| 395 | {
|
---|
| 396 | eof = true;
|
---|
| 397 | }
|
---|
| 398 | if (i==0) return getNextElement(token, type);
|
---|
| 399 | return true;
|
---|
| 400 | }
|
---|
| 401 | if ((buf[bufOffset] == ',') || (buf[bufOffset] == ']'))
|
---|
| 402 | {
|
---|
| 403 | token[i] = '\0';
|
---|
| 404 | bufOffset++;
|
---|
| 405 | classify(token, type);
|
---|
| 406 | return true;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 |
|
---|
| 410 |
|
---|
| 411 | } else if (isNewline(buf[bufOffset])) { eof = true; }
|
---|
| 412 |
|
---|
| 413 |
|
---|
| 414 | token[i] = buf[bufOffset];
|
---|
| 415 | i++;
|
---|
| 416 | if (buf[bufOffset] == '"')
|
---|
| 417 | {
|
---|
| 418 | quoted = !quoted; i--;
|
---|
| 419 | }
|
---|
| 420 | bufOffset++;
|
---|
| 421 |
|
---|
| 422 |
|
---|
| 423 | }
|
---|
| 424 | token[i] = '\0';
|
---|
| 425 | classify(token, type);
|
---|
| 426 | return true;
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | void IVReader::classify(char *token, IVTokenType *type)
|
---|
| 430 | {
|
---|
| 431 | bool isInt = true, isReal = true;
|
---|
| 432 | if (!isDigit(token[0]) && (token[0] != '-') && (token[0] != '+'))
|
---|
| 433 | {
|
---|
| 434 | isInt = false;
|
---|
| 435 | if (token[0] != '.') isReal = false;
|
---|
| 436 | }
|
---|
| 437 | int i = 1;
|
---|
| 438 | while ((token[i] != '\0') && isReal)
|
---|
| 439 | {
|
---|
| 440 | if (isInt && (i!=2) && !isHexDigit(token[i]))
|
---|
| 441 | {
|
---|
| 442 | isInt = false;
|
---|
| 443 | }
|
---|
| 444 | if (isInt && (i==2) && !isHexDigit(token[i]) && (token[i] != 'x') && (token[i] != 'X')) isInt = false;
|
---|
| 445 | if (isReal && (!isDigit(token[i])) && (token[i] != '.') && (token[i] != 'e') && (token[i] != 'E') && (token[i] != 'd') && (token[i] != 'D') && (token[i] != '-') && (token[i] != '+')) isReal = false;
|
---|
| 446 | i++;
|
---|
| 447 | }
|
---|
| 448 | if (isInt) *type = IV_TOKEN_INT;
|
---|
| 449 | else if (isReal) *type = IV_TOKEN_REAL;
|
---|
| 450 | else *type = IV_TOKEN_STRING;
|
---|
| 451 |
|
---|
| 452 | if (strcmp(token, "DEF") == 0) *type = IV_TOKEN_DEF;
|
---|
| 453 | if (strcmp(token, "USE") == 0) *type = IV_TOKEN_USE;
|
---|
| 454 |
|
---|
| 455 | if (token[1] == '\0')
|
---|
| 456 | {
|
---|
| 457 | if (token[0] == '{') *type = IV_TOKEN_BRACEOPEN;
|
---|
| 458 | if (token[0] == '}') *type = IV_TOKEN_BRACECLOSE;
|
---|
| 459 | if (token[0] == '[') *type = IV_TOKEN_BRACKETOPEN;
|
---|
| 460 | if (token[0] == ']') *type = IV_TOKEN_BRACKETCLOSE;
|
---|
| 461 | if (token[0] == '(') *type = IV_TOKEN_PARENTHESISOPEN;
|
---|
| 462 | if (token[0] == ')') *type = IV_TOKEN_PARENTHESISCLOSE;
|
---|
| 463 | }
|
---|
| 464 | if (token[0] == '\0')
|
---|
| 465 | {
|
---|
| 466 | *type = IV_TOKEN_INVALID;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | /*
|
---|
| 472 | IVNode *IVReader::getNode(const char* name)
|
---|
| 473 | {
|
---|
| 474 | return root->getNodeRecursive(name);
|
---|
| 475 | }
|
---|
| 476 | */
|
---|
| 477 | void IVReader::addMeshToList(std::string name, IVManualMeshLoader *loader)
|
---|
| 478 | {
|
---|
| 479 | if (meshList == NULL)
|
---|
| 480 | {
|
---|
| 481 | meshList = new IVMeshListNode(name, loader);
|
---|
| 482 | }
|
---|
| 483 | else
|
---|
| 484 | {
|
---|
| 485 | meshList->attachNewNode(name, loader);
|
---|
| 486 | }
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | void IVReader::addDefToList(std::string name, IVNode *node)
|
---|
| 490 | {
|
---|
| 491 | if (defList == NULL)
|
---|
| 492 | {
|
---|
| 493 | defList = new IVDefListNode(name, node);
|
---|
| 494 | }
|
---|
| 495 | else
|
---|
| 496 | {
|
---|
| 497 | defList->attachNewNode(name, node);
|
---|
| 498 | }
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 | Ogre::Entity *IVReader::createEntity(Ogre::SceneManager* sceneMgr, std::string name, IVMeshData *mData, Ogre::Vector3 *translation)
|
---|
| 502 | {
|
---|
| 503 | using namespace Ogre;
|
---|
| 504 |
|
---|
| 505 | std::string meshName = name.substr(name.find('/',0), name.length()) + "/Mesh";
|
---|
| 506 | std::string entityName = name + "/Entity";
|
---|
| 507 |
|
---|
| 508 | Mesh* pMesh = NULL;
|
---|
| 509 | if ((pMesh = (Mesh *) MeshManager::getSingleton().getByName(meshName).getPointer()) == NULL)
|
---|
| 510 | {
|
---|
| 511 |
|
---|
| 512 | IVMeshData *data = mData->expand();
|
---|
| 513 |
|
---|
| 514 | IVManualMeshLoader *loader = new IVManualMeshLoader(data);
|
---|
| 515 |
|
---|
| 516 | if (translation != NULL) *translation = data->boundingBox->getCenter();
|
---|
| 517 |
|
---|
| 518 | // data->collapse();
|
---|
| 519 | // delete data;
|
---|
| 520 |
|
---|
| 521 |
|
---|
| 522 | addMeshToList(meshName, loader);
|
---|
| 523 |
|
---|
| 524 | Mesh* pMesh = MeshManager::getSingleton().createManual(meshName, "IVGroup", loader).getPointer();
|
---|
| 525 |
|
---|
| 526 | pMesh->load();
|
---|
| 527 | pMesh->touch();
|
---|
| 528 | }
|
---|
| 529 | else
|
---|
| 530 | {
|
---|
| 531 | if (translation != NULL)
|
---|
| 532 | {
|
---|
| 533 | *translation = meshList->getManualMeshLoader(meshName)->getBoundingBox()->getCenter();
|
---|
| 534 | }
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | Entity *pEntity = sceneMgr->createEntity(entityName, meshName);
|
---|
| 538 |
|
---|
| 539 |
|
---|
| 540 | return pEntity;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 |
|
---|
| 544 |
|
---|
| 545 | void IVReader::buildTree(Ogre::SceneManager* sceneMgr, Ogre::SceneNode *sceneNodeRoot)
|
---|
| 546 | {
|
---|
| 547 | using namespace Ogre;
|
---|
| 548 | if (root == NULL) return;
|
---|
| 549 |
|
---|
| 550 | std::string s = intToStr(treeCnt) + "/" + fileName + "/Root";
|
---|
| 551 | SceneNode *actualSceneNode = sceneNodeRoot->createChildSceneNode(s);
|
---|
| 552 | s = "/"; s = s + fileName + "/RootMaterial";
|
---|
| 553 |
|
---|
| 554 | Material* pMaterial = NULL;
|
---|
| 555 | if ((pMaterial = (Material *) MaterialManager::getSingleton().getByName(s).getPointer()) == NULL)
|
---|
| 556 | {
|
---|
| 557 | pMaterial = (Material *)MaterialManager::getSingleton().create(s, "General").getPointer();
|
---|
| 558 | }
|
---|
| 559 | pMaterial->load();
|
---|
| 560 |
|
---|
| 561 | nodeCnt = 1; matCnt = 1;
|
---|
| 562 |
|
---|
| 563 |
|
---|
| 564 | buildTree(sceneMgr, actualSceneNode, root, NULL, pMaterial);
|
---|
| 565 | treeCnt++;
|
---|
| 566 |
|
---|
| 567 |
|
---|
| 568 | std::string message = "Tree of "; message = message + fileName + " built (" + intToStr(nodeCnt) + " Nodes)";
|
---|
| 569 | if (IVLog != NULL) IVLog->logMessage(message);
|
---|
| 570 |
|
---|
| 571 | }
|
---|
| 572 |
|
---|
[187] | 573 | void IVReader::buildTree(Ogre::SceneManager* sceneMgr, Ogre::SceneNode *sceneNodeRoot, IVNode *IVReaderoot, IVMeshData *mData, Ogre::Material *material)
|
---|
[183] | 574 | {
|
---|
| 575 | using namespace Ogre;
|
---|
| 576 |
|
---|
| 577 | Material *newMaterial = NULL;
|
---|
| 578 |
|
---|
| 579 | bool meshDataCreated = false;
|
---|
| 580 | if (mData == NULL)
|
---|
| 581 | {
|
---|
| 582 | mData = new IVMeshData();
|
---|
| 583 | meshDataCreated = true;
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 | IVType t; int i;
|
---|
| 587 |
|
---|
[187] | 588 | IVNode *help = IVReaderoot->getNextChildNode(true);
|
---|
[183] | 589 | while (help != NULL)
|
---|
| 590 | {
|
---|
| 591 | if (strcmp(help->getName(), "USE") == 0)
|
---|
| 592 | {
|
---|
| 593 | char *defLabel = (char *) help->getField("label", &t, &i);
|
---|
| 594 | if (t == IV_STRING)
|
---|
| 595 | {
|
---|
| 596 | help = defList->getDef(defLabel);
|
---|
| 597 | }
|
---|
| 598 | }
|
---|
| 599 | if ((strcmp(help->getName(), "Separator") == 0) || (strcmp(help->getName(), "TransformSeparator") == 0))
|
---|
| 600 | {
|
---|
| 601 | /* std::string s = sceneNodeRoot->getName() + "/Sep" + intToStr(nodeCnt);
|
---|
| 602 | nodeCnt++;
|
---|
| 603 | SceneNode *actualSceneNode = sceneNodeRoot->createChildSceneNode(s);
|
---|
| 604 | buildTree(sceneMgr, actualSceneNode, help, mData, actualMaterial);
|
---|
| 605 | */
|
---|
| 606 | if (newMaterial == NULL) newMaterial = material;
|
---|
| 607 | buildTree(sceneMgr, sceneNodeRoot, help, mData, newMaterial);
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | if (strcmp(help->getName(), "Texture2") == 0)
|
---|
| 611 | {
|
---|
| 612 | char *textureFileName = (char *) help->getField("filename", &t, &i);
|
---|
| 613 | if (t == IV_STRING)
|
---|
| 614 | {
|
---|
| 615 | std::string s = sceneNodeRoot->getName().substr(sceneNodeRoot->getName().find('/',0), sceneNodeRoot->getName().length()) + "/Material" + intToStr(matCnt);
|
---|
| 616 | matCnt++;
|
---|
| 617 | if ((newMaterial = (Material *) MaterialManager::getSingleton().getByName(s).getPointer()) == NULL)
|
---|
| 618 | {
|
---|
| 619 | newMaterial = (Material *)MaterialManager::getSingleton().create(s, "General").getPointer();
|
---|
| 620 | newMaterial->getTechnique(0)->getPass(0)->createTextureUnitState(textureFileName);
|
---|
| 621 | }
|
---|
| 622 | newMaterial->load();
|
---|
| 623 | newMaterial->reload();
|
---|
| 624 | }
|
---|
| 625 | }
|
---|
| 626 | // can be included
|
---|
| 627 | if (strcmp(help->getName(), "Material") == 0)
|
---|
| 628 | {
|
---|
| 629 | Real *ambientColor = (Real *) help->getField("ambientColor", &t, &i);
|
---|
| 630 | if ((i != 3) || (t != IV_REAL)) ambientColor = NULL;
|
---|
| 631 | Real *diffuseColor = (Real *) help->getField("diffuseColor", &t, &i);
|
---|
| 632 | if ((i != 3) || (t != IV_REAL)) diffuseColor = NULL;
|
---|
| 633 | Real *specularColor = (Real *) help->getField("specularColor", &t, &i);
|
---|
| 634 | if ((i != 3) || (t != IV_REAL)) specularColor = NULL;
|
---|
| 635 | Real *emissiveColor = (Real *) help->getField("emissiveColor", &t, &i);
|
---|
| 636 | if ((i != 3) || (t != IV_REAL)) emissiveColor = NULL;
|
---|
| 637 | Real *shininess = (Real *) help->getField("shininess", &t, &i);
|
---|
| 638 | if ((i != 1) || (t != IV_REAL)) shininess = NULL;
|
---|
| 639 | Real *transparency = (Real *) help->getField("transparency", &t, &i);
|
---|
| 640 | if ((i != 1) || (t != IV_REAL)) transparency = NULL;
|
---|
| 641 |
|
---|
| 642 | if (newMaterial == NULL)
|
---|
| 643 | {
|
---|
| 644 | std::string s = sceneNodeRoot->getName().substr(sceneNodeRoot->getName().find('/',0), sceneNodeRoot->getName().length()) + "/Material" + intToStr(matCnt);
|
---|
| 645 | matCnt++;
|
---|
| 646 | if ((newMaterial = (Material *) MaterialManager::getSingleton().getByName(s).getPointer()) == NULL)
|
---|
| 647 | {
|
---|
| 648 | if (material == NULL) newMaterial = (Material *)MaterialManager::getSingleton().create(s, "General").getPointer();
|
---|
| 649 | else newMaterial = material->clone(s).getPointer();
|
---|
| 650 |
|
---|
| 651 | Real alpha = 1.f;
|
---|
| 652 | if (transparency != NULL) alpha = 1.f - transparency[0];
|
---|
| 653 | if (ambientColor != NULL) newMaterial->setAmbient(ambientColor[0], ambientColor[1], ambientColor[2]);
|
---|
| 654 | if (diffuseColor != NULL) newMaterial->setDiffuse(diffuseColor[0], diffuseColor[1], diffuseColor[2], alpha);
|
---|
| 655 | if (specularColor != NULL) newMaterial->setSpecular(specularColor[0], specularColor[1], specularColor[2], alpha);
|
---|
| 656 | if (emissiveColor != NULL) newMaterial->setSelfIllumination(emissiveColor[0], emissiveColor[1], emissiveColor[2]);
|
---|
| 657 | if (shininess != NULL) newMaterial->setShininess(shininess[0]);
|
---|
| 658 |
|
---|
| 659 | // if (material->getTechnique(0)->getPass(0)->getNumTextureUnitStates() > 0)
|
---|
| 660 | // {
|
---|
| 661 | // newMaterial->getTechnique(0)->getPass(0)->createTextureUnitState(material->getTechnique(0)->getPass(0)->getTextureUnitState(0)->getTextureName());
|
---|
| 662 | // }
|
---|
| 663 | }
|
---|
| 664 | newMaterial->load();
|
---|
| 665 | newMaterial->reload();
|
---|
| 666 | }
|
---|
| 667 | else
|
---|
| 668 | {
|
---|
| 669 | Real alpha = 1.f;
|
---|
| 670 | if (transparency != NULL) alpha = 1.f - transparency[0];
|
---|
| 671 | if (ambientColor != NULL) newMaterial->setAmbient(ambientColor[0], ambientColor[1], ambientColor[2]);
|
---|
| 672 | if (diffuseColor != NULL) newMaterial->setDiffuse(diffuseColor[0], diffuseColor[1], diffuseColor[2], alpha);
|
---|
| 673 | if (specularColor != NULL) newMaterial->setSpecular(specularColor[0], specularColor[1], specularColor[2], alpha);
|
---|
| 674 | if (emissiveColor != NULL) newMaterial->setSelfIllumination(emissiveColor[0], emissiveColor[1], emissiveColor[2]);
|
---|
| 675 | if (shininess != NULL) newMaterial->setShininess(shininess[0]);
|
---|
| 676 | newMaterial->load();
|
---|
| 677 | }
|
---|
| 678 | }
|
---|
| 679 |
|
---|
| 680 | if (strcmp(help->getName(), "Coordinate3") == 0)
|
---|
| 681 | {
|
---|
| 682 | mData->vertices = (Real *) help->getField("point", &t, &mData->vcnt);
|
---|
| 683 | if ((t != IV_REAL) && (t != IV_INT)) mData->vertices = NULL;
|
---|
| 684 | }
|
---|
| 685 | if (strcmp(help->getName(), "Normal") == 0)
|
---|
| 686 | {
|
---|
| 687 | mData->normals = (Real *) help->getField("vector", &t, &mData->ncnt);
|
---|
| 688 | if ((t != IV_REAL) && (t != IV_INT)) mData->normals = NULL;
|
---|
| 689 | }
|
---|
| 690 | if (strcmp(help->getName(), "TextureCoordinate2") == 0)
|
---|
| 691 | {
|
---|
| 692 | mData->texCoords = (Real *) help->getField("point", &t, &mData->tcnt);
|
---|
| 693 | if ((t != IV_REAL) && (t != IV_INT)) mData->texCoords = NULL;
|
---|
| 694 | }
|
---|
| 695 |
|
---|
| 696 | if (strcmp(help->getName(), "IndexedFaceSet") == 0)
|
---|
| 697 | {
|
---|
| 698 | std::string s = sceneNodeRoot->getName() + "/IFS" + intToStr(nodeCnt);
|
---|
| 699 | nodeCnt++;
|
---|
| 700 | SceneNode *triangleStripSceneNode = sceneNodeRoot->createChildSceneNode(s);
|
---|
| 701 |
|
---|
| 702 | mData->indices = (Real *) help->getField("coordIndex", &t, &mData->icnt);
|
---|
| 703 | if (t != IV_INT) mData->indices = NULL;
|
---|
| 704 |
|
---|
| 705 | mData->normalIndices = (Real *) help->getField("normalIndex", &t, &mData->nicnt);
|
---|
| 706 | if (t != IV_INT) mData->normalIndices = NULL;
|
---|
| 707 |
|
---|
| 708 | mData->texCoordIndices = (Real *) help->getField("textureCoordIndex", &t, &mData->ticnt);
|
---|
| 709 | if (t != IV_INT) mData->texCoordIndices = NULL;
|
---|
| 710 |
|
---|
| 711 |
|
---|
| 712 | mData->roType = IV_ROT_FACE_SET;
|
---|
| 713 |
|
---|
| 714 | Vector3 translation = Vector3(0,0,0);
|
---|
| 715 | Entity *pEntity = createEntity(sceneMgr, s, mData, &translation);
|
---|
| 716 | if (newMaterial == NULL) newMaterial = material;
|
---|
| 717 | if (newMaterial != NULL) pEntity->setMaterialName(newMaterial->getName());
|
---|
| 718 |
|
---|
| 719 | triangleStripSceneNode->attachObject(pEntity);
|
---|
| 720 | triangleStripSceneNode->translate(translation);
|
---|
| 721 |
|
---|
| 722 | }
|
---|
| 723 |
|
---|
| 724 |
|
---|
| 725 | if (strcmp(help->getName(), "IndexedTriangleStripSet") == 0)
|
---|
| 726 | {
|
---|
| 727 | std::string s = sceneNodeRoot->getName() + "/ITS" + intToStr(nodeCnt);
|
---|
| 728 | nodeCnt++;
|
---|
| 729 | SceneNode *triangleStripSceneNode = sceneNodeRoot->createChildSceneNode(s);
|
---|
| 730 |
|
---|
| 731 | mData->indices = (Real *) help->getField("coordIndex", &t, &mData->icnt);
|
---|
| 732 | if (t != IV_INT) mData->indices = NULL;
|
---|
| 733 |
|
---|
| 734 | mData->normalIndices = (Real *) help->getField("normalIndex", &t, &mData->nicnt);
|
---|
| 735 | if (t != IV_INT) mData->normalIndices = NULL;
|
---|
| 736 |
|
---|
| 737 | mData->texCoordIndices = (Real *) help->getField("textureCoordIndex", &t, &mData->ticnt);
|
---|
| 738 | if (t != IV_INT) mData->texCoordIndices = NULL;
|
---|
| 739 |
|
---|
| 740 | IVNode *n = help->getNextChildNode(true);
|
---|
| 741 | while ((n != NULL) && (strcmp(help->getName(), "VertexProperty") == 0))
|
---|
| 742 | {
|
---|
| 743 | n = help->getNextChildNode();
|
---|
| 744 | }
|
---|
| 745 | if (n != NULL)
|
---|
| 746 | {
|
---|
| 747 | mData->vertices = (Real *) n->getField("vertex", &t, &mData->vcnt);
|
---|
| 748 | if ((t != IV_REAL) && (t != IV_INT)) mData->vertices = NULL;
|
---|
| 749 |
|
---|
| 750 | mData->normals = (Real *) n->getField("normal", &t, &mData->ncnt);
|
---|
| 751 | if ((t != IV_REAL) && (t != IV_INT)) mData->normals = NULL;
|
---|
| 752 | mData->texCoords = (Real *) n->getField("texCoord", &t, &mData->tcnt);
|
---|
| 753 | if ((t != IV_REAL) && (t != IV_INT)) mData->texCoords = NULL;
|
---|
| 754 | }
|
---|
| 755 |
|
---|
| 756 | mData->roType = IV_ROT_TRIANGLE_STRIP;
|
---|
| 757 |
|
---|
| 758 | Vector3 translation = Vector3(0,0,0);
|
---|
| 759 | Entity *pEntity = createEntity(sceneMgr, s, mData, &translation);
|
---|
| 760 | if (newMaterial == NULL) newMaterial = material;
|
---|
| 761 | if (newMaterial != NULL) pEntity->setMaterialName(newMaterial->getName());
|
---|
| 762 |
|
---|
| 763 | triangleStripSceneNode->attachObject(pEntity);
|
---|
| 764 | triangleStripSceneNode->translate(translation);
|
---|
| 765 |
|
---|
| 766 | }
|
---|
| 767 |
|
---|
[187] | 768 | help = IVReaderoot->getNextChildNode();
|
---|
[183] | 769 | }
|
---|
| 770 | if (meshDataCreated) if (mData != NULL) delete mData;
|
---|
| 771 | }
|
---|
| 772 |
|
---|